Models now heve their own relative position (needs optimiseation)

This commit is contained in:
Warwick 2022-04-22 12:43:51 +01:00
parent 9fcc98fdf4
commit fd0acf3dde
4 changed files with 31 additions and 4 deletions

View file

@ -4,11 +4,12 @@ layout (location = 1) in vec3 aNormal; // Currently Unused
layout (location = 2) in vec2 aTexCoord;
uniform mat4 MVP;
uniform mat4 Model;
out vec2 ourTexCoord;
void main()
{
gl_Position = MVP * vec4(aPos, 1.0);
gl_Position = MVP * Model * vec4(aPos, 1.0);
ourTexCoord = aTexCoord;
};

View file

@ -4,6 +4,18 @@ 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++)
for (unsigned int i = 0; i < this->meshes.size(); i++) {
shader.setMat4("Model", this->model);
this->meshes[i].draw(shader);
}
}
void Model::translate(glm::vec3 translation) {
// set worldspace postition
glm::mat4 trans = glm::mat4(1.0f);
trans = glm::translate(trans, translation);
this->position = trans * this->position;
// set model transform
this->model = glm::translate(glm::mat4(1.0f), glm::vec3(this->position));
}

View file

@ -2,18 +2,32 @@
#include "Error.h"
#include "Mesh.h"
#include "ShaderLoader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
class Model {
private:
Error error = Error("Model");
// All the meshes that make up the model
std::vector<Mesh> meshes;
// Model position/scale/more matrix
glm::mat4 model = glm::mat4(1.0f);
// Position
glm::vec4 position = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
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);
// Render the model
void draw(ShaderLoader &shader);
// Translate the model
void translate(glm::vec3 translation);
};

View file

@ -184,6 +184,8 @@ int main(int argc, char **argv) {
Model model2(mesh);
model.translate(glm::vec3(1.0f, 0.0f, 0.0f));
// Mess with perspective
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1
// unit <-> 100 units
@ -221,8 +223,6 @@ int main(int argc, char **argv) {
shader.setMat4("MVP", camera.getMVP());
// Draw Meshes
// mesh.draw(shader);
// mesh2.draw(shader);
model2.draw(shader);
model.draw(shader);