tavli

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