This commit is contained in:
Anselme 2015-06-30 18:51:37 +02:00
commit 1bf29cac13
12 changed files with 109 additions and 22 deletions

View File

@ -8,11 +8,15 @@ void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{ {
glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glm::mat4 modelViewMatrix = viewMatrix * modelMatrix;
glm::mat4 mvp = projectionMatrix * modelViewMatrix; glm::mat4 mvp = projectionMatrix * modelViewMatrix;
Shader* shader = mat->getShader();
shader->bind();
mat->bindAttributes(); mat->bindAttributes();
Shader* shader = mat->getShader();
shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix);
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix);
shader->bindMatrix(shader->getLocation("MVP"), mvp); shader->bindMatrix(shader->getLocation("MVP"), mvp);
mesh->draw(); mesh->draw();
} }
Shader* Entity::getShader()
{
return mat->getShader();
}

View File

@ -5,6 +5,7 @@
class Mesh; class Mesh;
class Material; class Material;
class Shader;
class Entity class Entity
{ {
@ -16,6 +17,7 @@ protected:
public: public:
Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : parent(myParent), mesh(myMesh), mat(myMat) {} Entity(Entity* myParent, Mesh* myMesh, Material* myMat) : parent(myParent), mesh(myMesh), mat(myMat) {}
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
Shader* getShader();
}; };
#endif // ENTITY_H #endif // ENTITY_H

14
lights.cpp Normal file
View File

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

