tavli

changeset 21:c3fbf9616dbd

slot bounds, and ray testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 02 Jul 2015 00:01:39 +0300
parents 111c1a9bca29
children c2a2069a49ec
files src/board.cc src/board.h src/game.cc src/game.h
diffstat 4 files changed, 101 insertions(+), 1 deletions(-) [+]
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  
     2.1 --- a/src/board.h	Mon Jun 29 21:55:36 2015 +0300
     2.2 +++ b/src/board.h	Thu Jul 02 00:01:39 2015 +0300
     2.3 @@ -3,6 +3,7 @@
     2.4  
     2.5  #include <vector>
     2.6  #include "object.h"
     2.7 +#include "mesh.h"
     2.8  #include "image.h"
     2.9  
    2.10  #define NUM_SLOTS		20
    2.11 @@ -24,11 +25,22 @@
    2.12  	void move_to(int slot, int level, bool anim = true);
    2.13  };
    2.14  
    2.15 +// for slot bounds
    2.16 +class Quad {
    2.17 +public:
    2.18 +	Triangle tri0, tri1;
    2.19 +
    2.20 +	Quad();
    2.21 +	Quad(const Vector3 &v0, const Vector3 &v1, const Vector3 &v2, const Vector3 &v3);
    2.22 +
    2.23 +	bool intersect(const Ray &ray, HitPoint *hit = 0) const;
    2.24 +};
    2.25  
    2.26  class Board {
    2.27  private:
    2.28  	Piece pieces[MAX_PIECES];
    2.29  	int hist[NUM_SLOTS + 1];
    2.30 +	Quad slotbb[NUM_SLOTS];
    2.31  
    2.32  	std::vector<Object*> obj;
    2.33  	Object *piece_obj;
    2.34 @@ -53,6 +65,8 @@
    2.35  
    2.36  	Vector3 piece_pos(int slot, int level = 0) const;
    2.37  
    2.38 +	int slot_hit(const Ray &ray) const;
    2.39 +
    2.40  	void draw() const;
    2.41  };
    2.42  
     3.1 --- a/src/game.cc	Mon Jun 29 21:55:36 2015 +0300
     3.2 +++ b/src/game.cc	Thu Jul 02 00:01:39 2015 +0300
     3.3 @@ -17,6 +17,7 @@
     3.4  unsigned int sdr_shadow, sdr_shadow_notex;
     3.5  unsigned int sdr_unlit;
     3.6  bool wireframe;
     3.7 +int dbg_int;
     3.8  
     3.9  static Board board;
    3.10  
    3.11 @@ -252,6 +253,18 @@
    3.12  			opt.shadows = !opt.shadows;
    3.13  			redisplay();
    3.14  			break;
    3.15 +
    3.16 +		case '=':
    3.17 +			dbg_int++;
    3.18 +			printf("dbg_int: %d\n", dbg_int);
    3.19 +			redisplay();
    3.20 +			break;
    3.21 +
    3.22 +		case '-':
    3.23 +			dbg_int--;
    3.24 +			printf("dbg_int: %d\n", dbg_int);
    3.25 +			redisplay();
    3.26 +			break;
    3.27  		}
    3.28  	}
    3.29  }
     4.1 --- a/src/game.h	Mon Jun 29 21:55:36 2015 +0300
     4.2 +++ b/src/game.h	Thu Jul 02 00:01:39 2015 +0300
     4.3 @@ -8,6 +8,7 @@
     4.4  extern unsigned long cur_time;
     4.5  
     4.6  extern bool wireframe;
     4.7 +extern int dbg_int;
     4.8  
     4.9  bool game_init();
    4.10  void game_cleanup();