diff --git a/Shader/phong.vsh b/Shader/phong.vsh index af5660d..db75dd4 100644 --- a/Shader/phong.vsh +++ b/Shader/phong.vsh @@ -1,7 +1,8 @@ #version 150 -uniform mat4 uViewMatrix; -uniform mat4 uProjectionMatrix; +uniform mat4 modelMatrix; +uniform mat4 viewMatrix; +uniform mat4 projectionMatrix; in vec3 aNormal; in vec3 aPosition; @@ -13,8 +14,8 @@ out vec4 fragPosition; void main() { - fragPosition = uViewMatrix * vec4(aPosition, 1.0); - vNormal = inverse(transpose(mat3(uViewMatrix))) * aNormal; + fragPosition = modelMatrix * vec4(aPosition, 1.0); + vNormal = inverse(transpose(mat3(modelMatrix))) * aNormal; vTexCoord = aTexCoord; - gl_Position = uProjectionMatrix * uViewMatrix * vec4(aPosition, 1.0); + gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(aPosition, 1.0); } diff --git a/graphics.cc b/graphics.cc index 95df54c..bec9550 100644 --- a/graphics.cc +++ b/graphics.cc @@ -46,10 +46,10 @@ void draw(float runTime) // clear the framebuffer: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // set view and projection matrix: - glm::mat4 viewMatrix = glm::translate(glm::vec3(0.0f, -1.0f, -2.0f)) * glm::rotate(1.0472f * runTime, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale(glm::vec3(0.25f)); - shader->setUniform( "uViewMatrix", viewMatrix ); - shader->setUniform( "uProjectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) ); + //set view and projection matrix + shader->setUniform("projectionMatrix", buildFrustum(75.0, 0.1, 100.0, (float)g_windowSize.x/(float)g_windowSize.y) ); + // the + (0,1,0) compensates bunny doesn't have its center at it's center + shader->setUniform("viewMatrix", glm::lookAt(level.getCameraPosition(), (level.getCameraCenter()->getPosition() + glm::vec3(0.0f, 1.0f, 0.0f)), glm::vec3(0.0f, 1.0f, 0.0f))); //set lighting parameters if (level.getLights().size() > 0) { diff --git a/level.cc b/level.cc index 4367595..f31db31 100644 --- a/level.cc +++ b/level.cc @@ -12,26 +12,26 @@ Level::~Level() { } void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { - this->terrain.load(); - - - // currently hard coded should later read this stuff out of a file + cameraPosition = glm::vec3(0.0f, 0.0f, 0.0f); // load the geometry of the stanford bunny and build a VAO: - Model model = Model("Bunny.obj"); + Model model = Model("Bunny.obj", 0.25f); // load a texture: Material material = Material("clownfishBunny.png", 0.1f, 0.5f, 0.5f, 3.0f); //Create object - Object object = Object(model, material, glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), + Object object = Object(model, material, glm::vec3(0.0f, -1.0f, -2.0f), + glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), shader); objects.push_back(object); + cameraCenter = &objects[0]; //set lighting parameters ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f), 2.0f); lights.push_back(light); Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), 2.0f); lights.push_back(light2); + // load terrain + this->terrain.load(); } void Level::render() { @@ -43,7 +43,7 @@ void Level::render() { void Level::update(float runTime) { // rotate bunny - objects[0].setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); + cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); } glm::vec3 Level::getAmbientLight() { @@ -53,3 +53,11 @@ glm::vec3 Level::getAmbientLight() { std::vector Level::getLights() { return lights; } + +glm::vec3 Level::getCameraPosition() { + return cameraPosition; +} + +Object* Level::getCameraCenter() { + return cameraCenter; +} diff --git a/level.hh b/level.hh index 924605f..d88086c 100644 --- a/level.hh +++ b/level.hh @@ -18,11 +18,15 @@ class Level { void render(); glm::vec3 getAmbientLight(); std::vector getLights(); + glm::vec3 getCameraPosition(); + Object* getCameraCenter(); private: std::string filePath; std::vector objects; std::vector lights; glm::vec3 ambientLight; + Object* cameraCenter; + glm::vec3 cameraPosition; Terrain terrain; }; diff --git a/model.cc b/model.cc index 17218a2..1800572 100644 --- a/model.cc +++ b/model.cc @@ -1,8 +1,15 @@ #include "model.hh" +Model::Model(std::string filePath, float scale) { + reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create(); + reference->bind(); + this->scale = scale; +} + Model::Model(std::string filePath) { reference = ACGL::OpenGL::VertexArrayObjectCreator(filePath).create(); reference->bind(); + this->scale = 1.0f; } Model::Model(){ @@ -14,3 +21,11 @@ Model::~Model() { ACGL::OpenGL::SharedVertexArrayObject Model::getReference() { return reference; } + +void Model::setScale(float scale) { + this->scale = scale; +} + +float Model::getScale() { + return scale; +} diff --git a/model.hh b/model.hh index 6b728b7..70391d6 100644 --- a/model.hh +++ b/model.hh @@ -6,12 +6,16 @@ class Model { public: + Model(std::string filePath, float scale); Model(std::string filePath); - Model(); + Model(); ~Model(); ACGL::OpenGL::SharedVertexArrayObject getReference(); + void setScale(float scale); + float getScale(); private: ACGL::OpenGL::SharedVertexArrayObject reference; + float scale; }; #endif diff --git a/object.cc b/object.cc index 8d4b910..a917b4f 100644 --- a/object.cc +++ b/object.cc @@ -3,7 +3,7 @@ Object::Object(Model model, Material material, glm::vec3 position, glm::vec3 rotation, glm::vec3 velocity, glm::vec3 angularVelocity, ACGL::OpenGL::SharedShaderProgram shader) : Entity(position, rotation) { - this->model = model.getReference(); + this->model = model; this->material = material; this->velocity = velocity; this->angularVelocity = angularVelocity; @@ -17,10 +17,17 @@ Object::~Object() { } void Object::render() { + // set lightning parameters for this object shader->setUniform("ambientFactor", material.getAmbientFactor()); shader->setUniform("diffuseFactor", material.getDiffuseFactor()); shader->setUniform("specularFactor", material.getSpecularFactor()); shader->setUniform("shininess", material.getShininess()); shader->setTexture("uTexture", material.getReference(), 0); - model->render(); + // set model matrix + glm::mat4 rotationMatrix = glm::rotate(this->getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) * + glm::rotate(this->getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(this->getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f)); + glm::mat4 modelMatrix = glm::translate(this->getPosition()) * rotationMatrix * glm::scale(glm::vec3(model.getScale())); + shader->setUniform( "modelMatrix", modelMatrix); + // draw + model.getReference()->render(); } diff --git a/object.hh b/object.hh index 000a4c9..d8e4029 100644 --- a/object.hh +++ b/object.hh @@ -14,11 +14,11 @@ class Object : public Entity { Object(Model model, Material material, glm::vec3 position, glm::vec3 rotation, glm::vec3 velocity, glm::vec3 angularVelocity, ACGL::OpenGL::SharedShaderProgram shader); - Object(); + Object(); ~Object(); void render(); private: - ACGL::OpenGL::SharedVertexArrayObject model; + Model model; Material material; glm::vec3 velocity; glm::vec3 angularVelocity;