Removing hard coded values from graphics and moving it into the properties of object and model.

This commit is contained in:
Faerbit 2014-11-13 01:22:33 +01:00
parent 16f4890458
commit 2ddf01ede2
8 changed files with 61 additions and 22 deletions

View File

@ -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);
}

View File

@ -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<float>(1.0472f * runTime, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::scale<float>(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) {

View File

@ -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<Light> Level::getLights() {
return lights;
}
glm::vec3 Level::getCameraPosition() {
return cameraPosition;
}
Object* Level::getCameraCenter() {
return cameraCenter;
}

View File

@ -18,11 +18,15 @@ class Level {
void render();
glm::vec3 getAmbientLight();
std::vector<Light> getLights();
glm::vec3 getCameraPosition();
Object* getCameraCenter();
private:
std::string filePath;
std::vector<Object> objects;
std::vector<Light> lights;
glm::vec3 ambientLight;
Object* cameraCenter;
glm::vec3 cameraPosition;
Terrain terrain;
};

View File

@ -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;
}

View File

@ -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

View File

@ -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<float>(this->getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate<float>(this->getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate<float>(this->getRotation()[2], glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(this->getPosition()) * rotationMatrix * glm::scale<float>(glm::vec3(model.getScale()));
shader->setUniform( "modelMatrix", modelMatrix);
// draw
model.getReference()->render();
}

View File

@ -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;