Guest User

Oculus fail code

a guest
May 6th, 2014
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void paintGL()
  2. {
  3.     paint_to_frameBuffer();
  4.     paint_to_display();
  5. }
  6.  
  7. void paint_to_frameBuffer()
  8. {
  9.     glPushAttrib( GL_TEXTURE_BIT | GL_DEPTH_TEST | GL_LIGHTING );
  10.  
  11.     glEnable(GL_DEPTH_TEST);
  12.     glEnable(GL_LIGHTING);
  13.     glEnable(GL_CULL_FACE);
  14.  
  15.     // Render to our framebuffer
  16.     glBindFramebuffer(GL_FRAMEBUFFER, gl_frame_buffer);
  17.  
  18.     // Render to screen (for debugging)
  19.     //    glBindFramebuffer(GL_FRAMEBUFFER, 0);
  20.  
  21.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  22.  
  23.     // draw left viewpoint
  24.     setUpCamera(OVR::Util::Render::StereoEye_Left);
  25.     drawEverything();
  26.     // draw right viewpoint
  27.     setUpCamera(OVR::Util::Render::StereoEye_Right);
  28.     drawEverything();
  29.  
  30.     glPopAttrib();
  31. }
  32.  
  33. void setUpCamera(OVR::Util::Render::StereoEye eye)
  34. {
  35.     const OVR::HMDInfo& hmd = m_HMD_info;
  36.     const OVR::Util::Render::StereoEyeParams& params = m_stereo_config.GetEyeRenderParams(eye);
  37.     glViewport(params.VP.x, params.VP.y/*+1152-params.VP.h*/, params.VP.w, params.VP.h);
  38.  
  39.     glMatrixMode(GL_PROJECTION);
  40.     glLoadIdentity();
  41.     applyGLMatrix(params.ViewAdjust);
  42.     applyGLMatrix(params.Projection);
  43.  
  44.     glMatrixMode(GL_MODELVIEW);
  45.     glLoadIdentity();
  46.  
  47.     OVR::Matrix4f eye_view = get_eye_view(eye);
  48.     applyGLMatrix(eye_view);
  49. }
  50.  
  51. void paint_to_display()
  52. {
  53.     glPushAttrib(GL_TEXTURE_BIT | GL_DEPTH_TEST | GL_LIGHTING );
  54.  
  55.     glDisable(GL_CULL_FACE);
  56.     glDisable(GL_DEPTH_TEST);
  57.     glDisable(GL_LIGHTING);
  58.     glEnable(GL_TEXTURE_2D);
  59.  
  60.     // Render to the screen
  61.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  62.  
  63.     // reset the screen params.
  64.     glViewport(0, 0, this->width(), this->height() );
  65.     glMatrixMode(GL_PROJECTION);
  66.     glLoadIdentity();
  67.     glMatrixMode(GL_MODELVIEW);
  68.     glLoadIdentity();
  69.  
  70.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  71.  
  72.     glUseProgram(gl_fragment_shader_program);
  73.  
  74.     glActiveTexture(GL_TEXTURE0);
  75.     glBindTexture(GL_TEXTURE_2D, gl_frame_buffer_texture);
  76.  
  77.     gl_uniform_1i("texture0", 0);
  78.  
  79.     renderEyePatch(OVR::Util::Render::StereoEye_Right);
  80.     renderEyePatch(OVR::Util::Render::StereoEye_Left);
  81.  
  82.     glBindTexture(GL_TEXTURE_2D, 0);
  83.     glUseProgram(0);
  84.     glPopAttrib();
  85. }
  86.  
  87. void renderEyePatch(OVR::Util::Render::StereoEye eye)
  88. {
  89.         // apply distortion for each eye.
  90.         const OVR::Util::Render::StereoEyeParams& params = m_stereo_config.GetEyeRenderParams(eye);
  91.         if(params.pDistortion)
  92.         {
  93.                 GLsizei width = m_HMD_info.HResolution;
  94.                 GLsizei height = m_HMD_info.VResolution;
  95.                 const OVR::Util::Render::DistortionConfig& distortion = *params.pDistortion;
  96.  
  97.                 float w  = float(params.VP.w) / float(width),
  98.                           h  = float(params.VP.h) / float(height),
  99.                           x  = float(params.VP.x) / float(width),
  100.                           y  = float(params.VP.y) / float(height);
  101.                 float as = float(params.VP.w) / float(params.VP.h);
  102.  
  103.                 // We are using 1/4 of DistortionCenter offset value here, since it is
  104.                 // relative to [-1,1] range that gets mapped to [0, 0.5].
  105.                 float HmdWarpParam[4]   = { distortion.K[0], distortion.K[1], distortion.K[2], distortion.K[3] };
  106.                 float ChromAbParam[4]   = { distortion.ChromaticAberration[0], distortion.ChromaticAberration[1], distortion.ChromaticAberration[2], distortion.ChromaticAberration[3] };
  107.                 float scaleFactor       = 1.0f / distortion.Scale;              
  108.                 OVR::Vector2f LensCenter    (x + (w + distortion.XCenterOffset * 0.5f)*0.5f, y + h*0.5f);
  109.                 if(eye == OVR::Util::Render::StereoEye_Right)
  110.                     LensCenter.x = x + (w - distortion.XCenterOffset * 0.5f)*0.5f;
  111.                 OVR::Vector2f ScreenCenter  (x + w*0.5f ,y + h*0.5f);
  112.                 OVR::Vector2f Scale         ((w/2) * scaleFactor, (h/2) * scaleFactor * as);
  113.                 OVR::Vector2f ScaleIn       ((2/w), (2/h) / as);
  114.  
  115.                 // fragment shader.
  116.                 gl_uniform_2f("LensCenter", LensCenter.x, LensCenter.y); qDebug() << "Lenscenter x y:" << LensCenter.x << LensCenter.y;
  117.                 gl_uniform_2f("ScreenCenter", ScreenCenter.x, ScreenCenter.y);
  118.                 gl_uniform_2f("Scale", Scale.x, Scale.y);
  119.                 gl_uniform_2f("ScaleIn", ScaleIn.x, ScaleIn.y);
  120.                 gl_uniform_4f("HmdWarpParam", HmdWarpParam[0], HmdWarpParam[1], HmdWarpParam[2], HmdWarpParam[3]);
  121.                 gl_uniform_4f("ChromAbParam", ChromAbParam[0], ChromAbParam[1], ChromAbParam[2], ChromAbParam[3]);
  122.         }
  123.  
  124.         // render the quad on display.
  125.         switch(eye)
  126.         {
  127.         case OVR::Util::Render::StereoEye_Left:
  128.             glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer_eye_1);
  129.             glEnableVertexAttribArray(0);
  130.             glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  131.  
  132.             glBindBuffer(GL_ARRAY_BUFFER, m_uv_buffer_eye_1);
  133.             glEnableVertexAttribArray(1);
  134.             glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  135.  
  136.             glDrawArrays(GL_QUADS, 0, 4);
  137.  
  138.             glBindBuffer(GL_ARRAY_BUFFER, 0);
  139.             glEnableVertexAttribArray(0);
  140.             glEnableVertexAttribArray(1);
  141.             break;
  142.         case OVR::Util::Render::StereoEye_Right:
  143.             glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer_eye_2);
  144.             glEnableVertexAttribArray(0);
  145.             glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  146.  
  147.             glBindBuffer(GL_ARRAY_BUFFER, m_uv_buffer_eye_2);
  148.             glEnableVertexAttribArray(1);
  149.             glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  150.  
  151.             glDrawArrays(GL_QUADS, 0, 4);
  152.  
  153.             glBindBuffer(GL_ARRAY_BUFFER, 0);
  154.             glEnableVertexAttribArray(0);
  155.             glEnableVertexAttribArray(1);
  156.             break;
  157.         default:
  158.             break;
  159.         }
  160. }
RAW Paste Data