vrchess

annotate src/game.cc @ 0:b326d53321f7

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 Apr 2014 05:20:53 +0300
parents
children cd7755e4663a
rev   line source
nuclear@0 1 #include "game.h"
nuclear@0 2 #include "opengl.h"
nuclear@0 3 #include "camera.h"
nuclear@0 4 #include "texture.h"
nuclear@0 5 #include "OVR_CAPI_GL.h"
nuclear@0 6
nuclear@0 7 static void draw_scene();
nuclear@0 8
nuclear@0 9 static const float move_speed = 10.0f;
nuclear@0 10
nuclear@0 11 static int fb_width, fb_height;
nuclear@0 12 static FlyCamera cam;
nuclear@0 13 static Texture floor_tex;
nuclear@0 14 static bool keystate[256];
nuclear@0 15
nuclear@0 16 bool game_init()
nuclear@0 17 {
nuclear@0 18 glEnable(GL_DEPTH_TEST);
nuclear@0 19 glEnable(GL_CULL_FACE);
nuclear@0 20 glEnable(GL_LIGHTING);
nuclear@0 21 glEnable(GL_LIGHT0);
nuclear@0 22
nuclear@0 23 glClearColor(0.1, 0.1, 0.1, 1);
nuclear@0 24
nuclear@0 25 if(!floor_tex.load("data/tiles.png")) {
nuclear@0 26 return false;
nuclear@0 27 }
nuclear@0 28
nuclear@0 29 cam.input_move(0, 0, 5);
nuclear@0 30 return true;
nuclear@0 31 }
nuclear@0 32
nuclear@0 33 void game_cleanup()
nuclear@0 34 {
nuclear@0 35 floor_tex.destroy();
nuclear@0 36 }
nuclear@0 37
nuclear@0 38
nuclear@0 39 void game_update(unsigned int msec)
nuclear@0 40 {
nuclear@0 41 static unsigned int prev_msec;
nuclear@0 42 float dt = (msec - prev_msec) / 1000.0f;
nuclear@0 43 float offs = dt * move_speed;
nuclear@0 44 prev_msec = msec;
nuclear@0 45
nuclear@0 46 Vector3 move;
nuclear@0 47 float roll = 0.0f;
nuclear@0 48
nuclear@0 49 if(keystate['d'] || keystate['D']) {
nuclear@0 50 move.x += offs;
nuclear@0 51 }
nuclear@0 52 if(keystate['a'] || keystate['A']) {
nuclear@0 53 move.x -= offs;
nuclear@0 54 }
nuclear@0 55 if(keystate['s'] || keystate['S']) {
nuclear@0 56 move.z += offs;
nuclear@0 57 }
nuclear@0 58 if(keystate['w'] || keystate['W']) {
nuclear@0 59 move.z -= offs;
nuclear@0 60 }
nuclear@0 61 if(keystate['e'] || keystate['E']) {
nuclear@0 62 roll += dt;
nuclear@0 63 }
nuclear@0 64 if(keystate['q'] || keystate['Q']) {
nuclear@0 65 roll -= dt;
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 cam.input_move(move.x, move.y, move.z);
nuclear@0 69 cam.input_rotate(0, 0, roll);
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 void game_render(int eye)
nuclear@0 73 {
nuclear@0 74 Matrix4x4 view_matrix = cam.get_matrix().inverse();
nuclear@0 75
nuclear@0 76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 77
nuclear@0 78 glMatrixMode(GL_PROJECTION);
nuclear@0 79 glLoadIdentity();
nuclear@0 80 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
nuclear@0 81
nuclear@0 82 glMatrixMode(GL_MODELVIEW);
nuclear@0 83 glLoadIdentity();
nuclear@0 84 glLoadTransposeMatrixf(view_matrix[0]);
nuclear@0 85
nuclear@0 86 draw_scene();
nuclear@0 87 }
nuclear@0 88
nuclear@0 89 void game_reshape(int x, int y)
nuclear@0 90 {
nuclear@0 91 glViewport(0, 0, x, y);
nuclear@0 92 fb_width = x;
nuclear@0 93 fb_height = y;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 void game_keyboard(int key, bool pressed, int x, int y)
nuclear@0 97 {
nuclear@0 98 if(pressed) {
nuclear@0 99 switch(key) {
nuclear@0 100 case 27:
nuclear@0 101 exit(0);
nuclear@0 102 }
nuclear@0 103 }
nuclear@0 104
nuclear@0 105 if(key < 256) {
nuclear@0 106 keystate[key] = pressed;
nuclear@0 107 }
nuclear@0 108 }
nuclear@0 109
nuclear@0 110 static int prev_x, prev_y;
nuclear@0 111 static bool bnstate[32];
nuclear@0 112
nuclear@0 113 void game_mouse(int bn, bool pressed, int x, int y)
nuclear@0 114 {
nuclear@0 115 bnstate[bn] = pressed;
nuclear@0 116 prev_x = x;
nuclear@0 117 prev_y = y;
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 void game_motion(int x, int y)
nuclear@0 121 {
nuclear@0 122 int dx = x - prev_x;
nuclear@0 123 int dy = y - prev_y;
nuclear@0 124 prev_x = x;
nuclear@0 125 prev_y = y;
nuclear@0 126
nuclear@0 127 if(!dx && !dy) return;
nuclear@0 128
nuclear@0 129 if(bnstate[0]) {
nuclear@0 130 float xrot = dy * 0.5;
nuclear@0 131 float yrot = dx * 0.5;
nuclear@0 132 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
nuclear@0 133 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
nuclear@0 134 }
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 void game_6dof_move(float x, float y, float z)
nuclear@0 138 {
nuclear@0 139 cam.input_move(x, y, z);
nuclear@0 140 }
nuclear@0 141
nuclear@0 142 void game_6dof_rotate(float x, float y, float z)
nuclear@0 143 {
nuclear@0 144 cam.input_rotate(x, y, z);
nuclear@0 145 }
nuclear@0 146
nuclear@0 147 static void draw_scene()
nuclear@0 148 {
nuclear@0 149 glMatrixMode(GL_MODELVIEW);
nuclear@0 150 glTranslatef(0, -1.5, 0);
nuclear@0 151
nuclear@0 152 float lpos[] = {-20, 30, 10, 1};
nuclear@0 153 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 154
nuclear@0 155 glEnable(GL_TEXTURE_2D);
nuclear@0 156 floor_tex.bind();
nuclear@0 157
nuclear@0 158 glMatrixMode(GL_TEXTURE);
nuclear@0 159 glScalef(8, 8, 8);
nuclear@0 160
nuclear@0 161 glBegin(GL_QUADS);
nuclear@0 162 glNormal3f(0, 1, 0);
nuclear@0 163 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
nuclear@0 164 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
nuclear@0 165 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
nuclear@0 166 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
nuclear@0 167 glEnd();
nuclear@0 168 glDisable(GL_TEXTURE_2D);
nuclear@0 169 glLoadIdentity();
nuclear@0 170
nuclear@0 171 glMatrixMode(GL_MODELVIEW);
nuclear@0 172 glPushMatrix();
nuclear@0 173 glTranslatef(0, 0.75, 0);
nuclear@0 174
nuclear@0 175 glFrontFace(GL_CW);
nuclear@0 176 glutSolidTeapot(1.0);
nuclear@0 177 glFrontFace(GL_CCW);
nuclear@0 178
nuclear@0 179 glPopMatrix();
nuclear@0 180 }