tavli

changeset 14:283eb6e9f0a3

scenery
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 28 Jun 2015 07:44:23 +0300
parents f3c5134b4914
children b1a195c3ee16
files src/board.cc src/game.cc src/game.h src/object.cc src/object.h src/revol.cc src/revol.h src/scenery.cc src/scenery.h
diffstat 9 files changed, 262 insertions(+), 56 deletions(-) [+]
line diff
     1.1 --- a/src/board.cc	Sat Jun 27 22:33:27 2015 +0300
     1.2 +++ b/src/board.cc	Sun Jun 28 07:44:23 2015 +0300
     1.3 @@ -1,8 +1,10 @@
     1.4  #include <float.h>
     1.5  #include "opengl.h"
     1.6  #include "board.h"
     1.7 +#include "game.h"
     1.8  #include "meshgen.h"
     1.9  #include "pnoise.h"
    1.10 +#include "revol.h"
    1.11  
    1.12  Board::Board()
    1.13  {
    1.14 @@ -46,7 +48,12 @@
    1.15  void Board::draw() const
    1.16  {
    1.17  	for(size_t i=0; i<obj.size(); i++) {
    1.18 -		obj[i]->draw();
    1.19 +		if(wireframe) {
    1.20 +			obj[i]->draw_wire();
    1.21 +			obj[i]->draw_normals(0.075);
    1.22 +		} else {
    1.23 +			obj[i]->draw();
    1.24 +		}
    1.25  	}
    1.26  }
    1.27  
    1.28 @@ -60,11 +67,6 @@
    1.29  #define HINGE_HEIGHT	(VSIZE * 0.075)
    1.30  #define PIECE_RAD		(0.45 * HSIZE / 5.0)
    1.31  
    1.32 -struct BezCurve {
    1.33 -	int numcp;
    1.34 -	vec2_t *cp;
    1.35 -	float scale;
    1.36 -};
    1.37  
    1.38  static const vec2_t piece_cp[] = {
    1.39  		{0, 0.25},
    1.40 @@ -79,51 +81,13 @@
    1.41  		{2.5, -0.5}, // mid4
    1.42  		{0, -0.5}
    1.43  };
    1.44 -static const BezCurve piece_curve = {sizeof piece_cp / sizeof *piece_cp, (vec2_t*)piece_cp, 0.25 * PIECE_RAD};
    1.45 +static const BezCurve piece_curve = {
    1.46 +	sizeof piece_cp / sizeof *piece_cp,
    1.47 +	(vec2_t*)piece_cp,
    1.48 +	0.25 * PIECE_RAD
    1.49 +};
    1.50  
    1.51  
    1.52 -static Vector2 piece_revol(float u, float v, void *cls)
    1.53 -{
    1.54 -	BezCurve *curve = (BezCurve*)cls;
    1.55 -	int nseg = (curve->numcp - 1) / 2;
    1.56 -
    1.57 -	if(v >= 1.0) v = 1.0 - 1e-6;
    1.58 -	int cidx = std::min((int)(v * nseg), nseg - 1);
    1.59 -	float t = fmod(v * (float)nseg, 1.0);
    1.60 -
    1.61 -	const vec2_t *cp = curve->cp + cidx * 2;
    1.62 -
    1.63 -	float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t);
    1.64 -	float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t);
    1.65 -	return Vector2(resx * curve->scale, resy * curve->scale);
    1.66 -}
    1.67 -
    1.68 -static Vector2 piece_revol_normal(float u, float v, void *cls)
    1.69 -{
    1.70 -	BezCurve *curve = (BezCurve*)cls;
    1.71 -	int nseg = (curve->numcp - 1) / 2;
    1.72 -
    1.73 -	if(v >= 1.0) v = 1.0 - 1e-6;
    1.74 -	int cidx = std::min((int)(v * nseg), nseg - 1);
    1.75 -	float t = fmod(v * (float)nseg, 1.0);
    1.76 -
    1.77 -	const vec2_t *cp = curve->cp + cidx * 2;
    1.78 -	Vector2 cp0 = cp[0];
    1.79 -	Vector2 cp1 = cp[1];
    1.80 -	Vector2 cp2 = cp[2];
    1.81 -
    1.82 -	Vector2 pprev, pnext;
    1.83 -	for(int i=0; i<2; i++) {
    1.84 -		pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05);
    1.85 -		pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05);
    1.86 -	}
    1.87 -
    1.88 -	float tx = pnext.x - pprev.x;
    1.89 -	float ty = pnext.y - pprev.y;
    1.90 -
    1.91 -	return Vector2(-ty, tx);
    1.92 -}
    1.93 -
    1.94  bool Board::generate()
    1.95  {
    1.96  	Mesh tmp;
    1.97 @@ -250,7 +214,7 @@
    1.98  	*/
    1.99  
   1.100  	Mesh *piece = new Mesh;
   1.101 -	gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, (void*)&piece_curve);
   1.102 +	gen_revol(piece, 18, 17, bezier_revol, bezier_revol_normal, (void*)&piece_curve);
   1.103  
   1.104  	Object *opiece = new Object;
   1.105  	opiece->set_mesh(piece);
   1.106 @@ -259,7 +223,6 @@
   1.107  	opiece->xform().set_translation(Vector3(0, 0.2, 0));
   1.108  	obj.push_back(opiece);
   1.109  
   1.110 -
   1.111  	// meshgen stats
   1.112  	printf("Generated board:\n  %u meshes\n", (unsigned int)obj.size());
   1.113  	unsigned int polycount = 0;
     2.1 --- a/src/game.cc	Sat Jun 27 22:33:27 2015 +0300
     2.2 +++ b/src/game.cc	Sun Jun 28 07:44:23 2015 +0300
     2.3 @@ -2,10 +2,12 @@
     2.4  #include <GL/glew.h>
     2.5  #include "game.h"
     2.6  #include "board.h"
     2.7 +#include "scenery.h"
     2.8  
     2.9  static void draw_backdrop();
    2.10  
    2.11  int win_width, win_height;
    2.12 +bool wireframe;
    2.13  
    2.14  static Board board;
    2.15  
    2.16 @@ -30,12 +32,17 @@
    2.17  		return false;
    2.18  	}
    2.19  
    2.20 +	if(!init_scenery()) {
    2.21 +		return false;
    2.22 +	}
    2.23 +
    2.24  	return true;
    2.25  }
    2.26  
    2.27  void game_cleanup()
    2.28  {
    2.29  	board.destroy();
    2.30 +	destroy_scenery();
    2.31  }
    2.32  
    2.33  void game_update(unsigned long time_msec)
    2.34 @@ -56,6 +63,7 @@
    2.35  	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    2.36  
    2.37  	draw_backdrop();
    2.38 +	draw_scenery();
    2.39  	board.draw();
    2.40  }
    2.41  
    2.42 @@ -104,6 +112,11 @@
    2.43  		switch(bn) {
    2.44  		case 27:
    2.45  			quit();
    2.46 +
    2.47 +		case 'w':
    2.48 +			wireframe = !wireframe;
    2.49 +			redisplay();
    2.50 +			break;
    2.51  		}
    2.52  	}
    2.53  }
     3.1 --- a/src/game.h	Sat Jun 27 22:33:27 2015 +0300
     3.2 +++ b/src/game.h	Sun Jun 28 07:44:23 2015 +0300
     3.3 @@ -3,6 +3,8 @@
     3.4  
     3.5  extern int win_width, win_height;
     3.6  
     3.7 +extern bool wireframe;
     3.8 +
     3.9  bool game_init();
    3.10  void game_cleanup();
    3.11  void game_update(unsigned long time_msec);
     4.1 --- a/src/object.cc	Sat Jun 27 22:33:27 2015 +0300
     4.2 +++ b/src/object.cc	Sun Jun 28 07:44:23 2015 +0300
     4.3 @@ -1,15 +1,29 @@
     4.4  #include "object.h"
     4.5  #include "opengl.h"
     4.6  
     4.7 +Material::Material()
     4.8 +	: diffuse(1, 1, 1), specular(0, 0, 0)
     4.9 +{
    4.10 +	shininess = 60.0;
    4.11 +	alpha = 1.0;
    4.12 +}
    4.13 +
    4.14 +RenderOps::RenderOps()
    4.15 +{
    4.16 +	zwrite = true;
    4.17 +}
    4.18 +
    4.19 +void RenderOps::setup() const
    4.20 +{
    4.21 +	if(!zwrite) {
    4.22 +		glDepthMask(0);
    4.23 +	}
    4.24 +}
    4.25 +
    4.26  Object::Object()
    4.27  {
    4.28  	mesh = 0;
    4.29  	tex = 0;
    4.30 -
    4.31 -	mtl.diffuse = Vector3(1, 1, 1);
    4.32 -	mtl.specular = Vector3(0, 0, 0);
    4.33 -	mtl.shininess = 60.0;
    4.34 -	mtl.alpha = 1.0;
    4.35  }
    4.36  
    4.37  Object::~Object()
    4.38 @@ -56,6 +70,9 @@
    4.39  {
    4.40  	if(!mesh) return;
    4.41  
    4.42 +	glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT);
    4.43 +	rop.setup();
    4.44 +
    4.45  	if(tex) {
    4.46  		glBindTexture(GL_TEXTURE_2D, tex);
    4.47  		glEnable(GL_TEXTURE_2D);
    4.48 @@ -88,6 +105,8 @@
    4.49  
    4.50  	glMatrixMode(GL_MODELVIEW);
    4.51  	glPopMatrix();
    4.52 +
    4.53 +	glPopAttrib();
    4.54  }
    4.55  
    4.56  void Object::draw_wire(const Vector4 &col) const
     5.1 --- a/src/object.h	Sat Jun 27 22:33:27 2015 +0300
     5.2 +++ b/src/object.h	Sun Jun 28 07:44:23 2015 +0300
     5.3 @@ -10,6 +10,15 @@
     5.4  	Vector3 specular;
     5.5  	float shininess;
     5.6  	float alpha;
     5.7 +
     5.8 +	Material();
     5.9 +};
    5.10 +
    5.11 +struct RenderOps {
    5.12 +	bool zwrite;
    5.13 +
    5.14 +	RenderOps();
    5.15 +	void setup() const;
    5.16  };
    5.17  
    5.18  class Object {
    5.19 @@ -21,6 +30,7 @@
    5.20  
    5.21  public:
    5.22  	Material mtl;
    5.23 +	RenderOps rop;
    5.24  
    5.25  	Object();
    5.26  	~Object();
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/revol.cc	Sun Jun 28 07:44:23 2015 +0300
     6.3 @@ -0,0 +1,44 @@
     6.4 +#include <algorithm>
     6.5 +#include "revol.h"
     6.6 +
     6.7 +Vector2 bezier_revol(float u, float v, void *cls)
     6.8 +{
     6.9 +	BezCurve *curve = (BezCurve*)cls;
    6.10 +	int nseg = (curve->numcp - 1) / 2;
    6.11 +
    6.12 +	if(v >= 1.0) v = 1.0 - 1e-6;
    6.13 +	int cidx = std::min((int)(v * nseg), nseg - 1);
    6.14 +	float t = fmod(v * (float)nseg, 1.0);
    6.15 +
    6.16 +	const vec2_t *cp = curve->cp + cidx * 2;
    6.17 +
    6.18 +	float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t);
    6.19 +	float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t);
    6.20 +	return Vector2(resx * curve->scale, resy * curve->scale);
    6.21 +}
    6.22 +
    6.23 +Vector2 bezier_revol_normal(float u, float v, void *cls)
    6.24 +{
    6.25 +	BezCurve *curve = (BezCurve*)cls;
    6.26 +	int nseg = (curve->numcp - 1) / 2;
    6.27 +
    6.28 +	if(v >= 1.0) v = 1.0 - 1e-6;
    6.29 +	int cidx = std::min((int)(v * nseg), nseg - 1);
    6.30 +	float t = fmod(v * (float)nseg, 1.0);
    6.31 +
    6.32 +	const vec2_t *cp = curve->cp + cidx * 2;
    6.33 +	Vector2 cp0 = cp[0];
    6.34 +	Vector2 cp1 = cp[1];
    6.35 +	Vector2 cp2 = cp[2];
    6.36 +
    6.37 +	Vector2 pprev, pnext;
    6.38 +	for(int i=0; i<2; i++) {
    6.39 +		pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05);
    6.40 +		pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05);
    6.41 +	}
    6.42 +
    6.43 +	float tx = pnext.x - pprev.x;
    6.44 +	float ty = pnext.y - pprev.y;
    6.45 +
    6.46 +	return Vector2(-ty, tx);
    6.47 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/revol.h	Sun Jun 28 07:44:23 2015 +0300
     7.3 @@ -0,0 +1,15 @@
     7.4 +#ifndef REVOL_H_
     7.5 +#define REVOL_H_
     7.6 +
     7.7 +#include "vmath/vmath.h"
     7.8 +
     7.9 +struct BezCurve {
    7.10 +	int numcp;
    7.11 +	vec2_t *cp;
    7.12 +	float scale;
    7.13 +};
    7.14 +
    7.15 +Vector2 bezier_revol(float u, float v, void *cls);
    7.16 +Vector2 bezier_revol_normal(float u, float v, void *cls);
    7.17 +
    7.18 +#endif	// REVOL_H_
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/scenery.cc	Sun Jun 28 07:44:23 2015 +0300
     8.3 @@ -0,0 +1,131 @@
     8.4 +#include <vector>
     8.5 +#include "opengl.h"
     8.6 +#include "scenery.h"
     8.7 +#include "game.h"
     8.8 +#include "mesh.h"
     8.9 +#include "meshgen.h"
    8.10 +#include "object.h"
    8.11 +#include "revol.h"
    8.12 +#include "image.h"
    8.13 +
    8.14 +static bool gen_textures();
    8.15 +
    8.16 +static std::vector<Object*> obj;
    8.17 +static Image img_marble;
    8.18 +
    8.19 +static const vec2_t table_cp[] = {
    8.20 +	{0, 0},
    8.21 +	{3, 0},	// mid 0
    8.22 +	{5.8, 0},
    8.23 +	{5.99, 0},	// mid 1
    8.24 +	{6, -0.15},
    8.25 +	{6.15, -0.15},	// mid 2
    8.26 +	{6.2, -0.4}
    8.27 +};
    8.28 +static const BezCurve table_curve = {
    8.29 +	sizeof table_cp / sizeof *table_cp,
    8.30 +	(vec2_t*)table_cp,
    8.31 +	1.0 / 6.2
    8.32 +};
    8.33 +
    8.34 +
    8.35 +
    8.36 +bool init_scenery()
    8.37 +{
    8.38 +	if(!gen_textures()) {
    8.39 +		return false;
    8.40 +	}
    8.41 +
    8.42 +	Matrix4x4 xform;
    8.43 +
    8.44 +	Mesh *table = new Mesh;
    8.45 +	gen_revol(table, 48, 12, bezier_revol, bezier_revol_normal, (void*)&table_curve);
    8.46 +	table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0));
    8.47 +	xform.set_scaling(Vector3(0.5, 0.5, 0.5));
    8.48 +	xform.translate(Vector3(1, 1, 0));
    8.49 +	table->texcoord_apply_xform(xform);
    8.50 +
    8.51 +	static const float table_scale = 1.8;
    8.52 +	xform.set_scaling(Vector3(table_scale, table_scale, table_scale));
    8.53 +	table->apply_xform(xform);
    8.54 +
    8.55 +	Object *otable = new Object;
    8.56 +	otable->set_mesh(table);
    8.57 +	otable->mtl.diffuse = Vector3(0.6, 0.6, 0.6);
    8.58 +	otable->mtl.specular = Vector3(0.8, 0.8, 0.8);
    8.59 +	otable->xform().set_translation(Vector3(0, -0.025, 0));
    8.60 +	otable->set_texture(img_marble.texture());
    8.61 +	obj.push_back(otable);
    8.62 +
    8.63 +
    8.64 +	// meshgen stats
    8.65 +	printf("Generated scenery:\n  %u meshes\n", (unsigned int)obj.size());
    8.66 +	unsigned int polycount = 0;
    8.67 +	for(size_t i=0; i<obj.size(); i++) {
    8.68 +		const Mesh *m = obj[i]->get_mesh();
    8.69 +		polycount += m->get_poly_count();
    8.70 +	}
    8.71 +	printf("  %u polygons\n", polycount);
    8.72 +
    8.73 +	return true;
    8.74 +}
    8.75 +
    8.76 +void destroy_scenery()
    8.77 +{
    8.78 +	for(size_t i=0; i<obj.size(); i++) {
    8.79 +		delete obj[i];
    8.80 +	}
    8.81 +	obj.clear();
    8.82 +
    8.83 +	img_marble.destroy();
    8.84 +}
    8.85 +
    8.86 +void draw_scenery()
    8.87 +{
    8.88 +	for(size_t i=0; i<obj.size(); i++) {
    8.89 +		if(wireframe) {
    8.90 +			obj[i]->draw_wire();
    8.91 +			obj[i]->draw_normals(0.075);
    8.92 +		} else {
    8.93 +			obj[i]->draw();
    8.94 +		}
    8.95 +	}
    8.96 +}
    8.97 +
    8.98 +static float marble(float x, float y)
    8.99 +{
   8.100 +	float theta = x * M_PI * 2.0 * cos(y * 1.5);
   8.101 +	theta += turbulence2(x * 10.0, y * 10.0, 4) * 2;
   8.102 +	float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0;
   8.103 +	return val * 0.5 + 0.5;
   8.104 +}
   8.105 +
   8.106 +static bool gen_textures()
   8.107 +{
   8.108 +	static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85);
   8.109 +	static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7);
   8.110 +
   8.111 +	img_marble.create(512, 512);
   8.112 +	unsigned char *pptr = img_marble.pixels;
   8.113 +	for(int i=0; i<img_marble.height; i++) {
   8.114 +		float v = (float)i / (float)img_marble.height;
   8.115 +		for(int j=0; j<img_marble.width; j++) {
   8.116 +			float u = (float)j / (float)img_marble.width;
   8.117 +
   8.118 +			float marble_val = marble(u, v);
   8.119 +			Vector3 color = lerp(marble_col2, marble_col1, marble_val);
   8.120 +
   8.121 +			int r = (int)(color.x * 255.0);
   8.122 +			int g = (int)(color.y * 255.0);
   8.123 +			int b = (int)(color.z * 255.0);
   8.124 +
   8.125 +			pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
   8.126 +			pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
   8.127 +			pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
   8.128 +			pptr += 3;
   8.129 +		}
   8.130 +	}
   8.131 +	img_marble.texture();
   8.132 +
   8.133 +	return true;
   8.134 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/src/scenery.h	Sun Jun 28 07:44:23 2015 +0300
     9.3 @@ -0,0 +1,9 @@
     9.4 +#ifndef SCENERY_H_
     9.5 +#define SCENERY_H_
     9.6 +
     9.7 +bool init_scenery();
     9.8 +void destroy_scenery();
     9.9 +
    9.10 +void draw_scenery();
    9.11 +
    9.12 +#endif	// SCENERY_H_