conworlds

view src/game.cc @ 6:3c36bc28c6c2

more stuff in the vr test
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Aug 2014 01:08:03 +0300
parents e6948e131526
children bd8202d6d28d
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 draw_scene();
8 static bool setup_rtarg(int x, int y);
10 static Texture *rtarg[2];
11 static unsigned int fbo, rtarg_depth;
13 static const float move_speed = 10.0f;
15 static int fb_width, fb_height;
16 static FlyCamera cam;
17 static Texture floor_tex;
18 static bool keystate[256];
20 bool game_init()
21 {
22 vr_init();
24 glEnable(GL_DEPTH_TEST);
25 glEnable(GL_CULL_FACE);
26 glEnable(GL_LIGHTING);
27 glEnable(GL_LIGHT0);
29 glClearColor(0.1, 0.1, 0.1, 1);
32 if(!floor_tex.load("data/tiles.png")) {
33 return false;
34 }
36 cam.input_move(0, 0, 5);
37 return true;
38 }
40 void game_cleanup()
41 {
42 floor_tex.destroy();
43 vr_shutdown();
45 if(fbo) {
46 glDeleteFramebuffers(1, &fbo);
47 glDeleteRenderbuffers(1, &rtarg_depth);
48 delete rtarg[0];
49 delete rtarg[1];
50 }
51 }
53 void game_update(unsigned int msec)
54 {
55 static unsigned int prev_msec;
56 float dt = (msec - prev_msec) / 1000.0f;
57 float offs = dt * move_speed;
58 prev_msec = msec;
60 Vector3 move;
61 float roll = 0.0f;
63 if(keystate['d'] || keystate['D']) {
64 move.x += offs;
65 }
66 if(keystate['a'] || keystate['A']) {
67 move.x -= offs;
68 }
69 if(keystate['s'] || keystate['S']) {
70 move.z += offs;
71 }
72 if(keystate['w'] || keystate['W']) {
73 move.z -= offs;
74 }
75 if(keystate['e'] || keystate['E']) {
76 roll += dt;
77 }
78 if(keystate['q'] || keystate['Q']) {
79 roll -= dt;
80 }
82 cam.input_move(move.x, move.y, move.z);
83 cam.input_rotate(0, 0, roll);
84 }
86 void game_render(int eye)
87 {
88 vr_begin(eye <= 0 ? VR_EYE_LEFT : VR_EYE_RIGHT);
90 float mat[16];
91 Matrix4x4 view_matrix = cam.get_matrix().inverse();
93 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
95 glMatrixMode(GL_PROJECTION);
96 glLoadIdentity();
97 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, mat)) {
98 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
99 } else {
100 glLoadTransposeMatrixf(mat);
101 }
103 glMatrixMode(GL_MODELVIEW);
104 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
105 glLoadIdentity();
106 } else {
107 glLoadTransposeMatrixf(mat);
108 }
109 glMultTransposeMatrixf(view_matrix[0]);
111 draw_scene();
113 vr_end();
114 }
116 void game_reshape(int x, int y)
117 {
118 glViewport(0, 0, x, y);
119 fb_width = x;
120 fb_height = y;
122 setup_rtarg(x, y);
123 }
125 void game_keyboard(int key, bool pressed, int x, int y)
126 {
127 if(pressed) {
128 switch(key) {
129 case 27:
130 exit(0);
131 }
132 }
134 if(key < 256) {
135 keystate[key] = pressed;
136 }
137 }
139 static int prev_x, prev_y;
140 static bool bnstate[32];
142 void game_mouse(int bn, bool pressed, int x, int y)
143 {
144 bnstate[bn] = pressed;
145 prev_x = x;
146 prev_y = y;
147 }
149 void game_motion(int x, int y)
150 {
151 int dx = x - prev_x;
152 int dy = y - prev_y;
153 prev_x = x;
154 prev_y = y;
156 if(!dx && !dy) return;
158 if(bnstate[0]) {
159 float xrot = dy * 0.5;
160 float yrot = dx * 0.5;
161 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
162 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
163 }
164 }
166 void game_mwheel(int dir)
167 {
168 cam.input_move(0, dir * 0.1, 0);
169 }
171 void game_6dof_move(float x, float y, float z)
172 {
173 cam.input_move(x, y, z);
174 }
176 void game_6dof_rotate(float x, float y, float z)
177 {
178 cam.input_rotate(x, y, z);
179 }
181 static void draw_scene()
182 {
183 glMatrixMode(GL_MODELVIEW);
184 glTranslatef(0, -1.5, 0);
186 float lpos[] = {-20, 30, 10, 1};
187 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
189 glEnable(GL_TEXTURE_2D);
190 floor_tex.bind();
192 glMatrixMode(GL_TEXTURE);
193 glScalef(8, 8, 8);
195 glBegin(GL_QUADS);
196 glNormal3f(0, 1, 0);
197 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
198 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
199 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
200 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
201 glEnd();
202 glDisable(GL_TEXTURE_2D);
203 glLoadIdentity();
205 glMatrixMode(GL_MODELVIEW);
206 glPushMatrix();
207 glTranslatef(0, 0.75, 0);
209 glFrontFace(GL_CW);
210 glutSolidTeapot(1.0);
211 glFrontFace(GL_CCW);
213 glPopMatrix();
214 }
216 static bool setup_rtarg(int x, int y)
217 {
218 int tex_width = next_pow2(x);
219 int tex_height = next_pow2(y);
221 /* create render targets for each eye */
222 if(!fbo) {
223 glGenFramebuffers(1, &fbo);
224 glGenRenderbuffers(1, &rtarg_depth);
226 for(int i=0; i<2; i++) {
227 rtarg[i] = new Texture;
228 }
229 }
231 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
233 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
234 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
235 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
237 for(int i=0; i<2; i++) {
238 rtarg[i] = new Texture;
239 rtarg[i]->create2d(tex_width, tex_height);
240 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
241 GL_TEXTURE_2D, rtarg[i]->get_texture_id(), 0);
242 }
244 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
245 fprintf(stderr, "incomplete framebuffer!\n");
246 return false;
247 }
248 glBindFramebuffer(GL_FRAMEBUFFER, 0);
249 return true;
250 }