rev |
line source |
nuclear@2
|
1 #include "game.h"
|
nuclear@2
|
2 #include "opengl.h"
|
nuclear@2
|
3 #include "camera.h"
|
nuclear@2
|
4 #include "texture.h"
|
nuclear@4
|
5 #include "vr/vr.h"
|
nuclear@2
|
6
|
nuclear@7
|
7 static void game_render_eye(int eye);
|
nuclear@2
|
8 static void draw_scene();
|
nuclear@6
|
9 static bool setup_rtarg(int x, int y);
|
nuclear@6
|
10
|
nuclear@7
|
11 static Texture *rtarg;
|
nuclear@6
|
12 static unsigned int fbo, rtarg_depth;
|
nuclear@7
|
13 static int rtwidth, rtheight;
|
nuclear@2
|
14
|
nuclear@2
|
15 static const float move_speed = 10.0f;
|
nuclear@2
|
16
|
nuclear@2
|
17 static int fb_width, fb_height;
|
nuclear@2
|
18 static FlyCamera cam;
|
nuclear@2
|
19 static Texture floor_tex;
|
nuclear@2
|
20 static bool keystate[256];
|
nuclear@2
|
21
|
nuclear@2
|
22 bool game_init()
|
nuclear@2
|
23 {
|
nuclear@4
|
24 vr_init();
|
nuclear@4
|
25
|
nuclear@2
|
26 glEnable(GL_DEPTH_TEST);
|
nuclear@2
|
27 glEnable(GL_CULL_FACE);
|
nuclear@2
|
28 glEnable(GL_LIGHTING);
|
nuclear@2
|
29 glEnable(GL_LIGHT0);
|
nuclear@2
|
30
|
nuclear@2
|
31 glClearColor(0.1, 0.1, 0.1, 1);
|
nuclear@2
|
32
|
nuclear@6
|
33
|
nuclear@2
|
34 if(!floor_tex.load("data/tiles.png")) {
|
nuclear@2
|
35 return false;
|
nuclear@2
|
36 }
|
nuclear@2
|
37
|
nuclear@2
|
38 cam.input_move(0, 0, 5);
|
nuclear@2
|
39 return true;
|
nuclear@2
|
40 }
|
nuclear@2
|
41
|
nuclear@2
|
42 void game_cleanup()
|
nuclear@2
|
43 {
|
nuclear@2
|
44 floor_tex.destroy();
|
nuclear@4
|
45 vr_shutdown();
|
nuclear@6
|
46
|
nuclear@6
|
47 if(fbo) {
|
nuclear@6
|
48 glDeleteFramebuffers(1, &fbo);
|
nuclear@6
|
49 glDeleteRenderbuffers(1, &rtarg_depth);
|
nuclear@7
|
50 delete rtarg;
|
nuclear@6
|
51 }
|
nuclear@2
|
52 }
|
nuclear@2
|
53
|
nuclear@2
|
54 void game_update(unsigned int msec)
|
nuclear@2
|
55 {
|
nuclear@2
|
56 static unsigned int prev_msec;
|
nuclear@2
|
57 float dt = (msec - prev_msec) / 1000.0f;
|
nuclear@2
|
58 float offs = dt * move_speed;
|
nuclear@2
|
59 prev_msec = msec;
|
nuclear@2
|
60
|
nuclear@2
|
61 Vector3 move;
|
nuclear@2
|
62 float roll = 0.0f;
|
nuclear@2
|
63
|
nuclear@2
|
64 if(keystate['d'] || keystate['D']) {
|
nuclear@2
|
65 move.x += offs;
|
nuclear@2
|
66 }
|
nuclear@2
|
67 if(keystate['a'] || keystate['A']) {
|
nuclear@2
|
68 move.x -= offs;
|
nuclear@2
|
69 }
|
nuclear@2
|
70 if(keystate['s'] || keystate['S']) {
|
nuclear@2
|
71 move.z += offs;
|
nuclear@2
|
72 }
|
nuclear@2
|
73 if(keystate['w'] || keystate['W']) {
|
nuclear@2
|
74 move.z -= offs;
|
nuclear@2
|
75 }
|
nuclear@2
|
76 if(keystate['e'] || keystate['E']) {
|
nuclear@2
|
77 roll += dt;
|
nuclear@2
|
78 }
|
nuclear@2
|
79 if(keystate['q'] || keystate['Q']) {
|
nuclear@2
|
80 roll -= dt;
|
nuclear@2
|
81 }
|
nuclear@2
|
82
|
nuclear@2
|
83 cam.input_move(move.x, move.y, move.z);
|
nuclear@2
|
84 cam.input_rotate(0, 0, roll);
|
nuclear@2
|
85 }
|
nuclear@2
|
86
|
nuclear@7
|
87 void game_render()
|
nuclear@2
|
88 {
|
nuclear@7
|
89 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@7
|
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@6
|
91
|
nuclear@7
|
92 glViewport(0, 0, rtwidth / 2.0, rtheight);
|
nuclear@7
|
93 vr_begin(VR_EYE_LEFT);
|
nuclear@7
|
94 game_render_eye(-1);
|
nuclear@7
|
95 vr_end();
|
nuclear@7
|
96
|
nuclear@7
|
97 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
|
nuclear@7
|
98 vr_begin(VR_EYE_RIGHT);
|
nuclear@7
|
99 game_render_eye(1);
|
nuclear@7
|
100 vr_end();
|
nuclear@7
|
101
|
nuclear@7
|
102 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@8
|
103 glViewport(0, 0, fb_width, fb_height);
|
nuclear@8
|
104
|
nuclear@7
|
105 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
|
nuclear@7
|
106 (float)rtheight / (float)rtarg->get_height());
|
nuclear@9
|
107
|
nuclear@9
|
108 vr_swap_buffers();
|
nuclear@7
|
109 }
|
nuclear@7
|
110
|
nuclear@7
|
111 static void game_render_eye(int eye)
|
nuclear@7
|
112 {
|
nuclear@4
|
113 float mat[16];
|
nuclear@2
|
114 Matrix4x4 view_matrix = cam.get_matrix().inverse();
|
nuclear@2
|
115
|
nuclear@2
|
116 glMatrixMode(GL_PROJECTION);
|
nuclear@2
|
117 glLoadIdentity();
|
nuclear@11
|
118 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
|
nuclear@4
|
119 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
|
nuclear@11
|
120 } else {
|
nuclear@7
|
121 glLoadMatrixf(mat);
|
nuclear@11
|
122 }
|
nuclear@2
|
123
|
nuclear@2
|
124 glMatrixMode(GL_MODELVIEW);
|
nuclear@11
|
125 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
|
nuclear@4
|
126 glLoadIdentity();
|
nuclear@11
|
127 } else {
|
nuclear@11
|
128 glLoadMatrixf(mat);
|
nuclear@11
|
129 }
|
nuclear@4
|
130 glMultTransposeMatrixf(view_matrix[0]);
|
nuclear@2
|
131
|
nuclear@2
|
132 draw_scene();
|
nuclear@2
|
133 }
|
nuclear@2
|
134
|
nuclear@2
|
135 void game_reshape(int x, int y)
|
nuclear@2
|
136 {
|
nuclear@2
|
137 glViewport(0, 0, x, y);
|
nuclear@2
|
138 fb_width = x;
|
nuclear@2
|
139 fb_height = y;
|
nuclear@6
|
140
|
nuclear@7
|
141 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
|
nuclear@7
|
142 if(lxres) {
|
nuclear@7
|
143 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
|
nuclear@7
|
144 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
|
nuclear@7
|
145 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
|
nuclear@7
|
146
|
nuclear@7
|
147 rtwidth = lxres + rxres;
|
nuclear@7
|
148 rtheight = lyres > ryres ? lyres : ryres;
|
nuclear@7
|
149 } else {
|
nuclear@7
|
150 rtwidth = x;
|
nuclear@7
|
151 rtheight = y;
|
nuclear@7
|
152 }
|
nuclear@7
|
153
|
nuclear@7
|
154 setup_rtarg(rtwidth, rtheight);
|
nuclear@2
|
155 }
|
nuclear@2
|
156
|
nuclear@2
|
157 void game_keyboard(int key, bool pressed, int x, int y)
|
nuclear@2
|
158 {
|
nuclear@2
|
159 if(pressed) {
|
nuclear@2
|
160 switch(key) {
|
nuclear@2
|
161 case 27:
|
nuclear@2
|
162 exit(0);
|
nuclear@9
|
163
|
nuclear@9
|
164 case ' ':
|
nuclear@9
|
165 vr_recenter();
|
nuclear@9
|
166 break;
|
nuclear@2
|
167 }
|
nuclear@2
|
168 }
|
nuclear@2
|
169
|
nuclear@2
|
170 if(key < 256) {
|
nuclear@2
|
171 keystate[key] = pressed;
|
nuclear@2
|
172 }
|
nuclear@2
|
173 }
|
nuclear@2
|
174
|
nuclear@2
|
175 static int prev_x, prev_y;
|
nuclear@2
|
176 static bool bnstate[32];
|
nuclear@2
|
177
|
nuclear@2
|
178 void game_mouse(int bn, bool pressed, int x, int y)
|
nuclear@2
|
179 {
|
nuclear@2
|
180 bnstate[bn] = pressed;
|
nuclear@2
|
181 prev_x = x;
|
nuclear@2
|
182 prev_y = y;
|
nuclear@2
|
183 }
|
nuclear@2
|
184
|
nuclear@2
|
185 void game_motion(int x, int y)
|
nuclear@2
|
186 {
|
nuclear@2
|
187 int dx = x - prev_x;
|
nuclear@2
|
188 int dy = y - prev_y;
|
nuclear@2
|
189 prev_x = x;
|
nuclear@2
|
190 prev_y = y;
|
nuclear@2
|
191
|
nuclear@2
|
192 if(!dx && !dy) return;
|
nuclear@2
|
193
|
nuclear@2
|
194 if(bnstate[0]) {
|
nuclear@2
|
195 float xrot = dy * 0.5;
|
nuclear@2
|
196 float yrot = dx * 0.5;
|
nuclear@2
|
197 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
|
nuclear@2
|
198 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
|
nuclear@2
|
199 }
|
nuclear@2
|
200 }
|
nuclear@2
|
201
|
nuclear@3
|
202 void game_mwheel(int dir)
|
nuclear@3
|
203 {
|
nuclear@3
|
204 cam.input_move(0, dir * 0.1, 0);
|
nuclear@3
|
205 }
|
nuclear@3
|
206
|
nuclear@2
|
207 void game_6dof_move(float x, float y, float z)
|
nuclear@2
|
208 {
|
nuclear@2
|
209 cam.input_move(x, y, z);
|
nuclear@2
|
210 }
|
nuclear@2
|
211
|
nuclear@2
|
212 void game_6dof_rotate(float x, float y, float z)
|
nuclear@2
|
213 {
|
nuclear@2
|
214 cam.input_rotate(x, y, z);
|
nuclear@2
|
215 }
|
nuclear@2
|
216
|
nuclear@2
|
217 static void draw_scene()
|
nuclear@2
|
218 {
|
nuclear@2
|
219 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
220 glTranslatef(0, -1.5, 0);
|
nuclear@2
|
221
|
nuclear@2
|
222 float lpos[] = {-20, 30, 10, 1};
|
nuclear@2
|
223 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@2
|
224
|
nuclear@2
|
225 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
226 floor_tex.bind();
|
nuclear@2
|
227
|
nuclear@2
|
228 glMatrixMode(GL_TEXTURE);
|
nuclear@2
|
229 glScalef(8, 8, 8);
|
nuclear@2
|
230
|
nuclear@2
|
231 glBegin(GL_QUADS);
|
nuclear@2
|
232 glNormal3f(0, 1, 0);
|
nuclear@2
|
233 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
|
nuclear@2
|
234 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
|
nuclear@2
|
235 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
|
nuclear@2
|
236 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
|
nuclear@2
|
237 glEnd();
|
nuclear@2
|
238 glDisable(GL_TEXTURE_2D);
|
nuclear@2
|
239 glLoadIdentity();
|
nuclear@2
|
240
|
nuclear@2
|
241 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
242 glPushMatrix();
|
nuclear@2
|
243 glTranslatef(0, 0.75, 0);
|
nuclear@2
|
244
|
nuclear@2
|
245 glFrontFace(GL_CW);
|
nuclear@2
|
246 glutSolidTeapot(1.0);
|
nuclear@2
|
247 glFrontFace(GL_CCW);
|
nuclear@2
|
248
|
nuclear@2
|
249 glPopMatrix();
|
nuclear@2
|
250 }
|
nuclear@6
|
251
|
nuclear@6
|
252 static bool setup_rtarg(int x, int y)
|
nuclear@6
|
253 {
|
nuclear@6
|
254 int tex_width = next_pow2(x);
|
nuclear@6
|
255 int tex_height = next_pow2(y);
|
nuclear@6
|
256
|
nuclear@6
|
257 /* create render targets for each eye */
|
nuclear@6
|
258 if(!fbo) {
|
nuclear@6
|
259 glGenFramebuffers(1, &fbo);
|
nuclear@6
|
260 glGenRenderbuffers(1, &rtarg_depth);
|
nuclear@7
|
261 rtarg = new Texture;
|
nuclear@6
|
262 }
|
nuclear@6
|
263
|
nuclear@6
|
264 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@6
|
265
|
nuclear@6
|
266 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
|
nuclear@6
|
267 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
|
nuclear@6
|
268 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
|
nuclear@6
|
269
|
nuclear@7
|
270 rtarg->create2d(tex_width, tex_height);
|
nuclear@7
|
271 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
nuclear@7
|
272 rtarg->get_texture_id(), 0);
|
nuclear@6
|
273
|
nuclear@6
|
274 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
nuclear@6
|
275 fprintf(stderr, "incomplete framebuffer!\n");
|
nuclear@6
|
276 return false;
|
nuclear@6
|
277 }
|
nuclear@6
|
278 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@7
|
279
|
nuclear@7
|
280 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
|
nuclear@6
|
281 return true;
|
nuclear@6
|
282 }
|