初始化vao时出错(glCreateVertexArrays())

时间:2016-01-03 15:47:58

标签: opengl

为了理解如何使用两个缓冲区并将其绑定到vao,我试图显示一个带有某种颜色的三角形。我收到这样的错误

ConsoleApplication12.exe中0x00000000处的第一次机会异常:0xC0000005:访问冲突执行位置0x00000000。

当我尝试调试时,此错误是由代码中的行引起的

glCreateVertexArrays(1,& vao);

我做了初始化vao,但我不明白错误发生的原因。请帮帮我..

这是代码

#include <stdlib.h>
#include<iostream>
#include <GL/glew.h> 
#include <GL/freeglut.h>
#include "glm/glm.hpp"
#define BUFFER_OFFSET(offset) ((GLvoid *) offset)
GLuint program; 
static int timeFor =0;
GLuint vPos;
GLuint buffer[2];
GLuint vao = 0;
void init()
{
    static const GLfloat positions[] = { 0.1f, 0.2f, 0.3f, 0.0f};

    static const GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 1.0f};

         glCreateVertexArrays(1, &vao);
         glCreateBuffers(2, &buffer[0]);
         glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0);
         glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(glm::vec4));
         glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
         glVertexArrayAttribBinding(vao, 0, 0);

         glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0);
         glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(glm::vec4));
         glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, 0);
         glVertexArrayAttribBinding(vao, 1, 1);


         const char* vSource = {
        "#version 330\n"
        "layout (location = 0) in vec4 offset;"
        "layout (location = 1) in vec4 color;"
        "out vec4 fColor;"
        "void main() {"
        "const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0), vec4( -0.25, -0.25, 0.5, 1.0), vec4( 0.25, 0.25, 0.5, 1.0));"
        "gl_Position = vertices[gl_VertexID] + offset;"
        "fColor = color;"
        "}"
    };

    const char* fSource = {
        "#version 330\n"
        "in vec4 fColor;"
        "out vec4 fragColor;"
        "void main() {"
        "fragColor = fColor;"
        "}"
    };


    GLuint vShader, fShader;
    vShader = glCreateShader(GL_VERTEX_SHADER);
    fShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(vShader, 1, &vSource, NULL);
    glShaderSource(fShader, 1, &fSource, NULL);
    glCompileShader(vShader);
    glCompileShader(fShader);
    program = glCreateProgram();
    glAttachShader(program, vShader);
    glAttachShader(program, fShader);
    glLinkProgram(program);
    glClearColor(1.0f, 1.0f, 0.0f, 1.0f);

}

void reshape(int width, int height) 
{
    glViewport(0, 0, width, height);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(program);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glutSwapBuffers();

}

void main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow(argv[0]);
    glewInit();
    init();
    glutDisplayFunc(display);       
    glutReshapeFunc(reshape);
    glutMainLoop();
}

1 个答案:

答案 0 :(得分:2)

我检查了我的openGL版本4.4,其中不支持glCreateVertexArrays而不是必须使用glGenVertexArrays。