conworlds

view src/game.cc @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents 3c36bc28c6c2
children 90abf4b93cc9
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 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
106 (float)rtheight / (float)rtarg->get_height());
107 }
109 static void game_render_eye(int eye)
110 {
111 float mat[16];
112 Matrix4x4 view_matrix = cam.get_matrix().inverse();
114 glMatrixMode(GL_PROJECTION);
115 glLoadIdentity();
116 //if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
117 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
118 /*} else {
119 glLoadMatrixf(mat);
120 }*/
122 glMatrixMode(GL_MODELVIEW);
123 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
124 glLoadIdentity();
125 } else {
126 glLoadTransposeMatrixf(mat);
127 }
128 glMultTransposeMatrixf(view_matrix[0]);
130 draw_scene();
131 }
133 void game_reshape(int x, int y)
134 {
135 glViewport(0, 0, x, y);
136 fb_width = x;
137 fb_height = y;
139 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
140 if(lxres) {
141 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
142 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
143 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
145 rtwidth = lxres + rxres;
146 rtheight = lyres > ryres ? lyres : ryres;
147 } else {
148 rtwidth = x;
149 rtheight = y;
150 }
152 setup_rtarg(rtwidth, rtheight);
153 }
155 void game_keyboard(int key, bool pressed, int x, int y)
156 {
157 if(pressed) {
158 switch(key) {
159 case 27:
160 exit(0);
161 }
162 }
164 if(key < 256) {
165 keystate[key] = pressed;
166 }
167 }
169 static int prev_x, prev_y;
170 static bool bnstate[32];
172 void game_mouse(int bn, bool pressed, int x, int y)
173 {
174 bnstate[bn] = pressed;
175 prev_x = x;
176 prev_y = y;
177 }
179 void game_motion(int x, int y)
180 {
181 int dx = x - prev_x;
182 int dy = y - prev_y;
183 prev_x = x;
184 prev_y = y;
186 if(!dx && !dy) return;
188 if(bnstate[0]) {
189 float xrot = dy * 0.5;
190 float yrot = dx * 0.5;
191 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
192 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
193 }
194 }
196 void game_mwheel(int dir)
197 {
198 cam.input_move(0, dir * 0.1, 0);
199 }
201 void game_6dof_move(float x, float y, float z)
202 {
203 cam.input_move(x, y, z);
204 }
206 void game_6dof_rotate(float x, float y, float z)
207 {
208 cam.input_rotate(x, y, z);
209 }
211 static void draw_scene()
212 {
213 glMatrixMode(GL_MODELVIEW);
214 glTranslatef(0, -1.5, 0);
216 float lpos[] = {-20, 30, 10, 1};
217 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
219 glEnable(GL_TEXTURE_2D);
220 floor_tex.bind();
222 glMatrixMode(GL_TEXTURE);
223 glScalef(8, 8, 8);
225 glBegin(GL_QUADS);
226 glNormal3f(0, 1, 0);
227 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
228 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
229 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
230 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
231 glEnd();
232 glDisable(GL_TEXTURE_2D);
233 glLoadIdentity();
235 glMatrixMode(GL_MODELVIEW);
236 glPushMatrix();
237 glTranslatef(0, 0.75, 0);
239 glFrontFace(GL_CW);
240 glutSolidTeapot(1.0);
241 glFrontFace(GL_CCW);
243 glPopMatrix();
244 }
246 static bool setup_rtarg(int x, int y)
247 {
248 int tex_width = next_pow2(x);
249 int tex_height = next_pow2(y);
251 /* create render targets for each eye */
252 if(!fbo) {
253 glGenFramebuffers(1, &fbo);
254 glGenRenderbuffers(1, &rtarg_depth);
255 rtarg = new Texture;
256 }
258 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
260 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
261 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
262 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
264 rtarg->create2d(tex_width, tex_height);
265 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
266 rtarg->get_texture_id(), 0);
268 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
269 fprintf(stderr, "incomplete framebuffer!\n");
270 return false;
271 }
272 glBindFramebuffer(GL_FRAMEBUFFER, 0);
274 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
275 return true;
276 }