rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@2
|
3 #include <assert.h>
|
nuclear@0
|
4 #include <SDL2/SDL.h>
|
nuclear@0
|
5 #include <GL/glew.h>
|
nuclear@0
|
6
|
nuclear@2
|
7 #ifdef WIN32
|
nuclear@2
|
8 #define OVR_OS_WIN32
|
nuclear@2
|
9 #endif
|
nuclear@2
|
10 #ifdef __APPLE__
|
nuclear@2
|
11 #define OVR_OS_MAC
|
nuclear@2
|
12 #endif
|
nuclear@2
|
13
|
nuclear@0
|
14 #include <OVR_CAPI.h>
|
nuclear@0
|
15 #include <OVR_CAPI_GL.h>
|
nuclear@0
|
16
|
nuclear@2
|
17 int init(void);
|
nuclear@2
|
18 void cleanup(void);
|
nuclear@4
|
19 void toggle_hmd_fullscreen(void);
|
nuclear@2
|
20 void display(void);
|
nuclear@2
|
21 void draw_scene(void);
|
nuclear@2
|
22 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
|
nuclear@0
|
23 void update_rtarg(int width, int height);
|
nuclear@0
|
24 int handle_event(SDL_Event *ev);
|
nuclear@0
|
25 int key_event(int key, int state);
|
nuclear@0
|
26 void reshape(int x, int y);
|
nuclear@0
|
27 unsigned int next_pow2(unsigned int x);
|
nuclear@4
|
28 void quat_to_matrix(const float *quat, float *mat);
|
nuclear@4
|
29 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1);
|
nuclear@0
|
30
|
nuclear@4
|
31 /* forward declaration to avoid including non-public headers of libovr */
|
nuclear@0
|
32 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enable);
|
nuclear@0
|
33
|
nuclear@0
|
34 static SDL_Window *win;
|
nuclear@0
|
35 static SDL_GLContext ctx;
|
nuclear@2
|
36 static int win_width, win_height;
|
nuclear@0
|
37
|
nuclear@0
|
38 static unsigned int fbo, fb_tex, fb_depth;
|
nuclear@0
|
39 static int fb_width, fb_height;
|
nuclear@0
|
40 static int fb_tex_width, fb_tex_height;
|
nuclear@0
|
41
|
nuclear@0
|
42 static ovrHmd hmd;
|
nuclear@0
|
43 static ovrSizei eyeres[2];
|
nuclear@2
|
44 static ovrEyeRenderDesc eye_rdesc[2];
|
nuclear@2
|
45 static ovrGLTexture fb_ovr_tex[2];
|
nuclear@0
|
46
|
nuclear@4
|
47 static unsigned int chess_tex;
|
nuclear@0
|
48
|
nuclear@4
|
49
|
nuclear@4
|
50 int main(int argc, char **argv)
|
nuclear@0
|
51 {
|
nuclear@0
|
52 if(init() == -1) {
|
nuclear@0
|
53 return 1;
|
nuclear@0
|
54 }
|
nuclear@0
|
55
|
nuclear@0
|
56 for(;;) {
|
nuclear@0
|
57 SDL_Event ev;
|
nuclear@0
|
58 while(SDL_PollEvent(&ev)) {
|
nuclear@0
|
59 if(handle_event(&ev) == -1) {
|
nuclear@0
|
60 goto done;
|
nuclear@0
|
61 }
|
nuclear@0
|
62 }
|
nuclear@0
|
63 display();
|
nuclear@0
|
64 }
|
nuclear@0
|
65
|
nuclear@0
|
66 done:
|
nuclear@0
|
67 cleanup();
|
nuclear@0
|
68 return 0;
|
nuclear@0
|
69 }
|
nuclear@0
|
70
|
nuclear@0
|
71
|
nuclear@2
|
72 int init(void)
|
nuclear@0
|
73 {
|
nuclear@2
|
74 int i, x, y;
|
nuclear@0
|
75 unsigned int flags, dcaps;
|
nuclear@0
|
76 union ovrGLConfig glcfg;
|
nuclear@0
|
77
|
nuclear@4
|
78 /* libovr must be initialized before we create the OpenGL context */
|
nuclear@0
|
79 ovr_Initialize();
|
nuclear@0
|
80
|
nuclear@0
|
81 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
|
nuclear@0
|
82
|
nuclear@0
|
83 x = y = SDL_WINDOWPOS_UNDEFINED;
|
nuclear@2
|
84 flags = SDL_WINDOW_OPENGL;
|
nuclear@4
|
85 if(!(win = SDL_CreateWindow("press 'f' to move to the HMD", x, y, 1280, 800, flags))) {
|
nuclear@0
|
86 fprintf(stderr, "failed to create window\n");
|
nuclear@0
|
87 return -1;
|
nuclear@0
|
88 }
|
nuclear@0
|
89 if(!(ctx = SDL_GL_CreateContext(win))) {
|
nuclear@0
|
90 fprintf(stderr, "failed to create OpenGL context\n");
|
nuclear@0
|
91 return -1;
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 glewInit();
|
nuclear@0
|
95
|
nuclear@0
|
96 if(!(hmd = ovrHmd_Create(0))) {
|
nuclear@0
|
97 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
|
nuclear@2
|
98 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
|
nuclear@0
|
99 fprintf(stderr, "failed to create virtual debug HMD\n");
|
nuclear@0
|
100 return -1;
|
nuclear@0
|
101 }
|
nuclear@0
|
102 }
|
nuclear@0
|
103 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
|
nuclear@0
|
104
|
nuclear@2
|
105 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
|
nuclear@4
|
106 SDL_SetWindowPosition(win, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
nuclear@4
|
107 win_width = hmd->Resolution.w;
|
nuclear@4
|
108 win_height = hmd->Resolution.h;
|
nuclear@0
|
109
|
nuclear@0
|
110 ovrHmd_ConfigureTracking(hmd, 0xffffffff, 0);
|
nuclear@0
|
111 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
|
nuclear@0
|
112 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
|
nuclear@0
|
113
|
nuclear@2
|
114 fb_width = eyeres[0].w + eyeres[1].w;
|
nuclear@0
|
115 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
|
nuclear@0
|
116 update_rtarg(fb_width, fb_height);
|
nuclear@0
|
117
|
nuclear@2
|
118 for(i=0; i<2; i++) {
|
nuclear@2
|
119 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
|
nuclear@2
|
120 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
|
nuclear@2
|
121 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
|
nuclear@3
|
122 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
|
nuclear@3
|
123 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = fb_tex_height - fb_height;
|
nuclear@2
|
124 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
|
nuclear@2
|
125 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
|
nuclear@2
|
126 fb_ovr_tex[i].OGL.TexId = fb_tex;
|
nuclear@2
|
127 }
|
nuclear@2
|
128
|
nuclear@0
|
129 memset(&glcfg, 0, sizeof glcfg);
|
nuclear@0
|
130 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
|
nuclear@0
|
131 glcfg.OGL.Header.RTSize = hmd->Resolution;
|
nuclear@0
|
132 glcfg.OGL.Header.Multisample = 1;
|
nuclear@0
|
133
|
nuclear@0
|
134 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
|
nuclear@0
|
135 printf("running in \"extended desktop\" mode\n");
|
nuclear@0
|
136 } else {
|
nuclear@2
|
137 #ifdef WIN32
|
nuclear@2
|
138 void *sys_win = GetActiveWindow();
|
nuclear@2
|
139 glcfg.OGL.Window = sys_win;
|
nuclear@2
|
140 glcfg.OGL.DC = wglGetCurrentDC();
|
nuclear@2
|
141 ovrHmd_AttachToWindow(hmd, sys_win, 0, 0);
|
nuclear@2
|
142 #endif
|
nuclear@0
|
143 printf("running in \"direct-hmd\" mode\n");
|
nuclear@0
|
144 }
|
nuclear@2
|
145 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
|
nuclear@0
|
146
|
nuclear@4
|
147 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp;
|
nuclear@0
|
148 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, hmd->DefaultEyeFov, eye_rdesc)) {
|
nuclear@0
|
149 fprintf(stderr, "failed to configure distortion renderer\n");
|
nuclear@0
|
150 }
|
nuclear@0
|
151
|
nuclear@3
|
152 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
|
nuclear@0
|
153
|
nuclear@0
|
154 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
155 glEnable(GL_CULL_FACE);
|
nuclear@0
|
156 glEnable(GL_LIGHTING);
|
nuclear@0
|
157 glEnable(GL_LIGHT0);
|
nuclear@4
|
158 glEnable(GL_LIGHT1);
|
nuclear@4
|
159 glEnable(GL_NORMALIZE);
|
nuclear@0
|
160
|
nuclear@4
|
161 glClearColor(0.1, 0.1, 0.1, 1);
|
nuclear@0
|
162
|
nuclear@4
|
163 chess_tex = gen_chess_tex(1.0, 0.7, 0.4, 0.4, 0.7, 1.0);
|
nuclear@0
|
164 return 0;
|
nuclear@0
|
165 }
|
nuclear@0
|
166
|
nuclear@2
|
167 void cleanup(void)
|
nuclear@0
|
168 {
|
nuclear@0
|
169 if(hmd) {
|
nuclear@0
|
170 ovrHmd_Destroy(hmd);
|
nuclear@0
|
171 }
|
nuclear@0
|
172 ovr_Shutdown();
|
nuclear@0
|
173
|
nuclear@0
|
174 SDL_Quit();
|
nuclear@0
|
175 }
|
nuclear@0
|
176
|
nuclear@4
|
177 void toggle_hmd_fullscreen(void)
|
nuclear@4
|
178 {
|
nuclear@4
|
179 static int fullscr, prev_x, prev_y;
|
nuclear@4
|
180 fullscr = !fullscr;
|
nuclear@4
|
181
|
nuclear@4
|
182 if(fullscr) {
|
nuclear@4
|
183 SDL_GetWindowPosition(win, &prev_x, &prev_y);
|
nuclear@4
|
184 SDL_SetWindowPosition(win, hmd->WindowsPos.x, hmd->WindowsPos.y);
|
nuclear@4
|
185 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
nuclear@4
|
186 } else {
|
nuclear@4
|
187 SDL_SetWindowFullscreen(win, 0);
|
nuclear@4
|
188 SDL_SetWindowPosition(win, prev_x, prev_y);
|
nuclear@4
|
189 }
|
nuclear@4
|
190 }
|
nuclear@4
|
191
|
nuclear@2
|
192 void display(void)
|
nuclear@0
|
193 {
|
nuclear@2
|
194 int i;
|
nuclear@2
|
195 ovrMatrix4f proj;
|
nuclear@2
|
196 ovrPosef pose[2];
|
nuclear@4
|
197 float rot_mat[16];
|
nuclear@2
|
198
|
nuclear@4
|
199 /* the drawing starts with a call to ovrHmd_BeginFrame */
|
nuclear@2
|
200 ovrHmd_BeginFrame(hmd, 0);
|
nuclear@2
|
201
|
nuclear@4
|
202 /* start drawing onto our texture render target */
|
nuclear@2
|
203 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@0
|
204 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
205
|
nuclear@2
|
206 /* for each eye ... */
|
nuclear@2
|
207 for(i=0; i<2; i++) {
|
nuclear@2
|
208 int eye = hmd->EyeRenderOrder[i];
|
nuclear@2
|
209
|
nuclear@4
|
210 /* -- viewport transformation --
|
nuclear@4
|
211 * setup the viewport to draw in the left half of the framebuffer when we're
|
nuclear@4
|
212 * rendering the left eye's view (0, 0, width/2, height), and in the right half
|
nuclear@4
|
213 * of the framebuffer for the right eye's view (width/2, 0, width/2, height)
|
nuclear@4
|
214 */
|
nuclear@2
|
215 glViewport(eye == 0 ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
|
nuclear@2
|
216
|
nuclear@4
|
217 /* -- projection transformation --
|
nuclear@4
|
218 * we'll just have to use the projection matrix supplied by the oculus SDK for this eye
|
nuclear@4
|
219 * note that libovr matrices are the transpose of what OpenGL expects, so we have to
|
nuclear@4
|
220 * use glLoadTransposeMatrixf instead of glLoadMatrixf to load it.
|
nuclear@4
|
221 */
|
nuclear@2
|
222 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
|
nuclear@2
|
223 glMatrixMode(GL_PROJECTION);
|
nuclear@4
|
224 glLoadTransposeMatrixf(proj.M[0]);
|
nuclear@2
|
225
|
nuclear@4
|
226 /* -- view/camera transformation --
|
nuclear@4
|
227 * we need to construct a view matrix by combining all the information provided by the oculus
|
nuclear@4
|
228 * SDK, about the position and orientation of the user's head in the world.
|
nuclear@4
|
229 */
|
nuclear@2
|
230 pose[eye] = ovrHmd_GetEyePose(hmd, eye);
|
nuclear@2
|
231 glMatrixMode(GL_MODELVIEW);
|
nuclear@4
|
232 glLoadIdentity();
|
nuclear@4
|
233 glTranslatef(eye_rdesc[eye].ViewAdjust.x, eye_rdesc[eye].ViewAdjust.y, eye_rdesc[eye].ViewAdjust.z);
|
nuclear@4
|
234 /* retrieve the orientation quaternion and convert it to a rotation matrix */
|
nuclear@4
|
235 quat_to_matrix(&pose[eye].Orientation.x, rot_mat);
|
nuclear@4
|
236 glMultMatrixf(rot_mat);
|
nuclear@4
|
237 /* translate the view matrix with the positional tracking */
|
nuclear@4
|
238 glTranslatef(-pose[eye].Position.x, -pose[eye].Position.y, -pose[eye].Position.z);
|
nuclear@4
|
239 /* move the camera to the eye level of the user */
|
nuclear@4
|
240 glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
|
nuclear@2
|
241
|
nuclear@4
|
242 /* finally draw the scene for this eye */
|
nuclear@2
|
243 draw_scene();
|
nuclear@2
|
244 }
|
nuclear@2
|
245
|
nuclear@4
|
246 /* after drawing both eyes into the texture render target, revert to drawing directly to the
|
nuclear@4
|
247 * display, and we call ovrHmd_EndFrame, to let the Oculus SDK draw both images properly
|
nuclear@4
|
248 * compensated for lens distortion and chromatic abberation onto the HMD screen.
|
nuclear@4
|
249 */
|
nuclear@2
|
250 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@2
|
251 glViewport(0, 0, win_width, win_height);
|
nuclear@2
|
252
|
nuclear@2
|
253 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
|
nuclear@2
|
254
|
nuclear@2
|
255 assert(glGetError() == GL_NO_ERROR);
|
nuclear@2
|
256 }
|
nuclear@2
|
257
|
nuclear@2
|
258 void draw_scene(void)
|
nuclear@2
|
259 {
|
nuclear@2
|
260 int i;
|
nuclear@4
|
261 float grey[] = {0.8, 0.8, 0.8, 1};
|
nuclear@4
|
262 float col[] = {0, 0, 0, 1};
|
nuclear@4
|
263 float lpos[][4] = {
|
nuclear@4
|
264 {-8, 2, 10, 1},
|
nuclear@4
|
265 {0, 15, 0, 1}
|
nuclear@4
|
266 };
|
nuclear@4
|
267 float lcol[][4] = {
|
nuclear@4
|
268 {0.8, 0.8, 0.8, 1},
|
nuclear@4
|
269 {0.4, 0.3, 0.3, 1}
|
nuclear@4
|
270 };
|
nuclear@4
|
271
|
nuclear@4
|
272 for(i=0; i<2; i++) {
|
nuclear@4
|
273 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
|
nuclear@4
|
274 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
|
nuclear@4
|
275 }
|
nuclear@4
|
276
|
nuclear@4
|
277 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
278
|
nuclear@3
|
279 glPushMatrix();
|
nuclear@4
|
280 glTranslatef(0, 10, 0);
|
nuclear@4
|
281 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
|
nuclear@4
|
282 glBindTexture(GL_TEXTURE_2D, chess_tex);
|
nuclear@4
|
283 glEnable(GL_TEXTURE_2D);
|
nuclear@4
|
284 draw_box(30, 20, 30, -1.0);
|
nuclear@4
|
285 glDisable(GL_TEXTURE_2D);
|
nuclear@3
|
286 glPopMatrix();
|
nuclear@3
|
287
|
nuclear@4
|
288 for(i=0; i<4; i++) {
|
nuclear@4
|
289 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, grey);
|
nuclear@4
|
290 glPushMatrix();
|
nuclear@4
|
291 glTranslatef(i & 1 ? 5 : -5, 1, i & 2 ? -5 : 5);
|
nuclear@4
|
292 draw_box(0.5, 2, 0.5, 1.0);
|
nuclear@4
|
293 glPopMatrix();
|
nuclear@2
|
294
|
nuclear@4
|
295 col[0] = i & 1 ? 1.0 : 0.3;
|
nuclear@4
|
296 col[1] = i == 0 ? 1.0 : 0.3;
|
nuclear@4
|
297 col[2] = i & 2 ? 1.0 : 0.3;
|
nuclear@4
|
298 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
|
nuclear@4
|
299
|
nuclear@2
|
300 glPushMatrix();
|
nuclear@2
|
301 if(i & 1) {
|
nuclear@4
|
302 glTranslatef(0, 0.25, i & 2 ? 2 : -2);
|
nuclear@2
|
303 } else {
|
nuclear@4
|
304 glTranslatef(i & 2 ? 2 : -2, 0.25, 0);
|
nuclear@2
|
305 }
|
nuclear@4
|
306 draw_box(0.5, 0.5, 0.5, 1.0);
|
nuclear@2
|
307 glPopMatrix();
|
nuclear@2
|
308 }
|
nuclear@2
|
309 }
|
nuclear@2
|
310
|
nuclear@2
|
311 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
|
nuclear@2
|
312 {
|
nuclear@2
|
313 glMatrixMode(GL_MODELVIEW);
|
nuclear@2
|
314 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
|
nuclear@2
|
315
|
nuclear@2
|
316 if(norm_sign < 0.0) {
|
nuclear@2
|
317 glFrontFace(GL_CW);
|
nuclear@2
|
318 }
|
nuclear@2
|
319
|
nuclear@2
|
320 glBegin(GL_QUADS);
|
nuclear@2
|
321 glNormal3f(0, 0, 1 * norm_sign);
|
nuclear@2
|
322 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
|
nuclear@2
|
323 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
|
nuclear@2
|
324 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
|
nuclear@2
|
325 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
|
nuclear@2
|
326 glNormal3f(1 * norm_sign, 0, 0);
|
nuclear@2
|
327 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
|
nuclear@2
|
328 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
|
nuclear@2
|
329 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
|
nuclear@2
|
330 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
|
nuclear@2
|
331 glNormal3f(0, 0, -1 * norm_sign);
|
nuclear@2
|
332 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
|
nuclear@2
|
333 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
|
nuclear@2
|
334 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
|
nuclear@2
|
335 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
|
nuclear@2
|
336 glNormal3f(-1 * norm_sign, 0, 0);
|
nuclear@2
|
337 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
|
nuclear@2
|
338 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
|
nuclear@2
|
339 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
|
nuclear@2
|
340 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
|
nuclear@4
|
341 glEnd();
|
nuclear@4
|
342 glBegin(GL_TRIANGLE_FAN);
|
nuclear@2
|
343 glNormal3f(0, 1 * norm_sign, 0);
|
nuclear@4
|
344 glTexCoord2f(0.5, 0.5); glVertex3f(0, 1, 0);
|
nuclear@2
|
345 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
|
nuclear@2
|
346 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
|
nuclear@2
|
347 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
|
nuclear@2
|
348 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
|
nuclear@4
|
349 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
|
nuclear@4
|
350 glEnd();
|
nuclear@4
|
351 glBegin(GL_TRIANGLE_FAN);
|
nuclear@2
|
352 glNormal3f(0, -1 * norm_sign, 0);
|
nuclear@4
|
353 glTexCoord2f(0.5, 0.5); glVertex3f(0, -1, 0);
|
nuclear@2
|
354 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
|
nuclear@2
|
355 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
|
nuclear@2
|
356 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
|
nuclear@2
|
357 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
|
nuclear@4
|
358 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
|
nuclear@2
|
359 glEnd();
|
nuclear@2
|
360
|
nuclear@2
|
361 glFrontFace(GL_CCW);
|
nuclear@0
|
362 }
|
nuclear@0
|
363
|
nuclear@0
|
364 void update_rtarg(int width, int height)
|
nuclear@0
|
365 {
|
nuclear@0
|
366 if(!fbo) {
|
nuclear@0
|
367 glGenFramebuffers(1, &fbo);
|
nuclear@0
|
368 glGenTextures(1, &fb_tex);
|
nuclear@0
|
369 glGenRenderbuffers(1, &fb_depth);
|
nuclear@0
|
370
|
nuclear@0
|
371 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@0
|
372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@0
|
373 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@0
|
374 }
|
nuclear@0
|
375
|
nuclear@0
|
376 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
nuclear@0
|
377
|
nuclear@0
|
378 fb_tex_width = next_pow2(width);
|
nuclear@0
|
379 fb_tex_height = next_pow2(height);
|
nuclear@0
|
380
|
nuclear@0
|
381 glBindTexture(GL_TEXTURE_2D, fb_tex);
|
nuclear@0
|
382 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
|
nuclear@0
|
383 GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
nuclear@0
|
384 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
|
nuclear@0
|
385
|
nuclear@0
|
386 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
|
nuclear@0
|
387 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
|
nuclear@0
|
388 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
|
nuclear@0
|
389
|
nuclear@0
|
390 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
nuclear@0
|
391 fprintf(stderr, "incomplete framebuffer!\n");
|
nuclear@0
|
392 }
|
nuclear@0
|
393
|
nuclear@0
|
394 glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
nuclear@0
|
395 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
|
nuclear@0
|
396 }
|
nuclear@0
|
397
|
nuclear@0
|
398 int handle_event(SDL_Event *ev)
|
nuclear@0
|
399 {
|
nuclear@0
|
400 switch(ev->type) {
|
nuclear@4
|
401 case SDL_QUIT:
|
nuclear@4
|
402 return -1;
|
nuclear@4
|
403
|
nuclear@0
|
404 case SDL_KEYDOWN:
|
nuclear@0
|
405 case SDL_KEYUP:
|
nuclear@0
|
406 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
|
nuclear@0
|
407 return -1;
|
nuclear@0
|
408 }
|
nuclear@0
|
409 break;
|
nuclear@0
|
410
|
nuclear@0
|
411 default:
|
nuclear@0
|
412 break;
|
nuclear@0
|
413 }
|
nuclear@0
|
414
|
nuclear@0
|
415 return 0;
|
nuclear@0
|
416 }
|
nuclear@0
|
417
|
nuclear@0
|
418 int key_event(int key, int state)
|
nuclear@0
|
419 {
|
nuclear@0
|
420 if(state) {
|
nuclear@4
|
421 ovrHSWDisplayState hsw;
|
nuclear@4
|
422 ovrHmd_GetHSWDisplayState(hmd, &hsw);
|
nuclear@4
|
423 if(hsw.Displayed) {
|
nuclear@4
|
424 ovrHmd_DismissHSWDisplay(hmd);
|
nuclear@4
|
425 }
|
nuclear@4
|
426
|
nuclear@0
|
427 switch(key) {
|
nuclear@0
|
428 case 27:
|
nuclear@0
|
429 return -1;
|
nuclear@0
|
430
|
nuclear@4
|
431 case ' ':
|
nuclear@4
|
432 /* allow the user to recenter by pressing space */
|
nuclear@4
|
433 ovrHmd_RecenterPose(hmd);
|
nuclear@4
|
434 break;
|
nuclear@4
|
435
|
nuclear@4
|
436 case 'f':
|
nuclear@4
|
437 /* press f to move the window to the HMD */
|
nuclear@4
|
438 toggle_hmd_fullscreen();
|
nuclear@4
|
439 break;
|
nuclear@4
|
440
|
nuclear@0
|
441 default:
|
nuclear@0
|
442 break;
|
nuclear@0
|
443 }
|
nuclear@0
|
444 }
|
nuclear@0
|
445 return 0;
|
nuclear@0
|
446 }
|
nuclear@4
|
447
|
nuclear@4
|
448 unsigned int next_pow2(unsigned int x)
|
nuclear@4
|
449 {
|
nuclear@4
|
450 x -= 1;
|
nuclear@4
|
451 x |= x >> 1;
|
nuclear@4
|
452 x |= x >> 2;
|
nuclear@4
|
453 x |= x >> 4;
|
nuclear@4
|
454 x |= x >> 8;
|
nuclear@4
|
455 x |= x >> 16;
|
nuclear@4
|
456 return x + 1;
|
nuclear@4
|
457 }
|
nuclear@4
|
458
|
nuclear@4
|
459 void quat_to_matrix(const float *quat, float *mat)
|
nuclear@4
|
460 {
|
nuclear@4
|
461 mat[0] = 1.0 - 2.0 * quat[1] * quat[1] - 2.0 * quat[2] * quat[2];
|
nuclear@4
|
462 mat[4] = 2.0 * quat[0] * quat[1] + 2.0 * quat[3] * quat[2];
|
nuclear@4
|
463 mat[8] = 2.0 * quat[2] * quat[0] - 2.0 * quat[3] * quat[1];
|
nuclear@4
|
464 mat[12] = 0.0f;
|
nuclear@4
|
465
|
nuclear@4
|
466 mat[1] = 2.0 * quat[0] * quat[1] - 2.0 * quat[3] * quat[2];
|
nuclear@4
|
467 mat[5] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[2]*quat[2];
|
nuclear@4
|
468 mat[9] = 2.0 * quat[1] * quat[2] + 2.0 * quat[3] * quat[0];
|
nuclear@4
|
469 mat[13] = 0.0f;
|
nuclear@4
|
470
|
nuclear@4
|
471 mat[2] = 2.0 * quat[2] * quat[0] + 2.0 * quat[3] * quat[1];
|
nuclear@4
|
472 mat[6] = 2.0 * quat[1] * quat[2] - 2.0 * quat[3] * quat[0];
|
nuclear@4
|
473 mat[10] = 1.0 - 2.0 * quat[0]*quat[0] - 2.0 * quat[1]*quat[1];
|
nuclear@4
|
474 mat[14] = 0.0f;
|
nuclear@4
|
475
|
nuclear@4
|
476 mat[3] = mat[7] = mat[11] = 0.0f;
|
nuclear@4
|
477 mat[15] = 1.0f;
|
nuclear@4
|
478 }
|
nuclear@4
|
479
|
nuclear@4
|
480 unsigned int gen_chess_tex(float r0, float g0, float b0, float r1, float g1, float b1)
|
nuclear@4
|
481 {
|
nuclear@4
|
482 int i, j;
|
nuclear@4
|
483 unsigned int tex;
|
nuclear@4
|
484 unsigned char img[8 * 8 * 3];
|
nuclear@4
|
485 unsigned char *pix = img;
|
nuclear@4
|
486
|
nuclear@4
|
487 for(i=0; i<8; i++) {
|
nuclear@4
|
488 for(j=0; j<8; j++) {
|
nuclear@4
|
489 int black = (i & 1) == (j & 1);
|
nuclear@4
|
490 pix[0] = (black ? r0 : r1) * 255;
|
nuclear@4
|
491 pix[1] = (black ? g0 : g1) * 255;
|
nuclear@4
|
492 pix[2] = (black ? b0 : b1) * 255;
|
nuclear@4
|
493 pix += 3;
|
nuclear@4
|
494 }
|
nuclear@4
|
495 }
|
nuclear@4
|
496
|
nuclear@4
|
497 glGenTextures(1, &tex);
|
nuclear@4
|
498 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@4
|
499 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
nuclear@4
|
500 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
nuclear@4
|
501 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, img);
|
nuclear@4
|
502
|
nuclear@4
|
503 return tex;
|
nuclear@2
|
504 } |