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

View File

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

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");
mat->setTexture(tex);
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);
@ -91,7 +91,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)

View File

@ -4,6 +4,9 @@
#include <glm/ext.hpp>
#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;

22
scene.h
View File

@ -1,25 +1,39 @@
#ifndef SCENE_H
#define SCENE_H
#include "mesh.h"
#include "material.h"
#include "lights.h"
#include <glew/glew.h>
#include <vector>
class Entity;
class Camera;
class Mesh;
class Material;
class Shader;
class Scene
{
// lights
Lights directionnalLights;
Lights pointLights;
std::vector<Entity*> 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();
};

View File

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

View File

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

View File

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

View File

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

View File

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