tavli

diff src/board.cc @ 21:c3fbf9616dbd

slot bounds, and ray testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 02 Jul 2015 00:01:39 +0300
parents 37dead56f01e
children c2a2069a49ec
line diff
     1.1 --- a/src/board.cc	Mon Jun 29 21:55:36 2015 +0300
     1.2 +++ b/src/board.cc	Thu Jul 02 00:01:39 2015 +0300
     1.3 @@ -19,6 +19,8 @@
     1.4  #define PIECE_RAD		(0.45 * HSIZE / 5.0)
     1.5  #define BOARD_OFFSET	(HSIZE / 2.0 + WALL_THICKNESS + HINGE_RAD * 0.25)
     1.6  #define PIECES_PER_LAYER	5
     1.7 +#define SLOT_WIDTH		(HSIZE / 5.0)
     1.8 +#define SLOT_HEIGHT		(VSIZE * 0.4)
     1.9  
    1.10  
    1.11  static const vec2_t piece_cp[] = {
    1.12 @@ -66,11 +68,37 @@
    1.13  	}
    1.14  }
    1.15  
    1.16 +Quad::Quad()
    1.17 +{
    1.18 +}
    1.19 +
    1.20 +Quad::Quad(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3)
    1.21 +	: tri0(v0, v1, v2), tri1(v0, v2, v3)
    1.22 +{
    1.23 +}
    1.24 +
    1.25 +bool Quad::intersect(const Ray &ray, HitPoint *hit) const
    1.26 +{
    1.27 +	return tri0.intersect(ray, hit) || tri1.intersect(ray, hit);
    1.28 +}
    1.29  
    1.30  Board::Board()
    1.31  {
    1.32  	piece_obj = 0;
    1.33  	clear();
    1.34 +
    1.35 +	for(int i=0; i<NUM_SLOTS; i++) {
    1.36 +		Vector3 p = piece_pos(i, 0);
    1.37 +		bool top_side = i >= NUM_SLOTS / 2;
    1.38 +
    1.39 +		float z0 = top_side ? -PIECE_RAD : PIECE_RAD;
    1.40 +		float z1 = top_side ? SLOT_HEIGHT : -SLOT_HEIGHT;
    1.41 +
    1.42 +		slotbb[i] = Quad(p + Vector3(-SLOT_WIDTH / 2.0, 0, z0),
    1.43 +				p + Vector3(SLOT_WIDTH / 2.0, 0, z0),
    1.44 +				p + Vector3(SLOT_WIDTH / 2.0, 0, z1),
    1.45 +				p + Vector3(-SLOT_WIDTH / 2.0, 0, z1));
    1.46 +	}
    1.47  }
    1.48  
    1.49  Board::~Board()
    1.50 @@ -163,13 +191,23 @@
    1.51  	pos.y = (layer + 0.5) * PIECE_HEIGHT;
    1.52  
    1.53  	pos.z = (-VSIZE * 0.5 + PIECE_RAD + PIECE_RAD * 2.0 * layer_level);
    1.54 -	if(top_side) {
    1.55 +	if(!top_side) {
    1.56  		pos.z = -pos.z;
    1.57  	}
    1.58  
    1.59  	return pos;
    1.60  }
    1.61  
    1.62 +int Board::slot_hit(const Ray &ray) const
    1.63 +{
    1.64 +	for(int i=0; i<NUM_SLOTS; i++) {
    1.65 +		if(slotbb[i].intersect(ray)) {
    1.66 +			return i;
    1.67 +		}
    1.68 +	}
    1.69 +	return -1;
    1.70 +}
    1.71 +
    1.72  void Board::draw() const
    1.73  {
    1.74  	bool use_shadows = opt.shadows && sdr_shadow;
    1.75 @@ -193,6 +231,40 @@
    1.76  		piece_obj->set_shader(piece_sdr);
    1.77  		piece_obj->draw();
    1.78  	}
    1.79 +
    1.80 +	// draw the slot bounds
    1.81 +	/*
    1.82 +	static const float pal[][3] = {
    1.83 +		{1, 0, 0},
    1.84 +		{0, 1, 0},
    1.85 +		{0, 0, 1},
    1.86 +		{1, 1, 0},
    1.87 +		{0, 1, 1},
    1.88 +		{1, 0, 1}
    1.89 +	};
    1.90 +	int idx = dbg_int % NUM_SLOTS;
    1.91 +	if(idx >= 0) {
    1.92 +		glUseProgram(0);
    1.93 +
    1.94 +		glPushAttrib(GL_ENABLE_BIT);
    1.95 +		glDisable(GL_LIGHTING);
    1.96 +		glDisable(GL_CULL_FACE);
    1.97 +		glDisable(GL_DEPTH_TEST);
    1.98 +
    1.99 +		glBegin(GL_TRIANGLES);
   1.100 +		glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
   1.101 +		glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z);
   1.102 +		glVertex3f(slotbb[idx].tri0.v[1].x, slotbb[idx].tri0.v[1].y, slotbb[idx].tri0.v[1].z);
   1.103 +		glVertex3f(slotbb[idx].tri0.v[2].x, slotbb[idx].tri0.v[2].y, slotbb[idx].tri0.v[2].z);
   1.104 +		glVertex3f(slotbb[idx].tri1.v[0].x, slotbb[idx].tri1.v[0].y, slotbb[idx].tri1.v[0].z);
   1.105 +		glVertex3f(slotbb[idx].tri1.v[1].x, slotbb[idx].tri1.v[1].y, slotbb[idx].tri1.v[1].z);
   1.106 +		glVertex3f(slotbb[idx].tri1.v[2].x, slotbb[idx].tri1.v[2].y, slotbb[idx].tri1.v[2].z);
   1.107 +		glEnd();
   1.108 +
   1.109 +		glPopAttrib();
   1.110 +	}
   1.111 +	*/
   1.112 +	// TODO slot highlighting
   1.113  }
   1.114  
   1.115