vrseasons

view src/game.cc @ 2:eea1b91dc3d4

added is_complete() to framebuffer object and fixed GL_UNSUPPORTED_FRAMEBUFFER on intel
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 13 Apr 2015 16:51:52 +0300
parents 393ef1143c9c
children
line source
1 #include <assert.h>
2 #include "opengl.h"
3 #include "game.h"
4 #include <stdio.h>
5 #include <goatvr.h>
6 #include "main.h"
7 #include "scene.h"
8 #include "timer.h"
9 #include "rtarg.h"
10 #include "opt.h"
12 static bool vr_mode = true;
13 static int win_width, win_height;
14 static RenderTarget vr_rtarg;
15 static Options opt;
17 bool game_init(int argc, char **argv)
18 {
19 if(!parse_args(&opt, argc, argv)) {
20 return false;
21 }
23 init_gl();
25 if(vr_mode) {
26 if(vr_init() == -1) {
27 return false;
28 }
30 win_width = vr_geti_def(VR_DISPLAY_WIDTH, 1280);
31 win_height = vr_geti_def(VR_DISPLAY_HEIGHT, 800);
32 resize_window(win_width, win_height);
34 int rt_width = vr_geti_def(VR_RENDER_XRES, win_width);
35 int rt_height = vr_geti_def(VR_RENDER_YRES, win_height);
36 vr_rtarg.create(rt_width, rt_height);
37 }
39 return true;
40 }
42 void game_shutdown()
43 {
44 vr_shutdown();
45 }
47 void game_draw()
48 {
49 static unsigned long prev_msec;
50 unsigned long msec = get_msec();
51 float dt = (float)(msec - prev_msec) / 1000.0;
53 update_scene(msec, dt);
55 if(vr_mode) {
56 set_render_target(&vr_rtarg);
58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
60 for(int i=0; i<2; i++) {
61 vr_begin(i);
63 glViewport(i == VR_EYE_LEFT ? 0 : vr_rtarg.get_width() / 2, 0, vr_rtarg.get_width() / 2, vr_rtarg.get_height());
65 glMatrixMode(GL_PROJECTION);
66 float proj[16];
67 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
68 glLoadMatrixf(proj);
69 } else {
70 glLoadIdentity();
71 gluPerspective(50.0, (float)win_width / 2.0 / (float)win_height, 0.5, 500.0);
72 }
74 float view[16];
75 vr_view_matrix(i, view);
77 glMatrixMode(GL_MODELVIEW);
78 glLoadMatrixf(view);
79 glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
81 draw_scene();
83 vr_end();
84 }
86 set_render_target(0);
87 glViewport(0, 0, win_width, win_height);
89 vr_swap_buffers();
91 } else {
92 glMatrixMode(GL_MODELVIEW);
93 glLoadIdentity();
95 draw_scene();
97 swap_buffers();
98 }
100 assert(glGetError() == GL_NO_ERROR);
101 }
103 void game_reshape(int x, int y)
104 {
105 glViewport(0, 0, x, y);
107 if(vr_mode) {
108 vr_rtarg.resize(x, y);
110 float umax = (float)vr_rtarg.get_width() / (float)vr_rtarg.get_tex_width();
111 float vmax = (float)vr_rtarg.get_height() / (float)vr_rtarg.get_tex_height();
112 vr_output_texture(vr_rtarg.get_texture(), 0, 0, umax, vmax);
113 }
114 }
116 void game_keyboard(int key, bool press)
117 {
118 if(press) {
119 switch(key) {
120 case 27:
121 quit();
123 case ' ':
124 if(vr_mode) {
125 vr_recenter();
126 }
127 break;
128 }
129 }
130 }
132 void game_mbutton(int bn, bool press, int x, int y)
133 {
134 }
136 void game_mmotion(int x, int y)
137 {
138 }