From ee06b834b9a09772b5b78b23f7e5a04429091eb2 Mon Sep 17 00:00:00 2001 From: miaurizius Date: Wed, 15 Apr 2026 23:08:28 +0200 Subject: [PATCH] added indexbuffer --- src/main.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index abb00af..88c2930 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,10 +38,23 @@ int main() { Vertex vertices[] = { Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, - Vertex{0.0f, 0.5f, 0.0f, 0.0, 1.0f, 0.0f, 1.0f}, + Vertex{-0.5f, 0.5f, 0.0f, 0.0, 1.0f, 0.0f, 1.0f}, Vertex{0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f}, + + Vertex{0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, }; - uint32_t numVertices = 3; + uint32 numVertices = sizeof(vertices) / sizeof(Vertex); + + uint32 indices[] = { + 0, 1, 2, + 1, 2, 3 + }; + uint32 numIndices = sizeof(indices) / sizeof(uint32); + + GLuint indexBuffer; + glGenBuffers(1, &indexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(indices[0]), indices, GL_STATIC_DRAW); VertexBuffer vertexBuffer(vertices, numVertices); vertexBuffer.unbind(); @@ -64,7 +77,8 @@ int main() { // FRAME GENERATION vertexBuffer.bind(); - glDrawArrays(GL_TRIANGLES, 0, numVertices); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); + glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0); vertexBuffer.unbind(); SDL_GL_SwapWindow(window); @@ -79,7 +93,7 @@ int main() { const uint64 counterElapsed = endCounter - lastCounter; delta = (static_cast(counterElapsed) / static_cast(perfCounterFrequency)); const auto FPS = static_cast(static_cast(perfCounterFrequency) / static_cast(counterElapsed)); - std::cout << "FPS: " << FPS << std::endl; + //std::cout << "FPS: " << FPS << std::endl; lastCounter = endCounter; }