25
lights.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef LIGHT_H
#define LIGHT_H
#include <glm/vec3.hpp>
#include <glew/glew.h>
#include <vector>
class Shader;
class Lights
{
private:
typedef struct
{
glm::vec3 position;
glm::vec3 color;
} Light;
std::vector<Light> 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

View File

@ -71,9 +71,9 @@ void MyGLWidget::buildScene()
Texture* tex = new Texture("../data/noise.png"); Texture* tex = new Texture("../data/noise.png");
mat->setTexture(tex); mat->setTexture(tex);
QString filenames[6] = { QString filenames[6] = {
"../data/skybox_lf", "../data/skybox_rt", "../data/skybox_ft", "../data/skybox_bk",
"../data/skybox_up", "../data/skybox_dn", "../data/skybox_up", "../data/skybox_dn",
"../data/skybox_bk", "../data/skybox_ft" "../data/skybox_lf", "../data/skybox_rt"
}; };
SkyBox* skybox = new SkyBox(filenames); SkyBox* skybox = new SkyBox(filenames);
scene->addEntity(skybox); scene->addEntity(skybox);
@ -91,7 +91,7 @@ void MyGLWidget::paintGL()
{ {
glAssert(glClearColor(0.60, 0.65, 0.75, 1.0)); glAssert(glClearColor(0.60, 0.65, 0.75, 1.0));
glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); glAssert(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
scene->drawEntities(); scene->drawAll();
} }
void MyGLWidget::mouseMoveEvent(QMouseEvent *e) void MyGLWidget::mouseMoveEvent(QMouseEvent *e)

View File

@ -4,6 +4,9 @@
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include "camera.h" #include "camera.h"
#include "entity.h" #include "entity.h"
#include "shader.h"
// MAIN METHODS
Scene::~Scene() Scene::~Scene()
{ {
@ -11,6 +14,22 @@ Scene::~Scene()
delete(e); 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) void Scene::addEntity(Entity* parent, Mesh* mesh, Material* mat)
{ {
entities.push_back(new Entity(parent, mesh, mat)); entities.push_back(new Entity(parent, mesh, mat));
@ -26,14 +45,20 @@ void Scene::addEntity(Entity* entity)
entities.push_back(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(); directionnalLights.addLight(position, color);
glm::mat4 projectionMatrix = camera->getProjectionMatrix();
for(Entity* e : entities)
e->draw(viewMatrix, projectionMatrix);
} }
void Scene::addPointLight(const glm::vec3 &position, const glm::vec3 &color)
{
pointLights.addLight(position, color);
}
// CAMERA
void Scene::setCamera(Camera* myCamera) void Scene::setCamera(Camera* myCamera)
{ {
camera = myCamera; camera = myCamera;

22
scene.h
View File

@ -1,25 +1,39 @@
#ifndef SCENE_H #ifndef SCENE_H
#define SCENE_H #define SCENE_H
#include "mesh.h" #include "lights.h"
#include "material.h"
#include <glew/glew.h> #include <glew/glew.h>
#include <vector>
class Entity; class Entity;
class Camera; class Camera;
class Mesh;
class Material;
class Shader;
class Scene class Scene
{ {
// lights Lights directionnalLights;
Lights pointLights;
std::vector<Entity*> entities; std::vector<Entity*> entities;
Camera* camera; Camera* camera;
public: public:
// main methods
Scene() : camera(NULL) {} Scene() : camera(NULL) {}
~Scene(); ~Scene();
void drawAll();
// entities
void addEntity(Entity* parent, Mesh* mesh, Material* mat); void addEntity(Entity* parent, Mesh* mesh, Material* mat);
void addEntity(Mesh* mesh, Material* mat); void addEntity(Mesh* mesh, Material* mat);
void addEntity(Entity* entity); 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); void setCamera(Camera* myCamera);
Camera* getCamera(); Camera* getCamera();
}; };

View File

@ -1,6 +1,7 @@
#include "scenecontroller.h" #include "scenecontroller.h"
#include "scene.h" #include "scene.h"
#include "camera.h" #include "camera.h"
#include <QObject>
void SceneController::setScene(Scene* myScene) void SceneController::setScene(Scene* myScene)
{ {

View File

@ -25,6 +25,7 @@ public:
void bindFloat(GLuint location, float val); void bindFloat(GLuint location, float val);
void bindMatrix(GLuint location, glm::mat4 mat); void bindMatrix(GLuint location, glm::mat4 mat);
void bindVec3(GLuint location, glm::vec3 vec); void bindVec3(GLuint location, glm::vec3 vec);
void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements);
void bindTexture(GLuint location, GLuint tex_id); void bindTexture(GLuint location, GLuint tex_id);
}; };

View File

@ -4,6 +4,7 @@
#include "material.h" #include "material.h"
#include "skyboxmaterial.h" #include "skyboxmaterial.h"
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/ext.hpp"
SkyBox::SkyBox(const QString filename[6]) : Entity(NULL, NULL, NULL) 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) void SkyBox::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{ {
glm::mat4 mvp = glm::mat4(glm::mat3(projectionMatrix * viewMatrix)); glm::mat4 mvp = projectionMatrix * glm::mat4(glm::mat3(viewMatrix));
Shader* shader = mat->getShader();
glAssert(glDepthMask(GL_FALSE)); glAssert(glDepthMask(GL_FALSE));
shader->bind();
mat->bindAttributes(); mat->bindAttributes();
Shader* shader = mat->getShader();
shader->bindMatrix(shader->getLocation("MVP"), mvp); shader->bindMatrix(shader->getLocation("MVP"), mvp);
glAssert(glBindVertexArray(vao)); glAssert(glBindVertexArray(vao));
glAssert(glDrawArrays(GL_TRIANGLES, 0, 36)); 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[] = { const GLfloat SkyBox::skyboxVertices[] = {
// Positions
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f,

View File

@ -34,7 +34,8 @@ SOURCES += main.cpp\
skyboxmaterial.cpp \ skyboxmaterial.cpp \
skybox.cpp \ skybox.cpp \
entity.cpp \ entity.cpp \
utils.cpp utils.cpp \
lights.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
myglwidget.h \ myglwidget.h \
@ -52,7 +53,8 @@ HEADERS += mainwindow.h \
skyboxmaterial.h \ skyboxmaterial.h \
skybox.h \ skybox.h \
entity.h \ entity.h \
utils.h utils.h \
lights.h
FORMS += mainwindow.ui FORMS += mainwindow.ui

View File

@ -23,11 +23,11 @@ Sphere::Sphere(int n)
int bottom = 7; int bottom = 7;
int offset = (i+1)%5; int offset = (i+1)%5;
// top cap // top cap
addFace(0, top+i, top+offset); addFace(0, top+offset, top+i);
// bottom cap // bottom cap
addFace(6, bottom+i, bottom+offset); addFace(6, bottom+i, bottom+offset);
// middle ribbon // middle ribbon
addFace(bottom+i, top+offset, top+i); addFace(top+i, top+offset, bottom+i);
addFace(top+offset, bottom+offset, bottom+i); addFace(top+offset, bottom+offset, bottom+i);
} }