diff --git a/examples/opengl/vr_context.c b/examples/opengl/vr_context.c index 81e4911..a5571c0 100644 --- a/examples/opengl/vr_context.c +++ b/examples/opengl/vr_context.c @@ -10,114 +10,29 @@ #include +#include "context.inl" + static EGLDisplay display; static EGLSurface surface; static EGLContext context; -static void create_vr_context(struct lvrcInstance * compositor) +static const EGLint attribs[] = { - static const EGLint attribs[] = - { - EGL_RED_SIZE, 8, - EGL_GREEN_SIZE, 8, - EGL_BLUE_SIZE, 8, - //EGL_ALPHA_SIZE, 8, - //EGL_DEPTH_SIZE, 16, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_NONE - }; + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + //EGL_ALPHA_SIZE, 8, + //EGL_DEPTH_SIZE, 16, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_NONE +}; - static const EGLint context_attributes[] = - { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - // - // Get Native Display - EGLNativeDisplayType nativeDisplay = lvrcSwapChainGetNativeDisplay(compositor); - - if (!nativeDisplay) - { - fprintf(stderr, "invalid native display\n"); - exit(-1); - } - - // - // get an EGL display connection - display = eglGetDisplay(nativeDisplay); - - if (EGL_NO_DISPLAY == display) - { - fprintf(stderr, "eglGetDisplay() failed\n"); - exit(-1); - } - - // - // initialize the EGL display connection - EGLint major, minor; - - if (!eglInitialize(display, &major, &minor)) - { - fprintf(stderr, "eglInitialize() failed\n"); - exit(-1); - } - - // - // bind OpenGL API - eglBindAPI(EGL_OPENGL_API); - - // - // get an appropriate EGL frame buffer configuration - EGLint num_config; - EGLConfig configs[128]; - - if (!eglChooseConfig(display, attribs, configs, 128, &num_config)) - { - fprintf(stderr, "eglChooseConfig() failed\n"); - exit(-1); - } - - EGLConfig config = configs[0]; // FIXME - - // - // create context - context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes); - - if (EGL_NO_CONTEXT == context) - { - fprintf(stderr, "failed to create context\n"); - exit(-1); - } - - // - // Get Native Window - EGLNativeWindowType nativeWindow = lvrcSwapChainGetNativeWindow(compositor); - - if (!nativeWindow) - { - fprintf(stderr, "invalid native window\n"); - exit(-1); - } - - // - // Create Surface - surface = eglCreateWindowSurface(display, config, nativeWindow, NULL); - if (EGL_NO_SURFACE == surface) - { - fprintf(stderr, "eglCreateWindowSurface() failed\n"); - exit(-1); - } - - // - // Set current context - if (!eglMakeCurrent(display, surface, surface, context)) - { - fprintf(stderr, "failed to make context current\n"); - exit(-1); - } -} +static const EGLint context_attributes[] = +{ + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE +}; GLuint left_color_tex = 0; GLuint right_color_tex = 0; @@ -132,7 +47,33 @@ void init_vr_context(struct lvrcInstance * compositor, dma_texture_params * left exit(-1); } - create_vr_context(compositor); + // + // Get Native Display + EGLNativeDisplayType nativeDisplay = lvrcSwapChainGetNativeDisplay(compositor); + + if (!nativeDisplay) + { + fprintf(stderr, "invalid native display\n"); + exit(-1); + } + + // + // Get Native Window + EGLNativeWindowType nativeWindow = lvrcSwapChainGetNativeWindow(compositor); + + if (!nativeWindow) + { + fprintf(stderr, "invalid native window\n"); + exit(-1); + } + + bool bContextCreated = create_context(nativeDisplay, nativeWindow, attribs, context_attributes, NULL, &display, &context, &surface); + + if (!bContextCreated) + { + fprintf(stderr, "couldn't create compositor context\n"); + exit(-1); + } PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) eglGetProcAddress ("glEGLImageTargetTexture2DOES");