# HG changeset patch # User John Tsiombikas # Date 1435381311 -10800 # Node ID a8e26f163f990b899fcec07e3a7f9566827c2444 # Parent fc5c68da7906ef77aaba7fedbc447d51b0fc0a77 poulia diff -r fc5c68da7906 -r a8e26f163f99 src/board.cc --- a/src/board.cc Fri Jun 26 05:41:18 2015 +0300 +++ b/src/board.cc Sat Jun 27 08:01:51 2015 +0300 @@ -1,3 +1,4 @@ +#include #include "opengl.h" #include "board.h" #include "meshgen.h" @@ -57,6 +58,31 @@ #define GAP (HSIZE * 0.025) #define HINGE_RAD (GAP * 0.5) #define HINGE_HEIGHT (VSIZE * 0.075) +#define PIECE_RAD (0.45 * HSIZE / 5.0) + +static const float piece_cp[][3][2] = { + {{0, 0.25}, {1, 0.25}, {2, 0.5}}, + {{2, 0.5}, {2.5, 0.5}, {3, 0.5}}, + {{3, 0.5}, {4, 0.5}, {4, 0}}, + {{4, 0}, {4, -0.5}, {3, -0.5}}, + {{3, -0.5}, {2.5, -0.5}, {0, -0.5}}, + //{{2, -0.5}, {1, -0.25}, {0, -0.25}} +}; +static const int piece_ncurves = sizeof piece_cp / sizeof *piece_cp; + +static Vector2 piece_revol(float u, float v, void *cls) +{ + if(v >= 1.0) v = 1.0 - 1e-6; + int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1); + float t = fmod(v * (float)piece_ncurves, 1.0); + + Vector2 res; + for(int i=0; i<2; i++) { + float mid = piece_cp[idx][1][i]; + res[i] = bezier(piece_cp[idx][0][i], mid, mid, piece_cp[idx][2][i], t); + } + return res * 0.25 * PIECE_RAD; +} bool Board::generate() { @@ -129,7 +155,7 @@ float sign = i * 2 - 1; // barrel - gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1); + gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1); xform.reset_identity(); xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0)); xform.rotate(Vector3(-M_PI / 2.0, 0, 0)); @@ -183,6 +209,15 @@ obj.push_back(dbgobj); */ + Mesh *piece = new Mesh; + gen_revol(piece, 18, 15, piece_revol, 0); + + Object *opiece = new Object; + opiece->set_mesh(piece); + 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 fc5c68da7906 -r a8e26f163f99 src/game.cc --- a/src/game.cc Fri Jun 26 05:41:18 2015 +0300 +++ b/src/game.cc Sat Jun 27 08:01:51 2015 +0300 @@ -18,7 +18,7 @@ { glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); - + glEnable(GL_NORMALIZE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); diff -r fc5c68da7906 -r a8e26f163f99 src/mesh.cc --- a/src/mesh.cc Fri Jun 26 05:41:18 2015 +0300 +++ b/src/mesh.cc Sat Jun 27 08:01:51 2015 +0300 @@ -6,6 +6,8 @@ #include "mesh.h" //#include "xform_node.h" +#define USE_OLDGL + int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 }; /* (int)SDR_ATTR_VERTEX, @@ -717,6 +719,9 @@ void Mesh::draw_normals() const { #ifdef USE_OLDGL + int cur_sdr; + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr); + Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); if(!varr || !norm) { @@ -724,7 +729,7 @@ } glBegin(GL_LINES); - if(get_current_shader()) { + if(cur_sdr) { int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; if(vert_loc < 0) { glEnd(); @@ -750,6 +755,9 @@ void Mesh::draw_tangents() const { #ifdef USE_OLDGL + int cur_sdr; + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr); + Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT); if(!varr || !tang) { @@ -757,7 +765,7 @@ } glBegin(GL_LINES); - if(get_current_shader()) { + if(cur_sdr) { int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; if(vert_loc < 0) { glEnd(); diff -r fc5c68da7906 -r a8e26f163f99 src/meshgen.cc --- a/src/meshgen.cc Fri Jun 26 05:41:18 2015 +0300 +++ b/src/meshgen.cc Sat Jun 27 08:01:51 2015 +0300 @@ -485,3 +485,86 @@ } } } + +static inline Vector3 rev_vert(float u, float v, Vector2 (*rf)(float, float, void*), void *cls) +{ + Vector2 pos = rf(u, v, cls); + + float angle = u * 2.0 * M_PI; + float x = pos.x * cos(angle); + float y = pos.y; + float z = pos.x * sin(angle); + + return Vector3(x, y, z); +} + +// ------ surface of revolution ------- +void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls) +{ + if(!rfunc) return; + if(usub < 3) usub = 3; + if(vsub < 1) vsub = 1; + + mesh->clear(); + + int uverts = usub + 1; + int vverts = vsub + 1; + int num_verts = uverts * vverts; + + int num_quads = usub * vsub; + int num_tri = num_quads * 2; + + Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); + Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); + Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); + Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); + unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); + + float du = 1.0 / (float)(uverts - 1); + float dv = 1.0 / (float)(vverts - 1); + + float u = 0.0; + for(int i=0; i 0.5 ? v - dv * 0.25 : v + dv * 0.25; + nextu = rev_vert(fmod(u + du, 1.0), new_v, rfunc, cls); + tang = nextu - pos; + } + + Vector3 nextv = rev_vert(u, v + dv, rfunc, cls); + Vector3 bitan = nextv - pos; + if(bitan.length_sq() < 1e-6) { + nextv = rev_vert(u, v - dv, rfunc, cls); + bitan = pos - nextv; + } + + Vector3 normal = cross_product(tang, bitan); + + *varr++ = pos; + *narr++ = normal.normalized(); + *tarr++ = tang.normalized(); + *uvarr++ = Vector2(u, v); + + if(i < usub && j < vsub) { + int idx = i * vverts + j; + + *idxarr++ = idx; + *idxarr++ = idx + vverts + 1; + *idxarr++ = idx + 1; + + *idxarr++ = idx; + *idxarr++ = idx + vverts; + *idxarr++ = idx + vverts + 1; + } + + v += dv; + } + u += du; + } +} diff -r fc5c68da7906 -r a8e26f163f99 src/meshgen.h --- a/src/meshgen.h Fri Jun 26 05:41:18 2015 +0300 +++ b/src/meshgen.h Sat Jun 27 08:01:51 2015 +0300 @@ -1,6 +1,8 @@ #ifndef MESHGEN_H_ #define MESHGEN_H_ +#include "vmath/vmath.h" + class Mesh; void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange = 1.0, float vrange = 1.0); @@ -9,5 +11,6 @@ void gen_plane(Mesh *mesh, float width, float height, int usub = 1, int vsub = 1); void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata = 0); void gen_box(Mesh *mesh, float xsz, float ysz, float zsz); +void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls); #endif // MESHGEN_H_ diff -r fc5c68da7906 -r a8e26f163f99 src/object.cc --- a/src/object.cc Fri Jun 26 05:41:18 2015 +0300 +++ b/src/object.cc Sat Jun 27 08:01:51 2015 +0300 @@ -90,6 +90,72 @@ glPopMatrix(); } +void Object::draw_wire(const Vector4 &col) const +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMultTransposeMatrixf(matrix[0]); + + glColor4f(col.x, col.y, col.z, col.w); + mesh->draw_wire(); + + glPopMatrix(); + glPopAttrib(); +} + +void Object::draw_vertices(const Vector4 &col) const +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMultTransposeMatrixf(matrix[0]); + + glColor4f(col.x, col.y, col.z, col.w); + mesh->draw_vertices(); + + glPopMatrix(); + glPopAttrib(); +} + +void Object::draw_normals(float len, const Vector4 &col) const +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMultTransposeMatrixf(matrix[0]); + + glColor4f(col.x, col.y, col.z, col.w); + mesh->set_vis_vecsize(len); + mesh->draw_normals(); + + glPopMatrix(); + glPopAttrib(); +} + +void Object::draw_tangents(float len, const Vector4 &col) const +{ + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glMultTransposeMatrixf(matrix[0]); + + glColor4f(col.x, col.y, col.z, col.w); + mesh->set_vis_vecsize(len); + mesh->draw_tangents(); + + glPopMatrix(); + glPopAttrib(); +} + bool Object::intersect(const Ray &ray, HitPoint *hit) const { return false; // TODO diff -r fc5c68da7906 -r a8e26f163f99 src/object.h --- a/src/object.h Fri Jun 26 05:41:18 2015 +0300 +++ b/src/object.h Sat Jun 27 08:01:51 2015 +0300 @@ -37,6 +37,10 @@ void set_texture(unsigned int tex); void draw() const; + void draw_wire(const Vector4 &col = Vector4(1, 1, 1, 1)) const; + void draw_vertices(const Vector4 &col = Vector4(1, 0.3, 0.2, 1)) const; + void draw_normals(float len = 1.0, const Vector4 &col = Vector4(0.1, 0.2, 1.0, 1)) const; + void draw_tangents(float len = 1.0, const Vector4 &col = Vector4(0.1, 1.0, 0.2, 1)) const; bool intersect(const Ray &ray, HitPoint *hit) const; };