Index Rendering

This commit is contained in:
Sebastian Böckelmann 2025-12-24 11:36:26 +01:00
parent 017bf6057a
commit 07f75966f2
4 changed files with 26 additions and 11 deletions

View File

@ -13,15 +13,18 @@ GameLayer::GameLayer() :model(0,0) //Platzhalter, echtes Model kommt in onAttach
void GameLayer::onAttach()
{
std::vector<float> vertices = {
-0.5f, 0.5f, 0.f,
-0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
0.5f, 0.5f, 0.f,
-0.5f, 0.5f, 0.f
-0.5f, 0.5f, 0.f,//v0
-0.5f, -0.5f, 0.f,//v1
0.5f, -0.5f, 0.f,//v2
0.5f, 0.5f, 0.f,//v3
};
model = loader.loadToVAO(vertices);
std::vector<int> indices = {
0,1,3,
3,1,2
};
model = loader.loadToVAO(vertices, indices);
}
void GameLayer::onDetach()

View File

@ -19,7 +19,7 @@ void Renderer::renderFrame(const RawModel& model) {
void Renderer::renderRawModel(const RawModel& model) {
glBindVertexArray(model.vaoID);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES,0 , model.vertexCount);
glDrawElements(GL_TRIANGLES , model.vertexCount, GL_UNSIGNED_INT, 0);
glDisableVertexAttribArray(0);
glBindVertexArray(0);

View File

@ -4,11 +4,12 @@
#include "Loader.h"
RawModel Loader::loadToVAO(std::vector<float> vertices){
RawModel Loader::loadToVAO(const std::vector<float>& vertices, const std::vector<int>& indices){
GLuint vaoID = createVAO();
storeDataInAttributeList(0, vertices);
bindIndicesBuffer(indices);
unbindVAO();
return {vaoID, static_cast<int>(vertices.size() / 3)};
return {vaoID, static_cast<int>(indices.size())};
}
Loader::~Loader() {
@ -44,6 +45,16 @@ void Loader::storeDataInAttributeList(int attributeNumber, const std::vector<flo
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Loader::bindIndicesBuffer(const std::vector<int>& indices) {
GLuint vboID;
glGenBuffers(1, &vboID);
vboIDs.push_back(vboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(int), indices.data(), GL_STATIC_DRAW);
}
void Loader::unbindVAO() {
glBindVertexArray(0);
}

View File

@ -13,13 +13,14 @@
class Loader
{
public:
RawModel loadToVAO(std::vector<float> vertices);
RawModel loadToVAO(const std::vector<float>& vertices, const std::vector<int>& indices);
~Loader();
void cleanUp();
private:
GLuint createVAO();
void storeDataInAttributeList(int attributeNumber, const std::vector<float> &data);
void bindIndicesBuffer(const std::vector<int> &indices);
void unbindVAO();
std::vector<GLuint> vaoIDs;