From d4df0cd6069859471b867d5edadbb3401a554d59 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 4 Aug 2015 20:10:07 +0200 Subject: [PATCH] refactoring has advanced, still work to do --- meshbuilder.cpp | 34 ++++++++++++++++++++-------------- phongentity.cpp | 36 ++++++++++++++++++++---------------- phongentity.h | 4 ++-- sparrowrenderer.cpp | 2 +- sphere.cpp | 17 ++++++++++------- sphere.h | 2 +- 6 files changed, 54 insertions(+), 41 deletions(-) diff --git a/meshbuilder.cpp b/meshbuilder.cpp index e67f49c..93edb83 100644 --- a/meshbuilder.cpp +++ b/meshbuilder.cpp @@ -52,9 +52,9 @@ void MeshBuilder::addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &t void MeshBuilder::addTriangle(int i1, int i2, int i3) { - indices.push_back(i1); - indices.push_back(i2); - indices.push_back(i3); + indiceGroups[currentGroup].indices.push_back(i1); + indiceGroups[currentGroup].indices.push_back(i2); + indiceGroups[currentGroup].indices.push_back(i3); } void MeshBuilder::addGroup(Material* myMaterial) @@ -81,15 +81,21 @@ void MeshBuilder::computeTangents() return; tangents = std::vector(positions.size()); - for (int i=0; i < indices.size(); i += 3) - { - const glm::vec3 &v1 = positions[i]; - const glm::vec3 &v2 = positions[i+1]; - const glm::vec3 &v3 = positions[i+2]; - const glm::vec2& w1 = texCoords[i]; - const glm::vec2& w2 = texCoords[i+1]; - const glm::vec2& w3 = texCoords[i+2]; + for(const Group &g : indiceGroups) + for (int j=0; j < g.indices.size(); j += 3) + { + int vertexId0 = g.indices[j]; + int vertexId1 = g.indices[j+1]; + int vertexId2 = g.indices[j+2]; + + const glm::vec3 &v1 = positions[vertexId0]; + const glm::vec3 &v2 = positions[vertexId1]; + const glm::vec3 &v3 = positions[vertexId2]; + + const glm::vec2& w1 = texCoords[vertexId0]; + const glm::vec2& w2 = texCoords[vertexId1]; + const glm::vec2& w3 = texCoords[vertexId2]; float x1 = v2.x - v1.x; float x2 = v3.x - v1.x; @@ -109,9 +115,9 @@ void MeshBuilder::computeTangents() glm::vec3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); - Tangents& tan1 = tangents[i]; - Tangents& tan2 = tangents[i+1]; - Tangents& tan3 = tangents[i+2]; + Tangents& tan1 = tangents[vertexId0]; + Tangents& tan2 = tangents[vertexId1]; + Tangents& tan3 = tangents[vertexId2]; tan1.tangent += sdir; tan2.tangent += sdir; diff --git a/phongentity.cpp b/phongentity.cpp index 7a899ec..71b43e8 100644 --- a/phongentity.cpp +++ b/phongentity.cpp @@ -1,9 +1,9 @@ #include "phongentity.h" #include "shader.h" -//#include +#include #include "material.h" #include "mesh.h" -//#include +#include #include "glassert.h" #define BUFFER_OFFSET(i) ((char *)NULL + (i)) @@ -15,13 +15,17 @@ void PhongEntity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMat glm::mat4 modelViewMatrix = viewMatrix * modelMatrix; glm::mat4 mvp = projectionMatrix * modelViewMatrix; glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix)); - mat->bindAttributes(); - Shader* shader = mat->getShader(); - shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); - shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); - shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); - shader->bindMatrix(shader->getLocation("MVP"), mvp); - mesh->draw(); + for(int i=0; iindiceGroups.size(); ++i) + { + Material* mat = mesh->indiceGroups[i].material; + mat->bindAttributes(); + Shader* shader = mat->getShader(); + shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); + shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); + shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); + shader->bindMatrix(shader->getLocation("MVP"), mvp); + drawGroup(i); + } } void PhongEntity::initGL(bool isDynamic) @@ -33,11 +37,11 @@ void PhongEntity::initGL(bool isDynamic) glAssert(glBindVertexArray(vao)); nb_buffers = 1; // positions buffer - if(hasNormals()) + if(mesh->hasNormals()) ++nb_buffers; - if(hasTexCoords()) + if(mesh->hasTexCoords()) ++nb_buffers; - if(hasTangents()) + if(mesh->hasTangents()) ++nb_buffers; nb_buffers += mesh->indiceGroups.size(); @@ -56,21 +60,21 @@ void PhongEntity::initGL(bool isDynamic) 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()) + if(mesh->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()) + if(mesh->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()) + if(mesh->hasTangents()) { // init tangents vbo glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER])); @@ -113,5 +117,5 @@ void PhongEntity::drawGroup(int groupId) 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())); + glAssert(glDrawElements(GL_TRIANGLES, mesh->indiceGroups[groupId].indices.size(), GL_UNSIGNED_INT, mesh->indiceGroups[groupId].indices.data())); } diff --git a/phongentity.h b/phongentity.h index 9eb9bac..f6620f4 100644 --- a/phongentity.h +++ b/phongentity.h @@ -2,7 +2,7 @@ #define PHONGENTITY_H #include -//#include +#include #include class Mesh; @@ -33,7 +33,7 @@ private: public: glm::mat4 modelMatrix; - PhongEntity(Mesh* myMesh, Material* myMat); + PhongEntity(Mesh* myMesh); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); diff --git a/sparrowrenderer.cpp b/sparrowrenderer.cpp index 3a0fa8c..01d7c21 100644 --- a/sparrowrenderer.cpp +++ b/sparrowrenderer.cpp @@ -3,7 +3,7 @@ #include "sparrowrenderer.h" #include "glassert.h" #include "camera.h" -#include "basicmodule.h" +#include "module.h" // main methods diff --git a/sphere.cpp b/sphere.cpp index ed10229..976dfa9 100644 --- a/sphere.cpp +++ b/sphere.cpp @@ -1,12 +1,15 @@ #include "sphere.h" #include "glm/ext.hpp" +#include "phongmaterial.h" #define M_PI 3.14159265358979323846 #define MAGIC_RATIO 0.37139f -Sphere::Sphere(int n) +Sphere::Sphere(Material* mat, int n) { // icosahedron : + addGroup(mat); + Group* group = &(indiceGroups[0]); // top cap createVertex(0, 1); @@ -34,25 +37,25 @@ Sphere::Sphere(int n) // geodesic subdivisions : for(int i=0; iindices.size()/3; for(int j=0; jindices[j*3 + k]; + int idB = group->indices[j*3 + (k+1)%3]; int a = idA < idB ? idA : idB; int b = idA > idB ? idA : idB; vid[k] = getEdge(a, b); } for(int k=0; k<3; k++) - addTriangle(indices[j*3 + k], vid[k], vid[(k+2)%3]); + addTriangle(group->indices[j*3 + k], vid[k], vid[(k+2)%3]); addTriangle(vid[0], vid[1], vid[2]); } delete[](edges); - indices.erase(indices.begin(), indices.begin()+nb_triangles*3); + group->indices.erase(group->indices.begin(), group->indices.begin()+nb_triangles*3); } } diff --git a/sphere.h b/sphere.h index ef2334b..b347490 100644 --- a/sphere.h +++ b/sphere.h @@ -19,7 +19,7 @@ private: int getEdge(int a, int b); void createVertex(float u, float v); public: - Sphere(int n = 0); + Sphere(Material* mat, int n = 0); }; #endif // SPHERE_H