OpenGL访问冲突

时间:2016-01-17 02:21:28

标签: visual-studio opengl d

我正在尝试在D中编写一个简单的OpenGL程序。我正在使用DerelictGL3和DerelictGLFW3进行绑定和上下文创建。当我尝试运行我编译的程序时,出现访问冲突错误。不幸的是,Visual Studio D扩展Visual D正在使调试变得困难。当我尝试调试异常时,Visual Studio只给了我一个不在模块中的框架'错误。对于一些打印语句,我已经缩小程序可以循环一次通过绘制循环,但是在调用glDisableVertexAttribArray(0);之后第二个循环通过它崩溃。我见过其他人用C ++和GLEW得到了这个错误,但我似乎无法找到任何与GLEW相关的东西。这是我的所有代码(它没有太多的错误检查)

import std.stdio;

import derelict.util.exception;
import derelict.glfw3.glfw3;
import derelict.opengl3.gl3;

GLuint compileShaders(const char* vertSource, const char* fragSource) {
    GLuint vertID = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragID = glCreateShader(GL_FRAGMENT_SHADER);

    GLint result = GL_FALSE;
    //int InfoLogLength;

    writeln("Compiling vertex shader...");
    glShaderSource(vertID, 1, &vertSource, cast(const(int)*)null);
    glCompileShader(vertID);

    glGetShaderiv(vertID, GL_COMPILE_STATUS, &result);
    if (result != GL_TRUE) {
        throw new Exception("Could not compile vertex shader - errors detected.");
        //put err log here
    }

    writeln("Compiling fragment shader...");
    glShaderSource(fragID, 1, &fragSource, cast(const(int)*)null);
    glCompileShader(fragID);

    glGetShaderiv(fragID, GL_COMPILE_STATUS, &result);
    if (result != GL_TRUE) {
        throw new Exception("Could not compile fragment shader - errors detected.");
        //put err log here
    }

    writeln("Linking shaders...");
    GLuint progID = glCreateProgram();
    glAttachShader(progID,vertID);
    glAttachShader(progID,fragID);
    glLinkProgram(progID);

    glGetProgramiv(progID, GL_LINK_STATUS, &result);
    if (result != GL_TRUE) {
        throw new Exception("Could not link shaders - errors detected.");
        //put err log here
    }

    glDetachShader(progID, vertID);
    glDetachShader(progID, fragID);

    glDeleteShader(vertID);
    glDeleteShader(fragID);

    return progID;
}

void main()
{
    writeln("Loading libraries...");
    DerelictGLFW3.load();
    DerelictGL3.load();
    writeln("Loaded Derelict libs - no errors detected.");

    writeln("Creating window and context...");
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    GLFWwindow* window = glfwCreateWindow(1280, 720, "particledemo", null, null);
    glfwMakeContextCurrent(window);
    writeln("Created window and context - no errors detected.");

    DerelictGL3.reload();
    writeln("Reloaded DerelictGL3 - no errors detected.");

    GLuint vao,vbo;
    float[] verts = [-1,-1,0, 1,-1,0, 0,1,0];

    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glGenBuffers(1,&vbo);
    glBindBuffer(1,vbo);
    glBufferData(GL_ARRAY_BUFFER, verts.length*float.sizeof, verts.ptr, GL_STATIC_DRAW);

    GLuint shaderID = compileShaders(
                                    "
                                    #version 330
                                    layout(location=0) in vec3 vertpos;
                                    void main() {
                                        gl_Position.xyz = vertpos;
                                        gl_Position.w = 1.0;
                                    }
                                    ",
                                    "
                                    #version 330
                                    out vec3 color;
                                    void main() {
                                        color = vec3(1,1,1);
                                    }
                                    "
                                     );

    while(!glfwWindowShouldClose(window)) {
        glClearColor(0.2f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glUseProgram(shaderID);
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,cast(void*)0);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    DerelictGL3.unload();
    DerelictGLFW3.unload();
}

0 个答案:

没有答案