added indexbuffer

This commit is contained in:
2026-04-15 23:08:28 +02:00
parent 4566b84f2c
commit ee06b834b9

View File

@@ -38,10 +38,23 @@ int main() {
Vertex vertices[] = { Vertex vertices[] = {
Vertex{-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f}, 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, 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 vertexBuffer(vertices, numVertices);
vertexBuffer.unbind(); vertexBuffer.unbind();
@@ -64,7 +77,8 @@ int main() {
// FRAME GENERATION // FRAME GENERATION
vertexBuffer.bind(); vertexBuffer.bind();
glDrawArrays(GL_TRIANGLES, 0, numVertices); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0);
vertexBuffer.unbind(); vertexBuffer.unbind();
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
@@ -79,7 +93,7 @@ int main() {
const uint64 counterElapsed = endCounter - lastCounter; const uint64 counterElapsed = endCounter - lastCounter;
delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency)); delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency));
const auto FPS = static_cast<uint32>(static_cast<float32>(perfCounterFrequency) / static_cast<float32>(counterElapsed)); const auto FPS = static_cast<uint32>(static_cast<float32>(perfCounterFrequency) / static_cast<float32>(counterElapsed));
std::cout << "FPS: " << FPS << std::endl; //std::cout << "FPS: " << FPS << std::endl;
lastCounter = endCounter; lastCounter = endCounter;
} }