dungeon_crawler

changeset 7:8fb37db44fd8

first person motion
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Aug 2012 14:29:37 +0300
parents e8434c6b208d
children 77c302306206
files prototype/data/deftile.dae prototype/data/deftile.mtl prototype/data/deftile.obj prototype/src/camera.cc prototype/src/camera.h prototype/src/level.cc prototype/src/main.cc prototype/src/mesh.cc prototype/src/mesh.h prototype/src/opengl.cc prototype/src/opengl.h prototype/src/tile.cc prototype/src/tile.h
diffstat 13 files changed, 318 insertions(+), 69 deletions(-) [+]
line diff
     1.1 --- a/prototype/data/deftile.dae	Tue Aug 14 08:49:38 2012 +0300
     1.2 +++ b/prototype/data/deftile.dae	Fri Aug 17 14:29:37 2012 +0300
     1.3 @@ -5,8 +5,8 @@
     1.4        <author>Blender User</author>
     1.5        <authoring_tool>Blender 2.61.0 r42615</authoring_tool>
     1.6      </contributor>
     1.7 -    <created>2012-08-12T06:32:28</created>
     1.8 -    <modified>2012-08-12T06:32:28</modified>
     1.9 +    <created>2012-08-17T03:23:47</created>
    1.10 +    <modified>2012-08-17T03:23:47</modified>
    1.11      <unit name="meter" meter="1"/>
    1.12      <up_axis>Z_UP</up_axis>
    1.13    </asset>
    1.14 @@ -934,7 +934,7 @@
    1.15            </bind_material>
    1.16          </instance_geometry>
    1.17        </node>
    1.18 -      <node id="Cube_007" type="NODE">
    1.19 +      <node id="NORTHWEST_pillar_base" type="NODE">
    1.20          <translate sid="location">-0.4999999 0.5000001 2.6226e-6</translate>
    1.21          <rotate sid="rotationZ">0 0 1 -2.73208e-5</rotate>
    1.22          <rotate sid="rotationY">0 1 0 0</rotate>
    1.23 @@ -950,7 +950,7 @@
    1.24            </bind_material>
    1.25          </instance_geometry>
    1.26        </node>
    1.27 -      <node id="Cube_005" type="NODE">
    1.28 +      <node id="NORTHEAST_pillar_base" type="NODE">
    1.29          <translate sid="location">0.5000001 0.5000001 2.6226e-6</translate>
    1.30          <rotate sid="rotationZ">0 0 1 -90.00002</rotate>
    1.31          <rotate sid="rotationY">0 1 0 0</rotate>
    1.32 @@ -966,7 +966,7 @@
    1.33            </bind_material>
    1.34          </instance_geometry>
    1.35        </node>
    1.36 -      <node id="Cube_004" type="NODE">
    1.37 +      <node id="NORTHEAST_pillar_cap" type="NODE">
    1.38          <translate sid="location">0.5000002 0.5 1.000003</translate>
    1.39          <rotate sid="rotationZ">0 0 1 -180</rotate>
    1.40          <rotate sid="rotationY">0 1 0 -8.65142e-6</rotate>
    1.41 @@ -982,7 +982,7 @@
    1.42            </bind_material>
    1.43          </instance_geometry>
    1.44        </node>
    1.45 -      <node id="Cube_003" type="NODE">
    1.46 +      <node id="NORTHWEST_pillar_cap" type="NODE">
    1.47          <translate sid="location">-0.4999999 0.5 1.000003</translate>
    1.48          <rotate sid="rotationZ">0 0 1 -90.00001</rotate>
    1.49          <rotate sid="rotationY">0 1 0 -8.65142e-6</rotate>
    1.50 @@ -998,7 +998,7 @@
    1.51            </bind_material>
    1.52          </instance_geometry>
    1.53        </node>
    1.54 -      <node id="Cube_001" type="NODE">
    1.55 +      <node id="SOUTHWEST_pillar_base" type="NODE">
    1.56          <translate sid="location">-0.4999999 -0.4999999 2.6226e-6</translate>
    1.57          <rotate sid="rotationZ">0 0 1 -270</rotate>
    1.58          <rotate sid="rotationY">0 1 0 0</rotate>
    1.59 @@ -1014,7 +1014,7 @@
    1.60            </bind_material>
    1.61          </instance_geometry>
    1.62        </node>
    1.63 -      <node id="Cube_000" type="NODE">
    1.64 +      <node id="SOUTHWEST_pillar_cap" type="NODE">
    1.65          <translate sid="location">-0.4999999 -0.4999999 1.000003</translate>
    1.66          <rotate sid="rotationZ">0 0 1 -1.00179e-5</rotate>
    1.67          <rotate sid="rotationY">0 1 0 -8.65142e-6</rotate>
    1.68 @@ -1030,7 +1030,7 @@
    1.69            </bind_material>
    1.70          </instance_geometry>
    1.71        </node>
    1.72 -      <node id="Cylinder_002" type="NODE">
    1.73 +      <node id="NORTHWEST_pillar" type="NODE">
    1.74          <translate sid="location">-0.4999999 0.5 0.5000013</translate>
    1.75          <rotate sid="rotationZ">0 0 1 90</rotate>
    1.76          <rotate sid="rotationY">0 1 0 0</rotate>
    1.77 @@ -1046,7 +1046,7 @@
    1.78            </bind_material>
    1.79          </instance_geometry>
    1.80        </node>
    1.81 -      <node id="Cylinder_001" type="NODE">
    1.82 +      <node id="NORTHEAST_pillar" type="NODE">
    1.83          <translate sid="location">0.5000001 0.5 0.5000013</translate>
    1.84          <rotate sid="rotationZ">0 0 1 359</rotate>
    1.85          <rotate sid="rotationY">0 1 0 0</rotate>
    1.86 @@ -1078,7 +1078,7 @@
    1.87            </bind_material>
    1.88          </instance_geometry>
    1.89        </node>
    1.90 -      <node id="Cube_006" type="NODE">
    1.91 +      <node id="SOUTHEAST_pillar_cap" type="NODE">
    1.92          <translate sid="location">0.5000001 -0.4999999 1.000003</translate>
    1.93          <rotate sid="rotationZ">0 0 1 -270</rotate>
    1.94          <rotate sid="rotationY">0 1 0 -1.63213e-12</rotate>
    1.95 @@ -1094,7 +1094,7 @@
    1.96            </bind_material>
    1.97          </instance_geometry>
    1.98        </node>
    1.99 -      <node id="Cube_002" type="NODE">
   1.100 +      <node id="SOUTHEAST_pillar_base" type="NODE">
   1.101          <translate sid="location">0.5000001 -0.4999999 2.6226e-6</translate>
   1.102          <rotate sid="rotationZ">0 0 1 -180</rotate>
   1.103          <rotate sid="rotationY">0 1 0 0</rotate>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/data/deftile.mtl	Fri Aug 17 14:29:37 2012 +0300
     2.3 @@ -0,0 +1,37 @@
     2.4 +# Blender MTL File: 'deftile.blend'
     2.5 +# Material Count: 3
     2.6 +newmtl floor_material_floor_diffuse.jpg
     2.7 +Ns 96.078431
     2.8 +Ka 0.000000 0.000000 0.000000
     2.9 +Kd 0.640000 0.640000 0.640000
    2.10 +Ks 0.200000 0.200000 0.200000
    2.11 +Ni 1.000000
    2.12 +d 1.000000
    2.13 +illum 2
    2.14 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/floor_diffuse.jpg
    2.15 +map_Bump /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/floor_normal.jpg
    2.16 +
    2.17 +
    2.18 +newmtl torch_material_rustiron.jpg
    2.19 +Ns 96.078431
    2.20 +Ka 0.000000 0.000000 0.000000
    2.21 +Kd 0.358832 0.198957 0.119706
    2.22 +Ks 0.107843 0.107843 0.107843
    2.23 +Ni 1.000000
    2.24 +d 1.000000
    2.25 +illum 2
    2.26 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/rustiron.jpg
    2.27 +
    2.28 +
    2.29 +newmtl wall_material_stonewall_diffuse.jpg
    2.30 +Ns 105.882353
    2.31 +Ka 0.000000 0.000000 0.000000
    2.32 +Kd 0.640000 0.640000 0.640000
    2.33 +Ks 0.200000 0.200000 0.200000
    2.34 +Ni 1.000000
    2.35 +d 1.000000
    2.36 +illum 2
    2.37 +map_Kd /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/stonewall_diffuse.jpg
    2.38 +map_Bump /home/nuclear/3ddata/dung_crawl_prot/deftile.blend/stonewall_normal.jpg
    2.39 +
    2.40 +
     3.1 --- a/prototype/data/deftile.obj	Tue Aug 14 08:49:38 2012 +0300
     3.2 +++ b/prototype/data/deftile.obj	Fri Aug 17 14:29:37 2012 +0300
     3.3 @@ -2525,7 +2525,7 @@
     3.4  f 554/760/312 559/900/311 558/895/306
     3.5  f 554/760/312 558/895/306 555/761/305
     3.6  f 497 498
     3.7 -o Cube.007
     3.8 +o NORTHWEST_pillar_base_Cube.007
     3.9  v -0.400000 0.000003 -0.500000
    3.10  v -0.400000 0.000003 -0.400000
    3.11  v -0.500000 0.000003 -0.400000
    3.12 @@ -2556,7 +2556,7 @@
    3.13  f 597/905/314 601/907/314 598/908/314
    3.14  f 598/909/315 601/910/315 602/911/315
    3.15  f 598/909/315 602/911/315 599/912/315
    3.16 -o Cube.005_Cube.007
    3.17 +o NORTHEAST_pillar_base_Cube.007
    3.18  v 0.500000 0.000003 -0.400000
    3.19  v 0.400000 0.000003 -0.400000
    3.20  v 0.400000 0.000003 -0.500000
    3.21 @@ -2586,7 +2586,7 @@
    3.22  f 604/917/316 608/919/316 605/920/316
    3.23  f 605/921/317 608/922/317 609/923/317
    3.24  f 605/921/317 609/923/317 606/924/317
    3.25 -o Cube.004_Cube.007
    3.26 +o NORTHEAST_pillar_cap_Cube.007
    3.27  v 0.400000 1.000003 -0.500000
    3.28  v 0.400000 1.000003 -0.400000
    3.29  v 0.500000 1.000003 -0.400000
    3.30 @@ -2616,7 +2616,7 @@
    3.31  f 611/929/243 615/931/243 612/932/243
    3.32  f 612/933/319 615/934/319 616/935/319
    3.33  f 612/933/319 616/935/319 613/936/319
    3.34 -o Cube.003_Cube.007
    3.35 +o NORTHWEST_pillar_cap_Cube.007
    3.36  v -0.500000 1.000003 -0.400000
    3.37  v -0.400000 1.000003 -0.400000
    3.38  v -0.400000 1.000003 -0.500000
    3.39 @@ -2646,7 +2646,7 @@
    3.40  f 618/941/58 622/943/58 619/944/58
    3.41  f 619/945/321 622/946/321 623/947/321
    3.42  f 619/945/321 623/947/321 620/948/321
    3.43 -o Cube.001_Cube.007
    3.44 +o SOUTHWEST_pillar_base_Cube.007
    3.45  v -0.500000 0.000003 0.400000
    3.46  v -0.400000 0.000003 0.400000
    3.47  v -0.400000 0.000003 0.500000
    3.48 @@ -2676,7 +2676,7 @@
    3.49  f 625/953/322 629/955/322 626/956/322
    3.50  f 626/957/323 629/958/323 630/959/323
    3.51  f 626/957/323 630/959/323 627/960/323
    3.52 -o Cube.000_Cube.007
    3.53 +o SOUTHWEST_pillar_cap_Cube.007
    3.54  v -0.400000 1.000003 0.500000
    3.55  v -0.400000 1.000003 0.400000
    3.56  v -0.500000 1.000003 0.400000
    3.57 @@ -2705,7 +2705,7 @@
    3.58  f 632/965/166 636/967/166 633/968/166
    3.59  f 633/969/89 636/970/89 637/971/89
    3.60  f 633/969/89 637/971/89 634/972/89
    3.61 -o Cylinder.002_Cylinder.005
    3.62 +o NORTHWEST_pillar_Cylinder.005
    3.63  v -0.425000 0.000001 -0.500000
    3.64  v -0.430709 0.000001 -0.471299
    3.65  v -0.446967 0.000001 -0.446967
    3.66 @@ -2815,7 +2815,7 @@
    3.67  f 657/986/338 663/996/348 658/987/339
    3.68  f 662/994/346 647/995/347 648/997/349
    3.69  f 662/994/346 648/997/349 663/996/348
    3.70 -o Cylinder.001_Cylinder.005
    3.71 +o NORTHEAST_pillar_Cylinder.005
    3.72  v 0.498691 0.000001 -0.425011
    3.73  v 0.470094 0.000001 -0.431220
    3.74  v 0.446050 0.000001 -0.447901
    3.75 @@ -3035,7 +3035,7 @@
    3.76  f 707/1036/388 713/1046/398 708/1037/389
    3.77  f 712/1044/396 697/1045/397 698/1047/399
    3.78  f 712/1044/396 698/1047/399 713/1046/398
    3.79 -o Cube.006_Cube.007
    3.80 +o SOUTHEAST_pillar_cap_Cube.007
    3.81  v 0.500000 1.000003 0.400000
    3.82  v 0.400000 1.000003 0.400000
    3.83  v 0.400000 1.000003 0.500000
    3.84 @@ -3063,7 +3063,7 @@
    3.85  f 714/1052/89 718/1054/89 715/1055/89
    3.86  f 715/1056/243 718/1057/243 719/1058/243
    3.87  f 715/1056/243 719/1058/243 716/1059/243
    3.88 -o Cube.002_Cube.007
    3.89 +o SOUTHEAST_pillar_base_Cube.007
    3.90  v 0.400000 0.000003 0.500000
    3.91  v 0.400000 0.000003 0.400000
    3.92  v 0.500000 0.000003 0.400000
     4.1 --- a/prototype/src/camera.cc	Tue Aug 14 08:49:38 2012 +0300
     4.2 +++ b/prototype/src/camera.cc	Fri Aug 17 14:29:37 2012 +0300
     4.3 @@ -106,13 +106,13 @@
     4.4  
     4.5  void OrbitCamera::input_rotate(float x, float y, float z)
     4.6  {
     4.7 -	theta += x;
     4.8 -	phi += y;
     4.9 +	theta += y;
    4.10 +	phi += x;
    4.11  
    4.12 -	if(phi < -M_PI / 2.0)
    4.13 -		phi = -M_PI / 2.0;
    4.14 -	if(phi > M_PI / 2.0)
    4.15 -		phi = M_PI / 2.0;
    4.16 +	if(phi < -M_PI / 2)
    4.17 +		phi = -M_PI / 2;
    4.18 +	if(phi > M_PI)
    4.19 +		phi = M_PI;
    4.20  
    4.21  	inval_cache();
    4.22  }
    4.23 @@ -127,6 +127,32 @@
    4.24  }
    4.25  
    4.26  
    4.27 +void FpsCamera::calc_matrix(Matrix4x4 *mat) const
    4.28 +{
    4.29 +	mat->reset_identity();
    4.30 +	/*mat->translate(Vector3(pos.x, pos.y, pos.z));
    4.31 +	mat->rotate(Vector3(phi, 0, 0));
    4.32 +	mat->rotate(Vector3(0, theta, 0));*/
    4.33 +}
    4.34 +
    4.35 +void FpsCamera::calc_inv_matrix(Matrix4x4 *mat) const
    4.36 +{
    4.37 +	mat->reset_identity();
    4.38 +	mat->rotate(Vector3(phi, 0, 0));
    4.39 +	mat->rotate(Vector3(0, theta, 0));
    4.40 +	mat->translate(Vector3(-pos.x, -pos.y, -pos.z));
    4.41 +}
    4.42 +
    4.43 +void FpsCamera::input_move(float x, float y, float z)
    4.44 +{
    4.45 +	pos.x += x * cos(theta) - z * sin(theta);
    4.46 +	pos.z += x * sin(theta) + z * cos(theta);
    4.47 +	pos.y += y;
    4.48 +	inval_cache();
    4.49 +}
    4.50 +
    4.51 +
    4.52 +
    4.53  FlyCamera::FlyCamera()
    4.54  {
    4.55  	pos.z = 10.0f;
     5.1 --- a/prototype/src/camera.h	Tue Aug 14 08:49:38 2012 +0300
     5.2 +++ b/prototype/src/camera.h	Fri Aug 17 14:29:37 2012 +0300
     5.3 @@ -33,7 +33,7 @@
     5.4  };
     5.5  
     5.6  class OrbitCamera : public Camera {
     5.7 -private:
     5.8 +protected:
     5.9  	float theta, phi, rad;
    5.10  
    5.11  	void calc_matrix(Matrix4x4 *mat) const;
    5.12 @@ -47,6 +47,17 @@
    5.13  	void input_zoom(float factor);
    5.14  };
    5.15  
    5.16 +class FpsCamera : public OrbitCamera {
    5.17 +protected:
    5.18 +	Vector3 pos;
    5.19 +
    5.20 +	void calc_matrix(Matrix4x4 *mat) const;
    5.21 +	void calc_inv_matrix(Matrix4x4 *mat) const;
    5.22 +
    5.23 +public:
    5.24 +	void input_move(float x, float y, float z);
    5.25 +};
    5.26 +
    5.27  class FlyCamera : public Camera {
    5.28  private:
    5.29  	Vector3 pos;
     6.1 --- a/prototype/src/level.cc	Tue Aug 14 08:49:38 2012 +0300
     6.2 +++ b/prototype/src/level.cc	Fri Aug 17 14:29:37 2012 +0300
     6.3 @@ -114,18 +114,18 @@
     6.4  				glTranslatef(pos.x, pos.y, pos.z);
     6.5  				glScalef(cell_size, cell_size, cell_size);
     6.6  
     6.7 -				unsigned int dmask = 0;
     6.8 -				if(i <= 0 || get_cell(j, i - 1) == 0) {
     6.9 -					dmask |= TILE_NORTH;
    6.10 +				unsigned int dmask = TILE_ALL;
    6.11 +				if(i > 0 && get_cell(j, i - 1)) {
    6.12 +					dmask &= ~TILE_NORTH;
    6.13  				}
    6.14 -				if(i > ysz || get_cell(j, i + 1) == 0) {
    6.15 -					dmask |= TILE_SOUTH;
    6.16 +				if(i < ysz - 1 && get_cell(j, i + 1)) {
    6.17 +					dmask &= ~TILE_SOUTH;
    6.18  				}
    6.19 -				if(j <= 0 || get_cell(j - 1, i) == 0) {
    6.20 -					dmask |= TILE_WEST;
    6.21 +				if(j > 0 && get_cell(j - 1, i)) {
    6.22 +					dmask &= ~TILE_WEST;
    6.23  				}
    6.24 -				if(j > xsz || get_cell(j + 1, i) == 0) {
    6.25 -					dmask |= TILE_EAST;
    6.26 +				if(j < xsz - 1 && get_cell(j + 1, i)) {
    6.27 +					dmask &= ~TILE_EAST;
    6.28  				}
    6.29  
    6.30  				cell->draw(dmask);
     7.1 --- a/prototype/src/main.cc	Tue Aug 14 08:49:38 2012 +0300
     7.2 +++ b/prototype/src/main.cc	Fri Aug 17 14:29:37 2012 +0300
     7.3 @@ -1,6 +1,7 @@
     7.4  #include <stdio.h>
     7.5  #include <stdlib.h>
     7.6  #include <assert.h>
     7.7 +#include <unistd.h>
     7.8  #include "opengl.h"
     7.9  #include "level.h"
    7.10  #include "camera.h"
    7.11 @@ -9,15 +10,20 @@
    7.12  
    7.13  bool init();
    7.14  void cleanup();
    7.15 +void idle();
    7.16  void disp();
    7.17 +void update(unsigned long msec);
    7.18  void reshape(int x, int y);
    7.19  void keyb(unsigned char key, int x, int y);
    7.20 +void key_release(unsigned char key, int x, int y);
    7.21  void mouse(int bn, int state, int x, int y);
    7.22  void motion(int x, int y);
    7.23  
    7.24  static TileSet *tileset;
    7.25  static Level *level;
    7.26 -static OrbitCamera cam;
    7.27 +
    7.28 +static FpsCamera cam;
    7.29 +static bool keystate[256];
    7.30  
    7.31  static const char *level_file = "0.level";
    7.32  
    7.33 @@ -33,9 +39,11 @@
    7.34  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    7.35  	glutCreateWindow("prototype");
    7.36  
    7.37 +	glutIdleFunc(idle);
    7.38  	glutDisplayFunc(disp);
    7.39  	glutReshapeFunc(reshape);
    7.40  	glutKeyboardFunc(keyb);
    7.41 +	glutKeyboardUpFunc(key_release);
    7.42  	glutMouseFunc(mouse);
    7.43  	glutMotionFunc(motion);
    7.44  
    7.45 @@ -70,25 +78,68 @@
    7.46  	set_active_tileset(tileset);
    7.47  
    7.48  	level = new Level;
    7.49 +	printf("loading level: %s\n", level_file);
    7.50  	if(!level->load(datafile_path(level_file))) {
    7.51  		return false;
    7.52  	}
    7.53  
    7.54 +	cam.input_move(0, 0.5, 0);
    7.55 +
    7.56  	return true;
    7.57  }
    7.58  
    7.59 +void idle()
    7.60 +{
    7.61 +	glutPostRedisplay();
    7.62 +}
    7.63 +
    7.64  void disp()
    7.65  {
    7.66 +	update(glutGet(GLUT_ELAPSED_TIME));
    7.67 +
    7.68  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    7.69  
    7.70  	glMatrixMode(GL_MODELVIEW);
    7.71  	glLoadIdentity();
    7.72 -	cam.use();
    7.73 +	cam.use_inverse();
    7.74  
    7.75  	level->draw();
    7.76  
    7.77  	glutSwapBuffers();
    7.78  	assert(glGetError() == GL_NO_ERROR);
    7.79 +
    7.80 +	usleep(10000);
    7.81 +}
    7.82 +
    7.83 +void update(unsigned long msec)
    7.84 +{
    7.85 +	static unsigned long last_upd;
    7.86 +
    7.87 +	if(last_upd == 0) {
    7.88 +		last_upd = msec;
    7.89 +	}
    7.90 +	float dt = (float)(msec - last_upd) / 1000.0;
    7.91 +
    7.92 +	float offs = 2.0 * dt;
    7.93 +	float dx = 0, dy = 0;
    7.94 +
    7.95 +	// handle key input
    7.96 +	if(keystate['w'] || keystate['W']) {
    7.97 +		dy -= offs;
    7.98 +	}
    7.99 +	if(keystate['s'] || keystate['S']) {
   7.100 +		dy += offs;
   7.101 +	}
   7.102 +	if(keystate['d'] || keystate['D']) {
   7.103 +		dx += offs;
   7.104 +	}
   7.105 +	if(keystate['a'] || keystate['A']) {
   7.106 +		dx -= offs;
   7.107 +	}
   7.108 +
   7.109 +	cam.input_move(dx, 0, dy);
   7.110 +
   7.111 +	last_upd = msec;
   7.112  }
   7.113  
   7.114  void reshape(int x, int y)
   7.115 @@ -96,7 +147,7 @@
   7.116  	glViewport(0, 0, x, y);
   7.117  	glMatrixMode(GL_PROJECTION);
   7.118  	glLoadIdentity();
   7.119 -	gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
   7.120 +	gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
   7.121  }
   7.122  
   7.123  void keyb(unsigned char key, int x, int y)
   7.124 @@ -105,6 +156,13 @@
   7.125  	case 27:
   7.126  		exit(0);
   7.127  	}
   7.128 +
   7.129 +	keystate[key] = true;
   7.130 +}
   7.131 +
   7.132 +void key_release(unsigned char key, int x, int y)
   7.133 +{
   7.134 +	keystate[key] = false;
   7.135  }
   7.136  
   7.137  static int prev_x, prev_y;
   7.138 @@ -125,7 +183,7 @@
   7.139  	prev_y = y;
   7.140  
   7.141  	if(bnstate[0]) {
   7.142 -		cam.input_rotate(dx * 0.01, dy * 0.01, 0);
   7.143 +		cam.input_rotate(dy * 0.01, dx * 0.01, 0);
   7.144  		glutPostRedisplay();
   7.145  	}
   7.146  	if(bnstate[2]) {
     8.1 --- a/prototype/src/mesh.cc	Tue Aug 14 08:49:38 2012 +0300
     8.2 +++ b/prototype/src/mesh.cc	Fri Aug 17 14:29:37 2012 +0300
     8.3 @@ -16,6 +16,11 @@
     8.4  	destroy();
     8.5  }
     8.6  
     8.7 +void Mesh::set_name(const char *name)
     8.8 +{
     8.9 +	this->name = name;
    8.10 +}
    8.11 +
    8.12  const char *Mesh::get_name() const
    8.13  {
    8.14  	return name.c_str();
    8.15 @@ -83,6 +88,16 @@
    8.16  	tang_loc = -1;
    8.17  }
    8.18  
    8.19 +void Mesh::set_xform(const Matrix4x4 &mat)
    8.20 +{
    8.21 +	xform = mat;
    8.22 +}
    8.23 +
    8.24 +const Matrix4x4 &Mesh::get_xform() const
    8.25 +{
    8.26 +	return xform;
    8.27 +}
    8.28 +
    8.29  void Mesh::set_attrib_location(int attr, int loc)
    8.30  {
    8.31  	if(attr == MESH_ATTR_TANGENT) {
    8.32 @@ -97,6 +112,10 @@
    8.33  
    8.34  void Mesh::draw() const
    8.35  {
    8.36 +	glMatrixMode(GL_MODELVIEW);
    8.37 +	glPushMatrix();
    8.38 +	mult_matrix(xform);
    8.39 +
    8.40  	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
    8.41  	glVertexPointer(3, GL_FLOAT, 0, 0);
    8.42  	glEnableClientState(GL_VERTEX_ARRAY);
    8.43 @@ -126,4 +145,6 @@
    8.44  	if(tang_loc >= 0) {
    8.45  		glDisableVertexAttribArray(tang_loc);
    8.46  	}
    8.47 +
    8.48 +	glPopMatrix();
    8.49  }
     9.1 --- a/prototype/src/mesh.h	Tue Aug 14 08:49:38 2012 +0300
     9.2 +++ b/prototype/src/mesh.h	Fri Aug 17 14:29:37 2012 +0300
     9.3 @@ -2,8 +2,8 @@
     9.4  #define MESH_H_
     9.5  
     9.6  #include <string>
     9.7 -#include <assimp/assimp.h>
     9.8 -#include <assimp/aiScene.h>
     9.9 +#include <assimp/scene.h>
    9.10 +#include "vmath.h"
    9.11  
    9.12  enum {
    9.13  	MESH_ATTR_VERTEX,
    9.14 @@ -26,15 +26,21 @@
    9.15  
    9.16  	int tang_loc;
    9.17  
    9.18 +	Matrix4x4 xform;
    9.19 +
    9.20  public:
    9.21  	Mesh();
    9.22  	~Mesh();
    9.23  
    9.24 +	void set_name(const char *name);
    9.25  	const char *get_name() const;
    9.26  
    9.27  	bool create(const aiScene *scn, aiMesh *aim);
    9.28  	void destroy();
    9.29  
    9.30 +	void set_xform(const Matrix4x4 &mat);
    9.31 +	const Matrix4x4 &get_xform() const;
    9.32 +
    9.33  	void set_attrib_location(int attr, int loc);
    9.34  	int get_attrib_location(int attr) const;
    9.35  
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/prototype/src/opengl.cc	Fri Aug 17 14:29:37 2012 +0300
    10.3 @@ -0,0 +1,40 @@
    10.4 +#include "opengl.h"
    10.5 +#include "vmath.h"
    10.6 +
    10.7 +void load_matrix(const Matrix4x4 &m)
    10.8 +{
    10.9 +#ifdef SINGLE_PRECISION_MATH
   10.10 +	if(glLoadTransposeMatrixfARB) {
   10.11 +		glLoadTransposeMatrixfARB((float*)&m);
   10.12 +	} else {
   10.13 +		Matrix4x4 tmat = m.transposed();
   10.14 +		glLoadMatrixf((float*)&tmat);
   10.15 +	}
   10.16 +#else
   10.17 +	if(glLoadTransposeMatrixdARB) {
   10.18 +		glLoadTransposeMatrixdARB((double*)&m);
   10.19 +	} else {
   10.20 +		Matrix4x4 tmat = m.transposed();
   10.21 +		glLoadMatrixd((double*)&tmat);
   10.22 +	}
   10.23 +#endif
   10.24 +}
   10.25 +
   10.26 +void mult_matrix(const Matrix4x4 &m)
   10.27 +{
   10.28 +#ifdef SINGLE_PRECISION_MATH
   10.29 +	if(glMultTransposeMatrixfARB) {
   10.30 +		glMultTransposeMatrixfARB((float*)&m);
   10.31 +	} else {
   10.32 +		Matrix4x4 tmat = m.transposed();
   10.33 +		glMultMatrixf((float*)&tmat);
   10.34 +	}
   10.35 +#else
   10.36 +	if(glMultTransposeMatrixdARB) {
   10.37 +		glMultTransposeMatrixdARB((double*)&m);
   10.38 +	} else {
   10.39 +		Matrix4x4 tmat = m.transposed();
   10.40 +		glMultMatrixd((double*)&tmat);
   10.41 +	}
   10.42 +#endif
   10.43 +}
    11.1 --- a/prototype/src/opengl.h	Tue Aug 14 08:49:38 2012 +0300
    11.2 +++ b/prototype/src/opengl.h	Fri Aug 17 14:29:37 2012 +0300
    11.3 @@ -9,4 +9,9 @@
    11.4  #include <GLUT/glut.h>
    11.5  #endif
    11.6  
    11.7 +class Matrix4x4;
    11.8 +
    11.9 +void load_matrix(const Matrix4x4 &m);
   11.10 +void mult_matrix(const Matrix4x4 &m);
   11.11 +
   11.12  #endif	/* OPENGL_H_ */
    12.1 --- a/prototype/src/tile.cc	Tue Aug 14 08:49:38 2012 +0300
    12.2 +++ b/prototype/src/tile.cc	Fri Aug 17 14:29:37 2012 +0300
    12.3 @@ -1,10 +1,16 @@
    12.4  #include <stdio.h>
    12.5 +#include <map>
    12.6  #include "opengl.h"
    12.7 -#include <assimp/assimp.h>
    12.8 -#include <assimp/aiScene.h>
    12.9 -#include <assimp/aiPostProcess.h>
   12.10 +#include <assimp/cimport.h>
   12.11 +#include <assimp/scene.h>
   12.12 +#include <assimp/postprocess.h>
   12.13  #include "tile.h"
   12.14  
   12.15 +using std::map;
   12.16 +
   12.17 +static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
   12.18 +
   12.19 +
   12.20  bool Tile::load(const char *fname)
   12.21  {
   12.22  	if(!fname) {
   12.23 @@ -12,8 +18,10 @@
   12.24  	}
   12.25  
   12.26  	unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
   12.27 -		aiProcess_PreTransformVertices | aiProcess_Triangulate |
   12.28 -		aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
   12.29 +		aiProcess_Triangulate |
   12.30 +		aiProcess_GenNormals |
   12.31 +		aiProcess_SortByPType |
   12.32 +		aiProcess_FlipUVs;
   12.33  
   12.34  	const aiScene *scn = aiImportFile(fname, proc_flags);
   12.35  	if(!scn) {
   12.36 @@ -21,9 +29,11 @@
   12.37  		return -1;
   12.38  	}
   12.39  
   12.40 +	map<aiMesh*, aiNode*> nodemap;
   12.41 +	build_nodemap(&nodemap, scn, scn->mRootNode);
   12.42 +
   12.43  	load_lights(scn);
   12.44 -
   12.45 -	load_meshes(scn);
   12.46 +	load_meshes(scn, nodemap);
   12.47  
   12.48  	printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
   12.49  	return true;
   12.50 @@ -70,7 +80,7 @@
   12.51  	return count;
   12.52  }
   12.53  
   12.54 -int Tile::load_meshes(const aiScene *scn)
   12.55 +int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
   12.56  {
   12.57  	int count = 0;
   12.58  
   12.59 @@ -81,28 +91,62 @@
   12.60  			continue;
   12.61  		}
   12.62  
   12.63 +		// retrieve the node pointer
   12.64 +		const char *name = "<unknown>";
   12.65 +
   12.66 +		auto iter = nmap.find(scn->mMeshes[i]);
   12.67 +		if(iter != nmap.end()) {
   12.68 +			aiNode *node = iter->second;
   12.69 +
   12.70 +			Matrix4x4 xform;
   12.71 +			//xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
   12.72 +			xform = *(Matrix4x4*)&node->mTransformation;
   12.73 +			mesh->set_xform(xform);
   12.74 +
   12.75 +			name = node->mName.data;
   12.76 +			mesh->set_name(name);
   12.77 +		}
   12.78  		meshes.push_back(mesh);
   12.79  
   12.80 +
   12.81 +		printf("name: \"%s\" ... ", name);
   12.82 +
   12.83  		// find which side is this mesh on
   12.84 -		const char *name = mesh->get_name();
   12.85 -		printf("name: %s ... ", name);
   12.86 -		unsigned int side;
   12.87 -
   12.88 -		if(strstr(name, "NORTH") == name) {
   12.89 -			side = TILE_NORTH;
   12.90 -		} else if(strstr(name, "SOUTH") == name) {
   12.91 -			side = TILE_SOUTH;
   12.92 -		} else if(strstr(name, "EAST") == name) {
   12.93 -			side = TILE_EAST;
   12.94 -		} else if(strstr(name, "WEST") == name) {
   12.95 -			side = TILE_WEST;
   12.96 -		} else {
   12.97 +		unsigned int side = 0;
   12.98 +		if(strstr(name, "NORTH")) {
   12.99 +			side |= TILE_NORTH;
  12.100 +		}
  12.101 +		if(strstr(name, "SOUTH")) {
  12.102 +			side |= TILE_SOUTH;
  12.103 +		}
  12.104 +		if(strstr(name, "EAST")) {
  12.105 +			side |= TILE_EAST;
  12.106 +		}
  12.107 +		if(strstr(name, "WEST")) {
  12.108 +			side |= TILE_WEST;
  12.109 +		}
  12.110 +		if(!side) {
  12.111  			side = TILE_ALL;
  12.112  		}
  12.113 -		printf("side: %x\n", side);
  12.114 +		printf("side: 0x0%x\n", side);
  12.115  		mesh_side.push_back(side);
  12.116  
  12.117  		count++;
  12.118  	}
  12.119  	return count;
  12.120  }
  12.121 +
  12.122 +static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
  12.123 +{
  12.124 +	unsigned int i;
  12.125 +
  12.126 +	for(i=0; i<node->mNumMeshes; i++) {
  12.127 +		aiMesh *m = scn->mMeshes[node->mMeshes[i]];
  12.128 +
  12.129 +		(*nmap)[m] = node;
  12.130 +	}
  12.131 +
  12.132 +	for(i=0; i<node->mNumChildren; i++) {
  12.133 +		build_nodemap(nmap, scn, node->mChildren[i]);
  12.134 +	}
  12.135 +}
    13.1 --- a/prototype/src/tile.h	Tue Aug 14 08:49:38 2012 +0300
    13.2 +++ b/prototype/src/tile.h	Fri Aug 17 14:29:37 2012 +0300
    13.3 @@ -2,7 +2,8 @@
    13.4  #define TILE_H_
    13.5  
    13.6  #include <vector>
    13.7 -#include <assimp/aiScene.h>
    13.8 +#include <map>
    13.9 +#include <assimp/scene.h>
   13.10  #include "mesh.h"
   13.11  #include "light.h"
   13.12  
   13.13 @@ -11,7 +12,7 @@
   13.14  	TILE_SOUTH	= 2,
   13.15  	TILE_EAST	= 4,
   13.16  	TILE_WEST	= 8,
   13.17 -	TILE_ALL	= 0xf
   13.18 +	TILE_ALL	= 0xffff
   13.19  };
   13.20  
   13.21  class Tile {
   13.22 @@ -21,7 +22,7 @@
   13.23  	std::vector<Light*> lights;
   13.24  
   13.25  	int load_lights(const aiScene *scn);
   13.26 -	int load_meshes(const aiScene *scn);
   13.27 +	int load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap);
   13.28  
   13.29  public:
   13.30  	bool load(const char *fname);