nuclear@2: #include "game.h" nuclear@2: #include "opengl.h" nuclear@2: #include "camera.h" nuclear@2: #include "texture.h" nuclear@4: #include "vr/vr.h" nuclear@2: nuclear@2: static void draw_scene(); nuclear@2: nuclear@2: static const float move_speed = 10.0f; nuclear@2: nuclear@2: static int fb_width, fb_height; nuclear@2: static FlyCamera cam; nuclear@2: static Texture floor_tex; nuclear@2: static bool keystate[256]; nuclear@2: nuclear@2: bool game_init() nuclear@2: { nuclear@4: vr_init(); nuclear@4: nuclear@2: glEnable(GL_DEPTH_TEST); nuclear@2: glEnable(GL_CULL_FACE); nuclear@2: glEnable(GL_LIGHTING); nuclear@2: glEnable(GL_LIGHT0); nuclear@2: nuclear@2: glClearColor(0.1, 0.1, 0.1, 1); nuclear@2: nuclear@2: if(!floor_tex.load("data/tiles.png")) { nuclear@2: return false; nuclear@2: } nuclear@2: nuclear@2: cam.input_move(0, 0, 5); nuclear@2: return true; nuclear@2: } nuclear@2: nuclear@2: void game_cleanup() nuclear@2: { nuclear@2: floor_tex.destroy(); nuclear@4: vr_shutdown(); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: void game_update(unsigned int msec) nuclear@2: { nuclear@2: static unsigned int prev_msec; nuclear@2: float dt = (msec - prev_msec) / 1000.0f; nuclear@2: float offs = dt * move_speed; nuclear@2: prev_msec = msec; nuclear@2: nuclear@2: Vector3 move; nuclear@2: float roll = 0.0f; nuclear@2: nuclear@2: if(keystate['d'] || keystate['D']) { nuclear@2: move.x += offs; nuclear@2: } nuclear@2: if(keystate['a'] || keystate['A']) { nuclear@2: move.x -= offs; nuclear@2: } nuclear@2: if(keystate['s'] || keystate['S']) { nuclear@2: move.z += offs; nuclear@2: } nuclear@2: if(keystate['w'] || keystate['W']) { nuclear@2: move.z -= offs; nuclear@2: } nuclear@2: if(keystate['e'] || keystate['E']) { nuclear@2: roll += dt; nuclear@2: } nuclear@2: if(keystate['q'] || keystate['Q']) { nuclear@2: roll -= dt; nuclear@2: } nuclear@2: nuclear@2: cam.input_move(move.x, move.y, move.z); nuclear@2: cam.input_rotate(0, 0, roll); nuclear@2: } nuclear@2: nuclear@2: void game_render(int eye) nuclear@2: { nuclear@4: float mat[16]; nuclear@2: Matrix4x4 view_matrix = cam.get_matrix().inverse(); nuclear@2: nuclear@2: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@2: nuclear@2: glMatrixMode(GL_PROJECTION); nuclear@2: glLoadIdentity(); nuclear@4: if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, mat)) { nuclear@4: gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0); nuclear@4: } else { nuclear@4: glLoadTransposeMatrixf(mat); nuclear@4: } nuclear@2: nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@4: if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) { nuclear@4: glLoadIdentity(); nuclear@4: } else { nuclear@4: glLoadTransposeMatrixf(mat); nuclear@4: } nuclear@4: glMultTransposeMatrixf(view_matrix[0]); nuclear@2: nuclear@2: draw_scene(); nuclear@2: } nuclear@2: nuclear@2: void game_reshape(int x, int y) nuclear@2: { nuclear@2: glViewport(0, 0, x, y); nuclear@2: fb_width = x; nuclear@2: fb_height = y; nuclear@2: } nuclear@2: nuclear@2: void game_keyboard(int key, bool pressed, int x, int y) nuclear@2: { nuclear@2: if(pressed) { nuclear@2: switch(key) { nuclear@2: case 27: nuclear@2: exit(0); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: if(key < 256) { nuclear@2: keystate[key] = pressed; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: static int prev_x, prev_y; nuclear@2: static bool bnstate[32]; nuclear@2: nuclear@2: void game_mouse(int bn, bool pressed, int x, int y) nuclear@2: { nuclear@2: bnstate[bn] = pressed; nuclear@2: prev_x = x; nuclear@2: prev_y = y; nuclear@2: } nuclear@2: nuclear@2: void game_motion(int x, int y) nuclear@2: { nuclear@2: int dx = x - prev_x; nuclear@2: int dy = y - prev_y; nuclear@2: prev_x = x; nuclear@2: prev_y = y; nuclear@2: nuclear@2: if(!dx && !dy) return; nuclear@2: nuclear@2: if(bnstate[0]) { nuclear@2: float xrot = dy * 0.5; nuclear@2: float yrot = dx * 0.5; nuclear@2: cam.input_rotate(DEG_TO_RAD(xrot), 0, 0); nuclear@2: cam.input_rotate(0, DEG_TO_RAD(yrot), 0); nuclear@2: } nuclear@2: } nuclear@2: nuclear@3: void game_mwheel(int dir) nuclear@3: { nuclear@3: cam.input_move(0, dir * 0.1, 0); nuclear@3: } nuclear@3: nuclear@2: void game_6dof_move(float x, float y, float z) nuclear@2: { nuclear@2: cam.input_move(x, y, z); nuclear@2: } nuclear@2: nuclear@2: void game_6dof_rotate(float x, float y, float z) nuclear@2: { nuclear@2: cam.input_rotate(x, y, z); nuclear@2: } nuclear@2: nuclear@2: static void draw_scene() nuclear@2: { nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glTranslatef(0, -1.5, 0); nuclear@2: nuclear@2: float lpos[] = {-20, 30, 10, 1}; nuclear@2: glLightfv(GL_LIGHT0, GL_POSITION, lpos); nuclear@2: nuclear@2: glEnable(GL_TEXTURE_2D); nuclear@2: floor_tex.bind(); nuclear@2: nuclear@2: glMatrixMode(GL_TEXTURE); nuclear@2: glScalef(8, 8, 8); nuclear@2: nuclear@2: glBegin(GL_QUADS); nuclear@2: glNormal3f(0, 1, 0); nuclear@2: glTexCoord2f(0, 0); glVertex3f(-25, 0, 25); nuclear@2: glTexCoord2f(1, 0); glVertex3f(25, 0, 25); nuclear@2: glTexCoord2f(1, 1); glVertex3f(25, 0, -25); nuclear@2: glTexCoord2f(0, 1); glVertex3f(-25, 0, -25); nuclear@2: glEnd(); nuclear@2: glDisable(GL_TEXTURE_2D); nuclear@2: glLoadIdentity(); nuclear@2: nuclear@2: glMatrixMode(GL_MODELVIEW); nuclear@2: glPushMatrix(); nuclear@2: glTranslatef(0, 0.75, 0); nuclear@2: nuclear@2: glFrontFace(GL_CW); nuclear@2: glutSolidTeapot(1.0); nuclear@2: glFrontFace(GL_CCW); nuclear@2: nuclear@2: glPopMatrix(); nuclear@2: }