Created
July 13, 2020 02:14
-
-
Save ChristophHaag/474c77f48fbddcb2b3ea0c67bf578d8f to your computer and use it in GitHub Desktop.
openvr sdl opengl overlay example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ overlay.cpp -o overlay $(pkg-config --cflags --libs gl sdl2) -lopenvr_api | |
#define GL_GLEXT_PROTOTYPES 1 | |
#define GL3_PROTOTYPES 1 | |
#include <GL/gl.h> | |
#include <iostream> | |
#include <SDL2/SDL.h> | |
#include <openvr/openvr.h> | |
using namespace vr; | |
void check_error(int line, EVRInitError error) { if (error != 0) printf("%d: error %s\n", line, VR_GetVRInitErrorAsSymbol(error)); } | |
int main(int argc, char **argv) | |
{ | |
float ppm = 64.; | |
int width = 64; | |
int height = 64; | |
SDL_Window *window = SDL_CreateWindow("SDL2/OpenGL Demo", 0, 0, 640, 480, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); | |
SDL_GLContext glcontext = SDL_GL_CreateContext(window); | |
SDL_GL_SetSwapInterval(0); | |
EVRInitError error; | |
VR_Init(&error, vr::VRApplication_Overlay); | |
check_error(__LINE__, error); | |
uint8_t *gradientImage = (uint8_t*) malloc(sizeof(uint8_t) * width * height * 4); | |
for (int i = 0; i < width * height; i++) { | |
gradientImage[i * 4] = (float)i / width / width * 255.; | |
gradientImage[i * 4 + 1] = 0; | |
gradientImage[i * 4 + 2] = 0; | |
gradientImage[i * 4 + 3] = 255; | |
} | |
GLuint texture; | |
VROverlayHandle_t handle; | |
char key[100]; | |
snprintf(key, 100, "testoverlay-%d", 1); | |
VROverlay()->CreateOverlay (key, key, &handle); | |
VROverlay()->SetOverlayWidthInMeters(handle, width / ppm); | |
VROverlay()->ShowOverlay(handle); | |
HmdMatrix34_t m = {0}; | |
m.m[0][0] = 1; | |
m.m[1][1] = 1; | |
m.m[2][2] = 1; | |
m.m[0][3] = 0.; // x | |
m.m[1][3] = 1.5; // y | |
m.m[2][3] = -3.; // z | |
VROverlay()->SetOverlayTransformAbsolute(handle, ETrackingUniverseOrigin::TrackingUniverseStanding, &m); | |
glClearColor(0, 0, 0, 1); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glGenTextures(1, &texture); | |
glBindTexture(GL_TEXTURE_2D, texture); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)gradientImage); | |
glFinish(); | |
Texture_t tex; | |
tex.handle = (void*)(uintptr_t) texture; | |
tex.eColorSpace = ColorSpace_Linear; | |
tex.eType = TextureType_OpenGL; | |
VROverlay()->SetOverlayTexture(handle, &tex); | |
while (true) { | |
SDL_GL_SwapWindow(window); | |
SDL_Delay(1. / 60. * 1000.); // doesn't matter that it's not actually 60 fps | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) { | |
goto quit; | |
} | |
} | |
} | |
quit: | |
VROverlay()->DestroyOverlay(handle); | |
glDeleteTextures(1, &texture); | |
SDL_Delay(100); | |
free (gradientImage); | |
SDL_GL_DeleteContext(glcontext); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment