本文介绍了为什么当使用OpenGL核心配置文件时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 当我尝试运行这个简单的OpenGL测试程序时,我得到一个分段故障。这只发生在我使用核心配置文件标志创建上下文。如果我使用兼容性配置文件标志,程序运行没有问题。 编辑:我查了函数指针 glGenVertexArrays 并返回 NULL 。如果 glfwCreateWindow 不返回 NULL 和 glGetString(GL_VERSION)确认,上下文是4.3版本, glewInit 收益 GLEW_OK 那么为什么 glGenVertexArrays = = NULL ? 我的操作系统是Windows 7 64位,我的GPU是一个Nvidia GTX 760与331.82 WHQL驱动程序。 代码: #include< GL / glew.h> #include< GLFW / glfw3.h> #include< stdlib.h> #include< stdio.h> #定义GLSL(SRC)的#Version 430 core\\\#src 无效key_callback(GLFWwindow *窗口,INT键,诠释扫描码,INT行动,INT MODS) {如果(键== GLFW_KEY_ESCAPE&放大器;&安培;行动== GLFW_PRESS) glfwSetWindowShouldClose(窗口,GL_TRUE); } GLuint create_program(为const char * vertex_source,为const char * fragment_source) { GLuint VS = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs,1,& vertex_source,NULL); glCompileShader(vs); unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs,1,& fragment_source,NULL); glCompileShader(fs); GLuint shader_program = glCreateProgram(); glAttachShader(shader_program,fs); glAttachShader(shader_program,vs); glLinkProgram(shader_program); return shader_program;在VEC3 vertex_position } 为const char * vertex_shader = GLSL(布局(位置= 0); 无效的主要() { gl_Position = vec4(vertex_position,1.0); } ); 为const char * fragment_shader = GLSL( OUT vec4 frag_color; 无效的主要() { frag_color = vec4(1.0, 0.0,0.0,1.0); } ); int main(int argc,char * argv []) { if(!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_RESIZABLE,GL_FALSE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,4); glfwWindowHint(GLFW_CONTEXT_VERSION_MIN OR,3); //如果我们设置为GLFW_OPENGL_PROFILE GLFW_OPENGL_CORE_PROFILE //而不是GLFW_OPENGL_COMPAT_PROFILE程序将在第98行 //内存设计缺陷,呼吁采取glGenVertexArrays glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); GLFWwindow * window = glfwCreateWindow(512,512,OpenGL,NULL,NULL); if(!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window,key_callback); glfwMakeContextCurrent(window); GLenum glewError = glewInit(); if(glewError!= GLEW_OK) { glfwTerminate(); exit(EXIT_FAILURE); } printf(OpenGL版本:%s\\\\\\,glGetString(GL_VERSION)); float position [] = { 1.0f,1.0f,0.0f, -1.0f,1.0f,0.0f, 1.0 f,-1.0f,0.0f, -1.0f,-1.0f,0.0f }; unsigned short indices [] = { 1,0,2, 3,1,2 } GLuint vao = 0; glGenVertexArrays(1,& vao); glBindVertexArray(vao); GLuint index_buffer = 0; GLuint vertex_buffer = 0; glGenBuffers(1,& index_buffer); glGenBuffers(1,& vertex_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,index_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),& indices,GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER,vertex_buffer); glBufferData(GL_ARRAY_BUFFER,sizeof(position),& position; GL_STATIC_DRAW); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0); glEnableVertexAttribArray(0); GLuint shader_program = create_program(vertex_shader,fragment_shader); glUseProgram(shader_program); while(!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_SHORT,NULL); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); } 解决方案 无效的枚举[1280] 从OpenGL,调用 glewInit()后最容易的解决办法是做。 > glewExperimental = GL_TRUE;在调用 glewInit()之前, $ b glewExperimental = GL_TRUE; GLenum glewError = glewInit(); if(glewError!= GLEW_OK) { glfwTerminate(); exit(EXIT_FAILURE); } 为什么?它与GLEW如何加载扩展,函数等默认情况下GLEW将设置一些功能等不支持,因此,你需要设置 实验驱动程序 GLEW从图形驱动程序获取支持的扩展的信息。然而,实验性或预发布驱动程序可能不会通过标准机制报告每个可用的扩展,在这种情况下GLEW将报告不支持。为了避免这种情况,可以在调用glewInit()之前将glewExperimental全局开关设置为GL_TRUE,这将确保所有具有有效入口点的扩展都被暴露。 来源 额外 始终记住检查OpenGL错误,他们通常会告诉您发生了什么问题和/或帮助您找到问题。 GLenum error = glGetError(); if(error!= GL_NO_ERROR) { std :: cout< OpenGL Error:<错误< std :: endl; } 您可以了解不同的错误在这里。 When I try to run this simple OpenGL test program I get a segmentation fault. This only happens when I create the context using the core profile flag. If I use the compatibility profile flag, the program runs without issue.Edit: I checked the pointer to the function glGenVertexArrays and it returned NULL. If glfwCreateWindow doesn't return NULL, and glGetString(GL_VERSION) confirms that the context is version 4.3 and glewInit returns GLEW_OK then why is glGenVertexArrays == NULL?My OS is Windows 7 64-bit and my GPU is a Nvidia GTX 760 with 331.82 WHQL driver.Code:#include <GL/glew.h>#include <GLFW/glfw3.h>#include <stdlib.h>#include <stdio.h>#define GLSL(src) "#version 430 core\n" #srcvoid key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){ if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE);}GLuint create_program(const char* vertex_source, const char* fragment_source){ GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vertex_source, NULL); glCompileShader(vs); unsigned int fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fragment_source, NULL); glCompileShader(fs); GLuint shader_program = glCreateProgram(); glAttachShader(shader_program, fs); glAttachShader(shader_program, vs); glLinkProgram(shader_program); return shader_program;}const char* vertex_shader = GLSL( layout(location = 0) in vec3 vertex_position; void main() { gl_Position = vec4(vertex_position, 1.0); });const char* fragment_shader = GLSL( out vec4 frag_color; void main() { frag_color = vec4(1.0, 0.0, 0.0, 1.0); });int main(int argc, char* argv[]){ if(!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //if we set GLFW_OPENGL_PROFILE to GLFW_OPENGL_CORE_PROFILE //instead of GLFW_OPENGL_COMPAT_PROFILE the program will //segfault at line 98, call to glGenVertexArrays glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(512, 512, "OpenGL", NULL, NULL); if(!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); GLenum glewError = glewInit(); if(glewError != GLEW_OK) { glfwTerminate(); exit(EXIT_FAILURE); } printf("OpenGL Version: %s\n\n", glGetString(GL_VERSION)); float position[] = { 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f }; unsigned short indices[] = { 1, 0, 2, 3, 1, 2 }; GLuint vao = 0; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint index_buffer = 0; GLuint vertex_buffer = 0; glGenBuffers(1, &index_buffer); glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(position), &position, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); GLuint shader_program = create_program(vertex_shader, fragment_shader); glUseProgram(shader_program); while(!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS);} 解决方案 You're actually getting an Invalid Enum [1280] from OpenGL, after you call glewInit() the easiest fix is to do.glewExperimental = GL_TRUE;Before you call glewInit() thereby.glewExperimental = GL_TRUE;GLenum glewError = glewInit();if (glewError != GLEW_OK){ glfwTerminate(); exit(EXIT_FAILURE);}Why? well it has to do with how GLEW loads extensions, functions, etc. by default GLEW will set some function, etc. as unsupported, thereby to get around that you need to set glewExperimental = GL_TRUE; or else it will generate an error like you where getting. Experimental Drivers GLEW obtains information on the supported extensions from the graphics driver. Experimental or pre-release drivers, however, might not report every available extension through the standard mechanism, in which case GLEW will report it unsupported. To circumvent this situation, the glewExperimental global switch can be turned on by setting it to GL_TRUE before calling glewInit(), which ensures that all extensions with valid entry points will be exposed.SourceExtraAlways remember to check for OpenGL Errors, they usually tell you what's wrong and/or help you find the problem.GLenum error = glGetError();if (error != GL_NO_ERROR){ std::cout << "OpenGL Error: " << error << std::endl;}You can read about the different errors here. 这篇关于为什么当使用OpenGL核心配置文件时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 03:05