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