work in progress on Mesh

This commit is contained in:
Anselme 2015-08-04 14:35:36 +02:00
parent b51c490eaa
commit 84f0712fc1
13 changed files with 234 additions and 233 deletions

View File

@ -1,19 +0,0 @@
#include "basicmodule.h"
#include "shader.h"
#include "phongentity.h"
#include "camera.h"
void BasicModule::addEntity(PhongEntity* myEntity)
{
entities.push_back(myEntity);
}
void BasicModule::renderGL(Camera* myCamera)
{
shader->bind();
bindModule();
for(PhongEntity* e : entities)
e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix());
}

View File

@ -1,26 +0,0 @@
#ifndef BASICMODULE_H
#define BASICMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
class Shader;
class PhongEntity;
class BasicModule : public Module
{
protected:
Shader* shader;
std::vector<PhongEntity*> entities;
BasicModule(Shader* myShader = NULL) : shader(myShader) {}
virtual void bindModule() = 0;
public:
void addEntity(PhongEntity* myEntity);
void virtual renderGL(Camera* myCamera);
};
#endif // BASICMODULE_H

13
gbuffermodule.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "gbuffermodule.h"
#include "camera.h"
#include "mesh.h"
void GBufferModule::addMesh(Mesh* myMesh)
{
}
void GBufferModule::renderGL(Camera* myCamera)
{
}

23
gbuffermodule.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef GBUFFERMODULE_H
#define GBUFFERMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
class Shader;
class Mesh;
class GBufferModule : public Module
{
public:
Shader* shader;
std::vector<Mesh*> meshes;
GBufferModule(Shader* myShader = NULL) : shader(myShader) {}
void addMesh(Mesh* myMesh);
void virtual renderGL(Camera* myCamera);
};
#endif // GBUFFERMODULE_H

125
mesh.cpp
View File

@ -1,125 +0,0 @@
#include "mesh.h"
#include <glm/ext.hpp>
#include "glassert.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
Mesh::~Mesh()
{
destroyGL();
}
bool Mesh::hasNormals()
{
return normals.size() != 0;
}
bool Mesh::hasTexCoords()
{
return texCoords.size() != 0;
}
bool Mesh::hasTangents()
{
return tangents.size() != 0;
}
void Mesh::initGL(bool isDynamic)
{
if(locked)
destroyGL();
GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
// create VAO
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
int nb_buffers = 2;
if(hasNormals())
++nb_buffers;
if(hasTexCoords())
++nb_buffers;
if(hasTangents())
++nb_buffers;
// create VBOs
glAssert(glGenBuffers(nb_buffers, vbo));
// init indices vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(GLuint), indices.data(), buffer_type));
// init positions vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, positions.size() * sizeof(glm::vec3), positions.data(), buffer_type));
if(hasNormals())
{
// init normals vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), buffer_type));
}
if(hasNormals())
{
// init texCoords vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, texCoords.size() * sizeof(glm::vec2), texCoords.data(), buffer_type));
}
if(hasTangents())
{
// init tangents vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, tangents.size() * sizeof(glm::vec3)*2, tangents.data(), buffer_type));
}
// unbind vao
glAssert(glBindVertexArray(0));
locked = true;
}
void Mesh::destroyGL()
{
if(locked)
{
locked = false;
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(2, vbo));
}
}
void Mesh::draw()
{
if(locked)
{
glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
if(hasNormals())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(1));
}
if(hasTexCoords())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(2));
}
if(hasTangents())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER]));
glAssert(glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Tangents), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(3));
glAssert(glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Tangents), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(4));
}
glAssert(glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data()));
}
}

53
mesh.h
View File

@ -1,56 +1,47 @@
#ifndef MESH_H
#define MESH_H
#include <glew/glew.h>
#include <vector>
#include <glm/vec3.hpp>
#include <glm/vec2.hpp>
class Material;
class Mesh
{
protected:
enum {
// required buffers
INDICES_BUFFER, POSITION_BUFFER,
// optionnal buffers :
NORMAL_BUFFER, TEXCOORD_BUFFER, TANGENT_BUFFER,
NB_BUFFERS
};
public:
typedef struct
{
glm::vec3 tangent;
glm::vec3 binormal;
} Tangents;
typedef struct
{
std::vector<unsigned int> indices;
Material* material;
} Group;
std::vector<glm::vec3> positions;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> texCoords;
std::vector<Tangents> tangents;
std::vector<unsigned int> indices;
std::vector<Group> indiceGroups;
GLuint vao;
GLuint vbo[NB_BUFFERS];
bool locked;
bool hasNormals()
{
return normals.size() != 0;
}
public:
Mesh() : vao(0), locked(false) {}
~Mesh();
bool hasTexCoords()
{
return texCoords.size() != 0;
}
bool hasNormals();
bool hasTexCoords();
bool hasTangents();
/*
* When the mesh is locked, addFace and addVertex do nothing, but the mesh can be drawn
* initGL locks the mesh
* destroyGL unlocks the mesh
*/
void initGL(bool isDynamic = false);
void destroyGL();
void draw();
bool isLocked(){return locked;}
bool hasTangents()
{
return tangents.size() != 0;
}
};
#endif // MESH_H

