changed how meshed and vertex are handled

This commit is contained in:
Anselme 2015-07-16 23:57:59 +02:00
parent a6c9a55fb9
commit 92c40cff48
21 changed files with 213 additions and 173 deletions

30
bumpmappedmesh.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "bumpmappedmesh.h"
#include "glassert.h"
void BumpMappedMesh::addVertex(const SRFVertex& v)
{
if(!locked)
vertices.push_back(v);
}
void BumpMappedMesh::setAttribPointer()
{
glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]));
// position attribute
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
// normal attribute
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(1));
// texCoord attribute
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(2*sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(2));
// normal attribute
glAssert(glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(2*sizeof(glm::vec3)+sizeof(glm::vec2))));
glAssert(glEnableVertexAttribArray(3));
// texCoord attribute
glAssert(glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(SRFVertex), BUFFER_OFFSET(3*sizeof(glm::vec3)+sizeof(glm::vec2))));
glAssert(glEnableVertexAttribArray(4));
}

34
bumpmappedmesh.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef BUMPMAPPEDMESH_H
#define BUMPMAPPEDMESH_H
#include "vertex.h"
#include "mesh.h"
class BumpMappedMesh : public Mesh
{
protected:
std::vector<SRFVertex> vertices;
virtual int getNbVertices()
{
return vertices.size();
}
virtual int getSizeofVertexType()
{
return sizeof(SRFVertex);
}
virtual void* getVertexData()
{
return (void*)(vertices.data());
}
virtual void setAttribPointer();
public:
void addVertex(const SRFVertex& v);
};
#endif // BUMPMAPPEDMESH_H

View File

