Add context creation helper function
This commit is contained in:
@@ -3,7 +3,7 @@ set(OpenGL_GL_PREFERENCE GLVND)
|
|||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(OpenHMD REQUIRED)
|
find_package(OpenHMD REQUIRED)
|
||||||
|
|
||||||
add_executable(openglexample gl.c gl.h main.c vr_context.c vr_context.h)
|
add_executable(openglexample gl.c gl.h main.c vr_context.c vr_context.h context.inl)
|
||||||
target_include_directories(openglexample PRIVATE ${OPENGL_INCLUDE_DIRS})
|
target_include_directories(openglexample PRIVATE ${OPENGL_INCLUDE_DIRS})
|
||||||
target_link_libraries(openglexample PRIVATE ${OPENGL_LIBRARIES})
|
target_link_libraries(openglexample PRIVATE ${OPENGL_LIBRARIES})
|
||||||
target_link_libraries(openglexample PRIVATE m)
|
target_link_libraries(openglexample PRIVATE m)
|
||||||
|
|||||||
69
examples/opengl/context.inl
Normal file
69
examples/opengl/context.inl
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <EGL/egl.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
static inline bool create_context(EGLNativeDisplayType nativeDisplay, EGLNativeWindowType nativeWindow, const EGLint * config_attribs, const EGLint * context_attribs, const EGLint * surface_attribs, EGLDisplay * outDisplay, EGLContext * outContext, EGLSurface * outSurface)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// get an EGL display connection
|
||||||
|
EGLDisplay display = eglGetDisplay(nativeDisplay);
|
||||||
|
|
||||||
|
if (EGL_NO_DISPLAY == display)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// initialize the EGL display connection
|
||||||
|
EGLint major, minor;
|
||||||
|
|
||||||
|
if (!eglInitialize(display, &major, &minor))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// bind OpenGL API
|
||||||
|
eglBindAPI(EGL_OPENGL_API);
|
||||||
|
|
||||||
|
//
|
||||||
|
// get an appropriate EGL frame buffer configuration
|
||||||
|
EGLint num_config;
|
||||||
|
EGLConfig configs[128];
|
||||||
|
|
||||||
|
if (!eglChooseConfig(display, config_attribs, configs, 128, &num_config))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
EGLConfig config = configs[0]; // FIXME
|
||||||
|
|
||||||
|
//
|
||||||
|
// create context
|
||||||
|
EGLContext context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
|
||||||
|
|
||||||
|
if (EGL_NO_CONTEXT == context)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Create Surface
|
||||||
|
EGLSurface surface = eglCreateWindowSurface(display, config, nativeWindow, surface_attribs);
|
||||||
|
if (EGL_NO_SURFACE == surface)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Set current context
|
||||||
|
if (!eglMakeCurrent(display, surface, surface, context))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
*outDisplay = display;
|
||||||
|
*outContext = context;
|
||||||
|
*outSurface = surface;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user