conworlds

annotate src/game.cc @ 4:e6948e131526

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