gpt4 book ai didi

c++ - OpenGL:glDrawArrays 有效但 glDrawElements 无效

转载 作者:行者123 更新时间:2023-11-30 05:11:19 25 4
gpt4 key购买 nike

我有一个创建正方形的类,但我使用的是 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);
}

有人可以向我解释错误是什么吗?

最佳答案

看起来您没有绑定(bind)索引缓冲区。它应该作为 glDrawElements 的最后一个参数传递或与 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _eao); 调用绑定(bind),就像您在 init 方法中所做的那样。

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

关于c++ - OpenGL:glDrawArrays 有效但 glDrawElements 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com