46 lines
1.3 KiB
C++
46 lines
1.3 KiB
C++
#include <cstddef>
|
|
|
|
#include "VertexBuffer.h"
|
|
#include "defines.h"
|
|
|
|
VertexBuffer::VertexBuffer(void *vertexData, void *indexData, uint32_t numVertices, uint32_t numIndices) {
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
glBindVertexArray(VAO);
|
|
|
|
glGenBuffers(1, &bufferId);
|
|
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
|
|
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(Vertex), vertexData, GL_STATIC_DRAW);
|
|
|
|
glGenBuffers(1, &EBO);
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(uint32_t), indexData, GL_STATIC_DRAW);
|
|
|
|
// Position
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, x));
|
|
|
|
// UV Texture Mapping
|
|
glEnableVertexAttribArray(1);
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, u));
|
|
|
|
// Color
|
|
glEnableVertexAttribArray(2);
|
|
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) offsetof(struct Vertex, r));
|
|
|
|
// This will also unbind the vertex buffer
|
|
glBindVertexArray(0);
|
|
}
|
|
|
|
VertexBuffer::~VertexBuffer() {
|
|
glDeleteBuffers(1, &bufferId);
|
|
}
|
|
|
|
void VertexBuffer::bind() {
|
|
glBindVertexArray(VAO);
|
|
}
|
|
|
|
void VertexBuffer::unbind() {
|
|
glBindVertexArray(0);
|
|
}
|