当我尝试使用glCreateShader创建着色器并运行我的程序时,我刚制作的窗口变为白色并给我一个错误:
OpenGl.exe中的0x03D312F0(atioglxx.dll)抛出异常:0xC0000005:访问冲突读取位置0x73553A43。
有谁知道,为什么会这样? 这是我的代码
#include <GL/glew.h>
#include <GLFW\glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float speed = 0.005;
float edge = 3.0;
float move = 0.0;
float zamik = 0.1;
GLFWwindow *window;
// initialize GLFW
if (!glfwInit())
{
return -1;
}
// create a window mode and its OpenGl context
window = glfwCreateWindow(640, 480, "Window", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
//make the windows contex current
glfwMakeContextCurrent(window);
glewInit();
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
GLfloat verts[] =
{
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
/*GLfloat color[] =
{
255, 0, 0,
100, 255, 0,
0, 0, 255,
255, 255, 255
};*/
GLuint vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, "C:\Users\Ghost.corp\Documents\Visual Studio 2015\Projects\OpenGl\OpenGl\VertexShader.shdVertx", NULL);
//glCompileShader(vertexShader);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_PROFILE, 0);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_SAMPLES, 4);
//loop
while (!glfwWindowShouldClose(window))
{
//clears our screen
glClear(GL_COLOR_BUFFER_BIT);
//render opengl content
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
//glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
//sweap front and back buffers
glfwSwapBuffers(window);
glfwPollEvents();
//glColorPointer(4, GL_FLOAT, 0, color);
}
glfwTerminate();
return -1;
}
如果我传递参数错误,请专注于glCreateShader。否则,我不知道。
答案 0 :(得分:5)
glCreateShader
看起来不错。但是对于下一行,您应该首先阅读着色器文件。 glShaderSource
期望着色器内容,而不是路径。一个例子是:
char *vs_source =
"attribute vec3 pos;\n"
"void main() {\n"
" gl_Position = vec4(pos,1.0);\n"
"}\n";
glShaderSource(vertexShader, 1, (const GLchar**)&vs_source, NULL);
因此,首先读取文件并将内容放入字符串中,然后将其传递给此函数。
在C中加载文件有点令人困惑,所以你需要一个像这样的函数:
int load_file(const char *filename, char **result)
{
int size = 0;
FILE *f = fopen(filename, "rb");
if (f == NULL)
{
*result = NULL;
return -1; // -1 means file opening fail
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
*result = (char *)malloc(size+1);
if (size != fread(*result, sizeof(char), size, f))
{
free(*result);
return -2; // -2 means file reading fail
}
fclose(f);
(*result)[size] = 0;
return size;
}
然后,您可以这样使用它:
char *vs_source;
int size;
size = load_file("C:\\Users\\Ghost.corp\\Documents\\Visual Studio 2015\\Projects\\OpenGl\\OpenGl\\VertexShader.shdVertx", &vs_source);
glShaderSource(vertexShader, 1, (const GLchar**)&vs_source, NULL);
您可以检查返回的size
,看看加载文件没有问题。此外,当您使用C时,您需要记住释放创建的新内存。如果你没有释放它,你就会有内存泄漏。
<强>资源:强>
加载文件功能取自here。