1
我有一個類來創建一個正方形,但我使用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);
}
有人能向我解釋什麼是錯誤?
哦,你是對的謝謝!我使用了'GLuint _indices [6] = {0,1,2,3,1,2};'而不是象我在頂點數組中那樣定義數組的每個部分 – TheBrenolibre