refactoring has advanced, still work to do
This commit is contained in:
parent
84f0712fc1
commit
d4df0cd606
@ -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<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];
|
||||
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;
|
||||
|
@ -1,9 +1,9 @@
|
||||
#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 <glm/ext.hpp>
|
||||
#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));
|
||||
for(int i=0; i<mesh->indiceGroups.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);
|
||||
mesh->draw();
|
||||
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()));
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define PHONGENTITY_H
|
||||
|
||||
#include <glew/glew.h>
|
||||
//#include <glm/mat4x4.hpp>
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <vector>
|
||||
|
||||
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);
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include "sparrowrenderer.h"
|
||||
#include "glassert.h"
|
||||
#include "camera.h"
|
||||
#include "basicmodule.h"
|
||||
#include "module.h"
|
||||
|
||||
// main methods
|
||||
|
||||
|
15
sphere.cpp
15
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);
|
||||
@ -35,24 +38,24 @@ Sphere::Sphere(int n)
|
||||
// geodesic subdivisions :
|
||||
for(int i=0; i<n; i++){
|
||||
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++)
|
||||
{
|
||||
int vid[3];
|
||||
for(int k=0; k<3; k++)
|
||||
{
|
||||
int idA = indices[j*3 + k];
|
||||
int idB = indices[j*3 + (k+1)%3];
|
||||
int idA = group->indices[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);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user