tavli

diff src/board.cc @ 13:f3c5134b4914

generalized the surfaces of revolution evaluator a bit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Jun 2015 22:33:27 +0300
parents ae1c60726c41
children 283eb6e9f0a3
line diff
     1.1 --- a/src/board.cc	Sat Jun 27 13:53:43 2015 +0300
     1.2 +++ b/src/board.cc	Sat Jun 27 22:33:27 2015 +0300
     1.3 @@ -60,44 +60,62 @@
     1.4  #define HINGE_HEIGHT	(VSIZE * 0.075)
     1.5  #define PIECE_RAD		(0.45 * HSIZE / 5.0)
     1.6  
     1.7 -static const float piece_cp[][3][2] = {
     1.8 -	{{0, 0.25}, {1, 0.25}, {2, 0.5}},
     1.9 -	{{2, 0.5}, {2.5, 0.5}, {3, 0.5}},
    1.10 -	{{3, 0.5}, {4, 0.5}, {4, 0}},
    1.11 -	{{4, 0}, {4, -0.5}, {3, -0.5}},
    1.12 -	{{3, -0.5}, {2.5, -0.5}, {0, -0.5}},
    1.13 -	//{{2, -0.5}, {1, -0.25}, {0, -0.25}}
    1.14 +struct BezCurve {
    1.15 +	int numcp;
    1.16 +	vec2_t *cp;
    1.17 +	float scale;
    1.18  };
    1.19 -static const int piece_ncurves = sizeof piece_cp / sizeof *piece_cp;
    1.20 +
    1.21 +static const vec2_t piece_cp[] = {
    1.22 +		{0, 0.25},
    1.23 +		{1, 0.25},	// mid0
    1.24 +		{2, 0.5},
    1.25 +		{2.5, 0.5},	// mid1
    1.26 +		{3, 0.5},
    1.27 +		{4, 0.5},	// mid2
    1.28 +		{4, 0},
    1.29 +		{4, -0.5},	// mid3
    1.30 +		{3, -0.5},
    1.31 +		{2.5, -0.5}, // mid4
    1.32 +		{0, -0.5}
    1.33 +};
    1.34 +static const BezCurve piece_curve = {sizeof piece_cp / sizeof *piece_cp, (vec2_t*)piece_cp, 0.25 * PIECE_RAD};
    1.35 +
    1.36  
    1.37  static Vector2 piece_revol(float u, float v, void *cls)
    1.38  {
    1.39 +	BezCurve *curve = (BezCurve*)cls;
    1.40 +	int nseg = (curve->numcp - 1) / 2;
    1.41 +
    1.42  	if(v >= 1.0) v = 1.0 - 1e-6;
    1.43 -	int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1);
    1.44 -	float t = fmod(v * (float)piece_ncurves, 1.0);
    1.45 +	int cidx = std::min((int)(v * nseg), nseg - 1);
    1.46 +	float t = fmod(v * (float)nseg, 1.0);
    1.47  
    1.48 -	Vector2 res;
    1.49 -	for(int i=0; i<2; i++) {
    1.50 -		float mid = piece_cp[idx][1][i];
    1.51 -		res[i] = bezier(piece_cp[idx][0][i], mid, mid, piece_cp[idx][2][i], t);
    1.52 -	}
    1.53 -	return res * 0.25 * PIECE_RAD;
    1.54 +	const vec2_t *cp = curve->cp + cidx * 2;
    1.55 +
    1.56 +	float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t);
    1.57 +	float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t);
    1.58 +	return Vector2(resx * curve->scale, resy * curve->scale);
    1.59  }
    1.60  
    1.61  static Vector2 piece_revol_normal(float u, float v, void *cls)
    1.62  {
    1.63 +	BezCurve *curve = (BezCurve*)cls;
    1.64 +	int nseg = (curve->numcp - 1) / 2;
    1.65 +
    1.66  	if(v >= 1.0) v = 1.0 - 1e-6;
    1.67 -	int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1);
    1.68 -	float t = fmod(v * (float)piece_ncurves, 1.0);
    1.69 +	int cidx = std::min((int)(v * nseg), nseg - 1);
    1.70 +	float t = fmod(v * (float)nseg, 1.0);
    1.71 +
    1.72 +	const vec2_t *cp = curve->cp + cidx * 2;
    1.73 +	Vector2 cp0 = cp[0];
    1.74 +	Vector2 cp1 = cp[1];
    1.75 +	Vector2 cp2 = cp[2];
    1.76  
    1.77  	Vector2 pprev, pnext;
    1.78  	for(int i=0; i<2; i++) {
    1.79 -		float start = piece_cp[idx][0][i];
    1.80 -		float mid = piece_cp[idx][1][i];
    1.81 -		float end = piece_cp[idx][2][i];
    1.82 -
    1.83 -		pprev[i] = bezier(start, mid, mid, end, t - 0.05);
    1.84 -		pnext[i] = bezier(start, mid, mid, end, t + 0.05);
    1.85 +		pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05);
    1.86 +		pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05);
    1.87  	}
    1.88  
    1.89  	float tx = pnext.x - pprev.x;
    1.90 @@ -232,7 +250,7 @@
    1.91  	*/
    1.92  
    1.93  	Mesh *piece = new Mesh;
    1.94 -	gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, 0);
    1.95 +	gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, (void*)&piece_curve);
    1.96  
    1.97  	Object *opiece = new Object;
    1.98  	opiece->set_mesh(piece);