diff --git a/data/shaders/vertex.glsl b/data/shaders/vertex.glsl index 6d14de8..f0f9a17 100644 --- a/data/shaders/vertex.glsl +++ b/data/shaders/vertex.glsl @@ -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; }; diff --git a/src/Model.cpp b/src/Model.cpp index 9493bc5..106cec6 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -4,6 +4,18 @@ Model::Model(Mesh mesh) { this->meshes.push_back(mesh); } Model::Model(std::vector 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)); } diff --git a/src/Model.h b/src/Model.h index 99b7c2c..309ab28 100644 --- a/src/Model.h +++ b/src/Model.h @@ -2,18 +2,32 @@ #include "Error.h" #include "Mesh.h" #include "ShaderLoader.h" +#include +#include #include class Model { private: Error error = Error("Model"); + // All the meshes that make up the model std::vector 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 meshes); + + // Render the model void draw(ShaderLoader &shader); + + // Translate the model + void translate(glm::vec3 translation); }; diff --git a/src/main.cpp b/src/main.cpp index c8ad3b8..4548ac1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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);