# HG changeset patch # User John Tsiombikas # Date 1345202977 -10800 # Node ID 8fb37db44fd89d0801cad1fa3e37f15431c83ec0 # Parent e8434c6b208d013cf7ebf58fd927caba1ac67210 first person motion diff -r e8434c6b208d -r 8fb37db44fd8 prototype/data/deftile.dae --- a/prototype/data/deftile.dae Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/data/deftile.dae Fri Aug 17 14:29:37 2012 +0300 @@ -5,8 +5,8 @@ Blender User Blender 2.61.0 r42615 - 2012-08-12T06:32:28 - 2012-08-12T06:32:28 + 2012-08-17T03:23:47 + 2012-08-17T03:23:47 Z_UP @@ -934,7 +934,7 @@ - + -0.4999999 0.5000001 2.6226e-6 0 0 1 -2.73208e-5 0 1 0 0 @@ -950,7 +950,7 @@ - + 0.5000001 0.5000001 2.6226e-6 0 0 1 -90.00002 0 1 0 0 @@ -966,7 +966,7 @@ - + 0.5000002 0.5 1.000003 0 0 1 -180 0 1 0 -8.65142e-6 @@ -982,7 +982,7 @@ - + -0.4999999 0.5 1.000003 0 0 1 -90.00001 0 1 0 -8.65142e-6 @@ -998,7 +998,7 @@ - + -0.4999999 -0.4999999 2.6226e-6 0 0 1 -270 0 1 0 0 @@ -1014,7 +1014,7 @@ - + -0.4999999 -0.4999999 1.000003 0 0 1 -1.00179e-5 0 1 0 -8.65142e-6 @@ -1030,7 +1030,7 @@ - + -0.4999999 0.5 0.5000013 0 0 1 90 0 1 0 0 @@ -1046,7 +1046,7 @@ - + 0.5000001 0.5 0.5000013 0 0 1 359 0 1 0 0 @@ -1078,7 +1078,7 @@ - + 0.5000001 -0.4999999 1.000003 0 0 1 -270 0 1 0 -1.63213e-12 @@ -1094,7 +1094,7 @@ - + 0.5000001 -0.4999999 2.6226e-6 0 0 1 -180 0 1 0 0 diff -r e8434c6b208d -r 8fb37db44fd8 prototype/data/deftile.mtl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/data/deftile.mtl Fri Aug 17 14:29:37 2012 +0300 @@ -0,0 +1,37 @@ +# Blender MTL File: 'deftile.blend' +# Material Count: 3 +newmtl floor_material_floor_diffuse.jpg +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.640000 0.640000 0.640000 +Ks 0.200000 0.200000 0.200000 +Ni 1.000000 +d 1.000000 +illum 2 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/floor_diffuse.jpg +map_Bump /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/floor_normal.jpg + + +newmtl torch_material_rustiron.jpg +Ns 96.078431 +Ka 0.000000 0.000000 0.000000 +Kd 0.358832 0.198957 0.119706 +Ks 0.107843 0.107843 0.107843 +Ni 1.000000 +d 1.000000 +illum 2 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/rustiron.jpg + + +newmtl wall_material_stonewall_diffuse.jpg +Ns 105.882353 +Ka 0.000000 0.000000 0.000000 +Kd 0.640000 0.640000 0.640000 +Ks 0.200000 0.200000 0.200000 +Ni 1.000000 +d 1.000000 +illum 2 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/stonewall_diffuse.jpg +map_Bump /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/stonewall_normal.jpg + + diff -r e8434c6b208d -r 8fb37db44fd8 prototype/data/deftile.obj --- a/prototype/data/deftile.obj Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/data/deftile.obj Fri Aug 17 14:29:37 2012 +0300 @@ -2525,7 +2525,7 @@ f 554/760/312 559/900/311 558/895/306 f 554/760/312 558/895/306 555/761/305 f 497 498 -o Cube.007 +o NORTHWEST_pillar_base_Cube.007 v -0.400000 0.000003 -0.500000 v -0.400000 0.000003 -0.400000 v -0.500000 0.000003 -0.400000 @@ -2556,7 +2556,7 @@ f 597/905/314 601/907/314 598/908/314 f 598/909/315 601/910/315 602/911/315 f 598/909/315 602/911/315 599/912/315 -o Cube.005_Cube.007 +o NORTHEAST_pillar_base_Cube.007 v 0.500000 0.000003 -0.400000 v 0.400000 0.000003 -0.400000 v 0.400000 0.000003 -0.500000 @@ -2586,7 +2586,7 @@ f 604/917/316 608/919/316 605/920/316 f 605/921/317 608/922/317 609/923/317 f 605/921/317 609/923/317 606/924/317 -o Cube.004_Cube.007 +o NORTHEAST_pillar_cap_Cube.007 v 0.400000 1.000003 -0.500000 v 0.400000 1.000003 -0.400000 v 0.500000 1.000003 -0.400000 @@ -2616,7 +2616,7 @@ f 611/929/243 615/931/243 612/932/243 f 612/933/319 615/934/319 616/935/319 f 612/933/319 616/935/319 613/936/319 -o Cube.003_Cube.007 +o NORTHWEST_pillar_cap_Cube.007 v -0.500000 1.000003 -0.400000 v -0.400000 1.000003 -0.400000 v -0.400000 1.000003 -0.500000 @@ -2646,7 +2646,7 @@ f 618/941/58 622/943/58 619/944/58 f 619/945/321 622/946/321 623/947/321 f 619/945/321 623/947/321 620/948/321 -o Cube.001_Cube.007 +o SOUTHWEST_pillar_base_Cube.007 v -0.500000 0.000003 0.400000 v -0.400000 0.000003 0.400000 v -0.400000 0.000003 0.500000 @@ -2676,7 +2676,7 @@ f 625/953/322 629/955/322 626/956/322 f 626/957/323 629/958/323 630/959/323 f 626/957/323 630/959/323 627/960/323 -o Cube.000_Cube.007 +o SOUTHWEST_pillar_cap_Cube.007 v -0.400000 1.000003 0.500000 v -0.400000 1.000003 0.400000 v -0.500000 1.000003 0.400000 @@ -2705,7 +2705,7 @@ f 632/965/166 636/967/166 633/968/166 f 633/969/89 636/970/89 637/971/89 f 633/969/89 637/971/89 634/972/89 -o Cylinder.002_Cylinder.005 +o NORTHWEST_pillar_Cylinder.005 v -0.425000 0.000001 -0.500000 v -0.430709 0.000001 -0.471299 v -0.446967 0.000001 -0.446967 @@ -2815,7 +2815,7 @@ f 657/986/338 663/996/348 658/987/339 f 662/994/346 647/995/347 648/997/349 f 662/994/346 648/997/349 663/996/348 -o Cylinder.001_Cylinder.005 +o NORTHEAST_pillar_Cylinder.005 v 0.498691 0.000001 -0.425011 v 0.470094 0.000001 -0.431220 v 0.446050 0.000001 -0.447901 @@ -3035,7 +3035,7 @@ f 707/1036/388 713/1046/398 708/1037/389 f 712/1044/396 697/1045/397 698/1047/399 f 712/1044/396 698/1047/399 713/1046/398 -o Cube.006_Cube.007 +o SOUTHEAST_pillar_cap_Cube.007 v 0.500000 1.000003 0.400000 v 0.400000 1.000003 0.400000 v 0.400000 1.000003 0.500000 @@ -3063,7 +3063,7 @@ f 714/1052/89 718/1054/89 715/1055/89 f 715/1056/243 718/1057/243 719/1058/243 f 715/1056/243 719/1058/243 716/1059/243 -o Cube.002_Cube.007 +o SOUTHEAST_pillar_base_Cube.007 v 0.400000 0.000003 0.500000 v 0.400000 0.000003 0.400000 v 0.500000 0.000003 0.400000 diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/camera.cc --- a/prototype/src/camera.cc Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/camera.cc Fri Aug 17 14:29:37 2012 +0300 @@ -106,13 +106,13 @@ void OrbitCamera::input_rotate(float x, float y, float z) { - theta += x; - phi += y; + theta += y; + phi += x; - if(phi < -M_PI / 2.0) - phi = -M_PI / 2.0; - if(phi > M_PI / 2.0) - phi = M_PI / 2.0; + if(phi < -M_PI / 2) + phi = -M_PI / 2; + if(phi > M_PI) + phi = M_PI; inval_cache(); } @@ -127,6 +127,32 @@ } +void FpsCamera::calc_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + /*mat->translate(Vector3(pos.x, pos.y, pos.z)); + mat->rotate(Vector3(phi, 0, 0)); + mat->rotate(Vector3(0, theta, 0));*/ +} + +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const +{ + mat->reset_identity(); + mat->rotate(Vector3(phi, 0, 0)); + mat->rotate(Vector3(0, theta, 0)); + mat->translate(Vector3(-pos.x, -pos.y, -pos.z)); +} + +void FpsCamera::input_move(float x, float y, float z) +{ + pos.x += x * cos(theta) - z * sin(theta); + pos.z += x * sin(theta) + z * cos(theta); + pos.y += y; + inval_cache(); +} + + + FlyCamera::FlyCamera() { pos.z = 10.0f; diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/camera.h --- a/prototype/src/camera.h Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/camera.h Fri Aug 17 14:29:37 2012 +0300 @@ -33,7 +33,7 @@ }; class OrbitCamera : public Camera { -private: +protected: float theta, phi, rad; void calc_matrix(Matrix4x4 *mat) const; @@ -47,6 +47,17 @@ void input_zoom(float factor); }; +class FpsCamera : public OrbitCamera { +protected: + Vector3 pos; + + void calc_matrix(Matrix4x4 *mat) const; + void calc_inv_matrix(Matrix4x4 *mat) const; + +public: + void input_move(float x, float y, float z); +}; + class FlyCamera : public Camera { private: Vector3 pos; diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/level.cc --- a/prototype/src/level.cc Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/level.cc Fri Aug 17 14:29:37 2012 +0300 @@ -114,18 +114,18 @@ glTranslatef(pos.x, pos.y, pos.z); glScalef(cell_size, cell_size, cell_size); - unsigned int dmask = 0; - if(i <= 0 || get_cell(j, i - 1) == 0) { - dmask |= TILE_NORTH; + unsigned int dmask = TILE_ALL; + if(i > 0 && get_cell(j, i - 1)) { + dmask &= ~TILE_NORTH; } - if(i > ysz || get_cell(j, i + 1) == 0) { - dmask |= TILE_SOUTH; + if(i < ysz - 1 && get_cell(j, i + 1)) { + dmask &= ~TILE_SOUTH; } - if(j <= 0 || get_cell(j - 1, i) == 0) { - dmask |= TILE_WEST; + if(j > 0 && get_cell(j - 1, i)) { + dmask &= ~TILE_WEST; } - if(j > xsz || get_cell(j + 1, i) == 0) { - dmask |= TILE_EAST; + if(j < xsz - 1 && get_cell(j + 1, i)) { + dmask &= ~TILE_EAST; } cell->draw(dmask); diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/main.cc --- a/prototype/src/main.cc Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/main.cc Fri Aug 17 14:29:37 2012 +0300 @@ -1,6 +1,7 @@ #include #include #include +#include #include "opengl.h" #include "level.h" #include "camera.h" @@ -9,15 +10,20 @@ bool init(); void cleanup(); +void idle(); void disp(); +void update(unsigned long msec); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); +void key_release(unsigned char key, int x, int y); void mouse(int bn, int state, int x, int y); void motion(int x, int y); static TileSet *tileset; static Level *level; -static OrbitCamera cam; + +static FpsCamera cam; +static bool keystate[256]; static const char *level_file = "0.level"; @@ -33,9 +39,11 @@ glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); glutCreateWindow("prototype"); + glutIdleFunc(idle); glutDisplayFunc(disp); glutReshapeFunc(reshape); glutKeyboardFunc(keyb); + glutKeyboardUpFunc(key_release); glutMouseFunc(mouse); glutMotionFunc(motion); @@ -70,25 +78,68 @@ set_active_tileset(tileset); level = new Level; + printf("loading level: %s\n", level_file); if(!level->load(datafile_path(level_file))) { return false; } + cam.input_move(0, 0.5, 0); + return true; } +void idle() +{ + glutPostRedisplay(); +} + void disp() { + update(glutGet(GLUT_ELAPSED_TIME)); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - cam.use(); + cam.use_inverse(); level->draw(); glutSwapBuffers(); assert(glGetError() == GL_NO_ERROR); + + usleep(10000); +} + +void update(unsigned long msec) +{ + static unsigned long last_upd; + + if(last_upd == 0) { + last_upd = msec; + } + float dt = (float)(msec - last_upd) / 1000.0; + + float offs = 2.0 * dt; + float dx = 0, dy = 0; + + // handle key input + if(keystate['w'] || keystate['W']) { + dy -= offs; + } + if(keystate['s'] || keystate['S']) { + dy += offs; + } + if(keystate['d'] || keystate['D']) { + dx += offs; + } + if(keystate['a'] || keystate['A']) { + dx -= offs; + } + + cam.input_move(dx, 0, dy); + + last_upd = msec; } void reshape(int x, int y) @@ -96,7 +147,7 @@ glViewport(0, 0, x, y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); + gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0); } void keyb(unsigned char key, int x, int y) @@ -105,6 +156,13 @@ case 27: exit(0); } + + keystate[key] = true; +} + +void key_release(unsigned char key, int x, int y) +{ + keystate[key] = false; } static int prev_x, prev_y; @@ -125,7 +183,7 @@ prev_y = y; if(bnstate[0]) { - cam.input_rotate(dx * 0.01, dy * 0.01, 0); + cam.input_rotate(dy * 0.01, dx * 0.01, 0); glutPostRedisplay(); } if(bnstate[2]) { diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/mesh.cc --- a/prototype/src/mesh.cc Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/mesh.cc Fri Aug 17 14:29:37 2012 +0300 @@ -16,6 +16,11 @@ destroy(); } +void Mesh::set_name(const char *name) +{ + this->name = name; +} + const char *Mesh::get_name() const { return name.c_str(); @@ -83,6 +88,16 @@ tang_loc = -1; } +void Mesh::set_xform(const Matrix4x4 &mat) +{ + xform = mat; +} + +const Matrix4x4 &Mesh::get_xform() const +{ + return xform; +} + void Mesh::set_attrib_location(int attr, int loc) { if(attr == MESH_ATTR_TANGENT) { @@ -97,6 +112,10 @@ void Mesh::draw() const { + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + mult_matrix(xform); + glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]); glVertexPointer(3, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); @@ -126,4 +145,6 @@ if(tang_loc >= 0) { glDisableVertexAttribArray(tang_loc); } + + glPopMatrix(); } diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/mesh.h --- a/prototype/src/mesh.h Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/mesh.h Fri Aug 17 14:29:37 2012 +0300 @@ -2,8 +2,8 @@ #define MESH_H_ #include -#include -#include +#include +#include "vmath.h" enum { MESH_ATTR_VERTEX, @@ -26,15 +26,21 @@ int tang_loc; + Matrix4x4 xform; + public: Mesh(); ~Mesh(); + void set_name(const char *name); const char *get_name() const; bool create(const aiScene *scn, aiMesh *aim); void destroy(); + void set_xform(const Matrix4x4 &mat); + const Matrix4x4 &get_xform() const; + void set_attrib_location(int attr, int loc); int get_attrib_location(int attr) const; diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/opengl.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/opengl.cc Fri Aug 17 14:29:37 2012 +0300 @@ -0,0 +1,40 @@ +#include "opengl.h" +#include "vmath.h" + +void load_matrix(const Matrix4x4 &m) +{ +#ifdef SINGLE_PRECISION_MATH + if(glLoadTransposeMatrixfARB) { + glLoadTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixf((float*)&tmat); + } +#else + if(glLoadTransposeMatrixdARB) { + glLoadTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glLoadMatrixd((double*)&tmat); + } +#endif +} + +void mult_matrix(const Matrix4x4 &m) +{ +#ifdef SINGLE_PRECISION_MATH + if(glMultTransposeMatrixfARB) { + glMultTransposeMatrixfARB((float*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixf((float*)&tmat); + } +#else + if(glMultTransposeMatrixdARB) { + glMultTransposeMatrixdARB((double*)&m); + } else { + Matrix4x4 tmat = m.transposed(); + glMultMatrixd((double*)&tmat); + } +#endif +} diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/opengl.h --- a/prototype/src/opengl.h Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/opengl.h Fri Aug 17 14:29:37 2012 +0300 @@ -9,4 +9,9 @@ #include #endif +class Matrix4x4; + +void load_matrix(const Matrix4x4 &m); +void mult_matrix(const Matrix4x4 &m); + #endif /* OPENGL_H_ */ diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/tile.cc --- a/prototype/src/tile.cc Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/tile.cc Fri Aug 17 14:29:37 2012 +0300 @@ -1,10 +1,16 @@ #include +#include #include "opengl.h" -#include -#include -#include +#include +#include +#include #include "tile.h" +using std::map; + +static void build_nodemap(map *nmap, const aiScene *scn, aiNode *node); + + bool Tile::load(const char *fname) { if(!fname) { @@ -12,8 +18,10 @@ } unsigned int proc_flags = aiProcess_JoinIdenticalVertices | - aiProcess_PreTransformVertices | aiProcess_Triangulate | - aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs; + aiProcess_Triangulate | + aiProcess_GenNormals | + aiProcess_SortByPType | + aiProcess_FlipUVs; const aiScene *scn = aiImportFile(fname, proc_flags); if(!scn) { @@ -21,9 +29,11 @@ return -1; } + map nodemap; + build_nodemap(&nodemap, scn, scn->mRootNode); + load_lights(scn); - - load_meshes(scn); + load_meshes(scn, nodemap); printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights); return true; @@ -70,7 +80,7 @@ return count; } -int Tile::load_meshes(const aiScene *scn) +int Tile::load_meshes(const aiScene *scn, const std::map &nmap) { int count = 0; @@ -81,28 +91,62 @@ continue; } + // retrieve the node pointer + const char *name = ""; + + auto iter = nmap.find(scn->mMeshes[i]); + if(iter != nmap.end()) { + aiNode *node = iter->second; + + Matrix4x4 xform; + //xform.rotate(Vector3(-M_PI / 2.0, 0, 0)); + xform = *(Matrix4x4*)&node->mTransformation; + mesh->set_xform(xform); + + name = node->mName.data; + mesh->set_name(name); + } meshes.push_back(mesh); + + printf("name: \"%s\" ... ", name); + // find which side is this mesh on - const char *name = mesh->get_name(); - printf("name: %s ... ", name); - unsigned int side; - - if(strstr(name, "NORTH") == name) { - side = TILE_NORTH; - } else if(strstr(name, "SOUTH") == name) { - side = TILE_SOUTH; - } else if(strstr(name, "EAST") == name) { - side = TILE_EAST; - } else if(strstr(name, "WEST") == name) { - side = TILE_WEST; - } else { + unsigned int side = 0; + if(strstr(name, "NORTH")) { + side |= TILE_NORTH; + } + if(strstr(name, "SOUTH")) { + side |= TILE_SOUTH; + } + if(strstr(name, "EAST")) { + side |= TILE_EAST; + } + if(strstr(name, "WEST")) { + side |= TILE_WEST; + } + if(!side) { side = TILE_ALL; } - printf("side: %x\n", side); + printf("side: 0x0%x\n", side); mesh_side.push_back(side); count++; } return count; } + +static void build_nodemap(map *nmap, const aiScene *scn, aiNode *node) +{ + unsigned int i; + + for(i=0; imNumMeshes; i++) { + aiMesh *m = scn->mMeshes[node->mMeshes[i]]; + + (*nmap)[m] = node; + } + + for(i=0; imNumChildren; i++) { + build_nodemap(nmap, scn, node->mChildren[i]); + } +} diff -r e8434c6b208d -r 8fb37db44fd8 prototype/src/tile.h --- a/prototype/src/tile.h Tue Aug 14 08:49:38 2012 +0300 +++ b/prototype/src/tile.h Fri Aug 17 14:29:37 2012 +0300 @@ -2,7 +2,8 @@ #define TILE_H_ #include -#include +#include +#include #include "mesh.h" #include "light.h" @@ -11,7 +12,7 @@ TILE_SOUTH = 2, TILE_EAST = 4, TILE_WEST = 8, - TILE_ALL = 0xf + TILE_ALL = 0xffff }; class Tile { @@ -21,7 +22,7 @@ std::vector lights; int load_lights(const aiScene *scn); - int load_meshes(const aiScene *scn); + int load_meshes(const aiScene *scn, const std::map &nmap); public: bool load(const char *fname);