summaryrefslogtreecommitdiff
path: root/game/terrain.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-31 14:02:13 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-04 19:29:49 +0000
commit74a2cf310f4891e17157b508edb0ea982b03619e (patch)
tree662751c0249a57d3988d4c8a624e6c8882a6ea0e /game/terrain.cpp
parentCreate mesh direct from vertices and indices (diff)
downloadilt-74a2cf310f4891e17157b508edb0ea982b03619e.tar.bz2
ilt-74a2cf310f4891e17157b508edb0ea982b03619e.tar.xz
ilt-74a2cf310f4891e17157b508edb0ea982b03619e.zip
Create terrain as collection of meshes
Diffstat (limited to 'game/terrain.cpp')
-rw-r--r--game/terrain.cpp44
1 files changed, 6 insertions, 38 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);
}