View File

@ -52,12 +52,27 @@ void MeshBuilder::addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &t
void MeshBuilder::addTriangle(int i1, int i2, int i3)
{
if(!locked)
{
indices.push_back(i1);
indices.push_back(i2);
indices.push_back(i3);
}
indices.push_back(i1);
indices.push_back(i2);
indices.push_back(i3);
}
void MeshBuilder::addGroup(Material* myMaterial)
{
Group g;
g.material = myMaterial;
setCurrentGroup(getNbGroups());
indiceGroups.push_back(g);
}
void MeshBuilder::setCurrentGroup(int groupId)
{
currentGroup = groupId;
}
int MeshBuilder::getNbGroups()
{
return indiceGroups.size();
}
void MeshBuilder::computeTangents()

View File

@ -5,6 +5,8 @@
class MeshBuilder : public Mesh
{
int currentGroup;
public:
void addTriangle(int i1, int i2, int i3);
@ -19,6 +21,10 @@ public:
void addVertex(glm::vec3 &position, glm::vec2 &texCoord);
void addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &texCoord);
void addGroup(Material* myMaterial);
void setCurrentGroup(int groupId);
int getNbGroups();
// require normals and texCoord
void computeTangents();
};

View File

@ -1,10 +1,14 @@
#include "phongentity.h"
#include "shader.h"
#include <glm/glm.hpp>
//#include <glm/glm.hpp>
#include "material.h"
#include "mesh.h"
//#include <glm/ext.hpp>
#include "glassert.h"
PhongEntity::PhongEntity(Mesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {}
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
PhongEntity::PhongEntity(Mesh* myMesh) : mesh(myMesh) {}
void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{
@ -20,18 +24,94 @@ void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMat
mesh->draw();
}
glm::mat4* PhongEntity::getTransform()
void PhongEntity::initGL(bool isDynamic)
{
return &modelMatrix;
GLenum buffer_type = isDynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW;
// create VAO
glAssert(glGenVertexArrays(1, &vao));
glAssert(glBindVertexArray(vao));
nb_buffers = 1; // positions buffer
if(hasNormals())
++nb_buffers;
if(hasTexCoords())
++nb_buffers;
if(hasTangents())
++nb_buffers;
nb_buffers += mesh->indiceGroups.size();
// create VBOs
glAssert(glGenBuffers(nb_buffers, vbo));
for(const Mesh::Group &g : mesh->indiceGroups)
{
// init indices vbos
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, g.indices.size() * sizeof(GLuint), g.indices.data(), buffer_type));
}
// init positions vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->positions.size() * sizeof(glm::vec3), mesh->positions.data(), buffer_type));
if(hasNormals())
{
// init normals vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->normals.size() * sizeof(glm::vec3), mesh->normals.data(), buffer_type));
}
if(hasNormals())
{
// init texCoords vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->texCoords.size() * sizeof(glm::vec2), mesh->texCoords.data(), buffer_type));
}
if(hasTangents())
{
// init tangents vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->tangents.size() * sizeof(glm::vec3)*2, mesh->tangents.data(), buffer_type));
}
// unbind vao
glAssert(glBindVertexArray(0));
}
Shader* PhongEntity::getShader()
void PhongEntity::destroyGL()
{
return mat->getShader();
glAssert(glDeleteVertexArrays(1, &vao));
glAssert(glDeleteBuffers(nb_buffers, vbo));
}
Material* PhongEntity::getMaterial()
void PhongEntity::drawGroup(int groupId)
{
return mat;
glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
if(mesh->hasNormals())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(1));
}
if(mesh->hasTexCoords())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(2));
}
if(mesh->hasTangents())
{
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER]));
glAssert(glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Mesh::Tangents), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(3));
glAssert(glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Mesh::Tangents), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(4));
}
glAssert(glDrawElements(GL_TRIANGLES, mesh->indiceGroups[groupId].size(), GL_UNSIGNED_INT, mesh->indiceGroups[groupId].data()));
}