@ -2,9 +2,9 @@
#include "shader.h" #include "shader.h"
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "material.h" #include "material.h"
#include "mesh.h" #include "standardmesh.h"
Entity::Entity(Mesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {} Entity::Entity(StandardMesh* myMesh, Material* myMat) : mesh(myMesh), mat(myMat) {}
void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix) void Entity::draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix)
{ {

View File

@ -4,18 +4,18 @@
#include "glm/mat4x4.hpp" #include "glm/mat4x4.hpp"
#include <vector> #include <vector>
class Mesh; class StandardMesh;
class Material; class Material;
class Shader; class Shader;
class Entity class Entity
{ {
protected: protected:
Mesh* mesh; StandardMesh* mesh;
Material* mat; Material* mat;
glm::mat4 modelMatrix; glm::mat4 modelMatrix;
public: public:
Entity(Mesh* myMesh, Material* myMat); Entity(StandardMesh* myMesh, Material* myMat);
virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix); virtual void draw(const glm::mat4 viewMatrix, const glm::mat4 projectionMatrix);
glm::mat4* getTransform(); glm::mat4* getTransform();
Shader* getShader(); Shader* getShader();

View File

@ -1,12 +1,12 @@
#include "gridmesh.h" #include "gridmesh.h"
GridMesh::GridMesh(int width, int height, bool alternate) : Mesh(), m_width(width), m_height(height) GridMesh::GridMesh(int width, int height, bool alternate) : StandardMesh(), m_width(width), m_height(height)
{ {
for(int i=0; i<=width; ++i) for(int i=0; i<=width; ++i)
{ {
for(int j=0; j<=height; ++j) for(int j=0; j<=height; ++j)
{ {
Vertex v; TexturedVertex v;
v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0); v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0);
v.normal = glm::vec3(0, 0, 1); v.normal = glm::vec3(0, 0, 1);
v.texCoord = glm::vec2(v.position.x, v.position.y); v.texCoord = glm::vec2(v.position.x, v.position.y);

View File

@ -1,9 +1,9 @@
#ifndef GRIDMESH_H #ifndef GRIDMESH_H
#define GRIDMESH_H #define GRIDMESH_H
#include "mesh.h" #include "standardmesh.h"
class GridMesh : public Mesh class GridMesh : public StandardMesh
{ {
public: public:
GridMesh(int width, int height, bool alternate); GridMesh(int width, int height, bool alternate);

View File

@ -1,4 +1,5 @@
#include "mesh.h" #include "mesh.h"
#include "glassert.h" #include "glassert.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i)) #define BUFFER_OFFSET(i) ((char *)NULL + (i))
@ -8,12 +9,6 @@ Mesh::~Mesh()
destroyGL(); destroyGL();
} }
void Mesh::addVertex(const Vertex& v)
{
if(!locked)
vertices.push_back(v);
}
void Mesh::addFace(int i1, int i2, int i3) void Mesh::addFace(int i1, int i2, int i3)
{ {
if(!locked) if(!locked)
@ -36,19 +31,13 @@ void Mesh::initGL()
// create VBOs // create VBOs
glAssert(glGenBuffers(NB_BUFFERS, vbo)); glAssert(glGenBuffers(NB_BUFFERS, vbo));
int size = getSizeofVertexType();
// init vertex vbo // init vertex vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]));
glAssert(glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW)); glAssert(glBufferData(GL_ARRAY_BUFFER, getNbVertices() * size, getVertexData(), GL_STATIC_DRAW));
// position attribute setAttribPointer();
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
// normal attribute
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(1));
// texCoord attribute
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(2*sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(2));
// init indices vbo // init indices vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER])); glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));

36
mesh.h
View File

@ -3,34 +3,30 @@
#include <glew/glew.h> #include <glew/glew.h>
#include <vector> #include <vector>
#include <glm/glm.hpp>
class Mesh #define BUFFER_OFFSET(i) ((char *)NULL + (i))
{
class Mesh{
protected:
enum {VERTEX_BUFFER, INDICES_BUFFER, NB_BUFFERS};
std::vector<unsigned int> indices;
GLuint vao;
GLuint vbo[2];
bool locked;
virtual int getNbVertices() = 0;
virtual int getSizeofVertexType() = 0;
virtual void* getVertexData() = 0;
virtual void setAttribPointer() = 0;
public: public:
typedef struct
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;
} Vertex;
~Mesh(); ~Mesh();
void addVertex(const Vertex& v);
void addFace(int i1, int i2, int i3); void addFace(int i1, int i2, int i3);
void initGL(); void initGL();
void destroyGL(); void destroyGL();
void draw(); void draw();
bool isLocked(){return locked;} bool isLocked(){return locked;}
protected:
enum {VERTEX_BUFFER, INDICES_BUFFER, NB_BUFFERS};
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
GLuint vao;
GLuint vbo[2];
bool locked;
}; };
#endif // MESH_H #endif // MESH_H

View File

@ -4,7 +4,7 @@
#include <glew/glew.h> #include <glew/glew.h>
#include "basicmodule.h" #include "basicmodule.h"
#include "phongmaterial.h" #include "phongmaterial.h"
#include "mesh.h" #include "standardmesh.h"
class Lights; class Lights;

View File

@ -1,7 +1,7 @@
#include "resourcebase.h" #include "resourcebase.h"
ResourceBase::DataBase<Texture> ResourceBase::textures; ResourceBase::DataBase<Texture> ResourceBase::textures;
ResourceBase::DataBase<Mesh> ResourceBase::meshes; ResourceBase::DataBase<StandardMesh> ResourceBase::meshes;
ResourceBase::DataBase<Material> ResourceBase::materials; ResourceBase::DataBase<Material> ResourceBase::materials;
ResourceBase::DataBase<Shader> ResourceBase::shaders; ResourceBase::DataBase<Shader> ResourceBase::shaders;
ResourceBase::DataBase<Entity> ResourceBase::entities; ResourceBase::DataBase<Entity> ResourceBase::entities;
@ -12,7 +12,7 @@ void ResourceBase::setTexture(const std::string &textureName, Texture* myTexture
textures.add(textureName, myTexture); textures.add(textureName, myTexture);
} }
void ResourceBase::setMesh(const std::string &meshName, Mesh* myMesh ) void ResourceBase::setMesh(const std::string &meshName, StandardMesh* myMesh )
{ {
meshes.add(meshName, myMesh); meshes.add(meshName, myMesh);
} }
@ -43,7 +43,7 @@ Texture* ResourceBase::getTexture(const std::string &textureName)
return textures.get(textureName); return textures.get(textureName);
} }
Mesh* ResourceBase::getMesh(const std::string &meshName) StandardMesh* ResourceBase::getMesh(const std::string &meshName)
{ {
return meshes.get(meshName); return meshes.get(meshName);
} }

View File

@ -2,7 +2,7 @@
#define RESOURCEBASE_H #define RESOURCEBASE_H
class Texture; class Texture;
class Mesh; class StandardMesh;
class Material; class Material;
class Shader; class Shader;
class Entity; class Entity;
@ -16,14 +16,14 @@ class ResourceBase
{ {
public: public:
static void setTexture(const std::string &textureName, Texture* myTexture); static void setTexture(const std::string &textureName, Texture* myTexture);
static void setMesh(const std::string &meshName, Mesh* myMesh); static void setMesh(const std::string &meshName, StandardMesh* myMesh);
static void setMaterial(const std::string &materialName, Material* myMaterial); static void setMaterial(const std::string &materialName, Material* myMaterial);
static void setShader(const std::string &shaderName, Shader* myShader); static void setShader(const std::string &shaderName, Shader* myShader);
static void setEntity(const std::string &entityName, Entity* myEntity); static void setEntity(const std::string &entityName, Entity* myEntity);
static void setLights(const std::string &lightsName, Lights* myLights); static void setLights(const std::string &lightsName, Lights* myLights);
static Texture* getTexture(const std::string &textureName); static Texture* getTexture(const std::string &textureName);
static Mesh* getMesh(const std::string &meshName); static StandardMesh* getMesh(const std::string &meshName);
static Material* getMaterial(const std::string &materialName); static Material* getMaterial(const std::string &materialName);
static Shader* getShader(const std::string &shaderName); static Shader* getShader(const std::string &shaderName);
static Entity* getEntity(const std::string &entityName); static Entity* getEntity(const std::string &entityName);
@ -53,7 +53,7 @@ protected:
}; };
static DataBase<Texture> textures; static DataBase<Texture> textures;
static DataBase<Mesh> meshes; static DataBase<StandardMesh> meshes;
static DataBase<Material> materials; static DataBase<Material> materials;
static DataBase<Shader> shaders; static DataBase<Shader> shaders;
static DataBase<Entity> entities; static DataBase<Entity> entities;

View File

@ -25,7 +25,6 @@ unix {
} }
SOURCES += shader.cpp \ SOURCES += shader.cpp \
mesh.cpp \
camera.cpp \ camera.cpp \
gridmesh.cpp \ gridmesh.cpp \
texture.cpp \ texture.cpp \
@ -41,10 +40,12 @@ SOURCES += shader.cpp \
phongmodule.cpp \ phongmodule.cpp \
skyboxmodule.cpp \ skyboxmodule.cpp \
basicmodule.cpp \ basicmodule.cpp \
framebuffer.cpp framebuffer.cpp \
standardmesh.cpp \
mesh.cpp \
bumpmappedmesh.cpp
HEADERS += shader.h \ HEADERS += shader.h \
mesh.h \
camera.h \ camera.h \
glassert.h \ glassert.h \
material.h \ material.h \
@ -64,7 +65,11 @@ HEADERS += shader.h \
basicmodule.h \ basicmodule.h \
module.h \ module.h \
resource.h \ resource.h \
framebuffer.h framebuffer.h \
standardmesh.h \
mesh.h \
bumpmappedmesh.h \
vertex.h
OTHER_FILES += *.frag *.vert *.glsl *.todo OTHER_FILES += *.frag *.vert *.glsl *.todo

View File

@ -62,101 +62,11 @@ void SparrowRenderer::addModule(Module* myModule, std::string name)
modules.push_back(ModuleNode(myModule, name)); modules.push_back(ModuleNode(myModule, name));
} }
void SparrowRenderer::addModule(Module* myModule, std::string name, int index)
{
if(index < 0)
index = 0;
if(index > modules.size())
modules.push_back(ModuleNode(myModule, name));
else
{
auto it = modules.begin();
std::advance(it, index);
modules.insert(it, ModuleNode(myModule, name));
}
}
void SparrowRenderer::removeModule(int index)
{
auto it = modules.begin();
std::advance(it, index);
modules.erase(it);
}
void SparrowRenderer::removeModule(std::string name)
{
modules.erase(getModuleNode(name));
}
Module* SparrowRenderer::getModule(int index)
{
auto it = modules.begin();
std::advance(it, index);
return (*it).module;
}
Module* SparrowRenderer::getModule(std::string name)
{
return (*getModuleNode(name)).module;
}
std::list<SparrowRenderer::ModuleNode>::iterator SparrowRenderer::getModuleNode(std::string name)
{
for(auto iterator = modules.begin(); iterator != modules.end(); ++iterator)
{
if(iterator->name == name)
return iterator;
}
}
int SparrowRenderer::getNbModules() int SparrowRenderer::getNbModules()
{ {
return modules.size(); return modules.size();
} }
std::string SparrowRenderer::getModuleName(int index)
{
auto it = modules.begin();
std::advance(it, index);
return (*it).name;
}
void SparrowRenderer::enableModule(std::string name)
{
(*getModuleNode(name)).isEnabled = true;
}
void SparrowRenderer::enableModule(int index)
{
auto it = modules.begin();
std::advance(it, index);
(*it).isEnabled = true;
}
void SparrowRenderer::disableModule(std::string name)
{
(*getModuleNode(name)).isEnabled = false;
}
void SparrowRenderer::disableModule(int index)
{
auto it = modules.begin();
std::advance(it, index);
(*it).isEnabled = false;
}
bool SparrowRenderer::isModuleEnabled(std::string name)
{
return (*getModuleNode(name)).isEnabled;
}
bool SparrowRenderer::isModuleEnabled(int index)
{
auto it = modules.begin();
std::advance(it, index);
return (*it).isEnabled;
}
// camera methods // camera methods
Camera* SparrowRenderer::getCamera() Camera* SparrowRenderer::getCamera()

