ended mesh refactoring

This commit is contained in:
Anselme 2016-01-07 15:50:02 +01:00
parent b303c4b1ff
commit 160ebc0d2d
4 changed files with 51 additions and 50 deletions

View File

@ -86,26 +86,24 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light)
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry(); for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next()) geometryIt->isValid(); geometryIt->next())
{ {
// compute matrix attributes
GeometryNode* node = geometryIt->getItem(); GeometryNode* node = geometryIt->getItem();
glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * node->modelMatrix; Material* mat = node->mesh->material;
glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix; if(mat->getFlags() == geometryFlagList[i]) // if flag matches material
glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
if(light != NULL && light->isShadowCaster())
{ {
glm::mat4 lightMVP = Light::biasMatrix * light->getProjectionMatrix() * light->getViewMatrix() * node->modelMatrix; // compute matrix attributes
shader->bindMat4(shader->getLocation("lightMVP"), lightMVP); glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * node->modelMatrix;
} glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix;
shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); if(light != NULL && light->isShadowCaster())
shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix)); {
shader->bindMat4(shader->getLocation("MVP"), mvp); glm::mat4 lightMVP = Light::biasMatrix * light->getProjectionMatrix() * light->getViewMatrix() * node->modelMatrix;
// loop over material groups shader->bindMat4(shader->getLocation("lightMVP"), lightMVP);
for(int j=0; j<node->mesh->indiceGroups.size(); ++j) }
{ shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix());
Material* mat = node->getMesh()->indiceGroups[j].material; shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix);
if(mat->getFlags() == geometryFlagList[i]) shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix));
node->mesh->draw(shader); shader->bindMat4(shader->getLocation("MVP"), mvp);
node->mesh->draw(shader);
} }
} }
} }
@ -134,12 +132,11 @@ void ForwardModule::compileShaders(Scene* scene)
for(int i=0; i<nb_geometry_flags; ++i) for(int i=0; i<nb_geometry_flags; ++i)
geometryFlags[i] = false; geometryFlags[i] = false;
for(SceneIterator<PhongEntity*>* entityIt = scene->getGeometry(); for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
entityIt->isValid(); entityIt->next()) geometryIt->isValid(); geometryIt->next())
{ {
Mesh* m = entityIt->getItem()->getMesh(); Mesh* m = geometryIt->getItem()->mesh;
for(Mesh::Group &g : m->indiceGroups) geometryFlags[m->material->getFlags()] = true;
geometryFlags[g.material->getFlags()] = true;
} }
for(int i=0; i<nb_geometry_flags; ++i) for(int i=0; i<nb_geometry_flags; ++i)
{ {

View File

@ -102,31 +102,28 @@ void Light::generateShadowMap(Scene* scene)
glAssert(glEnable(GL_DEPTH_TEST)); glAssert(glEnable(GL_DEPTH_TEST));
glAssert(glDepthFunc(GL_LESS)); glAssert(glDepthFunc(GL_LESS));
glAssert(glCullFace(GL_FRONT)); glAssert(glCullFace(GL_FRONT));
for(SceneIterator<PhongEntity*>* entityIt = scene->getGeometry(); for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
entityIt->isValid(); entityIt->next()) geometryIt->isValid(); geometryIt->next())
{ {
// compute matrix attributes // compute matrix attributes
PhongEntity* entity = entityIt->getItem(); GeometryNode* node = geometryIt->getItem();
glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * entity->modelMatrix); glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * node->modelMatrix);
// loop over material groups // loop over material groups
for(int j=0; j<entity->getMesh()->indiceGroups.size(); ++j) Material* mat = node->mesh->material;
if(mat->getFlags() & ALPHA_MASK_FLAG)
{ {
Material* mat = entity->getMesh()->indiceGroups[j].material; PhongMaterial* pmat = (PhongMaterial*)mat;
if(mat->getFlags() & ALPHA_MASK_FLAG) shaders[1]->bind();
{ pmat->alpha_mask->bind(ALPHA_MASK);
PhongMaterial* pmat = (PhongMaterial*)mat; shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
shaders[1]->bind(); shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK);
pmat->alpha_mask->bind(ALPHA_MASK); node->mesh->draw(shaders[1], false, true, false);
shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP); }
shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK); else
entity->drawGroup(j, false, true, false); {
} shaders[0]->bind();
else shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
{ node->mesh->draw(shaders[0], false, false, false);
entity->drawGroup(j, false, false, false);
shaders[0]->bind();
shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP);
}
} }
} }
glAssert(glCullFace(GL_BACK)); glAssert(glCullFace(GL_BACK));

