glCreateShader崩溃(glCreateShader is crashing)

2019-06-28 01:17发布

我应该有GLEW的最新版本和转运蛋白,使不应该成为问题。 一切都应该联系在一起的,我使用MS Visual Studio 2010中我的程序编译,但是当我到达glCreateShader(GL_FRAGMENT_SHADER)它显示一个错误:“0000005:访问冲突”

我的程序:

#include <GL\glew.h>
#include <GL\glut.h>
#include <stdio.h>
#include <stdlib.h>

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();
}

Answer 1:

你必须初始化GLEW之前,你可以使用它:

GLenum err = glewInit();



Answer 2:

还有的时候会发生这种情况,条件还远远没有明显的另一种情况。 如果你决定使用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()消失了。

不要问我,怎么两个库GLEW和GLFW互相...巫术警报扰!



Answer 3:

如果您使用GLFW GLEW / GLXW,得到一个访问冲突的地址0可能发生,如果你想创建具有GLFW一个有效的OpenGL上下文之前初始化GLEW / GLXW:

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()上下文创建之前将拿起任何默认情况下被设置,并且可以触发访问冲突(可能需要在运行时被拾起)。



Answer 4:

这里是我的变体,它是从@以上BenRujil的回答跟进。

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


文章来源: glCreateShader is crashing