# HG changeset patch # User John Tsiombikas # Date 1435466663 -10800 # Node ID 283eb6e9f0a37475f5ba0fea81c896bf6e7245a5 # Parent f3c5134b491485185d1caf4e37b374e8afc94c8c scenery diff -r f3c5134b4914 -r 283eb6e9f0a3 src/board.cc --- a/src/board.cc Sat Jun 27 22:33:27 2015 +0300 +++ b/src/board.cc Sun Jun 28 07:44:23 2015 +0300 @@ -1,8 +1,10 @@ #include #include "opengl.h" #include "board.h" +#include "game.h" #include "meshgen.h" #include "pnoise.h" +#include "revol.h" Board::Board() { @@ -46,7 +48,12 @@ void Board::draw() const { for(size_t i=0; idraw(); + if(wireframe) { + obj[i]->draw_wire(); + obj[i]->draw_normals(0.075); + } else { + obj[i]->draw(); + } } } @@ -60,11 +67,6 @@ #define HINGE_HEIGHT (VSIZE * 0.075) #define PIECE_RAD (0.45 * HSIZE / 5.0) -struct BezCurve { - int numcp; - vec2_t *cp; - float scale; -}; static const vec2_t piece_cp[] = { {0, 0.25}, @@ -79,51 +81,13 @@ {2.5, -0.5}, // mid4 {0, -0.5} }; -static const BezCurve piece_curve = {sizeof piece_cp / sizeof *piece_cp, (vec2_t*)piece_cp, 0.25 * PIECE_RAD}; +static const BezCurve piece_curve = { + sizeof piece_cp / sizeof *piece_cp, + (vec2_t*)piece_cp, + 0.25 * PIECE_RAD +}; -static Vector2 piece_revol(float u, float v, void *cls) -{ - BezCurve *curve = (BezCurve*)cls; - int nseg = (curve->numcp - 1) / 2; - - if(v >= 1.0) v = 1.0 - 1e-6; - int cidx = std::min((int)(v * nseg), nseg - 1); - float t = fmod(v * (float)nseg, 1.0); - - const vec2_t *cp = curve->cp + cidx * 2; - - float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t); - float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t); - return Vector2(resx * curve->scale, resy * curve->scale); -} - -static Vector2 piece_revol_normal(float u, float v, void *cls) -{ - BezCurve *curve = (BezCurve*)cls; - int nseg = (curve->numcp - 1) / 2; - - if(v >= 1.0) v = 1.0 - 1e-6; - int cidx = std::min((int)(v * nseg), nseg - 1); - float t = fmod(v * (float)nseg, 1.0); - - const vec2_t *cp = curve->cp + cidx * 2; - Vector2 cp0 = cp[0]; - Vector2 cp1 = cp[1]; - Vector2 cp2 = cp[2]; - - Vector2 pprev, pnext; - for(int i=0; i<2; i++) { - pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05); - pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05); - } - - float tx = pnext.x - pprev.x; - float ty = pnext.y - pprev.y; - - return Vector2(-ty, tx); -} - bool Board::generate() { Mesh tmp; @@ -250,7 +214,7 @@ */ Mesh *piece = new Mesh; - gen_revol(piece, 18, 17, piece_revol, piece_revol_normal, (void*)&piece_curve); + gen_revol(piece, 18, 17, bezier_revol, bezier_revol_normal, (void*)&piece_curve); Object *opiece = new Object; opiece->set_mesh(piece); @@ -259,7 +223,6 @@ opiece->xform().set_translation(Vector3(0, 0.2, 0)); obj.push_back(opiece); - // meshgen stats printf("Generated board:\n %u meshes\n", (unsigned int)obj.size()); unsigned int polycount = 0; diff -r f3c5134b4914 -r 283eb6e9f0a3 src/game.cc --- a/src/game.cc Sat Jun 27 22:33:27 2015 +0300 +++ b/src/game.cc Sun Jun 28 07:44:23 2015 +0300 @@ -2,10 +2,12 @@ #include #include "game.h" #include "board.h" +#include "scenery.h" static void draw_backdrop(); int win_width, win_height; +bool wireframe; static Board board; @@ -30,12 +32,17 @@ return false; } + if(!init_scenery()) { + return false; + } + return true; } void game_cleanup() { board.destroy(); + destroy_scenery(); } void game_update(unsigned long time_msec) @@ -56,6 +63,7 @@ glLightfv(GL_LIGHT0, GL_POSITION, ldir); draw_backdrop(); + draw_scenery(); board.draw(); } @@ -104,6 +112,11 @@ switch(bn) { case 27: quit(); + + case 'w': + wireframe = !wireframe; + redisplay(); + break; } } } diff -r f3c5134b4914 -r 283eb6e9f0a3 src/game.h --- a/src/game.h Sat Jun 27 22:33:27 2015 +0300 +++ b/src/game.h Sun Jun 28 07:44:23 2015 +0300 @@ -3,6 +3,8 @@ extern int win_width, win_height; +extern bool wireframe; + bool game_init(); void game_cleanup(); void game_update(unsigned long time_msec); diff -r f3c5134b4914 -r 283eb6e9f0a3 src/object.cc --- a/src/object.cc Sat Jun 27 22:33:27 2015 +0300 +++ b/src/object.cc Sun Jun 28 07:44:23 2015 +0300 @@ -1,15 +1,29 @@ #include "object.h" #include "opengl.h" +Material::Material() + : diffuse(1, 1, 1), specular(0, 0, 0) +{ + shininess = 60.0; + alpha = 1.0; +} + +RenderOps::RenderOps() +{ + zwrite = true; +} + +void RenderOps::setup() const +{ + if(!zwrite) { + glDepthMask(0); + } +} + Object::Object() { mesh = 0; tex = 0; - - mtl.diffuse = Vector3(1, 1, 1); - mtl.specular = Vector3(0, 0, 0); - mtl.shininess = 60.0; - mtl.alpha = 1.0; } Object::~Object() @@ -56,6 +70,9 @@ { if(!mesh) return; + glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); + rop.setup(); + if(tex) { glBindTexture(GL_TEXTURE_2D, tex); glEnable(GL_TEXTURE_2D); @@ -88,6 +105,8 @@ glMatrixMode(GL_MODELVIEW); glPopMatrix(); + + glPopAttrib(); } void Object::draw_wire(const Vector4 &col) const diff -r f3c5134b4914 -r 283eb6e9f0a3 src/object.h --- a/src/object.h Sat Jun 27 22:33:27 2015 +0300 +++ b/src/object.h Sun Jun 28 07:44:23 2015 +0300 @@ -10,6 +10,15 @@ Vector3 specular; float shininess; float alpha; + + Material(); +}; + +struct RenderOps { + bool zwrite; + + RenderOps(); + void setup() const; }; class Object { @@ -21,6 +30,7 @@ public: Material mtl; + RenderOps rop; Object(); ~Object(); diff -r f3c5134b4914 -r 283eb6e9f0a3 src/revol.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/revol.cc Sun Jun 28 07:44:23 2015 +0300 @@ -0,0 +1,44 @@ +#include +#include "revol.h" + +Vector2 bezier_revol(float u, float v, void *cls) +{ + BezCurve *curve = (BezCurve*)cls; + int nseg = (curve->numcp - 1) / 2; + + if(v >= 1.0) v = 1.0 - 1e-6; + int cidx = std::min((int)(v * nseg), nseg - 1); + float t = fmod(v * (float)nseg, 1.0); + + const vec2_t *cp = curve->cp + cidx * 2; + + float resx = bezier(cp[0].x, cp[1].x, cp[1].x, cp[2].x, t); + float resy = bezier(cp[0].y, cp[1].y, cp[1].y, cp[2].y, t); + return Vector2(resx * curve->scale, resy * curve->scale); +} + +Vector2 bezier_revol_normal(float u, float v, void *cls) +{ + BezCurve *curve = (BezCurve*)cls; + int nseg = (curve->numcp - 1) / 2; + + if(v >= 1.0) v = 1.0 - 1e-6; + int cidx = std::min((int)(v * nseg), nseg - 1); + float t = fmod(v * (float)nseg, 1.0); + + const vec2_t *cp = curve->cp + cidx * 2; + Vector2 cp0 = cp[0]; + Vector2 cp1 = cp[1]; + Vector2 cp2 = cp[2]; + + Vector2 pprev, pnext; + for(int i=0; i<2; i++) { + pprev[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t - 0.05); + pnext[i] = bezier(cp0[i], cp1[i], cp1[i], cp2[i], t + 0.05); + } + + float tx = pnext.x - pprev.x; + float ty = pnext.y - pprev.y; + + return Vector2(-ty, tx); +} diff -r f3c5134b4914 -r 283eb6e9f0a3 src/revol.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/revol.h Sun Jun 28 07:44:23 2015 +0300 @@ -0,0 +1,15 @@ +#ifndef REVOL_H_ +#define REVOL_H_ + +#include "vmath/vmath.h" + +struct BezCurve { + int numcp; + vec2_t *cp; + float scale; +}; + +Vector2 bezier_revol(float u, float v, void *cls); +Vector2 bezier_revol_normal(float u, float v, void *cls); + +#endif // REVOL_H_ diff -r f3c5134b4914 -r 283eb6e9f0a3 src/scenery.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scenery.cc Sun Jun 28 07:44:23 2015 +0300 @@ -0,0 +1,131 @@ +#include +#include "opengl.h" +#include "scenery.h" +#include "game.h" +#include "mesh.h" +#include "meshgen.h" +#include "object.h" +#include "revol.h" +#include "image.h" + +static bool gen_textures(); + +static std::vector obj; +static Image img_marble; + +static const vec2_t table_cp[] = { + {0, 0}, + {3, 0}, // mid 0 + {5.8, 0}, + {5.99, 0}, // mid 1 + {6, -0.15}, + {6.15, -0.15}, // mid 2 + {6.2, -0.4} +}; +static const BezCurve table_curve = { + sizeof table_cp / sizeof *table_cp, + (vec2_t*)table_cp, + 1.0 / 6.2 +}; + + + +bool init_scenery() +{ + if(!gen_textures()) { + return false; + } + + Matrix4x4 xform; + + Mesh *table = new Mesh; + gen_revol(table, 48, 12, bezier_revol, bezier_revol_normal, (void*)&table_curve); + table->texcoord_gen_plane(Vector3(0, 1, 0), Vector3(1, 0, 0)); + xform.set_scaling(Vector3(0.5, 0.5, 0.5)); + xform.translate(Vector3(1, 1, 0)); + table->texcoord_apply_xform(xform); + + static const float table_scale = 1.8; + xform.set_scaling(Vector3(table_scale, table_scale, table_scale)); + table->apply_xform(xform); + + Object *otable = new Object; + otable->set_mesh(table); + otable->mtl.diffuse = Vector3(0.6, 0.6, 0.6); + otable->mtl.specular = Vector3(0.8, 0.8, 0.8); + otable->xform().set_translation(Vector3(0, -0.025, 0)); + otable->set_texture(img_marble.texture()); + obj.push_back(otable); + + + // meshgen stats + printf("Generated scenery:\n %u meshes\n", (unsigned int)obj.size()); + unsigned int polycount = 0; + for(size_t i=0; iget_mesh(); + polycount += m->get_poly_count(); + } + printf(" %u polygons\n", polycount); + + return true; +} + +void destroy_scenery() +{ + for(size_t i=0; idraw_wire(); + obj[i]->draw_normals(0.075); + } else { + obj[i]->draw(); + } + } +} + +static float marble(float x, float y) +{ + float theta = x * M_PI * 2.0 * cos(y * 1.5); + theta += turbulence2(x * 10.0, y * 10.0, 4) * 2; + float val = 0.5 + sin(theta * 5.0) + sin(theta * 8.0) / 2.0 + sin(theta * 19.0) / 4.0; + return val * 0.5 + 0.5; +} + +static bool gen_textures() +{ + static const Vector3 marble_col1 = Vector3(0.78, 0.85, 0.85); + static const Vector3 marble_col2 = Vector3(0.56, 0.68, 0.7); + + img_marble.create(512, 512); + unsigned char *pptr = img_marble.pixels; + for(int i=0; i 255 ? 255 : (r < 0 ? 0 : r); + pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g); + pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b); + pptr += 3; + } + } + img_marble.texture(); + + return true; +} diff -r f3c5134b4914 -r 283eb6e9f0a3 src/scenery.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scenery.h Sun Jun 28 07:44:23 2015 +0300 @@ -0,0 +1,9 @@ +#ifndef SCENERY_H_ +#define SCENERY_H_ + +bool init_scenery(); +void destroy_scenery(); + +void draw_scenery(); + +#endif // SCENERY_H_