View File

@ -1,7 +1,9 @@
#ifndef PHONGENTITY_H
#define PHONGENTITY_H
#include "glm/mat4x4.hpp"
#include <glew/glew.h>
//#include <glm/mat4x4.hpp>
#include <vector>
class Mesh;
class Material;
@ -11,16 +13,34 @@ class Shader;
class PhongEntity : public Entity
{
protected:
private:
enum {
// required buffers
POSITION_BUFFER,
// optionnal buffers :
NORMAL_BUFFER, TEXCOORD_BUFFER, TANGENT_BUFFER,
// indices buffers
INDICES_BUFFER
};
Mesh* mesh;
Material* mat;
glm::mat4 modelMatrix;
GLuint vao;
int nb_buffers;
GLuint* vbo;
void drawGroup(int groupId);
public:
glm::mat4 modelMatrix;
PhongEntity(Mesh* myMesh, Material* myMat);
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
glm::mat4* getTransform();
Shader* getShader();
Material* getMaterial();
void initGL(bool isDynamic = false);
void destroyGL();
Mesh* getMesh() {return mesh;}
};
#endif // PHONGENTITY_H

View File

@ -2,9 +2,13 @@
#include "lights.h"
#include "shader.h"
#include "resourcebase.h"
#include "phongentity.h"
#include "camera.h"
PhongModule::PhongModule(Lights* myDirLights, Lights* myPointLights) :
BasicModule(ResourceBase::getShader("phong")), dirLights(myDirLights), pointLights(myPointLights)
dirLights(myDirLights),
pointLights(myPointLights),
shader(ResourceBase::getShader("phong"))
{
nbDirLightsLocation = shader->getLocation("nbDirLights");
dirLightsLocation = shader->getLocation("dirLights");
@ -12,8 +16,16 @@ PhongModule::PhongModule(Lights* myDirLights, Lights* myPointLights) :
pointLightsLocation = shader->getLocation("pointLights");
}
void PhongModule::bindModule()
void PhongModule::addEntity(PhongEntity* myEntity)
{
entities.push_back(myEntity);
}
void PhongModule::renderGL(Camera* myCamera)
{
shader->bind();
dirLights->bind(dirLightsLocation, nbDirLightsLocation, shader);
pointLights->bind(pointLightsLocation, nbPointLightsLocation, shader);
for(PhongEntity* e : entities)
e->draw(myCamera->getViewMatrix(), myCamera->getProjectionMatrix());
}

View File

@ -1,22 +1,34 @@
#ifndef PHONGMODULE_H
#define PHONGMODULE_H
#include "module.h"
#include <vector>
#include <cstddef>
#include <glew/glew.h>
#include "basicmodule.h"
class Shader;
class PhongEntity;
class Lights;
class PhongModule : public BasicModule
class PhongModule : public Module
{
private:
Lights* dirLights;
Lights* pointLights;
GLuint nbDirLightsLocation;
GLuint dirLightsLocation;
GLuint nbPointLightsLocation;
GLuint pointLightsLocation;
Shader* shader;
std::vector<PhongEntity*> entities;
public:
PhongModule(Lights* myDirLights, Lights* myPointLights);
virtual void bindModule();
void addEntity(PhongEntity* myEntity);
void virtual renderGL(Camera* myCamera);
};
#endif // PHONGMODULE_H

View File

@ -33,13 +33,12 @@ SOURCES += shader.cpp \
resourcebase.cpp \
phongmodule.cpp \
skyboxmodule.cpp \
basicmodule.cpp \
framebuffer.cpp \
meshbuilder.cpp \
mesh.cpp \
asciimodule.cpp \
asciientity.cpp \
phongentity.cpp
phongentity.cpp \
gbuffermodule.cpp
HEADERS += shader.h \
camera.h \
@ -54,7 +53,6 @@ HEADERS += shader.h \
resourcebase.h \
phongmodule.h \
skyboxmodule.h \
basicmodule.h \
module.h \
resource.h \
framebuffer.h \
@ -64,7 +62,8 @@ HEADERS += shader.h \
asciimodule.h \
asciientity.h \
phongentity.h \
entity.h
entity.h \
gbuffermodule.h
OTHER_FILES += *.frag *.vert *.glsl *.todo