58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
#include "Colored.hpp"
|
|
|
|
using namespace buffer;
|
|
|
|
Colored::Colored(GLenum shape, const unsigned long count, const std::vector<glm::vec3> &vertices, const std::vector<glm::vec4> &colors): Abstract(shape) {
|
|
glGenBuffers(1, &ColorBufferID);
|
|
setVertices(vertices.size() * sizeof(glm::vec3), &vertices[0]);
|
|
setColors(colors.size() * sizeof(glm::vec4), &colors[0]);
|
|
ElementCount = count;
|
|
}
|
|
Colored::~Colored() {
|
|
glDeleteBuffers(1, &ColorBufferID);
|
|
}
|
|
|
|
uint Colored::draw(params params) {
|
|
if (params.vertexOnly) {
|
|
enableVertexAttrib();
|
|
} else {
|
|
enableAllAttribs();
|
|
}
|
|
if (params.instances == 1) {
|
|
glDrawArrays(Shape, 0, ElementCount);
|
|
} else {
|
|
glDrawArraysInstanced(Shape, 0, ElementCount, params.instances);
|
|
}
|
|
|
|
if (params.vertexOnly) {
|
|
disableVertexAttrib();
|
|
} else {
|
|
disableAllAttribs();
|
|
}
|
|
return ElementCount * params.instances;
|
|
}
|
|
|
|
void Colored::enableAllAttribs() {
|
|
enableVertexAttrib();
|
|
|
|
glEnableVertexAttribArray(1);
|
|
glBindBuffer(GL_ARRAY_BUFFER, ColorBufferID);
|
|
glVertexAttribPointer(
|
|
1, // attribute
|
|
4, // size
|
|
GL_FLOAT, // type
|
|
GL_FALSE, // normalized?
|
|
0, // stride
|
|
(void *)0 // array buffer offset
|
|
);
|
|
}
|
|
void Colored::disableAllAttribs() {
|
|
glDisableVertexAttribArray(1);
|
|
|
|
disableVertexAttrib();
|
|
}
|
|
|
|
void Colored::setColors(const unsigned long size, const void *data) {
|
|
glBindBuffer(GL_ARRAY_BUFFER, ColorBufferID);
|
|
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
|
|
} |