From e03a9a5a9f1353916e3b6ba9124e11fac7f52e33 Mon Sep 17 00:00:00 2001 From: Bastien Date: Sat, 22 Sep 2018 19:01:05 +0200 Subject: [PATCH] Clean example --- examples/opengl/main.c | 123 ++++++++--------------------------------- 1 file changed, 23 insertions(+), 100 deletions(-) diff --git a/examples/opengl/main.c b/examples/opengl/main.c index af74a3b..b90e676 100644 --- a/examples/opengl/main.c +++ b/examples/opengl/main.c @@ -34,7 +34,8 @@ GLuint gen_cubes() glNewList(list, GL_COMPILE); - for(float a = 0.0f; a < 360.0f; a += 20.0f){ + for (float a = 0.0f; a < 360.0f; a += 20.0f) + { glPushMatrix(); glRotatef(a, 0, 1, 0); @@ -58,102 +59,51 @@ GLuint gen_cubes() return list; } -void draw_crosshairs(float len, float cx, float cy) -{ - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glBegin(GL_LINES); - float l = len/2.0f; - glVertex3f(cx - l, cy, 0.0); - glVertex3f(cx + l, cy, 0.0); - glVertex3f(cx, cy - l, 0.0); - glVertex3f(cx, cy + l, 0.0); - glEnd(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); -} - void draw_scene(GLuint list) { // draw cubes glCallList(list); } -static inline void -print_matrix(float m[]) -{ - printf("[[%0.4f, %0.4f, %0.4f, %0.4f],\n" - "[%0.4f, %0.4f, %0.4f, %0.4f],\n" - "[%0.4f, %0.4f, %0.4f, %0.4f],\n" - "[%0.4f, %0.4f, %0.4f, %0.4f]]\n", - m[0], m[4], m[8], m[12], - m[1], m[5], m[9], m[13], - m[2], m[6], m[10], m[14], - m[3], m[7], m[11], m[15]); -} int main(int argc, char** argv) { - int hmd_w, hmd_h; + ohmd_context * ctx = ohmd_ctx_create(); - ohmd_context* ctx = ohmd_ctx_create(); int num_devices = ohmd_ctx_probe(ctx); - if(num_devices < 0){ + if (num_devices < 0) + { printf("failed to probe devices: %s\n", ohmd_ctx_get_error(ctx)); return 1; } - ohmd_device_settings* settings = ohmd_device_settings_create(ctx); + ohmd_device_settings * settings = ohmd_device_settings_create(ctx); // If OHMD_IDS_AUTOMATIC_UPDATE is set to 0, ohmd_ctx_update() must be called at least 10 times per second. // It is enabled by default. - int auto_update = 1; ohmd_device_settings_seti(settings, OHMD_IDS_AUTOMATIC_UPDATE, &auto_update); - ohmd_device* hmd = ohmd_list_open_device_s(ctx, 0, settings); - if(!hmd){ + ohmd_device * hmd = ohmd_list_open_device_s(ctx, 0, settings); + if (!hmd) + { printf("failed to open device: %s\n", ohmd_ctx_get_error(ctx)); return 1; } + + int hmd_w = 0; + int hmd_h = 0; ohmd_device_geti(hmd, OHMD_SCREEN_HORIZONTAL_RESOLUTION, &hmd_w); ohmd_device_geti(hmd, OHMD_SCREEN_VERTICAL_RESOLUTION, &hmd_h); - float ipd; - ohmd_device_getf(hmd, OHMD_EYE_IPD, &ipd); - float viewport_scale[2]; - float distortion_coeffs[4]; - float aberr_scale[3]; - float sep; - float left_lens_center[2]; - float right_lens_center[2]; - //viewport is half the screen - ohmd_device_getf(hmd, OHMD_SCREEN_HORIZONTAL_SIZE, &(viewport_scale[0])); - viewport_scale[0] /= 2.0f; - ohmd_device_getf(hmd, OHMD_SCREEN_VERTICAL_SIZE, &(viewport_scale[1])); - //distortion coefficients - ohmd_device_getf(hmd, OHMD_UNIVERSAL_DISTORTION_K, &(distortion_coeffs[0])); - ohmd_device_getf(hmd, OHMD_UNIVERSAL_ABERRATION_K, &(aberr_scale[0])); - //calculate lens centers (assuming the eye separation is the distance between the lens centers) - ohmd_device_getf(hmd, OHMD_LENS_HORIZONTAL_SEPARATION, &sep); - ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(left_lens_center[1])); - ohmd_device_getf(hmd, OHMD_LENS_VERTICAL_POSITION, &(right_lens_center[1])); - left_lens_center[0] = viewport_scale[0] - sep/2.0f; - right_lens_center[0] = sep/2.0f; - //assume calibration was for lens view to which ever edge of screen is further away from lens center - float warp_scale = (left_lens_center[0] > right_lens_center[0]) ? left_lens_center[0] : right_lens_center[0]; - float warp_adj = 1.0f; ohmd_device_settings_destroy(settings); struct lvrcInstance * compositor = lvrcCreateInstance(hmd); + const int window_w = hmd_w/2; + const int window_h = hmd_h; + gl_ctx gl; - init_gl(&gl, hmd_w, hmd_h); + init_gl(&gl, window_w, window_h); GLuint list = gen_cubes(); @@ -178,9 +128,9 @@ int main(int argc, char** argv) create_fbo(eye_w, eye_h, &right_fbo, right_color_tex, &right_depth_tex); bool done = false; - bool crosshair_overlay = false; - while(!done){ + while (!done) + { ohmd_ctx_update(ctx); lvrcBeginFrame(compositor); @@ -207,12 +157,6 @@ int main(int argc, char** argv) glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(list); - if (crosshair_overlay) { - glClear(GL_DEPTH_BUFFER_BIT); - glLineWidth(2.0*OVERSAMPLE_SCALE); - glColor4f(1.0, 0.5, 0.0, 1.0); - draw_crosshairs(0.1, 2*left_lens_center[0]/viewport_scale[0] - 1.0f, 2*left_lens_center[1]/viewport_scale[1] - 1.0f); - } // set hmd rotation, for right eye. glMatrixMode(GL_PROJECTION); @@ -228,12 +172,6 @@ int main(int argc, char** argv) glViewport(0, 0, eye_w, eye_h); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(list); - if (crosshair_overlay) { - glClear(GL_DEPTH_BUFFER_BIT); - glLineWidth(5.0); - glColor4f(1.0, 0.5, 0.0, 1.0); - draw_crosshairs(0.1, 2*right_lens_center[0]/viewport_scale[0] - 1.0f, 2*right_lens_center[1]/viewport_scale[1] - 1.0f); - } // Clean up common draw state glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); @@ -241,7 +179,7 @@ int main(int argc, char** argv) glDisable(GL_DEPTH_TEST); // Setup ortho state. - glViewport(0, 0, hmd_w, hmd_h); + glViewport(0, 0, window_w, window_h); glEnable(GL_TEXTURE_2D); glColor4d(1, 1, 1, 1); @@ -255,26 +193,13 @@ int main(int argc, char** argv) glBindTexture(GL_TEXTURE_2D, left_color_tex); glBegin(GL_QUADS); glTexCoord2d( 0, 0); - glVertex3d( -1, -1, 0); + glVertex3d( -1, -1, 1); glTexCoord2d( 1, 0); - glVertex3d( 0, -1, 0); + glVertex3d( 1, -1, 1); glTexCoord2d( 1, 1); - glVertex3d( 0, 1, 0); + glVertex3d( 1, 1, 1); glTexCoord2d( 0, 1); - glVertex3d( -1, 1, 0); - glEnd(); - - // Draw right eye - glBindTexture(GL_TEXTURE_2D, right_color_tex); - glBegin(GL_QUADS); - glTexCoord2d( 0, 0); - glVertex3d( 0, -1, 0); - glTexCoord2d( 1, 0); - glVertex3d( 1, -1, 0); - glTexCoord2d( 1, 1); - glVertex3d( 1, 1, 0); - glTexCoord2d( 0, 1); - glVertex3d( 0, 1, 0); + glVertex3d( -1, 1, 1); glEnd(); // Clean up state. @@ -282,8 +207,6 @@ int main(int argc, char** argv) glDisable(GL_TEXTURE_2D); glUseProgram(0); - glFinish(); - // Da swap-dawup! eglSwapBuffers(gl.egl_display, gl.egl_surface);