Guest User

Oculus Rift SDL2 OpenGL Demo

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