ccdemos/textures.cc
branchpyrit
changeset 79 062b1c4143f7
parent 75 20dee9819b17
child 81 9dbb9c8c115b
equal deleted inserted replaced
78:9569e9f35374 79:062b1c4143f7
     6 
     6 
     7 Camera cam(Vector3(0.,6.,6.), Vector3(0.,2.,-7.), Vector3(0.,0.,-1.));
     7 Camera cam(Vector3(0.,6.,6.), Vector3(0.,2.,-7.), Vector3(0.,0.,-1.));
     8 Light light(Vector3(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9));
     8 Light light(Vector3(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9));
     9 
     9 
    10 Float lx, ly, lz, cf;
    10 Float lx, ly, lz, cf;
    11 
       
    12 
       
    13 /*class CloudTexture: public Texture
       
    14 {
       
    15 	Vector3 centre;
       
    16 public:
       
    17 	Colour evaluate(Vector3 point)
       
    18 	{
       
    19 		Float sum = 0.0;
       
    20 		for (int i = 1; i < 5; i++)
       
    21 			sum += fabsf(perlin(point.x*i, point.y*i, point.z*i))/i;
       
    22 		Float value = sinf(point.x + sum)/2 + 0.5;
       
    23 		return Colour(value, value*0.5, value*0.5);
       
    24 	};
       
    25 };*/
       
    26 
       
    27 void texture2D(Float u, Float v, Float &r, Float &g, Float &b)
       
    28 {
       
    29 	// checkers
       
    30 	r = fabs((int)(u*4.2)%2 + (int)(v*4.2)%2 - 1);
       
    31 	g=r; b=r;
       
    32 }
       
    33 
       
    34 class PlanarMapTexture: public Texture
       
    35 {
       
    36 	Vector3 centre;
       
    37 public:
       
    38 	PlanarMapTexture(Vector3 acentre): centre(acentre) {};
       
    39 	Colour evaluate(Vector3 point)
       
    40 	{
       
    41 		point = point - centre;
       
    42 		Float u = 1000+point.x;
       
    43 		Float v = 1000+point.y;
       
    44 		Float r,g,b;
       
    45 		texture2D(u,v, r,g,b);
       
    46 		return Colour(r, g, b);
       
    47 	};
       
    48 };
       
    49 
       
    50 class CubicMapTexture: public Texture
       
    51 {
       
    52 	Vector3 centre;
       
    53 public:
       
    54 	CubicMapTexture(Vector3 acentre): centre(acentre) {};
       
    55 	Colour evaluate(Vector3 point)
       
    56 	{
       
    57 		point = point - centre;
       
    58 		Float u,v;
       
    59 		if (fabs(point.x) > fabs(point.y))
       
    60 		{
       
    61 			if (fabs(point.x) > fabs(point.z))
       
    62 			{
       
    63 				if (point.x < 0)
       
    64 					u = -point.y;
       
    65 				else
       
    66 					u = point.y;
       
    67 				v = point.z;
       
    68 			}
       
    69 			else
       
    70 			{
       
    71 				if (point.z < 0)
       
    72 					u = -point.x;
       
    73 				else
       
    74 					u = point.x;
       
    75 				v = point.y;
       
    76 			}
       
    77 		}
       
    78 		else
       
    79 		{
       
    80 			if (fabs(point.y) > fabs(point.z))
       
    81 			{
       
    82 				if (point.y < 0)
       
    83 					u = -point.x;
       
    84 				else
       
    85 					u = point.x;
       
    86 				v = point.z;
       
    87 			}
       
    88 			else
       
    89 			{
       
    90 				if (point.z < 0)
       
    91 					u = -point.x;
       
    92 				else
       
    93 					u = point.x;
       
    94 				v = point.y;
       
    95 			}
       
    96 		}
       
    97 		u += 1000;
       
    98 		v += 1000;
       
    99 		Float r,g,b;
       
   100 		texture2D(u,v, r,g,b);
       
   101 		return Colour(r, g, b);
       
   102 	};
       
   103 };
       
   104 
       
   105 class CylinderMapTexture: public Texture
       
   106 {
       
   107 	Vector3 centre;
       
   108 public:
       
   109 	CylinderMapTexture(Vector3 acentre): centre(acentre) {};
       
   110 	Colour evaluate(Vector3 point)
       
   111 	{
       
   112 		point = point - centre;
       
   113 
       
   114 		Float u,v;
       
   115 		v = 1000+point.y;
       
   116 		u = M_PI + atan2(point.z, point.x);
       
   117 
       
   118 		Float r,g,b;
       
   119 		texture2D(u,v, r,g,b);
       
   120 		return Colour(r, g, b);
       
   121 	};
       
   122 };
       
   123 
       
   124 class SphereMapTexture: public Texture
       
   125 {
       
   126 	Vector3 centre;
       
   127 public:
       
   128 	SphereMapTexture(Vector3 acentre): centre(acentre) {};
       
   129 	Colour evaluate(Vector3 point)
       
   130 	{
       
   131 		point = point - centre;
       
   132 
       
   133 		Float u,v;
       
   134 		v = acos(point.y / point.mag());
       
   135 		u = M_PI + atan2(point.z, point.x);
       
   136 
       
   137 		Float r,g,b;
       
   138 		texture2D(u,v, r,g,b);
       
   139 		return Colour(r, g, b);
       
   140 	};
       
   141 };
       
   142 
    11 
   143 void update_callback()
    12 void update_callback()
   144 {
    13 {
   145 	if (lx != 0.0)
    14 	if (lx != 0.0)
   146 		light.pos.x += lx;
    15 		light.pos.x += lx;
   194 
    63 
   195 	rt.addLight(&light);
    64 	rt.addLight(&light);
   196 	light.castShadows(false);
    65 	light.castShadows(false);
   197 
    66 
   198 	Material mat0a(Colour(0.7, 0.7, 0.7));
    67 	Material mat0a(Colour(0.7, 0.7, 0.7));
   199 	mat0a. setReflectivity(0.0);
    68 	mat0a.setReflectivity(0.0);
   200 	Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a);
    69 	Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a);
   201 	rt.addShape(&box);
    70 	rt.addShape(&box);
   202 
    71 
   203 	Material mat0b(Colour(0.1, 0.7, 0.8));
    72 	Material mat0b(Colour(0.1, 0.7, 0.8));
   204 	mat0b.setReflectivity(0.7);
    73 	mat0b.setReflectivity(0.7);
   205 	Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b);
    74 	Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b);
   206 	rt.addShape(&box2);
    75 	rt.addShape(&box2);
   207 
    76 
       
    77 	Float bounds[] = {0.3, 0.6, 1.1};
       
    78 	Colour colours[] = {Colour(0,0,0), Colour(1,1,1), Colour(0,0,0)};
       
    79 	BoundColourMap cmap(bounds, colours);
       
    80 
   208 	// spheres
    81 	// spheres
   209 	Material mat1(Colour(1.0, 1.0, 1.0));
    82 	Material mat1(Colour(1.0, 1.0, 1.0));
   210 	mat1.texture = new PlanarMapTexture(Vector3(-4.5, 2.0, -7.0));
    83 	mat1.texture = new CheckersTexture(new PlanarMap(Vector3(-4.5, 2.0, -7.0), 0.48), &cmap);
   211 	Sphere sphere1(Vector3(-4.5, 2.0, -7.0), 1.0, &mat1);
    84 	Sphere sphere1(Vector3(-4.5, 2.0, -7.0), 1.0, &mat1);
   212 	rt.addShape(&sphere1);
    85 	rt.addShape(&sphere1);
   213 
    86 
   214 	Material mat2(Colour(1.0, 1.0, 1.0));
    87 	Material mat2(Colour(1.0, 1.0, 1.0));
   215 	mat2.texture = new CubicMapTexture(Vector3(-1.5, 2.0, -7.0));
    88 	mat2.texture = new CheckersTexture(new CubicMap(Vector3(-1.5, 2.0, -7.0), 0.48), &cmap);
   216 	Sphere sphere2(Vector3(-1.5, 2.0, -7.0), 1.0, &mat2);
    89 	Sphere sphere2(Vector3(-1.5, 2.0, -7.0), 1.0, &mat2);
   217 	rt.addShape(&sphere2);
    90 	rt.addShape(&sphere2);
   218 
    91 
   219 	Material mat3(Colour(1.0, 1.0, 1.0));
    92 	Material mat3(Colour(1.0, 1.0, 1.0));
   220 	mat3.texture = new CylinderMapTexture(Vector3(1.5, 2.0, -7.0));
    93 	mat3.texture = new CheckersTexture(new CylinderMap(Vector3(1.5, 2.0, -7.0), 0.48), &cmap);
   221 	Sphere sphere3(Vector3(1.5, 2.0, -7.0), 1.0, &mat3);
    94 	Sphere sphere3(Vector3(1.5, 2.0, -7.0), 1.0, &mat3);
   222 	rt.addShape(&sphere3);
    95 	rt.addShape(&sphere3);
   223 
    96 
   224 	Material mat4(Colour(1.0, 1.0, 1.0));
    97 	Material mat4(Colour(1.0, 1.0, 1.0));
   225 	mat4.texture = new SphereMapTexture(Vector3(4.5, 2.0, -7.0));
    98 	mat4.texture = new CheckersTexture(new SphereMap(Vector3(4.5, 2.0, -7.0), 0.48), &cmap);
   226 	Sphere sphere4(Vector3(4.5, 2.0, -7.0), 1.0, &mat4);
    99 	Sphere sphere4(Vector3(4.5, 2.0, -7.0), 1.0, &mat4);
   227 	rt.addShape(&sphere4);
   100 	rt.addShape(&sphere4);
   228 
   101 
   229 	// cubes
   102 	// cubes
   230 	Material mat5(Colour(1.0, 1.0, 1.0));
   103 	Material mat5(Colour(1.0, 1.0, 1.0));
   231 	mat5.texture = new PlanarMapTexture(Vector3(-4.5, 0.0, -7.0));
   104 	mat5.texture = new CheckersTexture(new PlanarMap(Vector3(-4.5, 0.0, -7.0), 0.48), &cmap);
   232 	Box cube1(Vector3(-4.5, 0.0, -7.0)-1.0, Vector3(-4.5, 0.0, -7.0)+1.0, &mat5);
   105 	Box cube1(Vector3(-4.5, 0.0, -7.0)-1.0, Vector3(-4.5, 0.0, -7.0)+1.0, &mat5);
   233 	rt.addShape(&cube1);
   106 	rt.addShape(&cube1);
   234 
   107 
   235 	Material mat6(Colour(1.0, 1.0, 1.0));
   108 	Material mat6(Colour(1.0, 1.0, 1.0));
   236 	mat6.texture = new CubicMapTexture(Vector3(-1.5, 0.0, -7.0));
   109 	mat6.texture = new CheckersTexture(new CubicMap(Vector3(-1.5, 0.0, -7.0), 0.48), &cmap);
   237 	Box cube2(Vector3(-1.5, 0.0, -7.0)-1.0, Vector3(-1.5, 0.0, -7.0)+1.0, &mat6);
   110 	Box cube2(Vector3(-1.5, 0.0, -7.0)-1.0, Vector3(-1.5, 0.0, -7.0)+1.0, &mat6);
   238 	rt.addShape(&cube2);
   111 	rt.addShape(&cube2);
   239 
   112 
   240 	Material mat7(Colour(1.0, 1.0, 1.0));
   113 	Material mat7(Colour(1.0, 1.0, 1.0));
   241 	mat7.texture = new CylinderMapTexture(Vector3(1.5, 0.0, -7.0));
   114 	mat7.texture = new CheckersTexture(new CylinderMap(Vector3(1.5, 0.0, -7.0), 0.48), &cmap);
   242 	Box cube3(Vector3(1.5, 0.0, -7.0)-1.0, Vector3(1.5, 0.0, -7.0)+1.0, &mat7);
   115 	Box cube3(Vector3(1.5, 0.0, -7.0)-1.0, Vector3(1.5, 0.0, -7.0)+1.0, &mat7);
   243 	rt.addShape(&cube3);
   116 	rt.addShape(&cube3);
   244 
   117 
   245 	Material mat8(Colour(1.0, 1.0, 1.0));
   118 	Material mat8(Colour(1.0, 1.0, 1.0));
   246 	mat8.texture = new SphereMapTexture(Vector3(4.5, 0.0, -7.0));
   119 	mat8.texture = new CheckersTexture(new SphereMap(Vector3(4.5, 0.0, -7.0), 0.48), &cmap);
   247 	Box cube4(Vector3(4.5, 0.0, -7.0)-1.0, Vector3(4.5, 0.0, -7.0)+1.0, &mat8);
   120 	Box cube4(Vector3(4.5, 0.0, -7.0)-1.0, Vector3(4.5, 0.0, -7.0)+1.0, &mat8);
   248 	rt.addShape(&cube4);
   121 	rt.addShape(&cube4);
   249 
   122 
   250 	mat1.setReflectivity(0);
   123 	mat1.setReflectivity(0);
   251 	mat2.setReflectivity(0);
   124 	mat2.setReflectivity(0);