gpt4 book ai didi

c - glCreateShader 正在崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:29:07 26 4
gpt4 key购买 nike

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

我的程序:

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

最佳答案

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

GLenum err = glewInit();

关于c - glCreateShader 正在崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12329082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com