conworlds
view src/game.cc @ 12:778ed91cb7fd
fullscreen to rift now works in extended mode with freeglut
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 23 Aug 2014 12:03:29 +0300 |
parents | 5dc4e2b8f6f5 |
children | 283cdfa7dda2 |
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();
25 //vr_use_module_named("null");
27 glEnable(GL_DEPTH_TEST);
28 glEnable(GL_CULL_FACE);
29 glEnable(GL_LIGHTING);
30 glEnable(GL_LIGHT0);
31 glEnable(GL_NORMALIZE);
33 glClearColor(0.1, 0.1, 0.1, 1);
36 if(!floor_tex.load("data/tiles.png")) {
37 return false;
38 }
40 cam.input_move(0, 0, 5);
41 return true;
42 }
44 void game_cleanup()
45 {
46 floor_tex.destroy();
47 vr_shutdown();
49 if(fbo) {
50 glDeleteFramebuffers(1, &fbo);
51 glDeleteRenderbuffers(1, &rtarg_depth);
52 delete rtarg;
53 }
54 }
56 void game_update(unsigned int msec)
57 {
58 static unsigned int prev_msec;
59 float dt = (msec - prev_msec) / 1000.0f;
60 float offs = dt * move_speed;
61 prev_msec = msec;
63 Vector3 move;
64 float roll = 0.0f;
66 if(keystate['d'] || keystate['D']) {
67 move.x += offs;
68 }
69 if(keystate['a'] || keystate['A']) {
70 move.x -= offs;
71 }
72 if(keystate['s'] || keystate['S']) {
73 move.z += offs;
74 }
75 if(keystate['w'] || keystate['W']) {
76 move.z -= offs;
77 }
78 if(keystate['e'] || keystate['E']) {
79 roll += dt;
80 }
81 if(keystate['q'] || keystate['Q']) {
82 roll -= dt;
83 }
85 cam.input_move(move.x, move.y, move.z);
86 cam.input_rotate(0, 0, roll);
87 }
89 void game_render()
90 {
91 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
92 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
94 glViewport(0, 0, rtwidth / 2.0, rtheight);
95 vr_begin(VR_EYE_LEFT);
96 game_render_eye(-1);
97 vr_end();
99 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
100 vr_begin(VR_EYE_RIGHT);
101 game_render_eye(1);
102 vr_end();
104 glBindFramebuffer(GL_FRAMEBUFFER, 0);
105 glViewport(0, 0, fb_width, fb_height);
107 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
108 (float)rtheight / (float)rtarg->get_height());
110 vr_swap_buffers();
111 }
113 static void game_render_eye(int eye)
114 {
115 float mat[16];
116 Matrix4x4 view_matrix = cam.get_matrix().inverse();
118 glMatrixMode(GL_PROJECTION);
119 glLoadIdentity();
120 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
121 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
122 } else {
123 glLoadMatrixf(mat);
124 }
126 glMatrixMode(GL_MODELVIEW);
127 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
128 glLoadIdentity();
129 } else {
130 glLoadMatrixf(mat);
131 }
132 glMultTransposeMatrixf(view_matrix[0]);
134 draw_scene();
135 }
137 void game_reshape(int x, int y)
138 {
139 glViewport(0, 0, x, y);
140 fb_width = x;
141 fb_height = y;
143 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
144 if(lxres) {
145 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
146 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
147 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
149 rtwidth = lxres + rxres;
150 rtheight = lyres > ryres ? lyres : ryres;
151 } else {
152 rtwidth = x;
153 rtheight = y;
154 }
156 setup_rtarg(rtwidth, rtheight);
157 }
159 void game_keyboard(int key, bool pressed, int x, int y)
160 {
161 if(pressed) {
162 switch(key) {
163 case 27:
164 exit(0);
166 case ' ':
167 vr_recenter();
168 break;
169 }
170 }
172 if(key < 256) {
173 keystate[key] = pressed;
174 }
175 }
177 static int prev_x, prev_y;
178 static bool bnstate[32];
180 void game_mouse(int bn, bool pressed, int x, int y)
181 {
182 bnstate[bn] = pressed;
183 prev_x = x;
184 prev_y = y;
185 }
187 void game_motion(int x, int y)
188 {
189 int dx = x - prev_x;
190 int dy = y - prev_y;
191 prev_x = x;
192 prev_y = y;
194 if(!dx && !dy) return;
196 if(bnstate[0]) {
197 float xrot = dy * 0.5;
198 float yrot = dx * 0.5;
199 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
200 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
201 }
202 }
204 void game_mwheel(int dir)
205 {
206 cam.input_move(0, dir * 0.1, 0);
207 }
209 void game_6dof_move(float x, float y, float z)
210 {
211 cam.input_move(x, y, z);
212 }
214 void game_6dof_rotate(float x, float y, float z)
215 {
216 cam.input_rotate(x, y, z);
217 }
219 static void draw_scene()
220 {
221 glMatrixMode(GL_MODELVIEW);
222 glTranslatef(0, -1.5, 0);
224 float lpos[] = {-20, 30, 10, 1};
225 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
227 glEnable(GL_TEXTURE_2D);
228 floor_tex.bind();
230 glMatrixMode(GL_TEXTURE);
231 glScalef(8, 8, 8);
233 glBegin(GL_QUADS);
234 glNormal3f(0, 1, 0);
235 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
236 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
237 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
238 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
239 glEnd();
240 glDisable(GL_TEXTURE_2D);
241 glLoadIdentity();
243 glMatrixMode(GL_MODELVIEW);
245 for(int i=0; i<4; i++) {
246 glPushMatrix();
247 glTranslatef(i & 1 ? -10 : 10, 0, i & 2 ? -10 : 10);
248 glScalef(2.0, 2.0, 2.0);
250 glBegin(GL_TRIANGLES);
251 glNormal3f(0, 1, 1);
252 glVertex3f(-1, 0, 1);
253 glVertex3f(1, 0, 1);
254 glVertex3f(0, 1.2, 0);
255 glNormal3f(1, 1, 0);
256 glVertex3f(1, 0, 1);
257 glVertex3f(1, 0, -1);
258 glVertex3f(0, 1.2, 0);
259 glNormal3f(0, 1, -1);
260 glVertex3f(1, 0, -1);
261 glVertex3f(-1, 0, -1);
262 glVertex3f(0, 1.2, 0);
263 glNormal3f(-1, 1, 0);
264 glVertex3f(-1, 0, -1);
265 glVertex3f(-1, 0, 1);
266 glVertex3f(0, 1.2, 0);
267 glEnd();
269 glPopMatrix();
270 }
271 }
273 static bool setup_rtarg(int x, int y)
274 {
275 int tex_width = next_pow2(x);
276 int tex_height = next_pow2(y);
278 /* create render targets for each eye */
279 if(!fbo) {
280 glGenFramebuffers(1, &fbo);
281 glGenRenderbuffers(1, &rtarg_depth);
282 rtarg = new Texture;
283 }
285 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
287 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
288 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
289 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
291 rtarg->create2d(tex_width, tex_height);
292 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
293 rtarg->get_texture_id(), 0);
295 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
296 fprintf(stderr, "incomplete framebuffer!\n");
297 return false;
298 }
299 glBindFramebuffer(GL_FRAMEBUFFER, 0);
301 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
302 return true;
303 }