diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-31 14:02:13 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-04 19:29:49 +0000 |
commit | 74a2cf310f4891e17157b508edb0ea982b03619e (patch) | |
tree | 662751c0249a57d3988d4c8a624e6c8882a6ea0e /game | |
parent | Create mesh direct from vertices and indices (diff) | |
download | ilt-74a2cf310f4891e17157b508edb0ea982b03619e.tar.bz2 ilt-74a2cf310f4891e17157b508edb0ea982b03619e.tar.xz ilt-74a2cf310f4891e17157b508edb0ea982b03619e.zip |
Create terrain as collection of meshes
Diffstat (limited to 'game')
-rw-r--r-- | game/terrain.cpp | 44 | ||||
-rw-r--r-- | game/terrain.h | 11 |
2 files changed, 9 insertions, 46 deletions
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 <array> +#include <GL/glew.h> #include <cache.h> #include <cmath> -#include <cstddef> #include <gfx/gl/shader.h> #include <gfx/gl/transform.h> #include <gfx/image.h> +#include <gfx/models/mesh.h> #include <gfx/models/vertex.hpp> #include <glm/glm.hpp> #include <random> #include <stb_image.h> -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<Mesh>(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 <GL/glew.h> -#include <array> +#include <gfx/models/mesh.h> #include <gfx/models/vertex.hpp> #include <gfx/renderable.h> #include <memory> -#include <special_members.hpp> #include <string> #include <vector> @@ -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<GLuint, NUM_BUFFERS> m_vertexArrayBuffers; + Collection<Mesh, false> meshes; std::vector<Vertex> vertices; std::vector<unsigned int> indices; std::shared_ptr<Texture> texture; |