tavli

view src/board.cc @ 14:283eb6e9f0a3

scenery
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 07:44:23 +0300
parents f3c5134b4914
children b1a195c3ee16
line source
1 #include <float.h>
2 #include "opengl.h"
3 #include "board.h"
4 #include "game.h"
5 #include "meshgen.h"
6 #include "pnoise.h"
7 #include "revol.h"
9 Board::Board()
10 {
11 puck_obj = 0;
12 clear();
13 }
15 Board::~Board()
16 {
17 destroy();
18 }
20 bool Board::init()
21 {
22 if(!generate_textures()) {
23 return false;
24 }
25 if(!generate()) {
26 return false;
27 }
29 return true;
30 }
32 void Board::destroy()
33 {
34 for(size_t i=0; i<obj.size(); i++) {
35 delete obj[i];
36 }
37 obj.clear();
39 delete puck_obj;
40 puck_obj = 0;
41 }
43 void Board::clear()
44 {
45 memset(slots, 0, sizeof slots);
46 }
48 void Board::draw() const
49 {
50 for(size_t i=0; i<obj.size(); i++) {
51 if(wireframe) {
52 obj[i]->draw_wire();
53 obj[i]->draw_normals(0.075);
54 } else {
55 obj[i]->draw();
56 }
57 }
58 }
60 #define HSIZE 1.0
61 #define VSIZE (2.0 * HSIZE)
62 #define BOT_THICKNESS (HSIZE * 0.01)
63 #define WALL_THICKNESS (HSIZE * 0.05)
64 #define WALL_HEIGHT (HSIZE * 0.1)
65 #define GAP (HSIZE * 0.025)
66 #define HINGE_RAD (GAP * 0.5)
67 #define HINGE_HEIGHT (VSIZE * 0.075)
68 #define PIECE_RAD (0.45 * HSIZE / 5.0)
71 static const vec2_t piece_cp[] = {
72 {0, 0.25},
73 {1, 0.25}, // mid0
74 {2, 0.5},
75 {2.5, 0.5}, // mid1
76 {3, 0.5},
77 {4, 0.5}, // mid2
78 {4, 0},
79 {4, -0.5}, // mid3
80 {3, -0.5},
81 {2.5, -0.5}, // mid4
82 {0, -0.5}
83 };
84 static const BezCurve piece_curve = {
85 sizeof piece_cp / sizeof *piece_cp,
86 (vec2_t*)piece_cp,
87 0.25 * PIECE_RAD
88 };
91 bool Board::generate()
92 {
93 Mesh tmp;
94 Matrix4x4 xform;
96 obj.clear();
98 for(int i=0; i<2; i++) {
99 int sign = i * 2 - 1;
101 // generate bottom
102 Mesh *bottom = new Mesh;
103 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
104 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
105 bottom->apply_xform(xform);
107 Object *obottom = new Object;
108 obottom->set_mesh(bottom);
109 obottom->xform().set_translation(Vector3(sign * (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25), 0, 0));
110 obottom->set_texture(img_field.texture());
111 obj.push_back(obottom);
114 // generate the 4 sides
115 Mesh *sides = new Mesh;
116 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
117 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
118 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
119 sides->apply_xform(xform);
121 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
122 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
123 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
124 tmp.apply_xform(xform);
125 sides->append(tmp);
126 tmp.clear();
128 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
129 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
130 (VSIZE + WALL_THICKNESS) / 2.0));
131 tmp.apply_xform(xform);
132 sides->append(tmp);
133 tmp.clear();
135 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
136 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
137 -(VSIZE + WALL_THICKNESS) / 2.0));
138 tmp.apply_xform(xform);
139 sides->append(tmp);
140 tmp.clear();
142 // generate texture coordinates
143 sides->texcoord_gen_box();
145 Object *osides = new Object;
146 osides->set_mesh(sides);
147 osides->xform() = obottom->xform();
148 osides->set_texture(img_wood.texture());
149 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
150 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
151 obj.push_back(osides);
153 }
156 // generate the hinges
157 Mesh *hinges = new Mesh;
158 for(int i=0; i<2; i++) {
159 float sign = i * 2 - 1;
161 // barrel
162 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1);
163 xform.reset_identity();
164 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
165 xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
166 tmp.apply_xform(xform);
167 hinges->append(tmp);
169 // flange
170 gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
171 tmp.apply_xform(xform);
173 Matrix4x4 tex_xform;
174 tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
175 tmp.texcoord_apply_xform(tex_xform);
176 hinges->append(tmp);
178 // studs
179 for(int j=0; j<4; j++) {
180 Vector3 pos;
182 pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
183 pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
185 Matrix4x4 stud_xform = xform;
186 stud_xform.translate(pos);
188 Matrix4x4 squash;
189 squash.set_scaling(Vector3(1, 1, 0.5));
191 gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
192 tmp.apply_xform(stud_xform * squash);
193 hinges->append(tmp);
194 }
195 }
197 Object *ohinges = new Object;
198 ohinges->set_mesh(hinges);
199 ohinges->set_texture(img_hinge.texture());
200 obj.push_back(ohinges);
202 // debug object
203 /*
204 Mesh *dbgmesh = new Mesh;
205 gen_box(dbgmesh, 0.5, 0.5, 0.5);
206 xform.set_translation(Vector3(0, 0.4, 0));
207 xform.set_scaling(Vector3(1, 1, 1));
208 dbgmesh->apply_xform(xform);
209 Object *dbgobj = new Object;
210 dbgobj->set_mesh(dbgmesh);
211 dbgobj->set_texture(img_hinge.texture());
212 //dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
213 obj.push_back(dbgobj);
214 */
216 Mesh *piece = new Mesh;
217 gen_revol(piece, 18, 17, bezier_revol, bezier_revol_normal, (void*)&piece_curve);
219 Object *opiece = new Object;
220 opiece->set_mesh(piece);
221 opiece->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
222 opiece->mtl.specular = Vector3(0.8, 0.8, 0.8);
223 opiece->xform().set_translation(Vector3(0, 0.2, 0));
224 obj.push_back(opiece);
226 // meshgen stats
227 printf("Generated board:\n %u meshes\n", (unsigned int)obj.size());
228 unsigned int polycount = 0;
229 for(size_t i=0; i<obj.size(); i++) {
230 const Mesh *m = obj[i]->get_mesh();
231 polycount += m->get_poly_count();
232 }
233 printf(" %u polygons\n", polycount);
235 return true;
236 }
238 static float wood(float x, float y)
239 {
240 float u = x;
241 float v = y;
242 x += 1.0;
243 x *= 10.0;
244 y *= 20.0;
246 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
247 turbulence2(u * 0.5, v, 2) * 15.0;
248 float val = fmod(len, 1.0);
250 //val = val * 0.5 + 0.5;
251 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
252 }
254 static float wood_tile(float x, float y)
255 {
256 float u = x;
257 float v = y;
258 x *= 10.0;
259 y *= 10.0;
261 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
262 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
264 val = fmod(val, 1.0);
265 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
266 }
268 static bool spike(float x, float y)
269 {
270 x = fmod(x * 5.0, 1.0);
271 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
272 }
274 static bool circle(float x, float y, float rad)
275 {
276 x = fmod(x * 5.0, 1.0) - 0.5;
277 y = (y - 0.65) * 5.0;
278 float len = sqrt(x * x + y * y);
279 return len < rad;
280 }
282 static bool diamond(float x, float y)
283 {
284 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
285 }
287 static bool center_circle(float x, float y, float rad)
288 {
289 x = x - 0.5;
290 y = 1.0 - y;
291 return sqrt(x * x + y * y) < rad;
292 }
294 bool Board::generate_textures()
295 {
296 // ---- board field texture ----
297 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
298 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
299 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
301 img_field.create(1024, 1024);
302 unsigned char *pptr = img_field.pixels;
303 for(int i=0; i<img_field.height; i++) {
304 float v = (float)i / (float)img_field.height;
306 for(int j=0; j<img_field.width; j++) {
307 float u = (float)j / (float)img_field.width;
309 int r = 0, g = 0, b = 0;
311 float wood_val = wood(u, v);
313 // pattern mask
314 float x = u;
315 float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
316 bool inside = false;
318 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
319 (spike(x, y + 0.5) && !spike(x, y + 0.68));
320 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
321 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
322 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
323 inside |= center_circle(x, y, 0.03);
325 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
326 if(inside) {
327 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
328 }
330 r = (int)(wood_color.x * 255.0);
331 g = (int)(wood_color.y * 255.0);
332 b = (int)(wood_color.z * 255.0);
334 pptr[0] = r > 255 ? 255 : r;
335 pptr[1] = g > 255 ? 255 : g;
336 pptr[2] = b > 255 ? 255 : b;
337 pptr += 3;
338 }
339 }
340 img_field.texture();
342 // ---- generic wood texture ----
343 img_wood.create(256, 256);
344 pptr = img_wood.pixels;
345 for(int i=0; i<img_wood.height; i++) {
346 float v = (float)i / (float)img_wood.height;
347 for(int j=0; j<img_wood.width; j++) {
348 float u = (float)j / (float)img_wood.width;
350 float wood_val = wood_tile(u, v);
351 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
353 int r = (int)(wood_color.x * 255.0);
354 int g = (int)(wood_color.y * 255.0);
355 int b = (int)(wood_color.z * 255.0);
357 pptr[0] = r > 255 ? 255 : r;
358 pptr[1] = g > 255 ? 255 : g;
359 pptr[2] = b > 255 ? 255 : b;
360 pptr += 3;
361 }
362 }
363 img_wood.texture();
365 // ---- metal hinge diffuse texture ----
366 Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
367 Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
369 img_hinge.create(128, 128);
370 pptr = img_hinge.pixels;
371 for(int i=0; i<img_hinge.height; i++) {
372 float v = (float)i / (float)img_hinge.height;
373 for(int j=0; j<img_hinge.width; j++) {
374 float u = (float)j / (float)img_hinge.width;
376 // rust pattern
377 float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
378 //float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
379 Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
381 // center hinge split
382 if(fabs(v - 0.5) < 0.01) {
383 col *= 0.5;
384 }
386 int r = (int)(col.x * 255.0);
387 int g = (int)(col.y * 255.0);
388 int b = (int)(col.z * 255.0);
390 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
391 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
392 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
394 pptr += 3;
395 }
396 }
397 img_hinge.texture();
399 return true;
400 }