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@7
|
25 vr_use_module_named("null");
|
nuclear@4
|
26
|
nuclear@2
|
27 glEnable(GL_DEPTH_TEST);
|
nuclear@2
|
28 glEnable(GL_CULL_FACE);
|
nuclear@2
|
29 glEnable(GL_LIGHTING);
|
nuclear@2
|
30 glEnable(GL_LIGHT0);
|
nuclear@2
|
31
|
nuclear@2
|
32 glClearColor(0.1, 0.1, 0.1, 1);
|
nuclear@2
|
33
|
nuclear@6
|
34
|
nuclear@2
|
35 if(!floor_tex.load("data/tiles.png")) {
|
nuclear@2
|
36 return false;
|
nuclear@2
|
37 }
|
nuclear@2
|
38
|
nuclear@2
|
39 cam.input_move(0, 0, 5);
|
nuclear@2
|
40 return true;
|
nuclear@2
|
41 }
|
nuclear@2
|
42
|
nuclear@2
|
43 void game_cleanup()
|
nuclear@2
|
44 {
|
nuclear@2
|
45 floor_tex.destroy();
|
nuclear@4
|
46 vr_shutdown();
|
nuclear@6
|
47
|
nuclear@6
|
48 if(fbo) {
|
nuclear@6
|
49 glDeleteFramebuffers(1, &fbo);
|
nuclear@6
|
50 glDeleteRenderbuffers(1, &rtarg_depth);
|
nuclear@7
|
51 delete rtarg;
|
nuclear@6
|
52 }
|
nuclear@2
|
53 }
|
nuclear@2
|
54
|
nuclear@2
|
55 void game_update(unsigned int msec)
|
nuclear@2
|
56 {
|
nuclear@2
|
57 static unsigned int prev_msec;
|
nuclear@2
|
58 float dt = (msec - prev_msec) / 1000.0f;
|
nuclear@2
|
59 float offs = dt * move_speed;
|
nuclear@2
|
60 prev_msec = msec;
|
nuclear@2
|
61
|
nuclear@2
|
62 Vector3 move;
|
nuclear@2
|
63 float roll = 0.0f;
|
nuclear@2
|
64
|
nuclear@2
|
65 if(keystate['d'] || keystate['D']) {
|
nuclear@2
|
66 move.x += offs;
|
nuclear@2
|
67 }
|
nuclear@2
|
68 if(keystate['a'] || keystate['A']) {
|
nuclear@2
|
69 move.x -= offs;
|
nuclear@2
|
70 }
|
nuclear@2
|
71 if(keystate['s'] || keystate['S']) {
|
nuclear@2
|
72 move.z += offs;
|
nuclear@2
|
73 }
|
nuclear@2
|
74 if(keystate['w'] || keystate['W']) {
|
nuclear@2
|
75 move.z -= offs;
|
nuclear@2
|
76 }
|
nuclear@2
|
77 if(keystate['e'] || keystate['E']) {
|
nuclear@2
|
78 roll += dt;
|
nuclear@2
|
79 }
|
nuclear@2
|
80 if(keystate['q'] || keystate['Q']) {
|
nuclear@2
|
81 roll -= dt;
|
nuclear@2
|
82 }
|
nuclear@2
|
83
|
nuclear@2
|
84 cam.input_move(move.x, move.y, move.z);
|
nuclear@2
|
85 cam.input_rotate(0, 0, roll);
|
nuclear@2
|
86 }
|
nuclear@2
|
87
|
nuclear@7
|
88 void game_render()
|
nuclear@2
|
89 {
|
nuclear@7
|
90 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@7
|
91 glClearColor(1, 0, 0, 1);
|
nuclear@7
|
92 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@6
|
93
|
nuclear@7
|
94 glViewport(0, 0, rtwidth / 2.0, rtheight);
|
nuclear@7
|
95 vr_begin(VR_EYE_LEFT);
|
nuclear@7
|
96 game_render_eye(-1);
|
nuclear@7
|
97 vr_end();
|
nuclear@7
|
98
|
nuclear@7
|
99 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
|
nuclear@7
|
100 vr_begin(VR_EYE_RIGHT);
|
nuclear@7
|
101 game_render_eye(1);
|
nuclear@7
|
102 vr_end();
|
nuclear@7
|
103
|
nuclear@7
|
104 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@8
|
105 glViewport(0, 0, fb_width, fb_height);
|
nuclear@8
|
106
|
nuclear@7
|
107 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
|
nuclear@7
|
108 (float)rtheight / (float)rtarg->get_height());
|
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@7
|
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@7
|
120 /*} else {
|
nuclear@7
|
121 glLoadMatrixf(mat);
|
nuclear@7
|
122 }*/
|
nuclear@2
|
123
|
nuclear@2
|
124 glMatrixMode(GL_MODELVIEW);
|
nuclear@4
|
125 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
|
nuclear@4
|
126 glLoadIdentity();
|
nuclear@4
|
127 } else {
|
nuclear@4
|
128 glLoadTransposeMatrixf(mat);
|
nuclear@4
|
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@2
|
163 }
|
nuclear@2
|
164 }
|
nuclear@2
|
165
|
nuclear@2
|
166 if(key < 256) {
|
nuclear@2
|
167 keystate[key] = pressed;
|
nuclear@2
|
168 }
|
nuclear@2
|
169 }
|
nuclear@2
|
170
|
nuclear@2
|
171 static int prev_x, prev_y;
|
nuclear@2
|
172 static bool bnstate[32];
|
nuclear@2
|
173
|
nuclear@2
|
174 void game_mouse(int bn, bool pressed, int x, int y)
|
nuclear@2
|
175 {
|
nuclear@2
|
176 bnstate[bn] = pressed;
|
nuclear@2
|
177 prev_x = x;
|
nuclear@2
|
178 prev_y = y;
|
nuclear@2
|
179 }
|
nuclear@2
|
180
|
nuclear@2
|
181 void game_motion(int x, int y)
|
nuclear@2
|
182 {
|
nuclear@2
|
183 int dx = x - prev_x;
|
nuclear@2
|
184 int dy = y - prev_y;
|
nuclear@2
|
185 prev_x = x;
|
nuclear@2
|
186 prev_y = y;
|
nuclear@2
|
187
|
nuclear@2
|
188 if(!dx && !dy) return;
|
nuclear@2
|
189
|
nuclear@2
|
190 if(bnstate[0]) {
|
nuclear@2
|
191 float xrot = dy * 0.5;
|
nuclear@2
|
192 float yrot = dx * 0.5;
|
nuclear@2
|
193 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
|
nuclear@2
|
194 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
|
nuclear@2
|
195 }
|
nuclear@2
|
196 }
|
nuclear@2
|
197
|
nuclear@3
|
198 void game_mwheel(int dir)
|
nuclear@3
|
199 {
|
nuclear@3
|
200 cam.input_move(0, dir * 0.1, 0);
|
nuclear@3
|
201 }
|
nuclear@3
|
202
|
nuclear@2
|
203 void game_6dof_move(float x, float y, float z)
|
nuclear@2
|
204 {
|
nuclear@2
|
205 cam.input_move(x, y, z);
|
nuclear@2
|
206 }
|
nuclear@2
|
207
|
nuclear@2
|
208 void game_6dof_rotate(float x, float y, float z)
|
nuclear@2
|
209 {
|
nuclear@2
|
210 cam.input_rotate(x, y, z);
|
nuclear@2
|
211 }
|
nuclear@2
|
212
|
nuclear@2
|
213 static void draw_scene()
|
nuclear@2
|
214 {
|
nuclear@2
|
215 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
216 glTranslatef(0, -1.5, 0);
|
nuclear@2
|
217
|
nuclear@2
|
218 float lpos[] = {-20, 30, 10, 1};
|
nuclear@2
|
219 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
|
nuclear@2
|
220
|
nuclear@2
|
221 glEnable(GL_TEXTURE_2D);
|
nuclear@2
|
222 floor_tex.bind();
|
nuclear@2
|
223
|
nuclear@2
|
224 glMatrixMode(GL_TEXTURE);
|
nuclear@2
|
225 glScalef(8, 8, 8);
|
nuclear@2
|
226
|
nuclear@2
|
227 glBegin(GL_QUADS);
|
nuclear@2
|
228 glNormal3f(0, 1, 0);
|
nuclear@2
|
229 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
|
nuclear@2
|
230 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
|
nuclear@2
|
231 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
|
nuclear@2
|
232 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
|
nuclear@2
|
233 glEnd();
|
nuclear@2
|
234 glDisable(GL_TEXTURE_2D);
|
nuclear@2
|
235 glLoadIdentity();
|
nuclear@2
|
236
|
nuclear@2
|
237 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
238 glPushMatrix();
|
nuclear@2
|
239 glTranslatef(0, 0.75, 0);
|
nuclear@2
|
240
|
nuclear@2
|
241 glFrontFace(GL_CW);
|
nuclear@2
|
242 glutSolidTeapot(1.0);
|
nuclear@2
|
243 glFrontFace(GL_CCW);
|
nuclear@2
|
244
|
nuclear@2
|
245 glPopMatrix();
|
nuclear@2
|
246 }
|
nuclear@6
|
247
|
nuclear@6
|
248 static bool setup_rtarg(int x, int y)
|
nuclear@6
|
249 {
|
nuclear@6
|
250 int tex_width = next_pow2(x);
|
nuclear@6
|
251 int tex_height = next_pow2(y);
|
nuclear@6
|
252
|
nuclear@6
|
253 /* create render targets for each eye */
|
nuclear@6
|
254 if(!fbo) {
|
nuclear@6
|
255 glGenFramebuffers(1, &fbo);
|
nuclear@6
|
256 glGenRenderbuffers(1, &rtarg_depth);
|
nuclear@7
|
257 rtarg = new Texture;
|
nuclear@6
|
258 }
|
nuclear@6
|
259
|
nuclear@6
|
260 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@6
|
261
|
nuclear@6
|
262 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
|
nuclear@6
|
263 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
|
nuclear@6
|
264 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
|
nuclear@6
|
265
|
nuclear@7
|
266 rtarg->create2d(tex_width, tex_height);
|
nuclear@7
|
267 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
nuclear@7
|
268 rtarg->get_texture_id(), 0);
|
nuclear@6
|
269
|
nuclear@6
|
270 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
nuclear@6
|
271 fprintf(stderr, "incomplete framebuffer!\n");
|
nuclear@6
|
272 return false;
|
nuclear@6
|
273 }
|
nuclear@6
|
274 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@7
|
275
|
nuclear@7
|
276 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
|
nuclear@6
|
277 return true;
|
nuclear@6
|
278 }
|