OpenGL:glDrawArrays的作品,但glDrawElements不

问题描述:

我有一个类来创建一个正方形,但我使用glDrawArrays,它完美的作品,但是当我尝试使用glDrawElements它不起作用。OpenGL:glDrawArrays的作品,但glDrawElements不

Sprite.h

#pragma once 
#include <GL/glew.h> 
#include <windows.h> 
#include <iostream> 
#include "shaderloader.h"; 

//#define USING_INDEX_BUFFER 1 
#ifdef USING_INDEX_BUFFER 
    #define NUM_VERTICES 4 
    #define NUM_INDICES 6 
#else 
    #define NUM_VERTICES 6 
#endif 

class Sprite 
{ 
public: 
    Sprite(float x, float y, float width, float height); 
    ~Sprite(); 
    void init(); 
    void draw(); 

private: 
    float _x, _y; 
    float _width, _height; 

    GLuint _vao, _vbo, _eao; 

#ifdef USING_INDEX_BUFFER 
    GLfloat _vertices[4][2] = { 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f } 
    }; // A Quad 
    GLfloat _color[4][4] = { 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f } 
    }; 
    GLuint _indices[6] = { 0, 0, 0, 0, 0, 0 }; 
#else 
    GLfloat _vertices[6][2] = { 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f }, 
     { 0.0f, 0.0f } 
    }; // A Quad 
    GLfloat _color[6][4] = { 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f }, 
     { 0.0f, 0.0f, 0.0f, 0.0f } 
    }; 
#endif 
}; 

Sprite.cpp

#include "Sprite.h" 

#define BUFFER_OFFSET(bytes) ((GLubyte*) NULL + (bytes)) 

Sprite::Sprite(float x, float y, float width, float height) 
{ 
    _x = x; 
    _y = y; 
    _width = width; 
    _height = height; 

#ifdef USING_INDEX_BUFFER 
    _vertices[0][0] = x; 
    _vertices[0][1] = y; 
    _vertices[1][0] = x; 
    _vertices[1][1] = y + height; 
    _vertices[2][0] = x + width; 
    _vertices[2][1] = y; 

    _vertices[3][0] = x + width; 
    _vertices[3][1] = y + height; 

    for (int i = 0; i < 4; i++) { 
     _color[i][0] = 1.0f; 
     _color[i][3] = 1.0f; 
    } 

    GLuint _indices[6] = { 0, 1, 2, 3, 1, 2 }; 

#else 
    _vertices[0][0] = x; 
    _vertices[0][1] = y; 
    _vertices[1][0] = x; 
    _vertices[1][1] = y + height; 
    _vertices[2][0] = x + width; 
    _vertices[2][1] = y; 

    _vertices[3][0] = x + width; 
    _vertices[3][1] = y + height; 
    _vertices[4][0] = x; 
    _vertices[4][1] = y + height; 
    _vertices[5][0] = x + width; 
    _vertices[5][1] = y; 

    for (int i = 0; i < 6; i++) { 
     _color[i][0] = 1.0f; 
     _color[i][3] = 1.0f; 
    } 
#endif 
} 


Sprite::~Sprite() 
{ 
    glDeleteVertexArrays(1, &_vao); 
    glDeleteBuffers(1, &_vbo); 
} 


void Sprite::init() 
{ 
    ShaderLoader shader; 
    GLuint programID = shader.getProgramID("basicShading"); 

    // Generate and bind the VAO 
    glGenVertexArrays(1, &_vao); 
    glBindVertexArray(_vao); 

    // Generate, bind and update data 
    glGenBuffers(1, &_vbo); 
    glBindBuffer(GL_ARRAY_BUFFER, _vbo); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices) + sizeof(_color), NULL, GL_STATIC_DRAW); 
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_vertices), _vertices); 
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(_vertices), sizeof(_color), _color); 

#ifdef USING_INDEX_BUFFER 
    // Generate, bind and update data of the EAO 
    glGenBuffers(1, &_eao); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eao); 
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES * sizeof(GLuint), _indices, GL_STATIC_DRAW); 
#endif 

    // Set up attributes: Vertices 
    GLuint vPos = glGetAttribLocation(programID, "vertexPosition"); 
    glEnableVertexAttribArray(vPos); 
    glVertexAttribPointer(vPos, 2, GL_FLOAT, GL_FALSE, 0, 0); 
    // Set up attributes: Colors 
    GLuint vCol = glGetAttribLocation(programID, "fragColor"); 
    glEnableVertexAttribArray(vCol); 
    glVertexAttribPointer(vCol, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(_vertices))); 

    // Unbind VAO, VBO and EAO 
    glBindVertexArray(0); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
#ifdef USING_INDEX_BUFFER 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
#endif 

    glUseProgram(programID); 
} 

void Sprite::draw() { 
    glBindVertexArray(_vao); 

#ifdef USING_INDEX_BUFFER 
    glDrawElements(GL_TRIANGLES, NUM_INDICES, GL_UNSIGNED_INT, NULL); // Doesn't work 
#else 
    glDrawArrays(GL_TRIANGLES, 0, NUM_VERTICES); // Works 
#endif 

    glBindVertexArray(0); 
} 

有人能向我解释什么是错误?

它看起来像你没有绑定索引缓冲区。它应该或者作为glDrawElements的最后一个参数传递,或者像init方法那样通过呼叫glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eao);绑定。

此外您的代码没有错误处理您应该在每次gl呼叫后调用glGetError

+0

哦,你是对的谢谢!我使用了'GLuint _indices [6] = {0,1,2,3,1,2};'而不是象我在顶点数组中那样定义数组的每个部分 – TheBrenolibre