当我尝试使用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。其他我不知道。

最佳答案

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

10-06 13:36