diff --git a/entity.cpp b/entity.cpp index d9dcaa7..bcb99ce 100644 --- a/entity.cpp +++ b/entity.cpp @@ -8,11 +8,15 @@ void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) { glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glm::mat4 mvp = projectionMatrix * modelViewMatrix; - Shader* shader = mat->getShader(); - shader->bind(); mat->bindAttributes(); + Shader* shader = mat->getShader(); shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMatrix(shader->getLocation("MVP"), mvp); mesh->draw(); } + +Shader* Entity::getShader() +{ + return mat->getShader(); +} diff --git a/entity.h b/entity.h index 1f92f06..b975676 100644 --- a/entity.h +++ b/entity.h @@ -5,6 +5,7 @@ class Mesh; class Material; +class Shader; class Entity { @@ -16,6 +17,7 @@ protected: public: Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : parent(myParent), mesh(myMesh), mat(myMat) {} virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); + Shader* getShader(); }; #endif // ENTITY_H diff --git a/lights.cpp b/lights.cpp new file mode 100644 index 0000000..d4455ce --- /dev/null +++ b/lights.cpp @@ -0,0 +1,14 @@ +#include "lights.h" +#include "shader.h" + +void Lights::addLight(const glm::vec3 &myPosition, const glm::vec3 myColor) +{ + Light l; + l.position = myPosition; + l.color = myColor; +} + +void Lights::bind(GLuint location, Shader* shader) +{ + //shader->bindVec3Array(location, (glm::vec3*)lights.data(), lights.size()*2); +} diff --git a/lights.h b/lights.h new file mode 100644 index 0000000..0db31b4 --- /dev/null +++ b/lights.h @@ -0,0 +1,25 @@ +#ifndef LIGHT_H +#define LIGHT_H + +#include +#include +#include + +class Shader; + +class Lights +{ +private: + typedef struct + { + glm::vec3 position; + glm::vec3 color; + } Light; + + std::vector lights; +public: + void addLight(const glm::vec3 &myPosition = glm::vec3(0), const glm::vec3 myColor = glm::vec3(1)); + void bind(GLuint location, Shader* shader); +}; + +#endif // LIGHT_H diff --git a/myglwidget.cpp b/myglwidget.cpp index d902afd..1444226 100644 --- a/myglwidget.cpp +++ b/myglwidget.cpp @@ -68,9 +68,9 @@ void MyGLWidget::buildScene() Shader* shader = new Shader(&vertSource, &fragSource); Material* mat = new PhongMaterial(shader); QString filenames[6] = { - "../data/skybox_lf", "../data/skybox_rt", + "../data/skybox_ft", "../data/skybox_bk", "../data/skybox_up", "../data/skybox_dn", - "../data/skybox_bk", "../data/skybox_ft" + "../data/skybox_lf", "../data/skybox_rt" }; SkyBox* skybox = new SkyBox(filenames); scene->addEntity(skybox); @@ -88,7 +88,7 @@ void MyGLWidget::paintGL() { glAssert(glClearColor(0.60, 0.65, 0.75, 1.0)); glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); - scene->drawEntities(); + scene->drawAll(); } void MyGLWidget::mouseMoveEvent(QMouseEvent *e) diff --git a/scene.cpp b/scene.cpp index b7a857b..2a620d3 100644 --- a/scene.cpp +++ b/scene.cpp @@ -4,6 +4,9 @@ #include #include "camera.h" #include "entity.h" +#include "shader.h" + +// MAIN METHODS Scene::~Scene() { @@ -11,6 +14,22 @@ Scene::~Scene() delete(e); } +void Scene::drawAll() +{ + glm::mat4 viewMatrix = camera->getViewMatrix(); + glm::mat4 projectionMatrix = camera->getProjectionMatrix(); + for(Entity* e : entities) + { + Shader* shader = e->getShader(); + shader->bind(); + directionnalLights.bind(shader->getLocation("dirLights"), shader); + pointLights.bind(shader->getLocation("pointLights"), shader); + e->draw(viewMatrix, projectionMatrix); + } +} + +// ENTITIES + void Scene::addEntity(Entity* parent, Mesh* mesh, Material* mat) { entities.push_back(new Entity(parent, mesh, mat)); @@ -26,14 +45,20 @@ void Scene::addEntity(Entity* entity) entities.push_back(entity); } -void Scene::drawEntities() +// LIGHTS + +void Scene::addDirectionnalLight(const glm::vec3 &position, const glm::vec3 &color) { - glm::mat4 viewMatrix = camera->getViewMatrix(); - glm::mat4 projectionMatrix = camera->getProjectionMatrix(); - for(Entity* e : entities) - e->draw(viewMatrix, projectionMatrix); + directionnalLights.addLight(position, color); } +void Scene::addPointLight(const glm::vec3 &position, const glm::vec3 &color) +{ + pointLights.addLight(position, color); +} + +// CAMERA + void Scene::setCamera(Camera* myCamera) { camera = myCamera; diff --git a/scene.h b/scene.h index ec9eaae..b79c84c 100644 --- a/scene.h +++ b/scene.h @@ -1,25 +1,39 @@ #ifndef SCENE_H #define SCENE_H -#include "mesh.h" -#include "material.h" +#include "lights.h" #include +#include class Entity; class Camera; +class Mesh; +class Material; +class Shader; class Scene { - // lights + Lights directionnalLights; + Lights pointLights; std::vector entities; Camera* camera; + public: + // main methods Scene() : camera(NULL) {} ~Scene(); + void drawAll(); + + // entities void addEntity(Entity* parent, Mesh* mesh, Material* mat); void addEntity(Mesh* mesh, Material* mat); void addEntity(Entity* entity); - void drawEntities(); + + // lights + void addDirectionnalLight(const glm::vec3 &position, const glm::vec3 &color); + void addPointLight(const glm::vec3 &position, const glm::vec3 &color); + + // camera void setCamera(Camera* myCamera); Camera* getCamera(); }; diff --git a/scenecontroller.cpp b/scenecontroller.cpp index 97cc1e2..8b659a2 100644 --- a/scenecontroller.cpp +++ b/scenecontroller.cpp @@ -1,6 +1,7 @@ #include "scenecontroller.h" #include "scene.h" #include "camera.h" +#include void SceneController::setScene(Scene* myScene) { diff --git a/shader.h b/shader.h index eb5270d..1923648 100644 --- a/shader.h +++ b/shader.h @@ -25,6 +25,7 @@ public: void bindFloat(GLuint location, float val); void bindMatrix(GLuint location, glm::mat4 mat); void bindVec3(GLuint location, glm::vec3 vec); + void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements); void bindTexture(GLuint location, GLuint tex_id); }; diff --git a/skybox.cpp b/skybox.cpp index 2a8c350..f3d9c26 100644 --- a/skybox.cpp +++ b/skybox.cpp @@ -4,6 +4,7 @@ #include "material.h" #include "skyboxmaterial.h" #include "glm/glm.hpp" +#include "glm/ext.hpp" SkyBox::SkyBox(const QString filename[6]) : Entity(NULL, NULL, NULL) { @@ -29,11 +30,10 @@ SkyBox::~SkyBox() void SkyBox::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) { - glm::mat4 mvp = glm::mat4(glm::mat3(projectionMatrix * viewMatrix)); - Shader* shader = mat->getShader(); + glm::mat4 mvp = projectionMatrix * glm::mat4(glm::mat3(viewMatrix)); glAssert(glDepthMask(GL_FALSE)); - shader->bind(); mat->bindAttributes(); + Shader* shader = mat->getShader(); shader->bindMatrix(shader->getLocation("MVP"), mvp); glAssert(glBindVertexArray(vao)); glAssert(glDrawArrays(GL_TRIANGLES, 0, 36)); @@ -41,7 +41,6 @@ void SkyBox::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) } const GLfloat SkyBox::skyboxVertices[] = { - // Positions -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro index ed716da..20ceb3c 100644 --- a/sparrowRenderer.pro +++ b/sparrowRenderer.pro @@ -34,7 +34,8 @@ SOURCES += main.cpp\ skyboxmaterial.cpp \ skybox.cpp \ entity.cpp \ - utils.cpp + utils.cpp \ + lights.cpp HEADERS += mainwindow.h \ myglwidget.h \ @@ -52,7 +53,8 @@ HEADERS += mainwindow.h \ skyboxmaterial.h \ skybox.h \ entity.h \ - utils.h + utils.h \ + lights.h FORMS += mainwindow.ui diff --git a/sphere.cpp b/sphere.cpp index 7d8608c..2ba9b12 100644 --- a/sphere.cpp +++ b/sphere.cpp @@ -23,11 +23,11 @@ Sphere::Sphere(int n) int bottom = 7; int offset = (i+1)%5; // top cap - addFace(0, top+i, top+offset); + addFace(0, top+offset, top+i); // bottom cap addFace(6, bottom+i, bottom+offset); // middle ribbon - addFace(bottom+i, top+offset, top+i); + addFace(top+i, top+offset, bottom+i); addFace(top+offset, bottom+offset, bottom+i); }