vrchess

view src/game.cc @ 9:c2eecf764daa

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 18:48:25 +0300
parents 90abf4b93cc9
children 5dc4e2b8f6f5
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 glClearColor(1, 0, 0, 1);
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93 glViewport(0, 0, rtwidth / 2.0, rtheight);
94 vr_begin(VR_EYE_LEFT);
95 game_render_eye(-1);
96 vr_end();
98 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
99 vr_begin(VR_EYE_RIGHT);
100 game_render_eye(1);
101 vr_end();
103 glBindFramebuffer(GL_FRAMEBUFFER, 0);
104 glViewport(0, 0, fb_width, fb_height);
106 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
107 (float)rtheight / (float)rtarg->get_height());
109 vr_swap_buffers();
110 }
112 static void game_render_eye(int eye)
113 {
114 float mat[16];
115 Matrix4x4 view_matrix = cam.get_matrix().inverse();
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();
119 //if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
120 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
121 /*} else {
122 glLoadMatrixf(mat);
123 }*/
125 glMatrixMode(GL_MODELVIEW);
126 //if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
127 glLoadIdentity();
128 /*} else {
129 glLoadTransposeMatrixf(mat);
130 }*/
131 glMultTransposeMatrixf(view_matrix[0]);
133 draw_scene();
134 }
136 void game_reshape(int x, int y)
137 {
138 glViewport(0, 0, x, y);
139 fb_width = x;
140 fb_height = y;
142 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
143 if(lxres) {
144 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
145 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
146 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
148 rtwidth = lxres + rxres;
149 rtheight = lyres > ryres ? lyres : ryres;
150 } else {
151 rtwidth = x;
152 rtheight = y;
153 }
155 setup_rtarg(rtwidth, rtheight);
156 }
158 void game_keyboard(int key, bool pressed, int x, int y)
159 {
160 if(pressed) {
161 switch(key) {
162 case 27:
163 exit(0);
165 case ' ':
166 vr_recenter();
167 break;
168 }
169 }
171 if(key < 256) {
172 keystate[key] = pressed;
173 }
174 }
176 static int prev_x, prev_y;
177 static bool bnstate[32];
179 void game_mouse(int bn, bool pressed, int x, int y)
180 {
181 bnstate[bn] = pressed;
182 prev_x = x;
183 prev_y = y;
184 }
186 void game_motion(int x, int y)
187 {
188 int dx = x - prev_x;
189 int dy = y - prev_y;
190 prev_x = x;
191 prev_y = y;
193 if(!dx && !dy) return;
195 if(bnstate[0]) {
196 float xrot = dy * 0.5;
197 float yrot = dx * 0.5;
198 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
199 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
200 }
201 }
203 void game_mwheel(int dir)
204 {
205 cam.input_move(0, dir * 0.1, 0);
206 }
208 void game_6dof_move(float x, float y, float z)
209 {
210 cam.input_move(x, y, z);
211 }
213 void game_6dof_rotate(float x, float y, float z)
214 {
215 cam.input_rotate(x, y, z);
216 }
218 static void draw_scene()
219 {
220 glMatrixMode(GL_MODELVIEW);
221 glTranslatef(0, -1.5, 0);
223 float lpos[] = {-20, 30, 10, 1};
224 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
226 glEnable(GL_TEXTURE_2D);
227 floor_tex.bind();
229 glMatrixMode(GL_TEXTURE);
230 glScalef(8, 8, 8);
232 glBegin(GL_QUADS);
233 glNormal3f(0, 1, 0);
234 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
235 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
236 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
237 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
238 glEnd();
239 glDisable(GL_TEXTURE_2D);
240 glLoadIdentity();
242 glMatrixMode(GL_MODELVIEW);
243 glPushMatrix();
244 glTranslatef(0, 0.75, 0);
246 glFrontFace(GL_CW);
247 glutSolidTeapot(1.0);
248 glFrontFace(GL_CCW);
250 glPopMatrix();
251 }
253 static bool setup_rtarg(int x, int y)
254 {
255 int tex_width = next_pow2(x);
256 int tex_height = next_pow2(y);
258 /* create render targets for each eye */
259 if(!fbo) {
260 glGenFramebuffers(1, &fbo);
261 glGenRenderbuffers(1, &rtarg_depth);
262 rtarg = new Texture;
263 }
265 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
267 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
268 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
269 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
271 rtarg->create2d(tex_width, tex_height);
272 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
273 rtarg->get_texture_id(), 0);
275 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
276 fprintf(stderr, "incomplete framebuffer!\n");
277 return false;
278 }
279 glBindFramebuffer(GL_FRAMEBUFFER, 0);
281 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
282 return true;
283 }