tavli

view src/board.cc @ 1:3fcd7b4d631f

board mesh generation
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Jun 2015 05:05:37 +0300
parents 52e0dd47753b
children 893192aea099
line source
1 #include "opengl.h"
2 #include "board.h"
3 #include "meshgen.h"
6 Board::Board()
7 {
8 puck_mesh = 0;
9 clear();
10 }
12 Board::~Board()
13 {
14 destroy();
15 }
17 bool Board::init()
18 {
19 if(!generate()) {
20 return false;
21 }
23 return true;
24 }
26 void Board::destroy()
27 {
28 for(size_t i=0; i<board_meshes.size(); i++) {
29 delete board_meshes[i];
30 }
31 board_meshes.clear();
33 delete puck_mesh;
34 puck_mesh = 0;
35 }
37 void Board::clear()
38 {
39 memset(slots, 0, sizeof slots);
40 }
42 void Board::draw() const
43 {
44 for(size_t i=0; i<board_meshes.size(); i++) {
45 board_meshes[i]->draw();
46 }
47 }
49 #define HSIZE 1.0
50 #define VSIZE (2.0 * HSIZE)
51 #define BOT_THICKNESS (HSIZE * 0.01)
52 #define WALL_THICKNESS (HSIZE * 0.05)
53 #define WALL_HEIGHT (HSIZE * 0.1)
54 #define GAP (HSIZE * 0.025)
55 #define HINGE_RAD (GAP * 0.5)
56 #define HINGE_HEIGHT (VSIZE * 0.075)
58 bool Board::generate()
59 {
60 Matrix4x4 xform;
62 // generate bottom
63 Mesh *bottom = new Mesh;
64 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
65 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
66 bottom->apply_xform(xform);
68 // generate the 4 sides
69 Mesh *sides = new Mesh;
70 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
71 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
72 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
73 sides->apply_xform(xform);
75 Mesh tmp;
76 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
77 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
78 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
79 tmp.apply_xform(xform);
80 sides->append(tmp);
81 tmp.clear();
83 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
84 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
85 (VSIZE + WALL_THICKNESS) / 2.0));
86 tmp.apply_xform(xform);
87 sides->append(tmp);
88 tmp.clear();
90 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
91 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
92 -(VSIZE + WALL_THICKNESS) / 2.0));
93 tmp.apply_xform(xform);
94 sides->append(tmp);
95 tmp.clear();
97 // generate the hinges
98 Mesh *hinges = new Mesh;
99 gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
100 xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
101 xform.translate(Vector3(0, VSIZE / 4.0, 0));
102 hinges->apply_xform(xform);
104 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
105 xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
106 xform.translate(Vector3(0, -VSIZE / 4.0, 0));
107 tmp.apply_xform(xform);
109 hinges->append(tmp);
112 board_meshes.clear();
113 board_meshes.push_back(bottom);
114 board_meshes.push_back(sides);
115 board_meshes.push_back(hinges);
116 return true;
117 }