# HG changeset patch # User John Tsiombikas # Date 1434938737 -10800 # Node ID 3fcd7b4d631f14d17a439b0e46d8b3efc1ac7733 # Parent 52e0dd47753ba714479f65198d786fb719cdf916 board mesh generation diff -r 52e0dd47753b -r 3fcd7b4d631f Makefile --- a/Makefile Sun Jun 21 06:30:39 2015 +0300 +++ b/Makefile Mon Jun 22 05:05:37 2015 +0300 @@ -6,7 +6,7 @@ bin = tavli CXXFLAGS = -pedantic -Wall -g -LDFLAGS = $(libgl) +LDFLAGS = $(libgl) -lvmath -limago -lm -lpthread ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lGLEW diff -r 52e0dd47753b -r 3fcd7b4d631f src/board.cc --- a/src/board.cc Sun Jun 21 06:30:39 2015 +0300 +++ b/src/board.cc Mon Jun 22 05:05:37 2015 +0300 @@ -1,11 +1,11 @@ #include "opengl.h" #include "board.h" +#include "meshgen.h" -static Mesh *gen_board_mesh(); -static Mesh *gen_puck_mesh(); Board::Board() { + puck_mesh = 0; clear(); } @@ -16,20 +16,22 @@ bool Board::init() { - if(!(board_mesh = gen_board_mesh())) { + if(!generate()) { return false; } - if(!(puck_mesh = gen_puck_mesh())) { - return false; - } + return true; } void Board::destroy() { - delete board_mesh; + for(size_t i=0; idraw(); + for(size_t i=0; idraw(); + } } -static Mesh *gen_board_mesh() +#define HSIZE 1.0 +#define VSIZE (2.0 * HSIZE) +#define BOT_THICKNESS (HSIZE * 0.01) +#define WALL_THICKNESS (HSIZE * 0.05) +#define WALL_HEIGHT (HSIZE * 0.1) +#define GAP (HSIZE * 0.025) +#define HINGE_RAD (GAP * 0.5) +#define HINGE_HEIGHT (VSIZE * 0.075) + +bool Board::generate() { - return 0; + Matrix4x4 xform; + + // generate bottom + Mesh *bottom = new Mesh; + gen_box(bottom, HSIZE, BOT_THICKNESS, HSIZE * 2.0); + xform.set_translation(Vector3(0, -BOT_THICKNESS / 2.0, 0)); + bottom->apply_xform(xform); + + // generate the 4 sides + Mesh *sides = new Mesh; + gen_box(sides, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2); + xform.set_translation(Vector3(-(HSIZE + WALL_THICKNESS) / 2.0, + WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0)); + sides->apply_xform(xform); + + Mesh tmp; + gen_box(&tmp, WALL_THICKNESS, WALL_HEIGHT, VSIZE + WALL_THICKNESS * 2); + xform.set_translation(Vector3((HSIZE + WALL_THICKNESS) / 2.0, + WALL_HEIGHT / 2.0 - BOT_THICKNESS, 0)); + tmp.apply_xform(xform); + sides->append(tmp); + tmp.clear(); + + gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS); + xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS, + (VSIZE + WALL_THICKNESS) / 2.0)); + tmp.apply_xform(xform); + sides->append(tmp); + tmp.clear(); + + gen_box(&tmp, HSIZE, WALL_HEIGHT, WALL_THICKNESS); + xform.set_translation(Vector3(0, WALL_HEIGHT / 2.0 - BOT_THICKNESS, + -(VSIZE + WALL_THICKNESS) / 2.0)); + tmp.apply_xform(xform); + sides->append(tmp); + tmp.clear(); + + // generate the hinges + Mesh *hinges = new Mesh; + gen_cylinder(hinges, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1); + xform.set_rotation(Vector3(M_PI / 2.0, 0, 0)); + xform.translate(Vector3(0, VSIZE / 4.0, 0)); + hinges->apply_xform(xform); + + gen_cylinder(&tmp, HINGE_RAD, HINGE_HEIGHT, 10, 1, 1); + xform.set_rotation(Vector3(M_PI / 2.0, 0, 0)); + xform.translate(Vector3(0, -VSIZE / 4.0, 0)); + tmp.apply_xform(xform); + + hinges->append(tmp); + + + board_meshes.clear(); + board_meshes.push_back(bottom); + board_meshes.push_back(sides); + board_meshes.push_back(hinges); + return true; } - -static Mesh *gen_puck_mesh() -{ - return 0; -} diff -r 52e0dd47753b -r 3fcd7b4d631f src/board.h --- a/src/board.h Sun Jun 21 06:30:39 2015 +0300 +++ b/src/board.h Mon Jun 22 05:05:37 2015 +0300 @@ -1,6 +1,7 @@ #ifndef BOARD_H_ #define BOARD_H_ +#include #include "mesh.h" #define NUM_SLOTS 24 @@ -11,7 +12,10 @@ class Board { private: int slots[NUM_SLOTS][MAX_PUCKS]; - Mesh *board_mesh, *puck_mesh; + std::vector board_meshes; + Mesh *puck_mesh; + + bool generate(); public: Board(); diff -r 52e0dd47753b -r 3fcd7b4d631f src/game.cc --- a/src/game.cc Sun Jun 21 06:30:39 2015 +0300 +++ b/src/game.cc Mon Jun 22 05:05:37 2015 +0300 @@ -1,11 +1,14 @@ #include #include #include "game.h" +#include "board.h" static void draw_backdrop(); int win_width, win_height; +static Board board; + static float cam_theta, cam_phi = 25, cam_dist = 6; static bool bnstate[8]; static int prev_x, prev_y; @@ -19,11 +22,16 @@ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); + if(!board.init()) { + return false; + } + return true; } void game_cleanup() { + board.destroy(); } void game_update(unsigned long time_msec) @@ -42,13 +50,16 @@ draw_backdrop(); + board.draw(); + + /* glBegin(GL_QUADS); glNormal3f(0, 1, 0); glVertex3f(-1, 0, 1); glVertex3f(1, 0, 1); glVertex3f(1, 0, -1); glVertex3f(-1, 0, -1); - glEnd(); + glEnd();*/ } static void draw_backdrop() @@ -85,7 +96,7 @@ { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(50, (float)x / (float)y, 0.5, 500.0); + gluPerspective(50, (float)x / (float)y, 0.2, 200.0); glViewport(0, 0, x, y); } diff -r 52e0dd47753b -r 3fcd7b4d631f src/mesh.cc --- a/src/mesh.cc Sun Jun 21 06:30:39 2015 +0300 +++ b/src/mesh.cc Mon Jun 22 05:05:37 2015 +0300 @@ -5,15 +5,16 @@ #include "opengl.h" #include "mesh.h" //#include "xform_node.h" -#include "shader.h" -int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { +int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 }; +/* (int)SDR_ATTR_VERTEX, (int)SDR_ATTR_NORMAL, (int)SDR_ATTR_TANGENT, (int)SDR_ATTR_TEXCOORD, (int)SDR_ATTR_COLOR, -1, -1}; +*/ unsigned int Mesh::intersect_mode = ISECT_DEFAULT; float Mesh::vertex_sel_dist = 0.01; float Mesh::vis_vecsize = 1.0; @@ -555,12 +556,9 @@ void Mesh::draw() const { -#ifdef GL_ES_VERSION_2_0 - if(!SdrProg::active) { - fprintf(stderr, "%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__); - return; - } -#endif + int cur_sdr; + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_sdr); + ((Mesh*)this)->update_buffers(); @@ -569,7 +567,7 @@ return; } - if(SdrProg::active) { + if(cur_sdr) { // rendering with shaders if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__); @@ -618,7 +616,7 @@ glDrawArrays(GL_TRIANGLES, 0, nverts); } - if(SdrProg::active) { + if(cur_sdr) { // rendered with shaders for(int i=0; i +#include "meshgen.h" +#include "mesh.h" + +// -------- sphere -------- + +#define SURAD(u) ((u) * 2.0 * M_PI) +#define SVRAD(v) ((v) * M_PI) + +static Vector3 sphvec(float theta, float phi) +{ + return Vector3(sin(theta) * sin(phi), + cos(phi), + cos(theta) * sin(phi)); +} + +void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange) +{ + if(usub < 4) usub = 4; + if(vsub < 2) vsub = 2; + + int uverts = usub + 1; + int vverts = vsub + 1; + + int num_verts = uverts * vverts; + int num_quads = usub * vsub; + int num_tri = num_quads * 2; + + mesh->clear(); + 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 = urange / (float)(uverts - 1); + float dv = vrange / (float)(vverts - 1); + + float u = 0.0; + for(int i=0; iclear(); + 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 = urange / (float)(uverts - 1); + float dv = vrange / (float)(vverts - 1); + + float u = 0.0; + for(int i=0; iclear(); + 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 = urange / (float)(uverts - 1); + float dv = vrange / (float)(vverts - 1); + + float u = 0.0; + for(int i=0; iclear(); + + 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)usub; + float dv = 1.0 / (float)vsub; + + float u = 0.0; + for(int i=0; iclear(); + + const int num_faces = 6; + int num_verts = num_faces * 4; + int num_tri = num_faces * 2; + + float x = xsz / 2.0; + float y = ysz / 2.0; + float z = zsz / 2.0; + + 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); + + static const Vector2 uv[] = { Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1) }; + + // front + for(int i=0; i<4; i++) { + *narr++ = Vector3(0, 0, 1); + *tarr++ = Vector3(1, 0, 0); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(-x, -y, z); + *varr++ = Vector3(x, -y, z); + *varr++ = Vector3(x, y, z); + *varr++ = Vector3(-x, y, z); + // right + for(int i=0; i<4; i++) { + *narr++ = Vector3(1, 0, 0); + *tarr++ = Vector3(0, 0, -1); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(x, -y, z); + *varr++ = Vector3(x, -y, -z); + *varr++ = Vector3(x, y, -z); + *varr++ = Vector3(x, y, z); + // back + for(int i=0; i<4; i++) { + *narr++ = Vector3(0, 0, -1); + *tarr++ = Vector3(-1, 0, 0); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(x, -y, -z); + *varr++ = Vector3(-x, -y, -z); + *varr++ = Vector3(-x, y, -z); + *varr++ = Vector3(x, y, -z); + // left + for(int i=0; i<4; i++) { + *narr++ = Vector3(-1, 0, 0); + *tarr++ = Vector3(0, 0, 1); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(-x, -y, -z); + *varr++ = Vector3(-x, -y, z); + *varr++ = Vector3(-x, y, z); + *varr++ = Vector3(-x, y, -z); + // top + for(int i=0; i<4; i++) { + *narr++ = Vector3(0, 1, 0); + *tarr++ = Vector3(1, 0, 0); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(-x, y, z); + *varr++ = Vector3(x, y, z); + *varr++ = Vector3(x, y, -z); + *varr++ = Vector3(-x, y, -z); + // bottom + for(int i=0; i<4; i++) { + *narr++ = Vector3(0, -1, 0); + *tarr++ = Vector3(1, 0, 0); + *uvarr++ = uv[i]; + } + *varr++ = Vector3(-x, -y, -z); + *varr++ = Vector3(x, -y, -z); + *varr++ = Vector3(x, -y, z); + *varr++ = Vector3(-x, -y, z); + + // index array + static const int faceidx[] = {0, 1, 2, 0, 2, 3}; + for(int i=0; imesh = m; +} + +Mesh *Object::get_mesh() const +{ + return mesh; +} + +void Object::draw() const +{ + if(!mesh) return; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadTransposeMatrixf(matrix[0]); + + mesh->draw(); + + glPopMatrix(); +} diff -r 52e0dd47753b -r 3fcd7b4d631f src/object.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.h Mon Jun 22 05:05:37 2015 +0300 @@ -0,0 +1,24 @@ +#ifndef OBJECT_H_ +#define OBJECT_H_ + +#include "mesh.h" + +class Object { +private: + Mesh *mesh; + Matrix4x4 matrix; + +public: + Object(); + ~Object(); + + Matrix4x4 &xform(); + const Matrix4x4 &xform() const; + + void set_mesh(Mesh *m); + Mesh *get_mesh() const; + + void draw() const; +}; + +#endif // OBJECT_H_