本文介绍了glCreateShader崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的程序:

 的#include< GL \\ glew.h>
#包括LT&; GL \\ glut.h>
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;GLuint方案;静态的char * readShaderSource(为const char * shaderFile)
{
    FILE * FP = FOPEN(shaderFile,R);
    字符* BUF;
    长尺寸;    如果(FP == NULL)返回NULL;
    fseek的(FP,0L,SEEK_END); //去结束
    大小= FTELL(FP); //获取大小
    fseek的(FP,0L,SEEK_SET); //去开始时    BUF =(字符*)malloc的((大小+1)* sizeof的(炭));
    的fread(BUF,1,尺寸,FP);
    BUF [大小] = NULL;
    FCLOSE(FP);
    返回BUF;
}静态无效initShader(常量GLchar * fsFile)
{
    闪烁状态;
    GLchar * FSOURCE;
    GLuint fShader;
    GLuint fShader2;    //读取文件
    FSOURCE = readShaderSource(fsFile);
    如果(FSOURCE == NULL)
    {
        的printf(无法加载文件);
        出口(EXIT_FAILURE);
    }    //创建计划和Shader对象
    fShader2 = glCreateShader(GL_VERTEX_SHADER);
    fShader = glCreateShader(GL_FRAGMENT_SHADER);
    程序= glCreateProgram();    //附加着色器程序
    glAttachShader(程序,fShader);    //读取着色器
    glShaderSource(fShader,1,(常量GLchar **)及FSOURCE,NULL);    //编译片段着色器
    glCompileShader(fShader);    //错误检查
    glGetShaderiv(fShader,GL_COMPILE_STATUS,&安培;状态);
    如果(状态== GL_FALSE)
    {
        的printf(无法编译片段着色器。);
        出口(EXIT_FAILURE);
    }    //链接和错误检查
    glLinkProgram(程序);
    glGetProgramiv(程序,GL_LINK_STATUS,&安培;状态);
    如果(状态== GL_FALSE)
    {
        的printf(程序错误);
        出口(EXIT_FAILURE);
    }    //使用程序对象
    glUseProgram(程序);    //设置统一的参数
    //跳过现在
}INT主(INT ARGC,字符** argv的)
{
    glutInit(安培; ARGC,ARGV);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutCreateWindow(黑客帝国分形);
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0.0,0.0,(GLfloat)500,(GLfloat)500);    glutDisplayFunc(平局);
    glutReshapeFunc(重塑);    initShader(fsFractal.glsl);    glutMainLoop();
}


解决方案

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

GLenum ERR = glewInit();

I should have the newest version of Glew and Glut so that shouldn't be the problem. Everything should be linked, and I'm using MS visual studio 2010.My program compiles but when I get to glCreateShader(GL_FRAGMENT_SHADER) it show an error: "0xC0000005: Access violation."

my program:

#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();
}
解决方案

You have to initialize GLEW before you can use it:

GLenum err = glewInit();

这篇关于glCreateShader崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:26