View File

@ -3,6 +3,9 @@
#include <set> #include <set>
#include "glassert.h" #include "glassert.h"
#include "sparrowrenderer.h" #include "sparrowrenderer.h"
#include "material.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
Mesh::Mesh() : Mesh::Mesh() :
material(NULL), material(NULL),
@ -28,11 +31,11 @@ void Mesh::initGL(bool isDynamic)
glAssert(glBindVertexArray(vao)); glAssert(glBindVertexArray(vao));
nb_buffers = 2; // positions and indices buffers nb_buffers = 2; // positions and indices buffers
if(mesh->hasNormals()) if(hasNormals())
++nb_buffers; ++nb_buffers;
if(mesh->hasTexCoords()) if(hasTexCoords())
++nb_buffers; ++nb_buffers;
if(mesh->hasTangents()) if(hasTangents())
++nb_buffers; ++nb_buffers;
// create VBOs // create VBOs
@ -75,7 +78,7 @@ void Mesh::initGL(bool isDynamic)
void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTangents) void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTangents)
{ {
bool crappy = (shader == NULL); bool crappy = (shader == NULL);
material.bindAttributes(shader); material->bindAttributes(shader);
glAssert(glBindVertexArray(vao)); glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
if(crappy) if(crappy)
@ -154,7 +157,7 @@ struct VertexComparator
{ {
// c'est plutot crade mais j'ai pas trouve d'autre moyen pour le moment // c'est plutot crade mais j'ai pas trouve d'autre moyen pour le moment
static Mesh* mesh; static Mesh* mesh;
static void setMesh(MeshBuilder* m) {VertexComparator::mesh = m;} static void setMesh(Mesh* m) {VertexComparator::mesh = m;}
bool operator() (const int& vertId1, const int& vertId2) const bool operator() (const int& vertId1, const int& vertId2) const
{ {
@ -198,7 +201,7 @@ void Mesh::mergeVertices()
{ {
if(indices[i] >= positions.size()) if(indices[i] >= positions.size())
indices[i] = swapped[swappedOffset - indices[i]]; indices[i] = swapped[swappedOffset - indices[i]];
std::pair<std::set<int,VertexComparator>::iterator,bool> ret = vertexSet.insert(g.indices[i]); std::pair<std::set<int,VertexComparator>::iterator,bool> ret = vertexSet.insert(indices[i]);
if(!ret.second) // duplicate found if(!ret.second) // duplicate found
{ {
// updating indices references // updating indices references

View File

@ -13,6 +13,7 @@ struct Mesh
{ {
protected: protected:
public: // TODO : see if there is a way to set this protected
// geometry data // geometry data
typedef struct typedef struct
@ -21,6 +22,7 @@ protected:
glm::vec3 binormal; glm::vec3 binormal;
} Tangents; } Tangents;
Material* material; Material* material;
std::vector<glm::vec3> positions; std::vector<glm::vec3> positions;
@ -29,6 +31,8 @@ protected:
std::vector<Tangents> tangents; std::vector<Tangents> tangents;
std::vector<unsigned int> indices; std::vector<unsigned int> indices;
protected:
// opengl // opengl
enum { enum {