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 <glm/glm.hpp>
#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)
{

View File

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

View File

@ -1,12 +1,12 @@
#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 j=0; j<=height; ++j)
{
Vertex v;
TexturedVertex v;
v.position = glm::vec3((float)i/(float)width, (float)j/(float)height, 0);
v.normal = glm::vec3(0, 0, 1);
v.texCoord = glm::vec2(v.position.x, v.position.y);

View File

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

View File

@ -1,4 +1,5 @@
#include "mesh.h"
#include "glassert.h"
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
@ -8,12 +9,6 @@ Mesh::~Mesh()
destroyGL();
}
void Mesh::addVertex(const Vertex& v)
{
if(!locked)
vertices.push_back(v);
}
void Mesh::addFace(int i1, int i2, int i3)
{
if(!locked)
@ -36,19 +31,13 @@ void Mesh::initGL()
// create VBOs
glAssert(glGenBuffers(NB_BUFFERS, vbo));
int size = getSizeofVertexType();
// init vertex vbo
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
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));
setAttribPointer();
// init indices vbo
glAssert(glBindBuffer(GL_ARRAY_BUFFER, vbo[INDICES_BUFFER]));

36
mesh.h
View File

@ -3,34 +3,30 @@
#include <glew/glew.h>
#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:
typedef struct
{
glm::vec3 position;
glm::vec3 normal;
glm::vec2 texCoord;
} Vertex;
~Mesh();
void addVertex(const Vertex& v);
void addFace(int i1, int i2, int i3);
void initGL();
void destroyGL();
void draw();
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

View File

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

View File

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

View File

@ -2,7 +2,7 @@
#define RESOURCEBASE_H
class Texture;
class Mesh;
class StandardMesh;
class Material;
class Shader;
class Entity;
@ -16,14 +16,14 @@ class ResourceBase
{
public:
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 setShader(const std::string &shaderName, Shader* myShader);
static void setEntity(const std::string &entityName, Entity* myEntity);
static void setLights(const std::string &lightsName, Lights* myLights);
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 Shader* getShader(const std::string &shaderName);
static Entity* getEntity(const std::string &entityName);
@ -53,7 +53,7 @@ protected:
};
static DataBase<Texture> textures;
static DataBase<Mesh> meshes;
static DataBase<StandardMesh> meshes;
static DataBase<Material> materials;
static DataBase<Shader> shaders;
static DataBase<Entity> entities;

View File

@ -25,7 +25,6 @@ unix {
}
SOURCES += shader.cpp \
mesh.cpp \
camera.cpp \
gridmesh.cpp \
texture.cpp \
@ -41,10 +40,12 @@ SOURCES += shader.cpp \
phongmodule.cpp \
skyboxmodule.cpp \
basicmodule.cpp \
framebuffer.cpp
framebuffer.cpp \
standardmesh.cpp \
mesh.cpp \
bumpmappedmesh.cpp
HEADERS += shader.h \
mesh.h \
camera.h \
glassert.h \
material.h \
@ -64,7 +65,11 @@ HEADERS += shader.h \
basicmodule.h \
module.h \
resource.h \
framebuffer.h
framebuffer.h \
standardmesh.h \
mesh.h \
bumpmappedmesh.h \
vertex.h
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));
}
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()
{
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* SparrowRenderer::getCamera()

View File

@ -18,19 +18,7 @@ public:
// modules methods
void addModule(Module* myModule, std::string name);
void addModule(Module* myModule, std::string name, int index);
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* getCamera();

View File

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

View File

@ -1,9 +1,9 @@
#ifndef SPHERE_H
#define SPHERE_H
#include "mesh.h"
#include "standardmesh.h"
class Sphere : public Mesh
class Sphere : public StandardMesh
{
private:
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 "mesh.h"
#include "standardmesh.h"
#include <QFile>
#include <QTextStream>
#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> norm;
std::vector<glm::vec2> tex;
QString line;
QStringList list;
QStringList faceList;
Mesh::Vertex v;
TexturedVertex v;
int nb_vertices = 0;
QFile f(QString(filename.c_str()));
if(!f.open(QFile::ReadOnly | QFile::Text))

View File

@ -3,13 +3,13 @@
#include <string>
class QImage;
class Mesh;
class StandardMesh;
class Utils
{
public:
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 loadMTL(const std::string &filename);
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