oculus2

annotate src/main.c @ 3:096b18432ba7

something is fucked
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Aug 2014 18:37:24 +0300
parents 0984fa94b490
children d64830551c32
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@2 19 void display(void);
nuclear@2 20 void draw_scene(void);
nuclear@2 21 void draw_box(float xsz, float ysz, float zsz, float norm_sign);
nuclear@0 22 void update_rtarg(int width, int height);
nuclear@0 23 int handle_event(SDL_Event *ev);
nuclear@0 24 int key_event(int key, int state);
nuclear@0 25 void reshape(int x, int y);
nuclear@0 26 unsigned int next_pow2(unsigned int x);
nuclear@0 27
nuclear@0 28 OVR_EXPORT void ovrhmd_EnableHSWDisplaySDKRender(ovrHmd hmd, ovrBool enable);
nuclear@0 29
nuclear@0 30 static SDL_Window *win;
nuclear@0 31 static SDL_GLContext ctx;
nuclear@2 32 static int win_width, win_height;
nuclear@0 33
nuclear@0 34 static unsigned int fbo, fb_tex, fb_depth;
nuclear@0 35 static int fb_width, fb_height;
nuclear@0 36 static int fb_tex_width, fb_tex_height;
nuclear@0 37
nuclear@0 38 static ovrHmd hmd;
nuclear@0 39 static ovrSizei eyeres[2];
nuclear@2 40 static ovrEyeRenderDesc eye_rdesc[2];
nuclear@2 41 static ovrGLTexture fb_ovr_tex[2];
nuclear@0 42
nuclear@0 43
nuclear@0 44 int main(void)
nuclear@0 45 {
nuclear@0 46 if(init() == -1) {
nuclear@0 47 return 1;
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 for(;;) {
nuclear@0 51 SDL_Event ev;
nuclear@0 52
nuclear@0 53 while(SDL_PollEvent(&ev)) {
nuclear@0 54 if(handle_event(&ev) == -1) {
nuclear@0 55 goto done;
nuclear@0 56 }
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 display();
nuclear@0 60 }
nuclear@0 61
nuclear@0 62 done:
nuclear@0 63 cleanup();
nuclear@0 64 return 0;
nuclear@0 65 }
nuclear@0 66
nuclear@0 67
nuclear@2 68 int init(void)
nuclear@0 69 {
nuclear@2 70 int i, x, y;
nuclear@0 71 unsigned int flags, dcaps;
nuclear@0 72 union ovrGLConfig glcfg;
nuclear@0 73
nuclear@0 74 // this must be called before any OpenGL init according to the docs
nuclear@0 75 ovr_Initialize();
nuclear@0 76
nuclear@0 77 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
nuclear@0 78
nuclear@0 79 x = y = SDL_WINDOWPOS_UNDEFINED;
nuclear@2 80 flags = SDL_WINDOW_OPENGL;
nuclear@0 81 if(!(win = SDL_CreateWindow("simple oculus example", x, y, 1280, 800, flags))) {
nuclear@0 82 fprintf(stderr, "failed to create window\n");
nuclear@0 83 return -1;
nuclear@0 84 }
nuclear@0 85 if(!(ctx = SDL_GL_CreateContext(win))) {
nuclear@0 86 fprintf(stderr, "failed to create OpenGL context\n");
nuclear@0 87 return -1;
nuclear@0 88 }
nuclear@0 89
nuclear@0 90 glewInit();
nuclear@0 91
nuclear@0 92 if(!(hmd = ovrHmd_Create(0))) {
nuclear@0 93 fprintf(stderr, "failed to open Oculus HMD, falling back to virtual debug HMD\n");
nuclear@2 94 if(!(hmd = ovrHmd_CreateDebug(ovrHmd_DK2))) {
nuclear@0 95 fprintf(stderr, "failed to create virtual debug HMD\n");
nuclear@0 96 return -1;
nuclear@0 97 }
nuclear@0 98 }
nuclear@0 99 printf("initialized HMD: %s - %s\n", hmd->Manufacturer, hmd->ProductName);
nuclear@0 100
nuclear@2 101 SDL_SetWindowSize(win, hmd->Resolution.w, hmd->Resolution.h);
nuclear@0 102
nuclear@0 103 ovrHmd_ConfigureTracking(hmd, 0xffffffff, 0);
nuclear@0 104 eyeres[0] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0);
nuclear@0 105 eyeres[1] = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0);
nuclear@0 106
nuclear@2 107 SDL_GetWindowSize(win, &win_width, &win_height);
nuclear@2 108
nuclear@2 109 fb_width = eyeres[0].w + eyeres[1].w;
nuclear@0 110 fb_height = eyeres[0].h > eyeres[1].h ? eyeres[0].h : eyeres[1].h;
nuclear@0 111 update_rtarg(fb_width, fb_height);
nuclear@0 112
nuclear@2 113 for(i=0; i<2; i++) {
nuclear@2 114 fb_ovr_tex[i].OGL.Header.API = ovrRenderAPI_OpenGL;
nuclear@2 115 fb_ovr_tex[i].OGL.Header.TextureSize.w = fb_tex_width;
nuclear@2 116 fb_ovr_tex[i].OGL.Header.TextureSize.h = fb_tex_height;
nuclear@3 117 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.x = i == 0 ? 0 : fb_width / 2.0;
nuclear@3 118 fb_ovr_tex[i].OGL.Header.RenderViewport.Pos.y = fb_tex_height - fb_height;
nuclear@2 119 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.w = fb_width / 2.0;
nuclear@2 120 fb_ovr_tex[i].OGL.Header.RenderViewport.Size.h = fb_height;
nuclear@2 121 fb_ovr_tex[i].OGL.TexId = fb_tex;
nuclear@2 122 }
nuclear@2 123
nuclear@0 124 memset(&glcfg, 0, sizeof glcfg);
nuclear@0 125 glcfg.OGL.Header.API = ovrRenderAPI_OpenGL;
nuclear@0 126 glcfg.OGL.Header.RTSize = hmd->Resolution;
nuclear@0 127 glcfg.OGL.Header.Multisample = 1;
nuclear@0 128
nuclear@0 129 if(hmd->HmdCaps & ovrHmdCap_ExtendDesktop) {
nuclear@0 130 printf("running in \"extended desktop\" mode\n");
nuclear@0 131 } else {
nuclear@2 132 #ifdef WIN32
nuclear@2 133 void *sys_win = GetActiveWindow();
nuclear@2 134 glcfg.OGL.Window = sys_win;
nuclear@2 135 glcfg.OGL.DC = wglGetCurrentDC();
nuclear@2 136 ovrHmd_AttachToWindow(hmd, sys_win, 0, 0);
nuclear@2 137 #endif
nuclear@0 138 printf("running in \"direct-hmd\" mode\n");
nuclear@0 139 }
nuclear@2 140 ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
nuclear@0 141
nuclear@3 142 dcaps = ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette;// | ovrDistortionCap_TimeWarp;
nuclear@0 143 if(!ovrHmd_ConfigureRendering(hmd, &glcfg.Config, dcaps, hmd->DefaultEyeFov, eye_rdesc)) {
nuclear@0 144 fprintf(stderr, "failed to configure distortion renderer\n");
nuclear@0 145 }
nuclear@0 146
nuclear@3 147 ovrhmd_EnableHSWDisplaySDKRender(hmd, 0);
nuclear@0 148
nuclear@0 149 glEnable(GL_DEPTH_TEST);
nuclear@0 150 glEnable(GL_CULL_FACE);
nuclear@0 151 glEnable(GL_LIGHTING);
nuclear@0 152 glEnable(GL_LIGHT0);
nuclear@0 153
nuclear@3 154 glClearColor(0.05, 0.05, 0.05, 1);
nuclear@0 155
nuclear@0 156 return 0;
nuclear@0 157 }
nuclear@0 158
nuclear@2 159 void cleanup(void)
nuclear@0 160 {
nuclear@0 161 if(hmd) {
nuclear@0 162 ovrHmd_Destroy(hmd);
nuclear@0 163 }
nuclear@0 164 ovr_Shutdown();
nuclear@0 165
nuclear@0 166 SDL_Quit();
nuclear@0 167 }
nuclear@0 168
nuclear@2 169 void display(void)
nuclear@0 170 {
nuclear@2 171 int i;
nuclear@2 172 ovrMatrix4f proj;
nuclear@2 173 ovrPosef pose[2];
nuclear@2 174
nuclear@2 175 ovrHmd_BeginFrame(hmd, 0);
nuclear@2 176
nuclear@2 177 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@0 178 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 179
nuclear@3 180 glViewport(0, 0, fb_width, fb_height);
nuclear@3 181
nuclear@3 182 /*glMatrixMode(GL_PROJECTION);
nuclear@3 183 glLoadIdentity();
nuclear@3 184 glMatrixMode(GL_MODELVIEW);
nuclear@3 185 glLoadIdentity();
nuclear@3 186
nuclear@3 187 glPushAttrib(GL_ENABLE_BIT);
nuclear@3 188 glDisable(GL_LIGHTING);
nuclear@3 189 glDisable(GL_DEPTH_TEST);
nuclear@3 190
nuclear@3 191 glBegin(GL_QUADS);
nuclear@3 192 glColor3f(1, 0, 0);
nuclear@3 193 glVertex2f(-1, -1);
nuclear@3 194 glVertex2f(0, -1);
nuclear@3 195 glVertex2f(0, 1);
nuclear@3 196 glVertex2f(-1, 1);
nuclear@3 197
nuclear@3 198 glColor3f(1, 0, 1);
nuclear@3 199 glVertex2f(-0.6, -0.1);
nuclear@3 200 glVertex2f(-0.4, -0.1);
nuclear@3 201 glVertex2f(-0.4, 0.1);
nuclear@3 202 glVertex2f(-0.6, 0.1);
nuclear@3 203
nuclear@3 204 glColor3f(0, 1, 0);
nuclear@3 205 glVertex2f(0, -1);
nuclear@3 206 glVertex2f(1, -1);
nuclear@3 207 glVertex2f(1, 1);
nuclear@3 208 glVertex2f(0, 1);
nuclear@3 209
nuclear@3 210 glColor3f(0, 1, 1);
nuclear@3 211 glVertex2f(0.4, -0.1);
nuclear@3 212 glVertex2f(0.6, -0.1);
nuclear@3 213 glVertex2f(0.6, 0.1);
nuclear@3 214 glVertex2f(0.4, 0.1);
nuclear@3 215 glEnd();
nuclear@3 216
nuclear@3 217 glPopAttrib();*/
nuclear@3 218
nuclear@2 219 /* for each eye ... */
nuclear@2 220 for(i=0; i<2; i++) {
nuclear@2 221 int eye = hmd->EyeRenderOrder[i];
nuclear@2 222
nuclear@2 223 /* vport0(0, 0, width/2, height), vport1(width/2, 0, width/2, height) */
nuclear@2 224 glViewport(eye == 0 ? 0 : fb_width / 2, 0, fb_width / 2, fb_height);
nuclear@2 225
nuclear@2 226 proj = ovrMatrix4f_Projection(hmd->DefaultEyeFov[eye], 0.5, 500.0, 1);
nuclear@2 227 glMatrixMode(GL_PROJECTION);
nuclear@3 228 //glLoadMatrixf(proj.M[0]);
nuclear@3 229 glLoadIdentity();
nuclear@3 230 gluPerspective(50.0, (float)fb_width / 2.0 / (float)fb_height, 0.5, 500.0);
nuclear@2 231
nuclear@2 232 pose[eye] = ovrHmd_GetEyePose(hmd, eye);
nuclear@2 233 glMatrixMode(GL_MODELVIEW);
nuclear@3 234 /* TODO: get HMD orientation data and use it */
nuclear@3 235 //glTranslatef(0, -ovrHmd_GetFloat(hmd, OVR_KEY_EYE_HEIGHT, 1.65), 0);
nuclear@2 236
nuclear@2 237 draw_scene();
nuclear@2 238 }
nuclear@2 239
nuclear@2 240 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@2 241 glViewport(0, 0, win_width, win_height);
nuclear@2 242
nuclear@2 243 ovrHmd_EndFrame(hmd, pose, &fb_ovr_tex[0].Texture);
nuclear@2 244
nuclear@2 245 assert(glGetError() == GL_NO_ERROR);
nuclear@2 246 }
nuclear@2 247
nuclear@2 248 void draw_scene(void)
nuclear@2 249 {
nuclear@2 250 int i;
nuclear@2 251 float lpos[] = {0, 5, 0, 1};
nuclear@2 252 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 253
nuclear@3 254 glPushMatrix();
nuclear@3 255 glTranslatef(0, 0, -8);
nuclear@3 256 draw_box(2, 2, 2, 1.0);
nuclear@3 257 glPopMatrix();
nuclear@3 258
nuclear@2 259 glTranslatef(0, 5, 0);
nuclear@2 260 draw_box(20, 10, 20, -1.0);
nuclear@2 261
nuclear@2 262 for(i=0; i<4; i++) {
nuclear@2 263 glPushMatrix();
nuclear@2 264 if(i & 1) {
nuclear@2 265 glTranslatef(0, 0, i & 2 ? 7.5 : -7.5);
nuclear@2 266 } else {
nuclear@2 267 glTranslatef(i & 2 ? 7.5 : -7.5, 0, 0);
nuclear@2 268 }
nuclear@2 269 draw_box(3, 0, 3, 1.0);
nuclear@2 270 glPopMatrix();
nuclear@2 271 }
nuclear@2 272 }
nuclear@2 273
nuclear@2 274 void draw_box(float xsz, float ysz, float zsz, float norm_sign)
nuclear@2 275 {
nuclear@2 276 glMatrixMode(GL_MODELVIEW);
nuclear@2 277 glScalef(xsz * 0.5, ysz * 0.5, zsz * 0.5);
nuclear@2 278
nuclear@2 279 if(norm_sign < 0.0) {
nuclear@2 280 glFrontFace(GL_CW);
nuclear@2 281 }
nuclear@2 282
nuclear@2 283 glBegin(GL_QUADS);
nuclear@2 284 glNormal3f(0, 0, 1 * norm_sign);
nuclear@2 285 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
nuclear@2 286 glTexCoord2f(1, 0); glVertex3f(1, -1, 1);
nuclear@2 287 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
nuclear@2 288 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
nuclear@2 289 glNormal3f(1 * norm_sign, 0, 0);
nuclear@2 290 glTexCoord2f(0, 0); glVertex3f(1, -1, 1);
nuclear@2 291 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
nuclear@2 292 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
nuclear@2 293 glTexCoord2f(0, 1); glVertex3f(1, 1, 1);
nuclear@2 294 glNormal3f(0, 0, -1 * norm_sign);
nuclear@2 295 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
nuclear@2 296 glTexCoord2f(1, 0); glVertex3f(-1, -1, -1);
nuclear@2 297 glTexCoord2f(1, 1); glVertex3f(-1, 1, -1);
nuclear@2 298 glTexCoord2f(0, 1); glVertex3f(1, 1, -1);
nuclear@2 299 glNormal3f(-1 * norm_sign, 0, 0);
nuclear@2 300 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
nuclear@2 301 glTexCoord2f(1, 0); glVertex3f(-1, -1, 1);
nuclear@2 302 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
nuclear@2 303 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
nuclear@2 304 glNormal3f(0, 1 * norm_sign, 0);
nuclear@2 305 glTexCoord2f(0, 0); glVertex3f(-1, 1, 1);
nuclear@2 306 glTexCoord2f(1, 0); glVertex3f(1, 1, 1);
nuclear@2 307 glTexCoord2f(1, 1); glVertex3f(1, 1, -1);
nuclear@2 308 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
nuclear@2 309 glNormal3f(0, -1 * norm_sign, 0);
nuclear@2 310 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
nuclear@2 311 glTexCoord2f(1, 0); glVertex3f(1, -1, -1);
nuclear@2 312 glTexCoord2f(1, 1); glVertex3f(1, -1, 1);
nuclear@2 313 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
nuclear@2 314 glEnd();
nuclear@2 315
nuclear@2 316 glFrontFace(GL_CCW);
nuclear@0 317 }
nuclear@0 318
nuclear@0 319 void update_rtarg(int width, int height)
nuclear@0 320 {
nuclear@0 321 if(!fbo) {
nuclear@0 322 glGenFramebuffers(1, &fbo);
nuclear@0 323 glGenTextures(1, &fb_tex);
nuclear@0 324 glGenRenderbuffers(1, &fb_depth);
nuclear@0 325
nuclear@0 326 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@0 327 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@0 328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@0 329 }
nuclear@0 330
nuclear@0 331 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@0 332
nuclear@0 333 fb_tex_width = next_pow2(width);
nuclear@0 334 fb_tex_height = next_pow2(height);
nuclear@0 335
nuclear@0 336 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@0 337 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_width, fb_tex_height, 0,
nuclear@0 338 GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@0 339 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
nuclear@0 340
nuclear@0 341 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
nuclear@0 342 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_width, fb_tex_height);
nuclear@0 343 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
nuclear@0 344
nuclear@0 345 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@0 346 fprintf(stderr, "incomplete framebuffer!\n");
nuclear@0 347 }
nuclear@0 348
nuclear@0 349 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@0 350 printf("created render target: %dx%d (texture size: %dx%d)\n", width, height, fb_tex_width, fb_tex_height);
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 int handle_event(SDL_Event *ev)
nuclear@0 354 {
nuclear@0 355 switch(ev->type) {
nuclear@0 356 case SDL_KEYDOWN:
nuclear@0 357 case SDL_KEYUP:
nuclear@0 358 if(key_event(ev->key.keysym.sym, ev->key.state == SDL_PRESSED) == -1) {
nuclear@0 359 return -1;
nuclear@0 360 }
nuclear@0 361 break;
nuclear@0 362
nuclear@0 363 default:
nuclear@0 364 break;
nuclear@0 365 }
nuclear@0 366
nuclear@0 367 return 0;
nuclear@0 368 }
nuclear@0 369
nuclear@0 370 int key_event(int key, int state)
nuclear@0 371 {
nuclear@0 372 if(state) {
nuclear@0 373 switch(key) {
nuclear@0 374 case 27:
nuclear@0 375 return -1;
nuclear@0 376
nuclear@0 377 default:
nuclear@0 378 break;
nuclear@0 379 }
nuclear@0 380 }
nuclear@0 381 return 0;
nuclear@0 382 }
nuclear@2 383
nuclear@2 384 unsigned int next_pow2(unsigned int x)
nuclear@2 385 {
nuclear@2 386 x -= 1;
nuclear@2 387 x |= x >> 1;
nuclear@2 388 x |= x >> 2;
nuclear@2 389 x |= x >> 4;
nuclear@2 390 x |= x >> 8;
nuclear@2 391 x |= x >> 16;
nuclear@2 392 return x + 1;
nuclear@2 393 }