根据SDL2文档,您应该始终询问在加载OpenGL扩展时是否支持该扩展。参见:https://wiki.libsdl.org/SDL_GL_GetProcAddress
我正在使用KUbuntu 16.04,但还希望支持其他平台,
当前,我总是获得指向扩展的有效指针,但是SDL_GL_ExtensionSupported总是表明不支持该扩展。
我使用CMake通过FindPackage(OpenGL REQUIRED)
查找opengl库,然后通过链接到它们。
target_link_libraries(MyTarget ${OPENGL_gl_LIBRARIES})
如果我问glewinfo:
$glewinfo | grep -i glcompileshader
glCompileShader: OK
glCompileShaderARB: OK
glCompileShaderIncludeARB: OK
我要打开一个带有以下内容的窗口:
SDL_Window* window;
SDL_GLContext context;
int flags = SDL_WINDOW_RESIZABLE|SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN;
window = SDL_CreateWindow("name", x, y, z, flags);
context = SDL_GL_CreateContext(window);
然后我打电话给allocate_glextension_for_context(context);
从下面的片段中。
// casting function pointers generates pedantic errors
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define LOAD_AND_CHECK_EXTENSION(name) \
ext->name = (ext_##name) SDL_GL_GetProcAddress(#name); \
if (ext->name != NULL) { \
if (SDL_GL_ExtensionSupported(#name) != SDL_TRUE) { \
ext->name = NULL; \
failed_to_load = 1; \
fprintf(stderr, "failed to load %s\n", #name); \
} \
} \
/**
* \brief This function loads the extensions for a given context.
*
* @param [in] The SDL_GLContext that should be active.
* @param extensions The set of extensions.
* @return PSY_GL_LOAD_OK if everything is alright.
*/
static int
load_extentions(GLExtensions* ext)
{
int failed_to_load = 0;
// Here below we load functions from the opengl library via SDL2
// via a macro, this is roughly what it does.
ext->glShaderSource = SDL_GL_GetProcAddress("glShaderSource");
if (ext->glShaderSource != NULL) {
if (SDL_GL_ExtensionSupported("glShaderSource") != SDL_TRUE) {
ext->glShaderSource = NULL;
failed_to_load = 1;
}
}
// Is already done here above.
//LOAD_AND_CHECK_EXTENSION(glShaderSource);
LOAD_AND_CHECK_EXTENSION(glCompileShader);
LOAD_AND_CHECK_EXTENSION(glGetShaderiv);
LOAD_AND_CHECK_EXTENSION(glGetShaderInfoLog);
if (failed_to_load)
return PSY_GL_LOAD_EXTENSION_MISSING;
else
return PSY_GL_LOAD_OK;
}
//reenable all warnings
#pragma GCC diagnostic pop
int
allocate_glextension_for_context(SDL_GLContext context)
{
ExtensionPair pair = {
.context = context,
.extensions = {0}
};
int ret;
if (context != SDL_GL_GetCurrentContext())
return PSY_GL_LOAD_CONTEXT_NOT_CURRENT;
if (g_array) {
for (size_t i = 0; i < see_dynamic_array_size(g_array); i++) {
ExtensionPair* pair = see_dynamic_array_get(g_array, i);
if (pair->context == context)
return PSY_GL_LOAD_CONTEXT_EXISTS;
}
}
else {
ret = see_dynamic_array_new(
&g_array,
sizeof (ExtensionPair),
NULL,
NULL,
NULL
);
if (ret != SEE_SUCCESS)
return PSY_GL_LOAD_NO_MEMORY;
}
ret = load_extentions(&pair.extensions);
if (see_dynamic_array_add(g_array, &pair) != SEE_SUCCESS)
return PSY_GL_LOAD_NO_MEMORY;
return ret;
}
如果我运行代码,我总是会得到许多其他信息:
无法加载glCompileShader
指示SDL_ExtensionSupported("glCompileShader")
返回SDL_False ...
但是,上面我显示了glew表示所有扩展都受支持。我在做什么错了?
答案 0 :(得分:3)
没有称为“ glShaderSource
”的扩展名。
GL extension strings generally look something像这样:GL_EXT_framebuffer_blit
glShaderSourceARB
由ARB_shader_objects
添加,但自OpenGL 2.0以来,它一直是核心功能。
除非您要针对2.0之前的GL实施,并实际计划支持ARB入口点,否则我建议改为进行GL版本检查。