vrchess

view src/game.cc @ 11:5dc4e2b8f6f5

LibOVR is broken
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 23 Aug 2014 00:24:20 +0300
parents c2eecf764daa
children 778ed91cb7fd
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 game_render_eye(int eye);
8 static void draw_scene();
9 static bool setup_rtarg(int x, int y);
11 static Texture *rtarg;
12 static unsigned int fbo, rtarg_depth;
13 static int rtwidth, rtheight;
15 static const float move_speed = 10.0f;
17 static int fb_width, fb_height;
18 static FlyCamera cam;
19 static Texture floor_tex;
20 static bool keystate[256];
22 bool game_init()
23 {
24 vr_init();
26 glEnable(GL_DEPTH_TEST);
27 glEnable(GL_CULL_FACE);
28 glEnable(GL_LIGHTING);
29 glEnable(GL_LIGHT0);
31 glClearColor(0.1, 0.1, 0.1, 1);
34 if(!floor_tex.load("data/tiles.png")) {
35 return false;
36 }
38 cam.input_move(0, 0, 5);
39 return true;
40 }
42 void game_cleanup()
43 {
44 floor_tex.destroy();
45 vr_shutdown();
47 if(fbo) {
48 glDeleteFramebuffers(1, &fbo);
49 glDeleteRenderbuffers(1, &rtarg_depth);
50 delete rtarg;
51 }
52 }
54 void game_update(unsigned int msec)
55 {
56 static unsigned int prev_msec;
57 float dt = (msec - prev_msec) / 1000.0f;
58 float offs = dt * move_speed;
59 prev_msec = msec;
61 Vector3 move;
62 float roll = 0.0f;
64 if(keystate['d'] || keystate['D']) {
65 move.x += offs;
66 }
67 if(keystate['a'] || keystate['A']) {
68 move.x -= offs;
69 }
70 if(keystate['s'] || keystate['S']) {
71 move.z += offs;
72 }
73 if(keystate['w'] || keystate['W']) {
74 move.z -= offs;
75 }
76 if(keystate['e'] || keystate['E']) {
77 roll += dt;
78 }
79 if(keystate['q'] || keystate['Q']) {
80 roll -= dt;
81 }
83 cam.input_move(move.x, move.y, move.z);
84 cam.input_rotate(0, 0, roll);
85 }
87 void game_render()
88 {
89 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 glViewport(0, 0, rtwidth / 2.0, rtheight);
93 vr_begin(VR_EYE_LEFT);
94 game_render_eye(-1);
95 vr_end();
97 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
98 vr_begin(VR_EYE_RIGHT);
99 game_render_eye(1);
100 vr_end();
102 glBindFramebuffer(GL_FRAMEBUFFER, 0);
103 glViewport(0, 0, fb_width, fb_height);
105 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
106 (float)rtheight / (float)rtarg->get_height());
108 vr_swap_buffers();
109 }
111 static void game_render_eye(int eye)
112 {
113 float mat[16];
114 Matrix4x4 view_matrix = cam.get_matrix().inverse();
116 glMatrixMode(GL_PROJECTION);
117 glLoadIdentity();
118 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
119 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
120 } else {
121 glLoadMatrixf(mat);
122 }
124 glMatrixMode(GL_MODELVIEW);
125 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
126 glLoadIdentity();
127 } else {
128 glLoadMatrixf(mat);
129 }
130 glMultTransposeMatrixf(view_matrix[0]);
132 draw_scene();
133 }
135 void game_reshape(int x, int y)
136 {
137 glViewport(0, 0, x, y);
138 fb_width = x;
139 fb_height = y;
141 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
142 if(lxres) {
143 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
144 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
145 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
147 rtwidth = lxres + rxres;
148 rtheight = lyres > ryres ? lyres : ryres;
149 } else {
150 rtwidth = x;
151 rtheight = y;
152 }
154 setup_rtarg(rtwidth, rtheight);
155 }
157 void game_keyboard(int key, bool pressed, int x, int y)
158 {
159 if(pressed) {
160 switch(key) {
161 case 27:
162 exit(0);
164 case ' ':
165 vr_recenter();
166 break;
167 }
168 }
170 if(key < 256) {
171 keystate[key] = pressed;
172 }
173 }
175 static int prev_x, prev_y;
176 static bool bnstate[32];
178 void game_mouse(int bn, bool pressed, int x, int y)
179 {
180 bnstate[bn] = pressed;
181 prev_x = x;
182 prev_y = y;
183 }
185 void game_motion(int x, int y)
186 {
187 int dx = x - prev_x;
188 int dy = y - prev_y;
189 prev_x = x;
190 prev_y = y;
192 if(!dx && !dy) return;
194 if(bnstate[0]) {
195 float xrot = dy * 0.5;
196 float yrot = dx * 0.5;
197 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
198 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
199 }
200 }
202 void game_mwheel(int dir)
203 {
204 cam.input_move(0, dir * 0.1, 0);
205 }
207 void game_6dof_move(float x, float y, float z)
208 {
209 cam.input_move(x, y, z);
210 }
212 void game_6dof_rotate(float x, float y, float z)
213 {
214 cam.input_rotate(x, y, z);
215 }
217 static void draw_scene()
218 {
219 glMatrixMode(GL_MODELVIEW);
220 glTranslatef(0, -1.5, 0);
222 float lpos[] = {-20, 30, 10, 1};
223 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
225 glEnable(GL_TEXTURE_2D);
226 floor_tex.bind();
228 glMatrixMode(GL_TEXTURE);
229 glScalef(8, 8, 8);
231 glBegin(GL_QUADS);
232 glNormal3f(0, 1, 0);
233 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
234 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
235 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
236 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
237 glEnd();
238 glDisable(GL_TEXTURE_2D);
239 glLoadIdentity();
241 glMatrixMode(GL_MODELVIEW);
242 glPushMatrix();
243 glTranslatef(0, 0.75, 0);
245 glFrontFace(GL_CW);
246 glutSolidTeapot(1.0);
247 glFrontFace(GL_CCW);
249 glPopMatrix();
250 }
252 static bool setup_rtarg(int x, int y)
253 {
254 int tex_width = next_pow2(x);
255 int tex_height = next_pow2(y);
257 /* create render targets for each eye */
258 if(!fbo) {
259 glGenFramebuffers(1, &fbo);
260 glGenRenderbuffers(1, &rtarg_depth);
261 rtarg = new Texture;
262 }
264 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
266 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
267 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
268 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
270 rtarg->create2d(tex_width, tex_height);
271 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
272 rtarg->get_texture_id(), 0);
274 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
275 fprintf(stderr, "incomplete framebuffer!\n");
276 return false;
277 }
278 glBindFramebuffer(GL_FRAMEBUFFER, 0);
280 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
281 return true;
282 }