vrchess

view src/game.cc @ 8:90abf4b93cc9

fixed line endings fixed viewport when returning to original framebuffer
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 17:24:43 +0300
parents bd8202d6d28d
children c2eecf764daa
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);
32 glClearColor(0.1, 0.1, 0.1, 1);
35 if(!floor_tex.load("data/tiles.png")) {
36 return false;
37 }
39 cam.input_move(0, 0, 5);
40 return true;
41 }
43 void game_cleanup()
44 {
45 floor_tex.destroy();
46 vr_shutdown();
48 if(fbo) {
49 glDeleteFramebuffers(1, &fbo);
50 glDeleteRenderbuffers(1, &rtarg_depth);
51 delete rtarg;
52 }
53 }
55 void game_update(unsigned int msec)
56 {
57 static unsigned int prev_msec;
58 float dt = (msec - prev_msec) / 1000.0f;
59 float offs = dt * move_speed;
60 prev_msec = msec;
62 Vector3 move;
63 float roll = 0.0f;
65 if(keystate['d'] || keystate['D']) {
66 move.x += offs;
67 }
68 if(keystate['a'] || keystate['A']) {
69 move.x -= offs;
70 }
71 if(keystate['s'] || keystate['S']) {
72 move.z += offs;
73 }
74 if(keystate['w'] || keystate['W']) {
75 move.z -= offs;
76 }
77 if(keystate['e'] || keystate['E']) {
78 roll += dt;
79 }
80 if(keystate['q'] || keystate['Q']) {
81 roll -= dt;
82 }
84 cam.input_move(move.x, move.y, move.z);
85 cam.input_rotate(0, 0, roll);
86 }
88 void game_render()
89 {
90 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
91 glClearColor(1, 0, 0, 1);
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());
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 glLoadTransposeMatrixf(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);
163 }
164 }
166 if(key < 256) {
167 keystate[key] = pressed;
168 }
169 }
171 static int prev_x, prev_y;
172 static bool bnstate[32];
174 void game_mouse(int bn, bool pressed, int x, int y)
175 {
176 bnstate[bn] = pressed;
177 prev_x = x;
178 prev_y = y;
179 }
181 void game_motion(int x, int y)
182 {
183 int dx = x - prev_x;
184 int dy = y - prev_y;
185 prev_x = x;
186 prev_y = y;
188 if(!dx && !dy) return;
190 if(bnstate[0]) {
191 float xrot = dy * 0.5;
192 float yrot = dx * 0.5;
193 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
194 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
195 }
196 }
198 void game_mwheel(int dir)
199 {
200 cam.input_move(0, dir * 0.1, 0);
201 }
203 void game_6dof_move(float x, float y, float z)
204 {
205 cam.input_move(x, y, z);
206 }
208 void game_6dof_rotate(float x, float y, float z)
209 {
210 cam.input_rotate(x, y, z);
211 }
213 static void draw_scene()
214 {
215 glMatrixMode(GL_MODELVIEW);
216 glTranslatef(0, -1.5, 0);
218 float lpos[] = {-20, 30, 10, 1};
219 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
221 glEnable(GL_TEXTURE_2D);
222 floor_tex.bind();
224 glMatrixMode(GL_TEXTURE);
225 glScalef(8, 8, 8);
227 glBegin(GL_QUADS);
228 glNormal3f(0, 1, 0);
229 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
230 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
231 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
232 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
233 glEnd();
234 glDisable(GL_TEXTURE_2D);
235 glLoadIdentity();
237 glMatrixMode(GL_MODELVIEW);
238 glPushMatrix();
239 glTranslatef(0, 0.75, 0);
241 glFrontFace(GL_CW);
242 glutSolidTeapot(1.0);
243 glFrontFace(GL_CCW);
245 glPopMatrix();
246 }
248 static bool setup_rtarg(int x, int y)
249 {
250 int tex_width = next_pow2(x);
251 int tex_height = next_pow2(y);
253 /* create render targets for each eye */
254 if(!fbo) {
255 glGenFramebuffers(1, &fbo);
256 glGenRenderbuffers(1, &rtarg_depth);
257 rtarg = new Texture;
258 }
260 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
262 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
263 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
264 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
266 rtarg->create2d(tex_width, tex_height);
267 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
268 rtarg->get_texture_id(), 0);
270 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
271 fprintf(stderr, "incomplete framebuffer!\n");
272 return false;
273 }
274 glBindFramebuffer(GL_FRAMEBUFFER, 0);
276 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
277 return true;
278 }