tavli

view src/board.cc @ 22:c2a2069a49ec

slot highlighting, TODO blur
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Jul 2015 21:56:37 +0300
parents c3fbf9616dbd
children 3e6430028d54
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"
8 #include "opt.h"
11 #define HSIZE 1.0
12 #define VSIZE (2.0 * HSIZE)
13 #define BOT_THICKNESS (HSIZE * 0.01)
14 #define WALL_THICKNESS (HSIZE * 0.05)
15 #define WALL_HEIGHT (HSIZE * 0.1)
16 #define GAP (HSIZE * 0.025)
17 #define HINGE_RAD (GAP * 0.5)
18 #define HINGE_HEIGHT (VSIZE * 0.075)
19 #define PIECE_RAD (0.45 * HSIZE / 5.0)
20 #define BOARD_OFFSET (HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25)
21 #define PIECES_PER_LAYER 5
22 #define SLOT_WIDTH (HSIZE / 5.0)
23 #define SLOT_HEIGHT (VSIZE * 0.4)
26 static const vec2_t piece_cp[] = {
27 {0, 0.25},
28 {1, 0.25}, // mid0
29 {2, 0.5},
30 {2.5, 0.5}, // mid1
31 {3, 0.5},
32 {4, 0.5}, // mid2
33 {4, 0},
34 {4, -0.5}, // mid3
35 {3, -0.5},
36 {2.5, -0.5}, // mid4
37 {0, -0.5}
38 };
39 static const BezCurve piece_curve = {
40 sizeof piece_cp / sizeof *piece_cp,
41 (vec2_t*)piece_cp,
42 0.25 * PIECE_RAD
43 };
45 #define PIECE_HEIGHT (0.25 * PIECE_RAD)
48 Piece::Piece()
49 {
50 owner = 0;
51 slot = prev_slot = -1;
52 level = 0;
53 move_start = 0;
54 }
56 void Piece::move_to(int slot, int level, bool anim)
57 {
58 int prev_slot = this->slot;
59 int prev_level = this->level;
61 this->slot = slot;
62 this->level = level;
64 if(anim) {
65 this->prev_slot = prev_slot;
66 this->prev_level = prev_level;
67 move_start = cur_time;
68 }
69 }
71 Quad::Quad()
72 {
73 }
75 Quad::Quad(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
76 : tri0(v0, v1, v2), tri1(v0, v2, v3)
77 {
78 }
80 bool Quad::intersect(const Ray &ray, HitPoint *hit) const
81 {
82 return tri0.intersect(ray, hit) || tri1.intersect(ray, hit);
83 }
85 Board::Board()
86 {
87 piece_obj = 0;
88 slot_sel = -1;
89 clear();
91 for(int i=0; i<NUM_SLOTS; i++) {
92 Vector3 p = piece_pos(i, 0);
93 bool top_side = i >= NUM_SLOTS / 2;
95 float z0 = top_side ? -PIECE_RAD : PIECE_RAD;
96 float z1 = top_side ? SLOT_HEIGHT : -SLOT_HEIGHT;
98 slotbb[i] = Quad(p + Vector3(-SLOT_WIDTH / 2.0, 0, z0),
99 p + Vector3(SLOT_WIDTH / 2.0, 0, z0),
100 p + Vector3(SLOT_WIDTH / 2.0, 0, z1),
101 p + Vector3(-SLOT_WIDTH / 2.0, 0, z1));
102 }
103 }
105 Board::~Board()
106 {
107 destroy();
108 }
110 bool Board::init()
111 {
112 if(!generate_textures()) {
113 return false;
114 }
115 if(!generate()) {
116 return false;
117 }
119 return true;
120 }
122 void Board::destroy()
123 {
124 for(size_t i=0; i<obj.size(); i++) {
125 delete obj[i];
126 }
127 obj.clear();
129 delete piece_obj;
130 piece_obj = 0;
131 }
133 void Board::clear()
134 {
135 memset(hist, 0, sizeof hist);
137 for(int i=0; i<MAX_PIECES; i++) {
138 pieces[i].owner = i < PLAYER_PIECES ? MINE : OTHER;
139 move_piece(i, -1, false);
140 }
141 }
143 void Board::setup()
144 {
145 static const int initial[] = { 0, 0, 0, 0, 5, 0, 3, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
147 clear();
149 int id = 0;
150 for(int i=0; i<NUM_SLOTS; i++) {
151 for(int j=0; j<initial[i]; j++) {
152 move_piece(id, i, false);
153 move_piece(PLAYER_PIECES + id, NUM_SLOTS - i - 1, false);
154 ++id;
155 }
156 }
157 }
159 int Board::slot_pieces(int slot) const
160 {
161 return hist[slot + 1];
162 }
164 bool Board::move_piece(int id, int slot, bool anim)
165 {
166 // TODO do validity checking first
167 int prev_slot = pieces[id].slot;
169 pieces[id].move_to(slot, slot_pieces(slot), anim);
170 --hist[prev_slot + 1];
171 ++hist[slot + 1];
172 return true;
173 }
175 Vector3 Board::piece_pos(int slot, int level) const
176 {
177 int top_side = slot / 10;
178 int sidx = (top_side ? (19 - slot) : slot) % 5;
179 int left_side = (top_side ? (19 - slot) : slot) / 5;
181 Vector3 pos;
183 if(left_side) {
184 pos.x = -(sidx * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0) - PIECE_RAD;
185 } else {
186 pos.x = (4 - sidx) * HSIZE / 5.0 + BOARD_OFFSET - HSIZE / 2.0 + PIECE_RAD;
187 }
189 int layer = level / PIECES_PER_LAYER;
190 int layer_level = level % PIECES_PER_LAYER;
192 pos.y = (layer + 0.5) * PIECE_HEIGHT;
194 pos.z = (-VSIZE * 0.5 + PIECE_RAD + PIECE_RAD * 2.0 * layer_level);
195 if(!top_side) {
196 pos.z = -pos.z;
197 }
199 return pos;
200 }
202 int Board::slot_hit(const Ray &ray) const
203 {
204 for(int i=0; i<NUM_SLOTS; i++) {
205 if(slotbb[i].intersect(ray)) {
206 return i;
207 }
208 }
209 return -1;
210 }
212 void Board::select_slot(int idx)
213 {
214 slot_sel = idx < 0 || idx >= NUM_SLOTS ? -1 : idx;
215 }
217 int Board::get_selected_slot() const
218 {
219 return slot_sel;
220 }
222 void Board::draw() const
223 {
224 bool use_shadows = opt.shadows && sdr_shadow;
225 unsigned int board_sdr = use_shadows ? sdr_shadow : sdr_phong;
226 unsigned int piece_sdr = use_shadows ? sdr_shadow_notex : sdr_phong_notex;
228 for(size_t i=0; i<obj.size(); i++) {
229 if(wireframe) {
230 obj[i]->draw_wire();
231 obj[i]->draw_normals(0.075);
232 } else {
233 obj[i]->set_shader(board_sdr);
234 obj[i]->draw();
235 }
236 }
238 for(int i=0; i<MAX_PIECES; i++) {
239 Vector3 pos = piece_pos(pieces[i].slot, pieces[i].level);
240 piece_obj->xform().set_translation(pos);
241 piece_obj->mtl.diffuse = opt.piece_color[pieces[i].owner];
242 piece_obj->set_shader(piece_sdr);
243 piece_obj->draw();
244 }
246 /*static const float pal[][3] = {
247 {1, 0, 0},
248 {0, 1, 0},
249 {0, 0, 1},
250 {1, 1, 0},
251 {0, 1, 1},
252 {1, 0, 1}
253 };*/
254 int idx = slot_sel % NUM_SLOTS;
255 if(idx >= 0) {
256 glUseProgram(0);
258 glPushAttrib(GL_ENABLE_BIT);
259 glDisable(GL_LIGHTING);
260 glDisable(GL_CULL_FACE);
261 glEnable(GL_DEPTH_TEST);
262 glEnable(GL_TEXTURE_2D);
263 glBindTexture(GL_TEXTURE_2D, img_highlight.texture());
264 glEnable(GL_BLEND);
265 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
267 glDepthMask(0);
269 glBegin(GL_TRIANGLES);
270 //glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
271 glColor4f(1, 1, 1, 0.4);
272 glTexCoord2f(0, 0);
273 glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z);
274 glTexCoord2f(1, 0);
275 glVertex3f(slotbb[idx].tri0.v[1].x, slotbb[idx].tri0.v[1].y, slotbb[idx].tri0.v[1].z);
276 glTexCoord2f(1, 1);
277 glVertex3f(slotbb[idx].tri0.v[2].x, slotbb[idx].tri0.v[2].y, slotbb[idx].tri0.v[2].z);
278 glTexCoord2f(0, 0);
279 glVertex3f(slotbb[idx].tri1.v[0].x, slotbb[idx].tri1.v[0].y, slotbb[idx].tri1.v[0].z);
280 glTexCoord2f(1, 1);
281 glVertex3f(slotbb[idx].tri1.v[1].x, slotbb[idx].tri1.v[1].y, slotbb[idx].tri1.v[1].z);
282 glTexCoord2f(0, 1);
283 glVertex3f(slotbb[idx].tri1.v[2].x, slotbb[idx].tri1.v[2].y, slotbb[idx].tri1.v[2].z);
284 glEnd();
286 glDepthMask(1);
288 glPopAttrib();
289 }
290 // TODO slot highlighting
291 }
294 bool Board::generate()
295 {
296 static const float board_spec = 0.4;
298 Mesh tmp;
299 Matrix4x4 xform;
301 obj.clear();
303 for(int i=0; i<2; i++) {
304 int sign = i * 2 - 1;
306 // generate bottom
307 Mesh *bottom = new Mesh;
308 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
309 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
310 bottom->apply_xform(xform);
312 Object *obottom = new Object;
313 obottom->set_mesh(bottom);
314 obottom->xform().set_translation(Vector3(sign * BOARD_OFFSET, 0, 0));
315 obottom->set_texture(img_field.texture());
316 obottom->mtl.specular = Vector3(board_spec, board_spec, board_spec);
317 obj.push_back(obottom);
320 // generate the 4 sides
321 Mesh *sides = new Mesh;
322 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
323 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
324 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
325 sides->apply_xform(xform);
327 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
328 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
329 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
330 tmp.apply_xform(xform);
331 sides->append(tmp);
332 tmp.clear();
334 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
335 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
336 (VSIZE + WALL_THICKNESS) / 2.0));
337 tmp.apply_xform(xform);
338 sides->append(tmp);
339 tmp.clear();
341 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
342 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
343 -(VSIZE + WALL_THICKNESS) / 2.0));
344 tmp.apply_xform(xform);
345 sides->append(tmp);
346 tmp.clear();
348 // generate texture coordinates
349 sides->texcoord_gen_box();
351 Object *osides = new Object;
352 osides->set_mesh(sides);
353 osides->xform() = obottom->xform();
354 osides->set_texture(img_wood.texture());
355 osides->tex_xform().set_scaling(Vector3(2, 2, 2));
356 osides->tex_xform().rotate(-Vector3(1, 0, 0.5), M_PI / 4.0);
357 osides->mtl.specular = Vector3(board_spec, board_spec, board_spec);
358 obj.push_back(osides);
360 }
363 // generate the hinges
364 Mesh *hinges = new Mesh;
365 for(int i=0; i<2; i++) {
366 float sign = i * 2 - 1;
368 // barrel
369 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1);
370 xform.reset_identity();
371 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0));
372 xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
373 tmp.apply_xform(xform);
374 hinges->append(tmp);
376 // flange
377 gen_plane(&tmp, HINGE_HEIGHT * 0.6, HINGE_HEIGHT * 0.8);
378 tmp.apply_xform(xform);
380 Matrix4x4 tex_xform;
381 tex_xform.set_rotation(Vector3(0, 0, M_PI / 2.0));
382 tmp.texcoord_apply_xform(tex_xform);
383 hinges->append(tmp);
385 // studs
386 for(int j=0; j<4; j++) {
387 Vector3 pos;
389 pos.x = (float)((j & 1) * 2 - 1) * HINGE_HEIGHT * 0.2;
390 pos.y = (float)((j & 2) - 1) * HINGE_HEIGHT * 0.3;
392 Matrix4x4 stud_xform = xform;
393 stud_xform.translate(pos);
395 Matrix4x4 squash;
396 squash.set_scaling(Vector3(1, 1, 0.5));
398 gen_sphere(&tmp, HINGE_RAD * 0.5, 8, 4);
399 tmp.apply_xform(stud_xform * squash);
400 hinges->append(tmp);
401 }
402 }
404 Object *ohinges = new Object;
405 ohinges->set_mesh(hinges);
406 ohinges->set_texture(img_hinge.texture());
407 obj.push_back(ohinges);
409 // debug object
410 /*
411 Mesh *dbgmesh = new Mesh;
412 gen_box(dbgmesh, 0.5, 0.5, 0.5);
413 xform.set_translation(Vector3(0, 0.4, 0));
414 xform.set_scaling(Vector3(1, 1, 1));
415 dbgmesh->apply_xform(xform);
416 Object *dbgobj = new Object;
417 dbgobj->set_mesh(dbgmesh);
418 dbgobj->set_texture(img_hinge.texture());
419 //dbgobj->tex_xform().set_scaling(Vector3(3, 3, 3));
420 obj.push_back(dbgobj);
421 */
423 Mesh *piece = new Mesh;
424 gen_revol(piece, 18, 17, bezier_revol, bezier_revol_normal, (void*)&piece_curve);
426 Object *opiece = new Object;
427 opiece->set_mesh(piece);
428 opiece->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
429 opiece->mtl.specular = Vector3(0.8, 0.8, 0.8);
430 opiece->xform().set_translation(Vector3(0, 0.2, 0));
431 //obj.push_back(opiece);
433 piece_obj = opiece;
435 // meshgen stats
436 printf("Generated board:\n %u meshes\n", (unsigned int)obj.size());
437 unsigned int polycount = 0;
438 for(size_t i=0; i<obj.size(); i++) {
439 const Mesh *m = obj[i]->get_mesh();
440 polycount += m->get_poly_count();
441 }
442 printf(" %u polygons\n", polycount);
444 return true;
445 }
447 static float wood(float x, float y)
448 {
449 float u = x;
450 float v = y;
451 x += 1.0;
452 x *= 10.0;
453 y *= 20.0;
455 float len = sqrt(x * x + y * y) + turbulence2(u * 6.0, v * 12.0, 2) * 1.2 +
456 turbulence2(u * 0.5, v, 2) * 15.0;
457 float val = fmod(len, 1.0);
459 //val = val * 0.5 + 0.5;
460 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
461 }
463 static float wood_tile(float x, float y)
464 {
465 float u = x;
466 float v = y;
467 x *= 10.0;
468 y *= 10.0;
470 float val = x + pnoise2(u * 6.0, v, 6, 1) * 3.0 +
471 pturbulence2(u * 4, v * 2, 4, 2, 2) * 1.5 + pturbulence2(u * 8, v * 8, 8, 8, 2) * 0.5;
473 val = fmod(val, 1.0);
474 return val < 0.0 ? 0.0 : (val > 1.0 ? 1.0 : val);
475 }
477 static bool spike(float x, float y)
478 {
479 x = fmod(x * 5.0, 1.0);
480 return y < (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x);
481 }
483 static bool circle(float x, float y, float rad)
484 {
485 x = fmod(x * 5.0, 1.0) - 0.5;
486 y = (y - 0.65) * 5.0;
487 float len = sqrt(x * x + y * y);
488 return len < rad;
489 }
491 static bool diamond(float x, float y)
492 {
493 return y >= (1.0 - (x < 0.5 ? 2.0 * x : 2.0 - 2.0 * x)) * 0.3333333 + 0.88;
494 }
496 static bool center_circle(float x, float y, float rad)
497 {
498 x = x - 0.5;
499 y = 1.0 - y;
500 return sqrt(x * x + y * y) < rad;
501 }
503 static bool field_pattern(float x, float y)
504 {
505 y = y < 0.5 ? y * 2.0 : 2.0 - y * 2.0;
506 bool inside = false;
508 inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
509 (spike(x, y + 0.5) && !spike(x, y + 0.68));
510 inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
511 inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
512 (diamond(x, y - 0.023) && !diamond(x, y - 0.028));
513 inside |= center_circle(x, y, 0.03);
515 return inside;
516 }
518 bool Board::generate_textures()
519 {
520 // ---- board field texture ----
521 static const Vector3 wcol1 = Vector3(0.6, 0.4, 0.2);
522 static const Vector3 wcol2 = Vector3(0.53, 0.32, 0.1);
523 static const Vector3 wcol3 = Vector3(0.38, 0.25, 0.08);
525 img_field.create(1024, 1024);
526 unsigned char *pptr = img_field.pixels;
527 for(int i=0; i<img_field.height; i++) {
528 float v = (float)i / (float)img_field.height;
530 for(int j=0; j<img_field.width; j++) {
531 float u = (float)j / (float)img_field.width;
533 // pattern mask
534 bool inside = field_pattern(u, v);
536 float wood_val = wood(u, v);
537 Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
538 if(inside) {
539 wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
540 }
542 int r = (int)(wood_color.x * 255.0);
543 int g = (int)(wood_color.y * 255.0);
544 int b = (int)(wood_color.z * 255.0);
546 pptr[0] = r > 255 ? 255 : r;
547 pptr[1] = g > 255 ? 255 : g;
548 pptr[2] = b > 255 ? 255 : b;
549 pptr[3] = 255;
550 pptr += 4;
551 }
552 }
553 img_field.texture();
556 // ---- slot highlighting texture ----
557 img_highlight.create(128, 256);
558 pptr = img_highlight.pixels;
559 for(int i=0; i<img_highlight.height; i++) {
560 float v = (float)i / (float)img_highlight.height;
561 for(int j=0; j<img_highlight.width; j++) {
562 float u = (float)j / (float)img_highlight.width;
564 bool inside = field_pattern(u / 5.0, v * 0.445);
566 pptr[0] = 255;
567 pptr[1] = 255;
568 pptr[2] = 255;
569 pptr[3] = inside ? 255 : 0;
570 pptr += 4;
571 }
572 }
573 img_highlight.texture();
576 // ---- generic wood texture ----
577 img_wood.create(256, 256);
578 pptr = img_wood.pixels;
579 for(int i=0; i<img_wood.height; i++) {
580 float v = (float)i / (float)img_wood.height;
581 for(int j=0; j<img_wood.width; j++) {
582 float u = (float)j / (float)img_wood.width;
584 float wood_val = wood_tile(u, v);
585 Vector3 wood_color = lerp(wcol2, wcol3, wood_val) * 0.7;
587 int r = (int)(wood_color.x * 255.0);
588 int g = (int)(wood_color.y * 255.0);
589 int b = (int)(wood_color.z * 255.0);
591 pptr[0] = r > 255 ? 255 : r;
592 pptr[1] = g > 255 ? 255 : g;
593 pptr[2] = b > 255 ? 255 : b;
594 pptr[3] = 255;
595 pptr += 4;
596 }
597 }
598 img_wood.texture();
600 // ---- metal hinge diffuse texture ----
601 Vector3 rusty_col1 = Vector3(0.43, 0.46, 0.52);
602 Vector3 rusty_col2 = Vector3(0.52, 0.47, 0.43);
604 img_hinge.create(128, 128);
605 pptr = img_hinge.pixels;
606 for(int i=0; i<img_hinge.height; i++) {
607 float v = (float)i / (float)img_hinge.height;
608 for(int j=0; j<img_hinge.width; j++) {
609 float u = (float)j / (float)img_hinge.width;
611 // rust pattern
612 float w1 = fbm2(u * 4.0, v * 4.0, 3) * 0.5 + 0.5;
613 //float w2 = fbm2(u * 8.0, v * 8.0, 1) * 0.5 + 0.5;
614 Vector3 col = lerp(rusty_col1, rusty_col2 * 0.5, w1);
616 // center hinge split
617 if(fabs(v - 0.5) < 0.01) {
618 col *= 0.5;
619 }
621 int r = (int)(col.x * 255.0);
622 int g = (int)(col.y * 255.0);
623 int b = (int)(col.z * 255.0);
625 pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
626 pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
627 pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
628 pptr[3] = 255;
630 pptr += 4;
631 }
632 }
633 img_hinge.texture();
635 return true;
636 }