refactoring has advanced, still work to do

This commit is contained in:
Anselme 2015-08-04 20:10:07 +02:00
parent 84f0712fc1
commit d4df0cd606
6 changed files with 54 additions and 41 deletions

View File

@ -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) void MeshBuilder::addTriangle(int i1, int i2, int i3)
{ {
indices.push_back(i1); indiceGroups[currentGroup].indices.push_back(i1);
indices.push_back(i2); indiceGroups[currentGroup].indices.push_back(i2);
indices.push_back(i3); indiceGroups[currentGroup].indices.push_back(i3);
} }
void MeshBuilder::addGroup(Material* myMaterial) void MeshBuilder::addGroup(Material* myMaterial)
@ -81,15 +81,21 @@ void MeshBuilder::computeTangents()
return; return;
tangents = std::vector<Tangents>(positions.size()); tangents = std::vector<Tangents>(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]; for(const Group &g : indiceGroups)
const glm::vec2& w2 = texCoords[i+1]; for (int j=0; j < g.indices.size(); j += 3)
const glm::vec2& w3 = texCoords[i+2]; {
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 x1 = v2.x - v1.x;
float x2 = v3.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, glm::vec3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
(s1 * z2 - s2 * z1) * r); (s1 * z2 - s2 * z1) * r);
Tangents& tan1 = tangents[i]; Tangents& tan1 = tangents[vertexId0];
Tangents& tan2 = tangents[i+1]; Tangents& tan2 = tangents[vertexId1];
Tangents& tan3 = tangents[i+2]; Tangents& tan3 = tangents[vertexId2];
tan1.tangent += sdir; tan1.tangent += sdir;
tan2.tangent += sdir; tan2.tangent += sdir;

View File

@ -1,9 +1,9 @@
#include "phongentity.h" #include "phongentity.h"
#include "shader.h" #include "shader.h"
//#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "material.h" #include "material.h"
#include "mesh.h" #include "mesh.h"
//#include <glm/ext.hpp> #include <glm/ext.hpp>
#include "glassert.h" #include "glassert.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) #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 modelViewMatrix = viewMatrix * modelMatrix;
glm::mat4 mvp = projectionMatrix * modelViewMatrix; glm::mat4 mvp = projectionMatrix * modelViewMatrix;
glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix)); glm::mat4 normalMatrix = glm::transpose(glm::inverse(modelViewMatrix));
mat->bindAttributes(); for(int i=0; i<mesh->indiceGroups.size(); ++i)
Shader* shader = mat->getShader(); {
shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix); Material* mat = mesh->indiceGroups[i].material;
shader->bindMatrix(shader->getLocation("modelViewMatrix"), modelViewMatrix); mat->bindAttributes();
shader->bindMatrix(shader->getLocation("normalMatrix"), normalMatrix); Shader* shader = mat->getShader();
shader->bindMatrix(shader->getLocation("MVP"), mvp); shader->bindMatrix(shader->getLocation("viewMatrix"), viewMatrix);
mesh->draw(); 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) void PhongEntity::initGL(bool isDynamic)
@ -33,11 +37,11 @@ void PhongEntity::initGL(bool isDynamic)
glAssert(glBindVertexArray(vao)); glAssert(glBindVertexArray(vao));
nb_buffers = 1; // positions buffer nb_buffers = 1; // positions buffer
if(hasNormals()) if(mesh->hasNormals())
++nb_buffers; ++nb_buffers;
if(hasTexCoords()) if(mesh->hasTexCoords())
++nb_buffers; ++nb_buffers;
if(hasTangents()) if(mesh->hasTangents())
++nb_buffers; ++nb_buffers;
nb_buffers += mesh->indiceGroups.size(); nb_buffers += mesh->indiceGroups.size();
@ -56,21 +60,21 @@ void PhongEntity::initGL(bool isDynamic)
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[POSITION_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->positions.size() * sizeof(glm::vec3), mesh->positions.data(), buffer_type)); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->positions.size() * sizeof(glm::vec3), mesh->positions.data(), buffer_type));
if(hasNormals()) if(mesh->hasNormals())
{ {
// init normals vbo // init normals vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[NORMAL_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->normals.size() * sizeof(glm::vec3), mesh->normals.data(), buffer_type)); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->normals.size() * sizeof(glm::vec3), mesh->normals.data(), buffer_type));
} }
if(hasNormals()) if(mesh->hasNormals())
{ {
// init texCoords vbo // init texCoords vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TEXCOORD_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->texCoords.size() * sizeof(glm::vec2), mesh->texCoords.data(), buffer_type)); glAssert(glBufferData(GL_ARRAY_BUFFER, mesh->texCoords.size() * sizeof(glm::vec2), mesh->texCoords.data(), buffer_type));
} }
if(hasTangents()) if(mesh->hasTangents())
{ {
// init tangents vbo // init tangents vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[TANGENT_BUFFER])); 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(glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Mesh::Tangents), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(4)); 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()));
} }

