tavli

view src/board.cc @ 6:a0d30f6f20d4

- texture coordinate generation in class Mesh - wood texture
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Jun 2015 04:22:06 +0300
parents e48b40a3c82a
children f1ecc2439802
line source
1 #include "opengl.h"
2 #include "board.h"
3 #include "meshgen.h"
4 #include "pnoise.h"
6 Board::Board()
7 {
8 puck_obj = 0;
9 clear();
10 }
12 Board::~Board()
13 {
14 destroy();
15 }
17 bool Board::init()
18 {
19 if(!generate_textures()) {
20 return false;
21 }
22 if(!generate()) {
23 return false;
24 }
26 return true;
27 }
29 void Board::destroy()
30 {
31 for(size_t i=0; i<obj.size(); i++) {
32 delete obj[i];
33 }
34 obj.clear();
36 delete puck_obj;
37 puck_obj = 0;
38 }
40 void Board::clear()
41 {
42 memset(slots, 0, sizeof slots);
43 }
45 void Board::draw() const
46 {
47 for(size_t i=0; i<obj.size(); i++) {
48 obj[i]->draw();
49 }
50 }
52 #define HSIZE 1.0
53 #define VSIZE (2.0 * HSIZE)
54 #define BOT_THICKNESS (HSIZE * 0.01)
55 #define WALL_THICKNESS (HSIZE * 0.05)
56 #define WALL_HEIGHT (HSIZE * 0.1)
57 #define GAP (HSIZE * 0.025)
58 #define HINGE_RAD (GAP * 0.5)
59 #define HINGE_HEIGHT (VSIZE * 0.075)
61 bool Board::generate()
62 {
63 Mesh tmp;
64 Matrix4x4 xform;
66 obj.clear();
68 for(int i=0; i<2; i++) {
69 int sign = i * 2 - 1;
71 // generate bottom
72 Mesh *bottom = new Mesh;
73 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
74 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
75 bottom->apply_xform(xform);
77 Object *obottom = new Object;
78 obottom->set_mesh(bottom);
79 obottom->xform().set_translation(Vector3(sign * (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25), 0, 0));
80 obottom->set_texture(img_field.texture());
81 obj.push_back(obottom);
84 // generate the 4 sides
85 Mesh *sides = new Mesh;
86 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
87 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
88 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
89 sides->apply_xform(xform);
91 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
92 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
93 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
94 tmp.apply_xform(xform);
95 sides->append(tmp);
96 tmp.clear();
98 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
99 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
100 (VSIZE + WALL_THICKNESS) / 2.0));
101 tmp.apply_xform(xform);
102 sides->append(tmp);
103 tmp.clear();
105 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
106 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
107 -(VSIZE + WALL_THICKNESS) / 2.0));
108 tmp.apply_xform(xform);
109 sides->append(tmp);
110 tmp.clear();
112 // generate texture coordinates
113 sides->texcoord_gen_box();
115 Object *osides = new Object;
116 osides->set_mesh(sides);
117 osides->xform() = obottom->xform();
118 osides->set_texture(img_wood.texture());
119 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
120 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
121 obj.push_back(osides);
123 }
126 // generate the hinges
127 Mesh *hinges = new Mesh;
128 gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
129 xform.reset_identity();
130 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, VSIZE / 4.0));
131 xform.rotate(Vector3(M_PI / 2.0, 0, 0));
132 hinges->apply_xform(xform);
134 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
135 xform.reset_identity();
136 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, -VSIZE / 4.0));
137 xform.rotate(Vector3(M_PI / 2.0, 0, 0));
138 tmp.apply_xform(xform);
140 hinges->append(tmp);
142 Object *ohinges = new Object;
143 ohinges->set_mesh(hinges);
144 obj.push_back(ohinges);
146 // debug object
147 /*Mesh *dbgmesh = new Mesh;
148 gen_box(dbgmesh, 0.5, 0.5, 0.5);
149 xform.set_translation(Vector3(0, 0.4, 0));
150 xform.set_scaling(Vector3(4, 1, 4));
151 dbgmesh->apply_xform(xform);
152 Object *dbgobj = new Object;
153 dbgobj->set_mesh(dbgmesh);
154 dbgobj->set_texture(img_wood.texture());
155 dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
156 obj.push_back(dbgobj);*/
158 return true;
159 }
161 static float wood(float x, float y)
162 {
163 float u = x;
164 float v = y;
165 x += 1.0;
166 x *= 10.0;
167 y *= 20.0;
169 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
170 turbulence2(u * 0.5, v, 2) * 15.0;
171 float val = fmod(len, 1.0);
173 //val = val * 0.5 + 0.5;
174 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
175 }
177 static float wood_tile(float x, float y)
178 {
179 float u = x;
180 float v = y;
181 x *= 10.0;
182 y *= 10.0;
184 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
185 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
187 val = fmod(val, 1.0);
188 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
189 }
191 static bool spike(float x, float y)
192 {
193 x = fmod(x * 5.0, 1.0);
194 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
195 }
197 static bool circle(float x, float y, float rad)
198 {
199 x = fmod(x * 5.0, 1.0) - 0.5;
200 y = (y - 0.65) * 5.0;
201 float len = sqrt(x * x + y * y);
202 return len < rad;
203 }
205 static bool diamond(float x, float y)
206 {
207 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
208 }
210 static bool center_circle(float x, float y, float rad)
211 {
212 x = x - 0.5;
213 y = 1.0 - y;
214 return sqrt(x * x + y * y) < rad;
215 }
217 bool Board::generate_textures()
218 {
219 // ---- board field texture ----
220 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
221 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
222 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
224 img_field.create(512, 1024);
225 unsigned char *pptr = img_field.pixels;
226 for(int i=0; i<img_field.height; i++) {
227 float v = (float)i / (float)img_field.height;
229 for(int j=0; j<img_field.width; j++) {
230 float u = (float)j / (float)img_field.width;
232 int r = 0, g = 0, b = 0;
234 float wood_val = wood(u, v);
236 // pattern mask
237 float x = u;
238 float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
239 bool inside = false;
241 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
242 (spike(x, y + 0.5) && !spike(x, y + 0.68));
243 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
244 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
245 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
246 inside |= center_circle(x, y, 0.03);
248 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
249 if(inside) {
250 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
251 }
253 r = (int)(wood_color.x * 255.0);
254 g = (int)(wood_color.y * 255.0);
255 b = (int)(wood_color.z * 255.0);
257 pptr[0] = r > 255 ? 255 : r;
258 pptr[1] = g > 255 ? 255 : g;
259 pptr[2] = b > 255 ? 255 : b;
260 pptr += 3;
261 }
262 }
263 img_field.texture();
265 // ---- generic wood texture ----
266 img_wood.create(256, 256);
267 pptr = img_wood.pixels;
268 for(int i=0; i<img_wood.height; i++) {
269 float v = (float)i / (float)img_wood.height;
270 for(int j=0; j<img_wood.width; j++) {
271 float u = (float)j / (float)img_wood.width;
273 float wood_val = wood_tile(u, v);
274 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
276 int r = (int)(wood_color.x * 255.0);
277 int g = (int)(wood_color.y * 255.0);
278 int b = (int)(wood_color.z * 255.0);
280 pptr[0] = r > 255 ? 255 : r;
281 pptr[1] = g > 255 ? 255 : g;
282 pptr[2] = b > 255 ? 255 : b;
283 pptr += 3;
284 }
285 }
286 img_wood.texture();
287 return true;
288 }