--- /dev/null
+#include "Floor.hpp"
+
+
+namespace tacos {
+
+constexpr int Floor::VAO_DIVISOR;
+
+Floor::Floor(int w, int d)
+: width(w)
+, depth(d)
+, elevation(w * d)
+, tile_width(width - 1)
+, tile_depth(depth - 1)
+, unclean_width(tile_width % VAO_DIVISOR)
+, unclean_depth(tile_depth % VAO_DIVISOR)
+, vao_width(tile_width / VAO_DIVISOR + bool(unclean_width))
+, vao_depth(tile_depth / VAO_DIVISOR + bool(unclean_depth))
+, vaos(vao_width * vao_depth)
+, general_elements(vao_width * vao_depth)
+, unclean_width_elements(general_elements + bool(unclean_width))
+, unclean_depth_elements(unclean_depth ? (unclean_width_elements + 1) : general_elements)
+, unclean_corner_elements(std::max(unclean_width_elements, unclean_depth_elements) + (unclean_width && unclean_depth))
+, buffers(unclean_corner_elements + 1) {
+ glGenVertexArrays(vaos.size(), vaos.data());
+ glGenBuffers(buffers.size(), buffers.data());
+ int x_end = vao_width - bool(unclean_width);
+ int z_end = vao_depth - bool(unclean_depth);
+ for (int z = 0; z < z_end; ++z) {
+ for (int x = 0; x < x_end; ++x) {
+ SetupVAO(z * vao_width + x, buffers[general_elements], (VAO_DIVISOR + 1) * (VAO_DIVISOR + 1));
+ }
+ }
+ // always fill general element buffer (there won't be one needed for maps with x or z less than divisor,
+ // but that shouldn't happen in practice, only during tests in which case I don't care about the overhead
+ FillElementBuffer(buffers[general_elements], VAO_DIVISOR, VAO_DIVISOR);
+ if (unclean_width) {
+ for (int z = 0; z < z_end; ++z) {
+ SetupVAO(z * vao_width + x_end, buffers[unclean_width_elements], (unclean_width + 1) * (VAO_DIVISOR + 1));
+ }
+ FillElementBuffer(buffers[unclean_width_elements], unclean_width, VAO_DIVISOR);
+ }
+ if (unclean_depth) {
+ for (int x = 0; x < x_end; ++x) {
+ SetupVAO(z_end * vao_width + x, buffers[unclean_depth_elements], (VAO_DIVISOR + 1) * (unclean_depth + 1));
+ }
+ FillElementBuffer(buffers[unclean_depth_elements], VAO_DIVISOR, unclean_depth);
+ }
+ if (unclean_width && unclean_depth) {
+ SetupVAO(z_end * vao_width + x_end, buffers[unclean_corner_elements], (unclean_width + 1) * (unclean_depth + 1));
+ FillElementBuffer(buffers[unclean_corner_elements], unclean_width, unclean_depth);
+ }
+}
+
+Floor::~Floor() noexcept {
+ glDeleteBuffers(buffers.size(), buffers.data());
+ glDeleteVertexArrays(vaos.size(), vaos.data());
+}
+
+void Floor::SetupVAO(int which, GLuint element_buffer, int vertex_count) noexcept {
+ glBindVertexArray(vaos[which]);
+ glBindBuffer(GL_ARRAY_BUFFER, buffers[which]);
+ glBufferData(GL_ARRAY_BUFFER, vertex_count * sizeof(Attributes), nullptr, GL_STATIC_DRAW);
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, 0, sizeof(Attributes), reinterpret_cast<const void *>(offsetof(Attributes, position)));
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 3, GL_FLOAT, 0, sizeof(Attributes), reinterpret_cast<const void *>(offsetof(Attributes, normal)));
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer);
+}
+
+void Floor::FillElementBuffer(GLuint which, int tile_width, int tile_depth) {
+ // unbind VAO so we don't accidentally trash an element buffer binding
+ 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));
+ 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 + 1] = (z + 0) * (tile_width + 1) + (x + 1);
+ data[i * 6 + 2] = (z + 1) * (tile_width + 1) + (x + 0);
+ data[i * 6 + 3] = (z + 0) * (tile_width + 1) + (x + 1);
+ data[i * 6 + 4] = (z + 1) * (tile_width + 1) + (x + 1);
+ 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));
+ 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].position = glm::vec3(x, GetElevation(abs_x, abs_z), z);
+ data[i].normal = GetNormal(abs_x, abs_z);
+ }
+ }
+ glUnmapBuffer(GL_ARRAY_BUFFER);
+}
+
+glm::vec3 Floor::GetNormal(int x, int z) const noexcept {
+ // TODO: not sure about the sign here
+ return normalize(glm::vec3(
+ ClampedElevation(x - 1, z) - ClampedElevation(x + 1, z),
+ 2.0f,
+ ClampedElevation(x, z - 1) - ClampedElevation(x, z + 1)
+ ));
+}
+
+glm::ivec2 Floor::Tiles(int vao_x, int vao_z) const noexcept {
+ return glm::ivec2(
+ (unclean_width && vao_x == vao_width - 1) ? unclean_width : VAO_DIVISOR,
+ (unclean_depth && vao_z == vao_depth - 1) ? unclean_depth : VAO_DIVISOR);
+}
+
+int Floor::NumTiles(int vao_x, int vao_z) const noexcept {
+ glm::ivec2 tiles = Tiles(vao_x, vao_z);
+ return tiles.x * tiles.y;
+}
+
+int Floor::NumVertices(int vao_x, int vao_z) const noexcept {
+ glm::ivec2 tiles = Tiles(vao_x, vao_z);
+ return (tiles.x + 1) * (tiles.y + 1);
+}
+
+void Floor::GenerateVertices() {
+ for (int z = 0; z < vao_depth; ++z) {
+ for (int x = 0; x < vao_width; ++x) {
+ FillAttribBuffer(x, z);
+ }
+ }
+}
+
+void Floor::DrawVAO(int vao_x, int vao_z) const noexcept {
+ glBindVertexArray(vaos[vao_z * vao_width + vao_x]);
+ // TODO: this cries for triangle strips
+ glDrawElements(GL_TRIANGLES, NumTiles(vao_x, vao_z) * 6, GL_UNSIGNED_SHORT, nullptr);
+}
+
+}