Skip to content

Instantly share code, notes, and snippets.

@dghost
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dghost/8ea97e3d4f55590d75e0 to your computer and use it in GitHub Desktop.
Save dghost/8ea97e3d4f55590d75e0 to your computer and use it in GitHub Desktop.
SDL2 / Rift Direct HMD / GL Context Troubles
#define GLEW_STATIC
#include "GL/glew.h"
// Uncomment your platform
#define OVR_OS_WIN32
//#define OVR_OS_MAC
//#define OVR_OS_LINUX
#include "OVR_CAPI_GL.h"
#include "Kernel/OVR_Math.h"
#include "SDL.h"
#include "SDL_syswm.h"
using namespace OVR;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
int x = SDL_WINDOWPOS_CENTERED;
int y = SDL_WINDOWPOS_CENTERED;
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
bool debug = false;
ovr_Initialize();
ovrHmd hmd = ovrHmd_Create(0);
if (hmd == NULL)
{
hmd = ovrHmd_CreateDebug(ovrHmd_DK1);
debug = true;
}
if (debug == false)
{
x = hmd->WindowsPos.x;
y = hmd->WindowsPos.y;
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
int w = hmd->Resolution.w;
int h = hmd->Resolution.h;
SDL_Window *window = SDL_CreateWindow("Oculus Rift SDL2 OpenGL Demo", x, y, w, h, flags);
SDL_GLContext context = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
glewInit();
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
SDL_GetWindowWMInfo(window, &info);
#if defined(OVR_OS_WIN32)
if (!(hmd->HmdCaps & ovrHmdCap_ExtendDesktop))
ovrHmd_AttachToWindow(hmd, info.info.win.window, NULL, NULL);
#endif
glClearColor(1.0,1.0,1.0,1.0);
bool running = true;
while (running == true)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
default:
break;
}
break;
default:
break;
}
}
// ovrFrameTiming frameTiming = ovrHmd_BeginFrameTiming(hmd, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// ovrHmd_EndFrameTiming(hmd);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
ovrHmd_Destroy(hmd);
ovr_Shutdown();
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment