tavli
changeset 11:a8e26f163f99
poulia
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 27 Jun 2015 08:01:51 +0300 |
parents | fc5c68da7906 |
children | ae1c60726c41 |
files | src/board.cc src/game.cc src/mesh.cc src/meshgen.cc src/meshgen.h src/object.cc src/object.h |
diffstat | 7 files changed, 203 insertions(+), 4 deletions(-) [+] |
line diff
1.1 --- a/src/board.cc Fri Jun 26 05:41:18 2015 +0300 1.2 +++ b/src/board.cc Sat Jun 27 08:01:51 2015 +0300 1.3 @@ -1,3 +1,4 @@ 1.4 +#include <float.h> 1.5 #include "opengl.h" 1.6 #include "board.h" 1.7 #include "meshgen.h" 1.8 @@ -57,6 +58,31 @@ 1.9 #define GAP (HSIZE * 0.025) 1.10 #define HINGE_RAD (GAP * 0.5) 1.11 #define HINGE_HEIGHT (VSIZE * 0.075) 1.12 +#define PIECE_RAD (0.45 * HSIZE / 5.0) 1.13 + 1.14 +static const float piece_cp[][3][2] = { 1.15 + {{0, 0.25}, {1, 0.25}, {2, 0.5}}, 1.16 + {{2, 0.5}, {2.5, 0.5}, {3, 0.5}}, 1.17 + {{3, 0.5}, {4, 0.5}, {4, 0}}, 1.18 + {{4, 0}, {4, -0.5}, {3, -0.5}}, 1.19 + {{3, -0.5}, {2.5, -0.5}, {0, -0.5}}, 1.20 + //{{2, -0.5}, {1, -0.25}, {0, -0.25}} 1.21 +}; 1.22 +static const int piece_ncurves = sizeof piece_cp / sizeof *piece_cp; 1.23 + 1.24 +static Vector2 piece_revol(float u, float v, void *cls) 1.25 +{ 1.26 + if(v >= 1.0) v = 1.0 - 1e-6; 1.27 + int idx = std::min((int)(v * piece_ncurves), piece_ncurves - 1); 1.28 + float t = fmod(v * (float)piece_ncurves, 1.0); 1.29 + 1.30 + Vector2 res; 1.31 + for(int i=0; i<2; i++) { 1.32 + float mid = piece_cp[idx][1][i]; 1.33 + res[i] = bezier(piece_cp[idx][0][i], mid, mid, piece_cp[idx][2][i], t); 1.34 + } 1.35 + return res * 0.25 * PIECE_RAD; 1.36 +} 1.37 1.38 bool Board::generate() 1.39 { 1.40 @@ -129,7 +155,7 @@ 1.41 float sign = i * 2 - 1; 1.42 1.43 // barrel 1.44 - gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1); 1.45 + gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 8, 1, 1); 1.46 xform.reset_identity(); 1.47 xform.translate(Vector3(0, WALL_HEIGHT - HINGE_RAD * 0.5, sign * VSIZE / 4.0)); 1.48 xform.rotate(Vector3(-M_PI / 2.0, 0, 0)); 1.49 @@ -183,6 +209,15 @@ 1.50 obj.push_back(dbgobj); 1.51 */ 1.52 1.53 + Mesh *piece = new Mesh; 1.54 + gen_revol(piece, 18, 15, piece_revol, 0); 1.55 + 1.56 + Object *opiece = new Object; 1.57 + opiece->set_mesh(piece); 1.58 + opiece->xform().set_translation(Vector3(0, 0.2, 0)); 1.59 + obj.push_back(opiece); 1.60 + 1.61 + 1.62 // meshgen stats 1.63 printf("Generated board:\n %u meshes\n", (unsigned int)obj.size()); 1.64 unsigned int polycount = 0;
2.1 --- a/src/game.cc Fri Jun 26 05:41:18 2015 +0300 2.2 +++ b/src/game.cc Sat Jun 27 08:01:51 2015 +0300 2.3 @@ -18,7 +18,7 @@ 2.4 { 2.5 glEnable(GL_DEPTH_TEST); 2.6 glEnable(GL_CULL_FACE); 2.7 - 2.8 + glEnable(GL_NORMALIZE); 2.9 glEnable(GL_LIGHTING); 2.10 glEnable(GL_LIGHT0); 2.11
3.1 --- a/src/mesh.cc Fri Jun 26 05:41:18 2015 +0300 3.2 +++ b/src/mesh.cc Sat Jun 27 08:01:51 2015 +0300 3.3 @@ -6,6 +6,8 @@ 3.4 #include "mesh.h" 3.5 //#include "xform_node.h" 3.6 3.7 +#define USE_OLDGL 3.8 + 3.9 int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 }; 3.10 /* 3.11 (int)SDR_ATTR_VERTEX, 3.12 @@ -717,6 +719,9 @@ 3.13 void Mesh::draw_normals() const 3.14 { 3.15 #ifdef USE_OLDGL 3.16 + int cur_sdr; 3.17 + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr); 3.18 + 3.19 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 3.20 Vector3 *norm = (Vector3*)get_attrib_data(MESH_ATTR_NORMAL); 3.21 if(!varr || !norm) { 3.22 @@ -724,7 +729,7 @@ 3.23 } 3.24 3.25 glBegin(GL_LINES); 3.26 - if(get_current_shader()) { 3.27 + if(cur_sdr) { 3.28 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; 3.29 if(vert_loc < 0) { 3.30 glEnd(); 3.31 @@ -750,6 +755,9 @@ 3.32 void Mesh::draw_tangents() const 3.33 { 3.34 #ifdef USE_OLDGL 3.35 + int cur_sdr; 3.36 + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr); 3.37 + 3.38 Vector3 *varr = (Vector3*)get_attrib_data(MESH_ATTR_VERTEX); 3.39 Vector3 *tang = (Vector3*)get_attrib_data(MESH_ATTR_TANGENT); 3.40 if(!varr || !tang) { 3.41 @@ -757,7 +765,7 @@ 3.42 } 3.43 3.44 glBegin(GL_LINES); 3.45 - if(get_current_shader()) { 3.46 + if(cur_sdr) { 3.47 int vert_loc = global_sdr_loc[MESH_ATTR_VERTEX]; 3.48 if(vert_loc < 0) { 3.49 glEnd();
4.1 --- a/src/meshgen.cc Fri Jun 26 05:41:18 2015 +0300 4.2 +++ b/src/meshgen.cc Sat Jun 27 08:01:51 2015 +0300 4.3 @@ -485,3 +485,86 @@ 4.4 } 4.5 } 4.6 } 4.7 + 4.8 +static inline Vector3 rev_vert(float u, float v, Vector2 (*rf)(float, float, void*), void *cls) 4.9 +{ 4.10 + Vector2 pos = rf(u, v, cls); 4.11 + 4.12 + float angle = u * 2.0 * M_PI; 4.13 + float x = pos.x * cos(angle); 4.14 + float y = pos.y; 4.15 + float z = pos.x * sin(angle); 4.16 + 4.17 + return Vector3(x, y, z); 4.18 +} 4.19 + 4.20 +// ------ surface of revolution ------- 4.21 +void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls) 4.22 +{ 4.23 + if(!rfunc) return; 4.24 + if(usub < 3) usub = 3; 4.25 + if(vsub < 1) vsub = 1; 4.26 + 4.27 + mesh->clear(); 4.28 + 4.29 + int uverts = usub + 1; 4.30 + int vverts = vsub + 1; 4.31 + int num_verts = uverts * vverts; 4.32 + 4.33 + int num_quads = usub * vsub; 4.34 + int num_tri = num_quads * 2; 4.35 + 4.36 + Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0); 4.37 + Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0); 4.38 + Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0); 4.39 + Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0); 4.40 + unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0); 4.41 + 4.42 + float du = 1.0 / (float)(uverts - 1); 4.43 + float dv = 1.0 / (float)(vverts - 1); 4.44 + 4.45 + float u = 0.0; 4.46 + for(int i=0; i<uverts; i++) { 4.47 + float v = 0.0; 4.48 + for(int j=0; j<vverts; j++) { 4.49 + Vector3 pos = rev_vert(u, v, rfunc, cls); 4.50 + 4.51 + Vector3 nextu = rev_vert(fmod(u + du, 1.0), v, rfunc, cls); 4.52 + Vector3 tang = nextu - pos; 4.53 + if(tang.length_sq() < 1e-6) { 4.54 + float new_v = v > 0.5 ? v - dv * 0.25 : v + dv * 0.25; 4.55 + nextu = rev_vert(fmod(u + du, 1.0), new_v, rfunc, cls); 4.56 + tang = nextu - pos; 4.57 + } 4.58 + 4.59 + Vector3 nextv = rev_vert(u, v + dv, rfunc, cls); 4.60 + Vector3 bitan = nextv - pos; 4.61 + if(bitan.length_sq() < 1e-6) { 4.62 + nextv = rev_vert(u, v - dv, rfunc, cls); 4.63 + bitan = pos - nextv; 4.64 + } 4.65 + 4.66 + Vector3 normal = cross_product(tang, bitan); 4.67 + 4.68 + *varr++ = pos; 4.69 + *narr++ = normal.normalized(); 4.70 + *tarr++ = tang.normalized(); 4.71 + *uvarr++ = Vector2(u, v); 4.72 + 4.73 + if(i < usub && j < vsub) { 4.74 + int idx = i * vverts + j; 4.75 + 4.76 + *idxarr++ = idx; 4.77 + *idxarr++ = idx + vverts + 1; 4.78 + *idxarr++ = idx + 1; 4.79 + 4.80 + *idxarr++ = idx; 4.81 + *idxarr++ = idx + vverts; 4.82 + *idxarr++ = idx + vverts + 1; 4.83 + } 4.84 + 4.85 + v += dv; 4.86 + } 4.87 + u += du; 4.88 + } 4.89 +}
5.1 --- a/src/meshgen.h Fri Jun 26 05:41:18 2015 +0300 5.2 +++ b/src/meshgen.h Sat Jun 27 08:01:51 2015 +0300 5.3 @@ -1,6 +1,8 @@ 5.4 #ifndef MESHGEN_H_ 5.5 #define MESHGEN_H_ 5.6 5.7 +#include "vmath/vmath.h" 5.8 + 5.9 class Mesh; 5.10 5.11 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange = 1.0, float vrange = 1.0); 5.12 @@ -9,5 +11,6 @@ 5.13 void gen_plane(Mesh *mesh, float width, float height, int usub = 1, int vsub = 1); 5.14 void gen_heightmap(Mesh *mesh, float width, float height, int usub, int vsub, float (*hf)(float, float, void*), void *hfdata = 0); 5.15 void gen_box(Mesh *mesh, float xsz, float ysz, float zsz); 5.16 +void gen_revol(Mesh *mesh, int usub, int vsub, Vector2 (*rfunc)(float, float, void*), void *cls); 5.17 5.18 #endif // MESHGEN_H_
6.1 --- a/src/object.cc Fri Jun 26 05:41:18 2015 +0300 6.2 +++ b/src/object.cc Sat Jun 27 08:01:51 2015 +0300 6.3 @@ -90,6 +90,72 @@ 6.4 glPopMatrix(); 6.5 } 6.6 6.7 +void Object::draw_wire(const Vector4 &col) const 6.8 +{ 6.9 + glPushAttrib(GL_ENABLE_BIT); 6.10 + glDisable(GL_LIGHTING); 6.11 + 6.12 + glMatrixMode(GL_MODELVIEW); 6.13 + glPushMatrix(); 6.14 + glMultTransposeMatrixf(matrix[0]); 6.15 + 6.16 + glColor4f(col.x, col.y, col.z, col.w); 6.17 + mesh->draw_wire(); 6.18 + 6.19 + glPopMatrix(); 6.20 + glPopAttrib(); 6.21 +} 6.22 + 6.23 +void Object::draw_vertices(const Vector4 &col) const 6.24 +{ 6.25 + glPushAttrib(GL_ENABLE_BIT); 6.26 + glDisable(GL_LIGHTING); 6.27 + 6.28 + glMatrixMode(GL_MODELVIEW); 6.29 + glPushMatrix(); 6.30 + glMultTransposeMatrixf(matrix[0]); 6.31 + 6.32 + glColor4f(col.x, col.y, col.z, col.w); 6.33 + mesh->draw_vertices(); 6.34 + 6.35 + glPopMatrix(); 6.36 + glPopAttrib(); 6.37 +} 6.38 + 6.39 +void Object::draw_normals(float len, const Vector4 &col) const 6.40 +{ 6.41 + glPushAttrib(GL_ENABLE_BIT); 6.42 + glDisable(GL_LIGHTING); 6.43 + 6.44 + glMatrixMode(GL_MODELVIEW); 6.45 + glPushMatrix(); 6.46 + glMultTransposeMatrixf(matrix[0]); 6.47 + 6.48 + glColor4f(col.x, col.y, col.z, col.w); 6.49 + mesh->set_vis_vecsize(len); 6.50 + mesh->draw_normals(); 6.51 + 6.52 + glPopMatrix(); 6.53 + glPopAttrib(); 6.54 +} 6.55 + 6.56 +void Object::draw_tangents(float len, const Vector4 &col) const 6.57 +{ 6.58 + glPushAttrib(GL_ENABLE_BIT); 6.59 + glDisable(GL_LIGHTING); 6.60 + 6.61 + glMatrixMode(GL_MODELVIEW); 6.62 + glPushMatrix(); 6.63 + glMultTransposeMatrixf(matrix[0]); 6.64 + 6.65 + glColor4f(col.x, col.y, col.z, col.w); 6.66 + mesh->set_vis_vecsize(len); 6.67 + mesh->draw_tangents(); 6.68 + 6.69 + glPopMatrix(); 6.70 + glPopAttrib(); 6.71 +} 6.72 + 6.73 bool Object::intersect(const Ray &ray, HitPoint *hit) const 6.74 { 6.75 return false; // TODO
7.1 --- a/src/object.h Fri Jun 26 05:41:18 2015 +0300 7.2 +++ b/src/object.h Sat Jun 27 08:01:51 2015 +0300 7.3 @@ -37,6 +37,10 @@ 7.4 void set_texture(unsigned int tex); 7.5 7.6 void draw() const; 7.7 + void draw_wire(const Vector4 &col = Vector4(1, 1, 1, 1)) const; 7.8 + void draw_vertices(const Vector4 &col = Vector4(1, 0.3, 0.2, 1)) const; 7.9 + void draw_normals(float len = 1.0, const Vector4 &col = Vector4(0.1, 0.2, 1.0, 1)) const; 7.10 + void draw_tangents(float len = 1.0, const Vector4 &col = Vector4(0.1, 1.0, 0.2, 1)) const; 7.11 7.12 bool intersect(const Ray &ray, HitPoint *hit) const; 7.13 };