glCreateShader正在崩溃

我应该有Glew和Glut的最新版本,所以这应该不是问题。 一切都应该链接,我正在使用MS visual studio 2010.我的程序编译但是当我到glCreateShader(GL_FRAGMENT_SHADER)它显示错误:“0xC0000005:访问冲突。”

我的节目:

#include  #include  #include  #include  GLuint program; static char* readShaderSource(const char * shaderFile) { FILE* fp = fopen(shaderFile, "r"); char* buf; long size; if (fp == NULL) return NULL; fseek(fp, 0L, SEEK_END);//go to end size = ftell(fp); //get size fseek(fp, 0L, SEEK_SET);//go to begining buf = (char*) malloc((size +1) * sizeof(char)); fread(buf, 1, size, fp); buf[size] = NULL; fclose(fp); return buf; } static void initShader(const GLchar * fsFile) { GLint status; GLchar * fSource; GLuint fShader; GLuint fShader2; //read file fSource = readShaderSource(fsFile); if (fSource == NULL) { printf("Fail to load file"); exit(EXIT_FAILURE); } //Create program and shader object fShader2 = glCreateShader(GL_VERTEX_SHADER); fShader = glCreateShader(GL_FRAGMENT_SHADER); program = glCreateProgram(); //Attach shaders to program glAttachShader(program, fShader); //read shaders glShaderSource(fShader, 1, (const GLchar**) &fSource, NULL); //compile fragment shader glCompileShader(fShader); //error check glGetShaderiv(fShader, GL_COMPILE_STATUS, &status); if (status == GL_FALSE) { printf("Failed to compile the fragment shader."); exit(EXIT_FAILURE); } //link and error check glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &status); if (status == GL_FALSE) { printf("program error"); exit(EXIT_FAILURE); } //use program object glUseProgram(program); //set up uniform parameter //skipped for now } int main(int argc, char ** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutCreateWindow("Matrix Fractal"); glClearColor(1.0, 1.0, 1.0, 1.0); gluOrtho2D(0.0,0.0,(GLfloat) 500, (GLfloat) 500); glutDisplayFunc(draw); glutReshapeFunc(reshape); initShader("fsFractal.glsl"); glutMainLoop(); } 

您必须先初始化GLEW才能使用它:

GLenum err = glewInit();

还有另一种情况,这种情况可能发生,而且情况远非明显。 如果你决定在你的应用程序中使用glfw和glew,你也可以在glCreateShader()ACCESS_VIOLATION中结束,如果你写的:

 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 

如果您将此行更改为

 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); 

由于NULL函数指针glCreateShader()导致的ACCESS_VIOLATION消失了。

不要问我,这两个图书馆如何互相干扰……伏都教警报!

如果您正在使用GLFW GLEW / GLXW,如果您在使用GLFW创建有效的openGL上下文之前尝试初始化GLEW / GLXW 则可能会发生地址0的访问冲突:

 if (!glfwInit()) { std::cerr << "GL initialization failed" << std::endl; return 1; } // Setup the openGL profile you need - we're going with a 4.3 core profile glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Context creation happens in the line below GLFWwindow *window = glfwCreateWindow(800, 600, "text", NULL, NULL); if (!window) { std::cerr << "Window or GL initialization failed"; glfwTerminate(); return 1; } glfwMakeContextCurrent(window); if (glxwInit()) { // Now it's a good time to initialize Xtension wranglers std::cerr << "Failed to initialize GLXW" << std::endl; return 1; } 

在创建上下文之前调用glxwInit()将获取设置的任何默认上下文,并且可以触发访问冲突(可能需要在运行时获取)。

这是我的变体,它是@ BenRujil上面回答的后续内容。

  glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) throw std::runtime_error("glewInit failed");