2016-01-03 41 views
1

爲了理解如何使用兩個緩衝區並將其綁定到vao,我試圖顯示帶有某種顏色的三角形。我遇到這樣的錯誤在初始化vao(glCreateVertexArrays())時出錯

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

看起來像glew沒有得到'glGenVertexArrays'函數指針。確保它在您的實施中受到支持。假設功能是沒有正確的方式來使用GL版本/擴展。 – derhass

+1

啓用'glewExperimental = GL_TRUE'看看是否可以解決您的問題。 – Poriferous

+0

@Poriferous:感謝您的回覆,我已經啓用之前初始化glew,即glewInit()之前,但仍然沒有工作,拋出相同的錯誤 – spod

回答

1

我檢查了我的openGL版本的4.4,在不支持的而不是glGenVertexArrays必須使用glCreateVertexArrays。