tavli

diff 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 diff
     1.1 --- a/src/board.cc	Sun Jun 21 06:30:39 2015 +0300
     1.2 +++ b/src/board.cc	Mon Jun 22 05:05:37 2015 +0300
     1.3 @@ -1,11 +1,11 @@
     1.4  #include "opengl.h"
     1.5  #include "board.h"
     1.6 +#include "meshgen.h"
     1.7  
     1.8 -static Mesh *gen_board_mesh();
     1.9 -static Mesh *gen_puck_mesh();
    1.10  
    1.11  Board::Board()
    1.12  {
    1.13 +	puck_mesh = 0;
    1.14  	clear();
    1.15  }
    1.16  
    1.17 @@ -16,20 +16,22 @@
    1.18  
    1.19  bool Board::init()
    1.20  {
    1.21 -	if(!(board_mesh = gen_board_mesh())) {
    1.22 +	if(!generate()) {
    1.23  		return false;
    1.24  	}
    1.25 -	if(!(puck_mesh = gen_puck_mesh())) {
    1.26 -		return false;
    1.27 -	}
    1.28 +
    1.29  	return true;
    1.30  }
    1.31  
    1.32  void Board::destroy()
    1.33  {
    1.34 -	delete board_mesh;
    1.35 +	for(size_t i=0; i<board_meshes.size(); i++) {
    1.36 +		delete board_meshes[i];
    1.37 +	}
    1.38 +	board_meshes.clear();
    1.39 +
    1.40  	delete puck_mesh;
    1.41 -	board_mesh = puck_mesh = 0;
    1.42 +	puck_mesh = 0;
    1.43  }
    1.44  
    1.45  void Board::clear()
    1.46 @@ -39,16 +41,77 @@
    1.47  
    1.48  void Board::draw() const
    1.49  {
    1.50 -	if(board_mesh)
    1.51 -		board_mesh->draw();
    1.52 +	for(size_t i=0; i<board_meshes.size(); i++) {
    1.53 +		board_meshes[i]->draw();
    1.54 +	}
    1.55  }
    1.56  
    1.57 -static Mesh *gen_board_mesh()
    1.58 +#define HSIZE	1.0
    1.59 +#define VSIZE	(2.0 * HSIZE)
    1.60 +#define BOT_THICKNESS	(HSIZE * 0.01)
    1.61 +#define WALL_THICKNESS	(HSIZE * 0.05)
    1.62 +#define WALL_HEIGHT		(HSIZE * 0.1)
    1.63 +#define GAP				(HSIZE * 0.025)
    1.64 +#define HINGE_RAD		(GAP * 0.5)
    1.65 +#define HINGE_HEIGHT	(VSIZE * 0.075)
    1.66 +
    1.67 +bool Board::generate()
    1.68  {
    1.69 -	return 0;
    1.70 +	Matrix4x4 xform;
    1.71 +
    1.72 +	// generate bottom
    1.73 +	Mesh *bottom = new Mesh;
    1.74 +	gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0);
    1.75 +	xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0));
    1.76 +	bottom->apply_xform(xform);
    1.77 +
    1.78 +	// generate the 4 sides
    1.79 +	Mesh *sides = new Mesh;
    1.80 +	gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
    1.81 +	xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0,
    1.82 +				WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
    1.83 +	sides->apply_xform(xform);
    1.84 +
    1.85 +	Mesh tmp;
    1.86 +	gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2);
    1.87 +	xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0,
    1.88 +				WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0));
    1.89 +	tmp.apply_xform(xform);
    1.90 +	sides->append(tmp);
    1.91 +	tmp.clear();
    1.92 +
    1.93 +	gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
    1.94 +	xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
    1.95 +				(VSIZE + WALL_THICKNESS) / 2.0));
    1.96 +	tmp.apply_xform(xform);
    1.97 +	sides->append(tmp);
    1.98 +	tmp.clear();
    1.99 +
   1.100 +	gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS);
   1.101 +	xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS,
   1.102 +				-(VSIZE + WALL_THICKNESS) / 2.0));
   1.103 +	tmp.apply_xform(xform);
   1.104 +	sides->append(tmp);
   1.105 +	tmp.clear();
   1.106 +
   1.107 +	// generate the hinges
   1.108 +	Mesh *hinges = new Mesh;
   1.109 +	gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
   1.110 +	xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
   1.111 +	xform.translate(Vector3(0, VSIZE / 4.0, 0));
   1.112 +	hinges->apply_xform(xform);
   1.113 +
   1.114 +	gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1);
   1.115 +	xform.set_rotation(Vector3(M_PI / 2.0, 0, 0));
   1.116 +	xform.translate(Vector3(0, -VSIZE / 4.0, 0));
   1.117 +	tmp.apply_xform(xform);
   1.118 +
   1.119 +	hinges->append(tmp);
   1.120 +
   1.121 +
   1.122 +	board_meshes.clear();
   1.123 +	board_meshes.push_back(bottom);
   1.124 +	board_meshes.push_back(sides);
   1.125 +	board_meshes.push_back(hinges);
   1.126 +	return true;
   1.127  }
   1.128 -
   1.129 -static Mesh *gen_puck_mesh()
   1.130 -{
   1.131 -	return 0;
   1.132 -}