marching-cubes-opengl/VertexBuffer.cpp

37 lines
1.0 KiB
C++
Raw Permalink Normal View History

#include "VertexBuffer.h"
VertexBuffer::VertexBuffer() {
glGenVertexArrays(1, &vertex_array);
glGenBuffers(1, &vertex_buffer);
}
2021-03-20 00:57:18 +01:00
void VertexBuffer::set_data(int size, int dimension, const void *data, int flag) {
// We can't just do size = sizeof(data) here because of array to pointer decay!
this->size = size;
2021-03-20 00:57:18 +01:00
bind_array();
bind_buffer();
2021-03-19 19:42:35 +01:00
2021-03-20 00:57:18 +01:00
// Create the data on the GPU
glBufferData(GL_ARRAY_BUFFER, size, data, flag);
2021-03-19 19:42:35 +01:00
2021-03-20 00:57:18 +01:00
// Specify the dimension of the vectors in the data, so that the GPU knows how much to read from
// it at a time. (Currently only one type of vector is supported)
2021-03-19 19:42:35 +01:00
glEnableVertexAttribArray(0);
2021-03-20 00:57:18 +01:00
glVertexAttribPointer(0, dimension, GL_FLOAT, false, dimension * sizeof(float), 0);
2021-03-19 19:42:35 +01:00
glBindVertexArray(0);
}
2021-03-20 00:57:18 +01:00
void VertexBuffer::bind_buffer() {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
}
void VertexBuffer::bind_array() {
glBindVertexArray(vertex_array);
2021-03-19 19:42:35 +01:00
}
void VertexBuffer::draw() {
2021-03-20 00:57:18 +01:00
bind_array();
glDrawArrays(GL_TRIANGLES, 0, size);
}