View File

@ -17,20 +17,8 @@ public:
void render(); void render();
// modules methods // modules methods
void addModule(Module* myModule, std::string name); void addModule(Module* myModule, std::string name);
void addModule(Module* myModule, std::string name, int index); int getNbModules();
void removeModule(std::string name);
void removeModule(int index);
Module* getModule(std::string name);
Module* getModule(int index);
int getNbModules();
std::string getModuleName(int index);
void enableModule(std::string name);
void enableModule(int index);
void disableModule(std::string name);
void disableModule(int index);
bool isModuleEnabled(std::string name);
bool isModuleEnabled(int index);
// camera methods // camera methods
Camera* getCamera(); Camera* getCamera();

View File

@ -1,4 +1,5 @@
#include "sphere.h" #include "sphere.h"
#include "glm/ext.hpp"
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#define MAGIC_RATIO 0.37139f #define MAGIC_RATIO 0.37139f
@ -66,9 +67,9 @@ int Sphere::getEdge(int a, int b)
else if(current->next == NULL) else if(current->next == NULL)
{ {
// creating subdivision vertex // creating subdivision vertex
Vertex v; TexturedVertex v;
Vertex v0 = vertices[a]; TexturedVertex v0 = vertices[a];
Vertex v1 = vertices[b]; TexturedVertex v1 = vertices[b];
v.position = glm::normalize((v0.position + v1.position)/2.f); v.position = glm::normalize((v0.position + v1.position)/2.f);
v.normal = v.position; v.normal = v.position;
@ -98,7 +99,7 @@ int Sphere::getEdge(int a, int b)
void Sphere::createVertex(float u, float v) void Sphere::createVertex(float u, float v)
{ {
Vertex vert; TexturedVertex vert;
vert.position = glm::vec3(cos(u*2*M_PI)*sin(v*M_PI), vert.position = glm::vec3(cos(u*2*M_PI)*sin(v*M_PI),
cos(v*M_PI), cos(v*M_PI),
sin(u*2*M_PI)*sin(v*M_PI)); sin(u*2*M_PI)*sin(v*M_PI));

View File

@ -1,9 +1,9 @@
#ifndef SPHERE_H #ifndef SPHERE_H
#define SPHERE_H #define SPHERE_H
#include "mesh.h" #include "standardmesh.h"
class Sphere : public Mesh class Sphere : public StandardMesh
{ {
private: private:
class Edge{ class Edge{

24
standardmesh.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "standardmesh.h"
#include "glassert.h"
void StandardMesh::addVertex(const TexturedVertex& v)
{
if(!locked)
vertices.push_back(v);
}
void StandardMesh::setAttribPointer()
{
glAssert(glBindVertexArray(vao));
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[VERTEX_BUFFER]));
// position attribute
glAssert(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(0)));
glAssert(glEnableVertexAttribArray(0));
// normal attribute
glAssert(glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(1));
// texCoord attribute
glAssert(glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), BUFFER_OFFSET(2*sizeof(glm::vec3))));
glAssert(glEnableVertexAttribArray(2));
}

34
standardmesh.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef STANDARDMESH_H
#define STANDARDMESH_H
#include "vertex.h"
#include "mesh.h"
class StandardMesh : public Mesh{
protected:
std::vector<TexturedVertex> vertices;
virtual int getNbVertices()
{
return vertices.size();
}
virtual int getSizeofVertexType()
{
return sizeof(TexturedVertex);
}
virtual void* getVertexData()
{
return (void*)(vertices.data());
}
virtual void setAttribPointer();
public:
void addVertex(const TexturedVertex& v);
};
#endif // STANDARDMESH_H

View File

@ -1,5 +1,5 @@
#include "utils.h" #include "utils.h"
#include "mesh.h" #include "standardmesh.h"
#include <QFile> #include <QFile>
#include <QTextStream> #include <QTextStream>
#include <QImage> #include <QImage>
@ -90,16 +90,16 @@ void Utils::loadMTL(const std::string &filename)
} }
Mesh* Utils::loadOBJ(const std::string &filename) StandardMesh* Utils::loadOBJ(const std::string &filename)
{ {
Mesh* mesh = new Mesh(); StandardMesh* mesh = new StandardMesh();
std::vector<glm::vec3> pos; std::vector<glm::vec3> pos;
std::vector<glm::vec3> norm; std::vector<glm::vec3> norm;
std::vector<glm::vec2> tex; std::vector<glm::vec2> tex;
QString line; QString line;
QStringList list; QStringList list;
QStringList faceList; QStringList faceList;
Mesh::Vertex v; TexturedVertex v;
int nb_vertices = 0; int nb_vertices = 0;
QFile f(QString(filename.c_str())); QFile f(QString(filename.c_str()));
if(!f.open(QFile::ReadOnly | QFile::Text)) if(!f.open(QFile::ReadOnly | QFile::Text))

View File

@ -3,13 +3,13 @@
#include <string> #include <string>
class QImage; class QImage;
class Mesh; class StandardMesh;
class Utils class Utils
{ {
public: public:
static std::string fileToString(const std::string &filename); static std::string fileToString(const std::string &filename);
static Mesh* loadOBJ(const std::string &filename); static StandardMesh* loadOBJ(const std::string &filename);
//static void loadOBJ(const std::string &filename); //static void loadOBJ(const std::string &filename);
static void loadMTL(const std::string &filename); static void loadMTL(const std::string &filename);
class Image class Image

29
vertex.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef VERTEX_H
#define VERTEX_H
#include "glm/vec3.hpp"
#include "glm/vec2.hpp"
struct Vertex
{
glm::vec3 position;
};
struct BasicVertex : public Vertex
{
glm::vec3 normal;
};
struct TexturedVertex : public BasicVertex
{
glm::vec2 texCoord;
};
struct SRFVertex : public TexturedVertex
{
glm::vec3 tangent;
glm::vec3 bitangent;
};
#endif // VERTEX_H