Added model class with the ability to hold multiple models.

This commit is contained in:
Warwick 2022-04-21 14:15:02 +01:00
parent 1b82ef0556
commit 9fcc98fdf4
5 changed files with 42 additions and 4 deletions

View file

@ -39,7 +39,7 @@ void Mesh::setupMesh() {
glBindVertexArray(0); glBindVertexArray(0);
} }
void Mesh::Draw(ShaderLoader &shader) { void Mesh::draw(ShaderLoader &shader) {
unsigned int diffuseNr = 1; unsigned int diffuseNr = 1;
unsigned int specularNr = 1; unsigned int specularNr = 1;
for (unsigned int i = 0; i < textures.size(); i++) { for (unsigned int i = 0; i < textures.size(); i++) {

View file

@ -28,7 +28,7 @@ public:
std::vector<unsigned int> indices; std::vector<unsigned int> indices;
std::vector<Texture> textures; std::vector<Texture> textures;
void Draw(ShaderLoader &shader); void draw(ShaderLoader &shader);
Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices,
std::vector<Texture> textures); std::vector<Texture> textures);

9
src/Model.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "Model.h"
Model::Model(Mesh mesh) { this->meshes.push_back(mesh); }
Model::Model(std::vector<Mesh> meshes) { this->meshes = meshes; }
void Model::draw(ShaderLoader &shader) {
for (unsigned int i = 0; i < this->meshes.size(); i++)
this->meshes[i].draw(shader);
}

19
src/Model.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include "Error.h"
#include "Mesh.h"
#include "ShaderLoader.h"
#include <vector>
class Model {
private:
Error error = Error("Model");
std::vector<Mesh> meshes;
public:
// Used to create a mesh out of a single mesh.
Model(Mesh mesh);
// Used to create a mesh out of multiple meshes.
Model(std::vector<Mesh> meshes);
void draw(ShaderLoader &shader);
};

View file

@ -19,6 +19,7 @@
#include "helpers/RootDir.h" #include "helpers/RootDir.h"
// Objects // Objects
#include "Mesh.h" #include "Mesh.h"
#include "Model.h"
#include <vector> #include <vector>
// Include error class // Include error class
@ -176,6 +177,13 @@ int main(int argc, char **argv) {
Mesh mesh2(mvertices2, mindices, mtextures); Mesh mesh2(mvertices2, mindices, mtextures);
std::vector<Mesh> modelMeshes;
modelMeshes.push_back(mesh);
modelMeshes.push_back(mesh2);
Model model(modelMeshes);
Model model2(mesh);
// Mess with perspective // Mess with perspective
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units // unit <-> 100 units
@ -213,8 +221,10 @@ int main(int argc, char **argv) {
shader.setMat4("MVP", camera.getMVP()); shader.setMat4("MVP", camera.getMVP());
// Draw Meshes // Draw Meshes
mesh.Draw(shader); // mesh.draw(shader);
mesh2.Draw(shader); // mesh2.draw(shader);
model2.draw(shader);
model.draw(shader);
// Finally render everything // Finally render everything
shader.use(); shader.use();