tavli

view src/board.cc @ 2:893192aea099

board keeps objects instead of raw meshes
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Jun 2015 05:15:39 +0300
parents 3fcd7b4d631f
children b41ceead1708
line source
1 #include "opengl.h"
2 #include "board.h"
3 #include "meshgen.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()) {
20 return false;
21 }
23 return true;
24 }
26 void Board::destroy()
27 {
28 for(size_t i=0; i<obj.size(); i++) {
29 delete obj[i];
30 }
31 obj.clear();
33 delete puck_obj;
34 puck_obj = 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<obj.size(); i++) {
45 obj[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 obj.clear();
64 // generate bottom
65 Mesh *bottom = new Mesh;
66 gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
67 xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
68 bottom->apply_xform(xform);
70 Object *obottom = new Object;
71 obottom->set_mesh(bottom);
72 obj.push_back(obottom);
75 // generate the 4 sides
76 Mesh *sides = new Mesh;
77 gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
78 xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
79 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
80 sides->apply_xform(xform);
82 Mesh tmp;
83 gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
84 xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
85 WALL_HEIGHT / 2.0 - BOT_THICKNESS, 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 gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
98 xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
99 -(VSIZE + WALL_THICKNESS) / 2.0));
100 tmp.apply_xform(xform);
101 sides->append(tmp);
102 tmp.clear();
104 Object *osides = new Object;
105 osides->set_mesh(sides);
106 obj.push_back(osides);
109 // generate the hinges
110 Mesh *hinges = new Mesh;
111 gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
112 xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
113 xform.translate(Vector3(0, VSIZE / 4.0, 0));
114 hinges->apply_xform(xform);
116 gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
117 xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
118 xform.translate(Vector3(0, -VSIZE / 4.0, 0));
119 tmp.apply_xform(xform);
121 hinges->append(tmp);
123 Object *ohinges = new Object;
124 ohinges->set_mesh(hinges);
125 obj.push_back(ohinges);
128 return true;
129 }