diff --git a/src/forwardmodule.cpp b/src/forwardmodule.cpp index 686f754..c128339 100644 --- a/src/forwardmodule.cpp +++ b/src/forwardmodule.cpp @@ -86,26 +86,24 @@ void ForwardModule::lightPass(Camera* myCamera, Scene* scene, Light* light) for(SceneIterator* geometryIt = scene->getGeometry(); geometryIt->isValid(); geometryIt->next()) { - // compute matrix attributes GeometryNode* node = geometryIt->getItem(); - glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * node->modelMatrix; - glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix; - glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix)); - if(light != NULL && light->isShadowCaster()) + Material* mat = node->mesh->material; + if(mat->getFlags() == geometryFlagList[i]) // if flag matches material { - glm::mat4 lightMVP = Light::biasMatrix * light->getProjectionMatrix() * light->getViewMatrix() * node->modelMatrix; - shader->bindMat4(shader->getLocation("lightMVP"), lightMVP); - } - shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); - shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); - shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix)); - shader->bindMat4(shader->getLocation("MVP"), mvp); - // loop over material groups - for(int j=0; jmesh->indiceGroups.size(); ++j) - { - Material* mat = node->getMesh()->indiceGroups[j].material; - if(mat->getFlags() == geometryFlagList[i]) - node->mesh->draw(shader); + // compute matrix attributes + glm::mat4 modelViewMatrix = myCamera->getViewMatrix() * node->modelMatrix; + glm::mat4 mvp = myCamera->getProjectionMatrix() * modelViewMatrix; + glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix)); + if(light != NULL && light->isShadowCaster()) + { + glm::mat4 lightMVP = Light::biasMatrix * light->getProjectionMatrix() * light->getViewMatrix() * node->modelMatrix; + shader->bindMat4(shader->getLocation("lightMVP"), lightMVP); + } + shader->bindMat4(shader->getLocation("viewMatrix"), myCamera->getViewMatrix()); + shader->bindMat4(shader->getLocation("modelViewMatrix"), modelViewMatrix); + shader->bindMat3(shader->getLocation("normalMatrix"), glm::mat3(normalMatrix)); + shader->bindMat4(shader->getLocation("MVP"), mvp); + node->mesh->draw(shader); } } } @@ -134,12 +132,11 @@ void ForwardModule::compileShaders(Scene* scene) for(int i=0; i* entityIt = scene->getGeometry(); - entityIt->isValid(); entityIt->next()) + for(SceneIterator* geometryIt = scene->getGeometry(); + geometryIt->isValid(); geometryIt->next()) { - Mesh* m = entityIt->getItem()->getMesh(); - for(Mesh::Group &g : m->indiceGroups) - geometryFlags[g.material->getFlags()] = true; + Mesh* m = geometryIt->getItem()->mesh; + geometryFlags[m->material->getFlags()] = true; } for(int i=0; i* entityIt = scene->getGeometry(); - entityIt->isValid(); entityIt->next()) + for(SceneIterator* geometryIt = scene->getGeometry(); + geometryIt->isValid(); geometryIt->next()) { // compute matrix attributes - PhongEntity* entity = entityIt->getItem(); - glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * entity->modelMatrix); + GeometryNode* node = geometryIt->getItem(); + glm::mat4 lightMVP = getProjectionMatrix() * (getViewMatrix() * node->modelMatrix); // loop over material groups - for(int j=0; jgetMesh()->indiceGroups.size(); ++j) + Material* mat = node->mesh->material; + if(mat->getFlags() & ALPHA_MASK_FLAG) { - Material* mat = entity->getMesh()->indiceGroups[j].material; - if(mat->getFlags() & ALPHA_MASK_FLAG) - { - PhongMaterial* pmat = (PhongMaterial*)mat; - shaders[1]->bind(); - pmat->alpha_mask->bind(ALPHA_MASK); - shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP); - shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK); - entity->drawGroup(j, false, true, false); - } - else - { - entity->drawGroup(j, false, false, false); - shaders[0]->bind(); - shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP); - } + PhongMaterial* pmat = (PhongMaterial*)mat; + shaders[1]->bind(); + pmat->alpha_mask->bind(ALPHA_MASK); + shaders[1]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP); + shaders[1]->bindInteger(shaders[1]->getLocation("alphaMask"), ALPHA_MASK); + node->mesh->draw(shaders[1], false, true, false); + } + else + { + shaders[0]->bind(); + shaders[0]->bindMat4(shaders[1]->getLocation("MVP"), lightMVP); + node->mesh->draw(shaders[0], false, false, false); } } glAssert(glCullFace(GL_BACK)); diff --git a/src/mesh.cpp b/src/mesh.cpp index 20dd468..63f06ee 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -3,6 +3,9 @@ #include #include "glassert.h" #include "sparrowrenderer.h" +#include "material.h" + +#define BUFFER_OFFSET(i) ((char *)NULL + (i)) Mesh::Mesh() : material(NULL), @@ -28,11 +31,11 @@ void Mesh::initGL(bool isDynamic) glAssert(glBindVertexArray(vao)); nb_buffers = 2; // positions and indices buffers - if(mesh->hasNormals()) + if(hasNormals()) ++nb_buffers; - if(mesh->hasTexCoords()) + if(hasTexCoords()) ++nb_buffers; - if(mesh->hasTangents()) + if(hasTangents()) ++nb_buffers; // create VBOs @@ -75,7 +78,7 @@ void Mesh::initGL(bool isDynamic) void Mesh::draw(Shader* shader, bool drawNormals, bool drawTexCoord, bool drawTangents) { bool crappy = (shader == NULL); - material.bindAttributes(shader); + material->bindAttributes(shader); glAssert(glBindVertexArray(vao)); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER])); if(crappy) @@ -154,7 +157,7 @@ struct VertexComparator { // c'est plutot crade mais j'ai pas trouve d'autre moyen pour le moment 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 { @@ -198,7 +201,7 @@ void Mesh::mergeVertices() { if(indices[i] >= positions.size()) indices[i] = swapped[swappedOffset - indices[i]]; - std::pair::iterator,bool> ret = vertexSet.insert(g.indices[i]); + std::pair::iterator,bool> ret = vertexSet.insert(indices[i]); if(!ret.second) // duplicate found { // updating indices references diff --git a/src/mesh.h b/src/mesh.h index 5e4b42e..8b9d2bf 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -13,6 +13,7 @@ struct Mesh { protected: +public: // TODO : see if there is a way to set this protected // geometry data typedef struct @@ -21,6 +22,7 @@ protected: glm::vec3 binormal; } Tangents; + Material* material; std::vector positions; @@ -29,6 +31,8 @@ protected: std::vector tangents; std::vector indices; +protected: + // opengl enum {