Guest User

Oculus SDK 0.4.3 C++ SFML OpenGL Example

a guest
Nov 11th, 2014
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 KB | None | 0 0
  1. #include <GL/glew.h>
  2.  
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5. #include <SFML/OpenGL.hpp>
  6. #include <SFML/Audio.hpp>
  7.  
  8. #include <iostream>
  9.  
  10. // Uncomment your platform
  11. #define OVR_OS_WIN32
  12. //#define OVR_OS_MAC
  13. //#define OVR_OS_LINUX
  14. #include "OVR_CAPI_GL.h"
  15. #include "Kernel/OVR_Math.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     ovr_Initialize();
  20.  
  21.     sf::Window testWindow;
  22.  
  23.     bool debug = false;
  24.    
  25.     ovrHmd hmd = ovrHmd_Create(0);
  26.  
  27.     int x = 1;
  28.     int y = 1;
  29.    
  30.     if (hmd == NULL)
  31.     {
  32.         hmd = ovrHmd_CreateDebug(ovrHmd_DK1);
  33.  
  34.         debug = true;
  35.     }
  36.  
  37.     if (debug == false)
  38.     {
  39.         x = hmd->WindowsPos.x;
  40.         y = hmd->WindowsPos.y;
  41.     }
  42.  
  43.     int w = hmd->Resolution.w;
  44.     int h = hmd->Resolution.h;
  45.  
  46.     testWindow.create(sf::VideoMode::getDesktopMode(), "TEST SFML DK2", sf::Style::None);
  47.    
  48.     OVR::Sizei recommendedTex0Size = ovrHmd_GetFovTextureSize(hmd, ovrEye_Left, hmd->DefaultEyeFov[0], 1.0f);
  49.     OVR::Sizei recommendedTex1Size = ovrHmd_GetFovTextureSize(hmd, ovrEye_Right, hmd->DefaultEyeFov[1], 1.0f);
  50.  
  51.     OVR::Sizei renderTargetSize;
  52.     renderTargetSize.w = recommendedTex0Size.w + recommendedTex1Size.w;
  53.  
  54.     renderTargetSize.h = recommendedTex0Size.h;
  55.     if( recommendedTex1Size.h > renderTargetSize.h)
  56.     {
  57.         renderTargetSize.h = recommendedTex1Size.h;
  58.     }
  59.  
  60.     glewInit();
  61.  
  62.     GLuint frameBuffer;
  63.     glGenFramebuffers(1, &frameBuffer);
  64.  
  65.     GLuint texture;
  66.     glGenTextures(1, &texture);
  67.  
  68.     GLuint renderBuffer;
  69.     glGenRenderbuffers(1, &renderBuffer);
  70.  
  71.     glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  72.     glBindTexture(GL_TEXTURE_2D, texture);
  73.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, renderTargetSize.w, renderTargetSize.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  74.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  75.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  76.     glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0);
  77.  
  78.     glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer);
  79.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, renderTargetSize.w, renderTargetSize.h);
  80.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
  81.  
  82.     ovrFovPort eyeFov[2] = { hmd->DefaultEyeFov[0], hmd->DefaultEyeFov[1] };
  83.  
  84.     ovrRecti eyeRenderViewport[2];
  85.     eyeRenderViewport[0].Pos = OVR::Vector2i(0, 0);
  86.     eyeRenderViewport[0].Size = OVR::Sizei(renderTargetSize.w / 2, renderTargetSize.h);
  87.     eyeRenderViewport[1].Pos = OVR::Vector2i((renderTargetSize.w + 1) / 2, 0);
  88.     eyeRenderViewport[1].Size = eyeRenderViewport[0].Size;
  89.  
  90.     ovrGLTexture eyeTexture[2];
  91.     eyeTexture[0].OGL.Header.API = ovrRenderAPI_OpenGL;
  92.     eyeTexture[0].OGL.Header.TextureSize = renderTargetSize;
  93.     eyeTexture[0].OGL.Header.RenderViewport = eyeRenderViewport[0];
  94.     eyeTexture[0].OGL.TexId = texture;
  95.  
  96.     eyeTexture[1] = eyeTexture[0];
  97.     eyeTexture[1].OGL.Header.RenderViewport = eyeRenderViewport[1];
  98.  
  99.     ovrGLConfig cfg;
  100.     cfg.OGL.Header.API = ovrRenderAPI_OpenGL;
  101.     cfg.OGL.Header.RTSize = OVR::Sizei(hmd->Resolution.w, hmd->Resolution.h);
  102.     cfg.OGL.Header.Multisample = 1;
  103.  
  104. #if defined(OVR_OS_WIN32)
  105.     if (!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop))
  106.         ovrHmd_AttachToWindow(hmd, testWindow.getSystemHandle(), NULL, NULL);
  107.  
  108.     cfg.OGL.Window = testWindow.getSystemHandle();
  109.     cfg.OGL.DC = NULL;
  110. #elif defined(OVR_OS_LINUX)
  111.     cfg.OGL.Disp = info.info.x11.display;
  112.     cfg.OGL.Win = info.info.x11.window;
  113. #endif
  114.  
  115.     ovrEyeRenderDesc eyeRenderDesc[2];
  116.  
  117.     ovrHmd_ConfigureRendering(hmd, &cfg.Config, ovrDistortionCap_Chromatic | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp | ovrDistortionCap_Overdrive, eyeFov, eyeRenderDesc);
  118.    
  119.     ovrHmd_SetEnabledCaps(hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction);
  120.  
  121.     ovrHmd_ConfigureTracking(hmd, ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position, 0);
  122.  
  123.     const GLchar *vertexShaderSource[] = {
  124.         "#version 150\n"
  125.         "uniform mat4 MVPMatrix;\n"
  126.         "in vec3 position;\n"
  127.         "void main()\n"
  128.         "{\n"
  129.         "    gl_Position = MVPMatrix * vec4(position, 1.0);\n"
  130.         "}"
  131.     };
  132.  
  133.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  134.     glShaderSource(vertexShader, 1, vertexShaderSource, NULL);
  135.     glCompileShader(vertexShader);
  136.  
  137.     const GLchar *fragmentShaderSource[] = {
  138.         "#version 150\n"
  139.         "out vec4 outputColor;\n"
  140.         "void main()\n"
  141.         "{\n"
  142.         "    outputColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
  143.         "}"
  144.     };
  145.  
  146.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  147.     glShaderSource(fragmentShader, 1, fragmentShaderSource, NULL);
  148.     glCompileShader(fragmentShader);
  149.  
  150.     GLuint program = glCreateProgram();
  151.     glAttachShader(program, vertexShader);
  152.     glAttachShader(program, fragmentShader);
  153.     glLinkProgram(program);
  154.     glUseProgram(program);
  155.  
  156.     GLuint MVPMatrixLocation = glGetUniformLocation(program, "MVPMatrix");
  157.     GLuint positionLocation = glGetAttribLocation(program, "position");
  158.  
  159.     GLuint vertexArray;
  160.     glGenVertexArrays(1, &vertexArray);
  161.     glBindVertexArray(vertexArray);
  162.  
  163.     GLfloat vertices[] = {
  164.         0.0f, 1.0f, -2.0f,
  165.         -1.0f, -1.0f, -2.0f,
  166.         1.0f, -1.0f, -2.0f
  167.     };
  168.  
  169.     GLuint positionBuffer;
  170.     glGenBuffers(1, &positionBuffer);
  171.     glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
  172.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  173.     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
  174.     glEnableVertexAttribArray(positionLocation);
  175.  
  176.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  177.     glClearDepth(1.0f);
  178.  
  179.     glDepthFunc(GL_LEQUAL);
  180.     glEnable(GL_DEPTH_TEST);
  181.  
  182.     //----------------------------------------------------------------------------------------------------------------------------------------------
  183.    
  184.     ovrHmd_DismissHSWDisplay(hmd);
  185.  
  186.     bool running = true;
  187.  
  188.     while (running == true)
  189.     {
  190.         ovrFrameTiming frameTiming = ovrHmd_BeginFrame(hmd, 0);
  191.  
  192.         glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  193.  
  194.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  195.  
  196.         ovrPosef eyeRenderPose[2];
  197.  
  198.         for (int eyeIndex = 0; eyeIndex < ovrEye_Count; eyeIndex++)
  199.         {
  200.             ovrEyeType eye = hmd->EyeRenderOrder[eyeIndex];
  201.             ovrVector3f hmdToEyeViewOffset[2];
  202.             hmdToEyeViewOffset[0] = eyeRenderDesc[0].HmdToEyeViewOffset;
  203.             hmdToEyeViewOffset[1] = eyeRenderDesc[1].HmdToEyeViewOffset;
  204.             ovrHmd_GetEyePoses(hmd, 0, hmdToEyeViewOffset, eyeRenderPose, NULL);
  205.  
  206.             OVR::Matrix4f MVPMatrix = OVR::Matrix4f(ovrMatrix4f_Projection(eyeRenderDesc[eye].Fov, 0.01f, 10000.0f, true)) * OVR::Matrix4f::Translation(eyeRenderDesc[eye].HmdToEyeViewOffset) * OVR::Matrix4f(OVR::Quatf(eyeRenderPose[eye].Orientation).Inverted());
  207.  
  208.             glUniformMatrix4fv(MVPMatrixLocation, 1, GL_FALSE, &MVPMatrix.Transposed().M[0][0]);
  209.  
  210.             glViewport(eyeRenderViewport[eye].Pos.x, eyeRenderViewport[eye].Pos.y, eyeRenderViewport[eye].Size.w, eyeRenderViewport[eye].Size.h);
  211.  
  212.             glBindVertexArray(vertexArray);
  213.             glDrawArrays(GL_TRIANGLES, 0, 3);
  214.             glBindVertexArray(0);
  215.         }
  216.  
  217.         ovrHmd_EndFrame(hmd, eyeRenderPose, &eyeTexture[0].Texture);
  218.     }
  219.  
  220.     glDeleteVertexArrays(1, &vertexArray);
  221.     glDeleteBuffers(1, &positionBuffer);
  222.  
  223.     glDeleteShader(vertexShader);
  224.     glDeleteShader(fragmentShader);
  225.     glDeleteProgram(program);
  226.  
  227.     glDeleteFramebuffers(1, &frameBuffer);
  228.     glDeleteTextures(1, &texture);
  229.     glDeleteRenderbuffers(1, &renderBuffer);
  230.  
  231.     testWindow.close();
  232.  
  233.     ovrHmd_Destroy(hmd);
  234.  
  235.     ovr_Shutdown();
  236.  
  237.     return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment