BlockModelManager

master
Nicole Collings 2020-05-31 20:03:06 -07:00
parent 225be941fc
commit 2d1275f6fb
11 changed files with 179 additions and 35 deletions

View File

@ -10,21 +10,7 @@ App::App() :
renderer(&window, &camera),
input(window.getInput()),
controller(input, camera),
test({
{{-20, -20, -20}, {0, 0}, {0, 0, 0}},
{{-20, 20, -20}, {0, 1}, {0, 0, 0}},
{{-20, 20, 20}, {1, 1}, {0, 0, 0}},
{{-20, -20, 20}, {1, 0}, {0, 0, 0}},
}, {0, 1, 2, 2, 3, 0}),
test2({
{{ 20, -20, -20}, {0, 0}, {0, 0, 0}},
{{ 20, 20, -20}, {0, 1}, {0, 0, 0}},
{{ 20, 20, 20}, {1, 1}, {0, 0, 0}},
{{ 20, -20, 20}, {1, 0}, {0, 0, 0}},
}, {0, 3, 2, 2, 1, 0}),
tex("../assets/textures/dirt.png") {
controller(input, camera) {
while (!window.shouldEnd()) {
update();
@ -39,12 +25,6 @@ void App::update() {
void App::render() {
renderer.reset();
glm::mat4 model = glm::mat4(1.0);
tex.use(0);
renderer.setModelMatrix(model);
test.draw();
test2.draw();
blockManager.render(renderer);
renderer.swap();
}

View File

@ -9,9 +9,7 @@
#include "graph/Renderer.h"
#include "input/ViewportControl.h"
#include "graph/Mesh.h"
#include "graph/Texture.h"
#include "model/BlockModelManager.h"
class App {
public:
@ -27,7 +25,5 @@ private:
Input& input;
ViewportControl controller;
BlockMesh test,test2;
Texture tex;
BlockModelManager blockManager;
};

View File

@ -1,3 +1,3 @@
set(MODELLER_SRC
graph/Renderer.cpp graph/Renderer.h App.cpp App.h graph/Window.cpp graph/Window.h graph/Shader.cpp graph/Shader.h graph/Camera.h graph/Camera.cpp graph/Mesh.inl graph/Mesh.h graph/Vertex.h graph/Texture.h graph/Texture.cpp input/ViewportControl.cpp input/ViewportControl.h input/Input.cpp input/Input.h)
graph/Renderer.cpp graph/Renderer.h App.cpp App.h graph/Window.cpp graph/Window.h graph/Shader.cpp graph/Shader.h graph/Camera.h graph/Camera.cpp graph/Mesh.inl graph/Mesh.h graph/Vertex.h graph/Texture.h graph/Texture.cpp input/ViewportControl.cpp input/ViewportControl.h input/Input.cpp input/Input.h model/BlockModelManager.cpp model/BlockModelManager.h model/BlockModel.cpp model/BlockModel.h model/BlockFace.cpp model/BlockFace.h)
add_library(${MAIN_LIB_NAME} ${MODELLER_SRC})

View File

@ -9,19 +9,19 @@
#include <memory>
#include <vector>
#include <GL/glew.h>
#include <iostream>
template <class V>
class Mesh {
public:
Mesh() = default;
Mesh(const std::vector<V>& vertices, const std::vector<unsigned int>& indices);
void create(const std::vector<V>& vertices, const std::vector<unsigned int>& indices);
virtual void create(const std::vector<V>& vertices, const std::vector<unsigned int>& indices);
virtual void draw() const;
void cleanup();
~Mesh();
protected:
void createArrays(unsigned int vboLength, unsigned int iboLength, const void* verticesPtr, const void* indicesPtr);
void createVertexAttrib(unsigned int offset, unsigned int size, int type, unsigned int stride, const void* pointer);
@ -36,7 +36,10 @@ protected:
class BlockMesh : public Mesh<BlockVertex> {
public:
BlockMesh(const std::vector<BlockVertex>& vertices, const std::vector<unsigned int>& indices) : Mesh(vertices, indices) {
BlockMesh() : Mesh() {}
BlockMesh(const std::vector<BlockVertex>& vertices, const std::vector<unsigned int>& indices) : Mesh(vertices, indices) {}
void create(const std::vector<BlockVertex>& vertices, const std::vector<unsigned int>& indices) override {
Mesh::create(vertices, indices);
auto& p = BlockVertexParams;
for (unsigned int idx = 0; idx < p.count; idx++)
createVertexAttrib(idx, p.params[idx].size, p.params[idx].type,
@ -47,8 +50,11 @@ public:
class GuiMesh : public Mesh<GuiVertex> {
public:
GuiMesh(const std::vector<GuiVertex>& vertices, const std::vector<unsigned int>& indices) : Mesh(vertices, indices) {
auto& p = BlockVertexParams;
GuiMesh() : Mesh() {}
GuiMesh(const std::vector<GuiVertex>& vertices, const std::vector<unsigned int>& indices) : Mesh(vertices, indices) {}
void create(const std::vector<GuiVertex>& vertices, const std::vector<unsigned int>& indices) override {
Mesh::create(vertices, indices);
auto& p = GuiVertexParams;
for (unsigned int idx = 0; idx < p.count; idx++)
createVertexAttrib(idx, p.params[idx].size, p.params[idx].type,
p.params[idx].stride, reinterpret_cast<const void*>(p.params[idx].pointer));

View File

@ -21,7 +21,7 @@ private:
Camera& camera;
double panFactor = 0.01;
double distance = 120;
double distance = 4;
double pitch, yaw;

5
src/model/BlockFace.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by aurailus on 2020-05-31.
//
#include "BlockFace.h"

18
src/model/BlockFace.h Normal file
View File

@ -0,0 +1,18 @@
//
// Created by aurailus on 2020-05-31.
//
#pragma once
#include <array>
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
class BlockFace {
public:
BlockFace(std::array<glm::vec3, 4> points, std::array<glm::vec2, 4> texCoords) :
points(points), texCoords(texCoords) {}
std::array<glm::vec3, 4> points;
std::array<glm::vec2, 4> texCoords;
};

67
src/model/BlockModel.cpp Normal file
View File

@ -0,0 +1,67 @@
//
// Created by aurailus on 2020-05-31.
//
#include <glm/gtc/matrix_transform.hpp>
#include "BlockModel.h"
BlockModel::BlockModel() {
faces = {
BlockFace({ glm::vec3 {-0.5, -0.5, 0.5}, {-0.5, 0.5, 0.5}, {-0.5, 0.5, -0.5}, {-0.5, -0.5, -0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
BlockFace({ glm::vec3 {0.5, -0.5, -0.5}, {0.5, 0.5, -0.5}, {0.5, 0.5, 0.5}, {0.5, -0.5, 0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
BlockFace({ glm::vec3 {-0.5, -0.5, -0.5}, {-0.5, 0.5, -0.5}, {0.5, 0.5, -0.5}, {0.5, -0.5, -0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
BlockFace({ glm::vec3 {0.5, -0.5, 0.5}, {0.5, 0.5, 0.5}, {-0.5, 0.5, 0.5}, {-0.5, -0.5, 0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
BlockFace({ glm::vec3 {-0.5, -0.5, 0.5}, {-0.5, -0.5, -0.5}, {0.5, -0.5, -0.5}, {0.5, -0.5, 0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
BlockFace({ glm::vec3 {0.5, 0.5, 0.5}, {0.5, 0.5, -0.5}, {-0.5, 0.5, -0.5}, {-0.5, 0.5, 0.5}},
{ glm::vec2 {0, 0}, {0, 1}, {1, 1}, {1, 0}}),
};
updateMesh();
}
glm::ivec3 BlockModel::getPos() {
return pos;
}
void BlockModel::setPos(glm::ivec3 pos) {
this->pos = pos;
}
void BlockModel::updateMesh() {
std::vector<BlockVertex> vertices {};
std::vector<unsigned int> indices {};
unsigned int vertexOffset = 0;
for (auto& face : faces) {
vertices.push_back(BlockVertex { face.points[0], face.texCoords[0], {} });
vertices.push_back(BlockVertex { face.points[1], face.texCoords[1], {} });
vertices.push_back(BlockVertex { face.points[2], face.texCoords[2], {} });
vertices.push_back(BlockVertex { face.points[3], face.texCoords[3], {} });
indices.push_back(0 + vertexOffset);
indices.push_back(1 + vertexOffset);
indices.push_back(2 + vertexOffset);
indices.push_back(2 + vertexOffset);
indices.push_back(3 + vertexOffset);
indices.push_back(0 + vertexOffset);
vertexOffset += 4;
}
mesh.create(vertices, indices);
}
void BlockModel::render(Renderer &renderer) {
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, pos);
renderer.setModelMatrix(model);
mesh.draw();
}

25
src/model/BlockModel.h Normal file
View File

@ -0,0 +1,25 @@
//
// Created by aurailus on 2020-05-31.
//
#pragma once
#include "BlockFace.h"
#include "../graph/Mesh.h"
#include "../graph/Renderer.h"
class BlockModel {
public:
BlockModel();
glm::ivec3 getPos();
void setPos(glm::ivec3 pos);
void updateMesh();
void render(Renderer& renderer);
private:
glm::vec3 pos;
BlockMesh mesh;
std::vector<BlockFace> faces {};
};

View File

@ -0,0 +1,26 @@
//
// Created by aurailus on 2020-05-31.
//
#include "BlockModelManager.h"
#include "../graph/Renderer.h"
BlockModelManager::BlockModelManager() :
dirt("../assets/textures/dirt.png") {
models.emplace_back();
models.emplace_back();
models.emplace_back();
models.emplace_back();
models.emplace_back();
models[1].setPos({1, 0, 0});
models[2].setPos({-1, 0, 0});
models[3].setPos({0, 0, 1});
models[4].setPos({0, 0, -1});
}
void BlockModelManager::render(Renderer &renderer) {
dirt.use();
for (auto& model : models) model.render(renderer);
}

View File

@ -0,0 +1,21 @@
//
// Created by aurailus on 2020-05-31.
//
#pragma once
#include "../graph/Mesh.h"
#include "../graph/Texture.h"
#include "BlockModel.h"
class Renderer;
class BlockModelManager {
public:
BlockModelManager();
void render(Renderer& renderer);
private:
std::vector<BlockModel> models;
Texture dirt;
};