#include "Cursor.hpp"
#include "Floor.hpp"
+#include "../graphics/buffer.hpp"
+
#include <iostream>
#include <glm/gtx/io.hpp>
namespace tacos {
Cursor::Cursor()
-: vao(0)
-, buffers{0}
+: vao()
, size(3)
, offset(0.1f)
, mode(HIDDEN) {
- glGenVertexArrays(1, &vao);
- glGenBuffers(2, buffers);
-
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, 0, sizeof(Attributes), reinterpret_cast<const void *>(offsetof(Attributes, position)));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
- glBindVertexArray(0);
-}
-
-Cursor::~Cursor() {
- glDeleteBuffers(2, buffers);
- glDeleteVertexArrays(1, &vao);
+ vao.Bind();
+ vao.BindAttributes();
+ vao.EnableAttribute(0);
+ vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
+ vao.BindElements();
+ vao.Unbind();
}
void Cursor::Hide() noexcept {
int z_begin = glm::clamp(tile_z, 0, floor.Depth() - size);
int z_end = z_begin + size;
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
- glBufferData(GL_ARRAY_BUFFER, size * size * sizeof(Attributes), nullptr, GL_DYNAMIC_DRAW);
- Attributes *attrib = reinterpret_cast<Attributes *>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
- for (int z = z_begin, index = 0; z < z_end; ++z) {
- for (int x = x_begin; x < x_end; ++x, ++index) {
- attrib[index].position = glm::vec3(x, floor.GetElevation(x, z) + offset, z);
+ vao.Bind();
+ vao.BindAttributes();
+ vao.ReserveAttributes(size * size, GL_DYNAMIC_DRAW);
+ {
+ MappedBuffer<Attributes> attrib(vao.MapAttributes(GL_WRITE_ONLY));
+ for (int z = z_begin, index = 0; z < z_end; ++z) {
+ for (int x = x_begin; x < x_end; ++x, ++index) {
+ attrib[index].position = glm::vec3(x, floor.GetElevation(x, z) + offset, z);
+ }
}
}
- glUnmapBuffer(GL_ARRAY_BUFFER);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, (size - 1) * (size - 1) * 6, nullptr, GL_DYNAMIC_DRAW);
- unsigned char *element = reinterpret_cast<unsigned char *>(glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY));
- for (int z = 0, index = 0; z < size - 1; ++z) {
- for (int x = 0; x < size - 1; ++x, ++index) {
- element[index * 6 + 0] = (z + 0) * size + (x + 0);
- element[index * 6 + 1] = (z + 0) * size + (x + 1);
- element[index * 6 + 2] = (z + 1) * size + (x + 0);
- element[index * 6 + 3] = (z + 0) * size + (x + 1);
- element[index * 6 + 4] = (z + 1) * size + (x + 1);
- element[index * 6 + 5] = (z + 1) * size + (x + 0);
+
+ vao.BindElements();
+ vao.ReserveElements((size - 1) * (size - 1) * 6, GL_DYNAMIC_DRAW);
+ {
+ MappedBuffer<unsigned char> element(vao.MapElements(GL_WRITE_ONLY));
+ for (int z = 0, index = 0; z < size - 1; ++z) {
+ for (int x = 0; x < size - 1; ++x, ++index) {
+ element[index * 6 + 0] = (z + 0) * size + (x + 0);
+ element[index * 6 + 1] = (z + 0) * size + (x + 1);
+ element[index * 6 + 2] = (z + 1) * size + (x + 0);
+ element[index * 6 + 3] = (z + 0) * size + (x + 1);
+ element[index * 6 + 4] = (z + 1) * size + (x + 1);
+ element[index * 6 + 5] = (z + 1) * size + (x + 0);
+ }
}
}
- glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
- glBindVertexArray(0);
+ vao.Unbind();
}
void Cursor::Draw() const noexcept {
- glBindVertexArray(vao);
- glDrawElements(GL_TRIANGLES, (size - 1) * (size - 1) * 6, GL_UNSIGNED_BYTE, nullptr);
+ vao.Bind();
+ vao.DrawTriangles((size - 1) * (size - 1) * 6);
}
glBindVertexArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, which);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, tile_width * tile_depth * sizeof(short) * 6, nullptr, GL_STATIC_DRAW);
- // TODO: this can return null on error (out of memory in this case)
- // might be worth checking eventually
- short *data = reinterpret_cast<short *>(glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY));
+ MappedBuffer<short> data(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
for (int z = 0, i = 0; z < tile_depth; ++z) {
for (int x = 0; x < tile_width; ++x, ++i) {
data[i * 6 + 0] = (z + 0) * (tile_width + 1) + (x + 0);
data[i * 6 + 5] = (z + 1) * (tile_width + 1) + (x + 0);
}
}
- glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
void Floor::FillAttribBuffer(int vao_x, int vao_z) {
glBindBuffer(GL_ARRAY_BUFFER, buffers[vao_z * vao_width + vao_x]);
- Attributes *data = reinterpret_cast<Attributes *>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
+ MappedBuffer<Attributes> data(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
glm::ivec2 tiles(Tiles(vao_x, vao_z));
for (int z = 0, abs_z = vao_z * VAO_DIVISOR, i = 0; z < tiles.y + 1; ++z, ++abs_z) {
for (int x = 0, abs_x = vao_x * VAO_DIVISOR; x < tiles.x + 1; ++x, ++abs_x, ++i) {
data[i].normal = GetNormal(abs_x, abs_z);
}
}
- glUnmapBuffer(GL_ARRAY_BUFFER);
}
glm::vec3 Floor::GetNormal(int x, int z) const noexcept {