View File

@ -2,7 +2,7 @@
#define PHONGENTITY_H #define PHONGENTITY_H
#include <glew/glew.h> #include <glew/glew.h>
//#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <vector> #include <vector>
class Mesh; class Mesh;
@ -33,7 +33,7 @@ private:
public: public:
glm::mat4 modelMatrix; glm::mat4 modelMatrix;
PhongEntity(Mesh* myMesh, Material* myMat); PhongEntity(Mesh* myMesh);
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);

View File

@ -3,7 +3,7 @@
#include "sparrowrenderer.h" #include "sparrowrenderer.h"
#include "glassert.h" #include "glassert.h"
#include "camera.h" #include "camera.h"
#include "basicmodule.h" #include "module.h"
// main methods // main methods

View File

@ -1,12 +1,15 @@
#include "sphere.h" #include "sphere.h"
#include "glm/ext.hpp" #include "glm/ext.hpp"
#include "phongmaterial.h"
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#define MAGIC_RATIO 0.37139f #define MAGIC_RATIO 0.37139f
Sphere::Sphere(int n) Sphere::Sphere(Material* mat, int n)
{ {
// icosahedron : // icosahedron :
addGroup(mat);
Group* group = &(indiceGroups[0]);
// top cap // top cap
createVertex(0, 1); createVertex(0, 1);
@ -34,25 +37,25 @@ Sphere::Sphere(int n)
// geodesic subdivisions : // geodesic subdivisions :
for(int i=0; i<n; i++){ for(int i=0; i<n; i++){
edges = new Edge[positions.size()-1]; edges = new Edge[positions.size()-1];
int nb_triangles = indices.size()/3; int nb_triangles = group->indices.size()/3;
for(int j=0; j<nb_triangles; j++) for(int j=0; j<nb_triangles; j++)
{ {
int vid[3]; int vid[3];
for(int k=0; k<3; k++) for(int k=0; k<3; k++)
{ {
int idA = indices[j*3 + k]; int idA = group->indices[j*3 + k];
int idB = indices[j*3 + (k+1)%3]; int idB = group->indices[j*3 + (k+1)%3];
int a = idA < idB ? idA : idB; int a = idA < idB ? idA : idB;
int b = idA > idB ? idA : idB; int b = idA > idB ? idA : idB;
vid[k] = getEdge(a, b); vid[k] = getEdge(a, b);
} }
for(int k=0; k<3; k++) 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]); addTriangle(vid[0], vid[1], vid[2]);
} }
delete[](edges); delete[](edges);
indices.erase(indices.begin(), indices.begin()+nb_triangles*3); group->indices.erase(group->indices.begin(), group->indices.begin()+nb_triangles*3);
} }
} }

View File

@ -19,7 +19,7 @@ private:
int getEdge(int a, int b); int getEdge(int a, int b);
void createVertex(float u, float v); void createVertex(float u, float v);
public: public:
Sphere(int n = 0); Sphere(Material* mat, int n = 0);
}; };
#endif // SPHERE_H #endif // SPHERE_H