From 74a2cf310f4891e17157b508edb0ea982b03619e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 31 Jan 2021 14:02:13 +0000 Subject: Create terrain as collection of meshes --- game/terrain.cpp | 44 ++++++-------------------------------------- game/terrain.h | 11 +++-------- 2 files changed, 9 insertions(+), 46 deletions(-) (limited to 'game') diff --git a/game/terrain.cpp b/game/terrain.cpp index 092374c..3167972 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -1,19 +1,18 @@ #include "terrain.h" #include "gfx/models/texture.h" -#include +#include #include #include -#include #include #include #include +#include #include #include #include #include -Terrain::Terrain() : - m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("terrain.png")} +Terrain::Terrain() : texture {Texture::cachedTexture.get("terrain.png")} { constexpr auto size {241}; // Vertices constexpr auto offset {(size - 1) / 2}; @@ -64,8 +63,7 @@ Terrain::Terrain() : finish(size, size, resolution); } -Terrain::Terrain(const std::string & fileName) : - m_vertexArrayObject {}, m_vertexArrayBuffers {}, texture {Texture::cachedTexture.get("terrain.png")} +Terrain::Terrain(const std::string & fileName) : texture {Texture::cachedTexture.get("terrain.png")} { constexpr auto resolution {100}; @@ -131,27 +129,7 @@ Terrain::finish(unsigned int width, unsigned int height, unsigned int resolution indices.push_back(verticesCount + 2); indices.push_back(verticesCount + 3); - glGenVertexArrays(1, &m_vertexArrayObject); - glBindVertexArray(m_vertexArrayObject); - - glGenBuffers(2, m_vertexArrayBuffers.data()); - - glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[0]); - glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, pos)); - - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, texCoord)); - - glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, normal)); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[1]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(), GL_STATIC_DRAW); - - glBindVertexArray(0); + meshes.create(vertices, indices); } Vertex & @@ -160,12 +138,6 @@ Terrain::v(unsigned int width, unsigned int x, unsigned int z) return vertices[x + (z * width)]; } -Terrain::~Terrain() -{ - glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data()); - glDeleteVertexArrays(1, &m_vertexArrayObject); -} - static const Transform identity {}; static const auto identityModel {identity.GetModel()}; @@ -174,9 +146,5 @@ Terrain::render(const Shader & shader) const { shader.setModel(identityModel); texture->Bind(); - glBindVertexArray(m_vertexArrayObject); - - glDrawElementsBaseVertex(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr, 0); - - glBindVertexArray(0); + meshes.apply(&Mesh::Draw); } diff --git a/game/terrain.h b/game/terrain.h index 64c4f96..81ee128 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -1,13 +1,12 @@ #ifndef TERRAIN_H #define TERRAIN_H +#include "collection.hpp" #include "worldobject.h" -#include -#include +#include #include #include #include -#include #include #include @@ -18,9 +17,6 @@ class Terrain : public WorldObject, public Renderable { public: Terrain(); explicit Terrain(const std::string &); - ~Terrain() override; - NO_COPY(Terrain); - NO_MOVE(Terrain); void render(const Shader & shader) const override; @@ -33,8 +29,7 @@ private: Vertex & v(unsigned int width, unsigned int x, unsigned int z); - GLuint m_vertexArrayObject; - std::array m_vertexArrayBuffers; + Collection meshes; std::vector vertices; std::vector indices; std::shared_ptr texture; -- cgit v1.2.3