lights and meshes are now almost fully serializables

This commit is contained in:
Anselme 2017-09-05 20:34:13 +02:00
parent 0f0ff32f16
commit a4843e277a
4 changed files with 99 additions and 78 deletions

View File

@ -59,11 +59,10 @@ void AmbientLight::bindAttributes(Shader *shader, Camera *camera)
int DirectionnalLight::m_shaderRefCounter = 0; int DirectionnalLight::m_shaderRefCounter = 0;
Shader* DirectionnalLight::m_shaders[4] = {NULL}; Shader* DirectionnalLight::m_shaders[4] = {NULL};
DirectionnalLight::DirectionnalLight(glm::vec3 dir, glm::vec3 lightColor) : DirectionnalLight::DirectionnalLight(glm::vec3 dir, glm::vec3 lightColor)
m_direction(dir),
m_shadowCaster(false)
{ {
m_direction = dir;
m_shadowCaster = false;
m_color = lightColor; m_color = lightColor;
m_shadowMap = NULL; m_shadowMap = NULL;
} }
@ -252,12 +251,11 @@ void DirectionnalLight::updateShadowMap(Scene* scene)
int PointLight::m_shaderRefCounter = 0; int PointLight::m_shaderRefCounter = 0;
Shader* PointLight::m_shaders[2] = {NULL}; Shader* PointLight::m_shaders[2] = {NULL};
PointLight::PointLight(glm::vec3 pos, float range, glm::vec3 lightColor) : PointLight::PointLight(glm::vec3 pos, float range, glm::vec3 lightColor)
m_position(pos),
m_range(range),
m_shadowCaster(false)
{ {
m_position = pos;
m_range = range;
m_shadowCaster = false;
m_color = lightColor; m_color = lightColor;
m_shadowMap = NULL; m_shadowMap = NULL;
} }

View File

@ -5,6 +5,7 @@
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <SparrowSerializer/serializable.h>
class FrameBuffer; class FrameBuffer;
class Shader; class Shader;
@ -12,7 +13,7 @@ class Scene;
class Texture; class Texture;
class Camera; class Camera;
class Light class Light : public Serializable
{ {
public: public:
enum LightType { enum LightType {
@ -47,7 +48,7 @@ public:
virtual unsigned int getFlags() = 0; virtual unsigned int getFlags() = 0;
protected: protected:
glm::vec3 m_color; P_VEC3(m_color)
}; };
class AmbientLight : public Light class AmbientLight : public Light
@ -56,6 +57,8 @@ class AmbientLight : public Light
Texture* reflect; Texture* reflect;
static Texture* brdfLUT; static Texture* brdfLUT;
public: public:
SERIALIZABLE(AmbientLight, CAST(m_color))
AmbientLight(Texture* ambientTex = nullptr, Texture* reflectTex = nullptr) : ambient(ambientTex), reflect(reflectTex) {} AmbientLight(Texture* ambientTex = nullptr, Texture* reflectTex = nullptr) : ambient(ambientTex), reflect(reflectTex) {}
virtual LightType getType() { return AMBIENT; } virtual LightType getType() { return AMBIENT; }
virtual void bindAttributes(Shader *shader, Camera *camera); virtual void bindAttributes(Shader *shader, Camera *camera);
@ -70,6 +73,13 @@ public:
class DirectionnalLight : public Light class DirectionnalLight : public Light
{ {
public: public:
SERIALIZABLE(DirectionnalLight,
CAST(m_color),
CAST(m_direction),
CAST(m_center),
CAST(m_shadowCaster),
CAST(m_shadowMapResolution))
DirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 0), glm::vec3 lightColor = glm::vec3(1)); DirectionnalLight(glm::vec3 dir = glm::vec3(0, -1, 0), glm::vec3 lightColor = glm::vec3(1));
void setDir(const glm::vec3 &dir) { m_direction = dir; } void setDir(const glm::vec3 &dir) { m_direction = dir; }
@ -87,12 +97,12 @@ public:
void updateShadowMap(Scene* scene); void updateShadowMap(Scene* scene);
private: private:
glm::vec3 m_direction; P_VEC3(m_direction)
glm::vec3 m_center; P_VEC3(m_center)
bool m_shadowCaster; P_BOOL(m_shadowCaster)
int m_shadowMapResolution; P_INT(m_shadowMapResolution)
FrameBuffer* m_shadowMap; FrameBuffer* m_shadowMap;
std::vector<glm::mat4> m_viewMatrices; std::vector<glm::mat4> m_viewMatrices;
std::vector<glm::mat4> m_projectionMatrices; std::vector<glm::mat4> m_projectionMatrices;
@ -104,6 +114,12 @@ private:
class PointLight : public Light class PointLight : public Light
{ {
public: public:
SERIALIZABLE(PointLight,
CAST(m_color),
CAST(m_position),
CAST(m_range),
CAST(m_shadowCaster),
CAST(m_shadowMapResolution))
PointLight(glm::vec3 pos = glm::vec3(0), float attenuation = 1, glm::vec3 lightColor = glm::vec3(1)); PointLight(glm::vec3 pos = glm::vec3(0), float attenuation = 1, glm::vec3 lightColor = glm::vec3(1));
glm::vec3 getPos() { return m_position; } glm::vec3 getPos() { return m_position; }
@ -125,11 +141,11 @@ public:
private: private:
void updateTransform(); void updateTransform();
glm::vec3 m_position; P_VEC3(m_position)
float m_range; P_FLOAT(m_range)
bool m_shadowCaster; P_BOOL(m_shadowCaster)
int m_shadowMapResolution; P_INT(m_shadowMapResolution)
FrameBuffer* m_shadowMap; FrameBuffer* m_shadowMap;
std::vector<glm::mat4> m_shadowTransforms; std::vector<glm::mat4> m_shadowTransforms;

View File

@ -36,18 +36,18 @@ const char* const Mesh::flagStr[Mesh::NB_FLAGS] =
"BUMP_MAP" "BUMP_MAP"
}; };
Mesh::Mesh() : Mesh::Mesh()
m_name("mesh"),
material(NULL),
isDoubleSided(false),
isBillboard(false),
isWireframe(false),
isShadowCaster(true),
depth(0),
m_flags(0),
primitive_type(GL_TRIANGLES),
vao(0)
{ {
m_name = "mesh";
m_material = NULL;
m_isDoubleSided = false;
m_isBillboard = false;
m_isWireframe = false;
m_isShadowCaster = true;
m_depth = 0;
m_flags = 0;
m_primitive_type = GL_TRIANGLES;
m_vao = 0;
clearBuffers(); clearBuffers();
} }
@ -94,19 +94,19 @@ unsigned int Mesh::updateFlags()
if(!m_tangents.empty()) if(!m_tangents.empty())
m_flags |= 1 << MESH_TANGENT_SPACE; m_flags |= 1 << MESH_TANGENT_SPACE;
if(isDoubleSided) if(m_isDoubleSided)
m_flags |= 1 << MESH_DOUBLE_SIDED; m_flags |= 1 << MESH_DOUBLE_SIDED;
if(isBillboard) if(m_isBillboard)
m_flags |= 1 << MESH_BILLBOARD; m_flags |= 1 << MESH_BILLBOARD;
if(isWireframe) if(m_isWireframe)
m_flags |= 1 << MESH_WIREFRAME; m_flags |= 1 << MESH_WIREFRAME;
if(isShadowCaster) if(m_isShadowCaster)
m_flags |= 1 << MESH_SHADOWED; m_flags |= 1 << MESH_SHADOWED;
} }
else else
m_flags |= 1 << MESH_2D; m_flags |= 1 << MESH_2D;
return m_flags | (material ? material->getFlags() : 0); return m_flags | (m_material ? m_material->getFlags() : 0);
} }
void Mesh::initGL() void Mesh::initGL()
@ -115,8 +115,8 @@ void Mesh::initGL()
destroyGL(); destroyGL();
// create VAO // create VAO
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &m_vao);
glBindVertexArray(vao); glBindVertexArray(m_vao);
// init positions VBO // init positions VBO
if(!m_positions3D.empty()) if(!m_positions3D.empty())
@ -181,26 +181,26 @@ void Mesh::initGL()
void Mesh::draw(Shader* shader) void Mesh::draw(Shader* shader)
{ {
if(isWireframe) if(m_isWireframe)
glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
else else
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
if(isDoubleSided) if(m_isDoubleSided)
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
if(material != NULL) if(m_material != NULL)
material->bindAttributes(shader); m_material->bindAttributes(shader);
glBindVertexArray(vao); glBindVertexArray(m_vao);
if(m_indices.empty()) if(m_indices.empty())
{ {
int size = m_positions3D.empty() ? m_positions2D.size() : m_positions3D.size(); int size = m_positions3D.empty() ? m_positions2D.size() : m_positions3D.size();
if(!m_instances_offsets.empty()) if(!m_instances_offsets.empty())
{ {
glDrawArraysInstanced(primitive_type, 0, size, m_instances_offsets.size()); glDrawArraysInstanced(m_primitive_type, 0, size, m_instances_offsets.size());
} }
else else
{ {
glDrawArrays(primitive_type, 0, size); glDrawArrays(m_primitive_type, 0, size);
} }
} }
else else
@ -209,26 +209,26 @@ void Mesh::draw(Shader* shader)
b->bind(); b->bind();
if(!m_instances_offsets.empty()) if(!m_instances_offsets.empty())
{ {
glDrawElementsInstanced(primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL, m_instances_offsets.size()); glDrawElementsInstanced(m_primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL, m_instances_offsets.size());
} }
else else
{ {
glDrawElements(primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL); glDrawElements(m_primitive_type, m_indices.size(), GL_UNSIGNED_INT, NULL);
} }
b->unbind(); b->unbind();
} }
glBindVertexArray(0); glBindVertexArray(0);
if(isDoubleSided) if(m_isDoubleSided)
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
} }
void Mesh::destroyGL() void Mesh::destroyGL()
{ {
if(vao != 0) if(m_vao != 0)
{ {
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &m_vao);
vao = 0; m_vao = 0;
} }
clearBuffers(); clearBuffers();
} }
@ -246,7 +246,7 @@ void Mesh::clearData()
unsigned int Mesh::getFlags() unsigned int Mesh::getFlags()
{ {
return m_flags | material->getFlags(); return m_flags | m_material->getFlags();
} }
void Mesh::addRectangle2D(const glm::vec2 &pos, const glm::vec2 &dim, const glm::vec2 &texCoord, const glm::vec2 &texRange, bool indexed) void Mesh::addRectangle2D(const glm::vec2 &pos, const glm::vec2 &dim, const glm::vec2 &texCoord, const glm::vec2 &texRange, bool indexed)
@ -275,7 +275,7 @@ void Mesh::addRectangle2D(const glm::vec2 &pos, const glm::vec2 &dim, const glm:
void Mesh::setIsDoubleSided(bool val) void Mesh::setIsDoubleSided(bool val)
{ {
isDoubleSided = val; m_isDoubleSided = val;
if(val) if(val)
m_flags |= 1 << MESH_DOUBLE_SIDED; m_flags |= 1 << MESH_DOUBLE_SIDED;
else else
@ -284,7 +284,7 @@ void Mesh::setIsDoubleSided(bool val)
void Mesh::setIsBillboard(bool val) void Mesh::setIsBillboard(bool val)
{ {
isBillboard = val; m_isBillboard = val;
if(val) if(val)
m_flags |= 1 << MESH_BILLBOARD; m_flags |= 1 << MESH_BILLBOARD;
else else
@ -293,7 +293,7 @@ void Mesh::setIsBillboard(bool val)
void Mesh::setWireframe(bool val) void Mesh::setWireframe(bool val)
{ {
isWireframe = val; m_isWireframe = val;
if(val) if(val)
m_flags |= 1 << MESH_WIREFRAME; m_flags |= 1 << MESH_WIREFRAME;
else else
@ -302,7 +302,7 @@ void Mesh::setWireframe(bool val)
void Mesh::setIsShadowCaster(bool val) void Mesh::setIsShadowCaster(bool val)
{ {
isShadowCaster = val; m_isShadowCaster = val;
if(val) if(val)
m_flags |= 1 << MESH_SHADOWED; m_flags |= 1 << MESH_SHADOWED;
else else

View File

@ -94,7 +94,14 @@ public:
CAST(m_positions2D), CAST(m_positions2D),
CAST(m_texCoords), CAST(m_texCoords),
CAST(m_instances_offsets), CAST(m_instances_offsets),
CAST(m_indices)) CAST(m_indices),
CAST(m_name),
CAST(m_isDoubleSided),
CAST(m_isBillboard),
CAST(m_isWireframe),
CAST(m_isShadowCaster),
CAST(m_depth),
CAST(m_primitive_type))
/** /**
* @brief Mesh builds an empty mesh, to be renderable, a mesh must have vertices and a material. * @brief Mesh builds an empty mesh, to be renderable, a mesh must have vertices and a material.
@ -149,8 +156,8 @@ public:
void addLine(int i1, int i2) {m_indices.push_back(i1), m_indices.push_back(i2);} void addLine(int i1, int i2) {m_indices.push_back(i1), m_indices.push_back(i2);}
// Material accessers // Material accessers
void setMaterial(Material* mat) {material = mat;} void setMaterial(Material* mat) {m_material = mat;}
Material *getMaterial() {return material;} Material *getMaterial() {return m_material;}
// other accessers // other accessers
void addNormal(float x, float y, float z) {addNormal(glm::vec3(x, y, z));} void addNormal(float x, float y, float z) {addNormal(glm::vec3(x, y, z));}
@ -165,8 +172,8 @@ public:
/** /**
* @brief setDepth allows to set the depth of a 2D mesh, the depth must be between -1 and 1, -1 being the closest to the camera * @brief setDepth allows to set the depth of a 2D mesh, the depth must be between -1 and 1, -1 being the closest to the camera
*/ */
void setDepth(float d) { depth = d; } void setDepth(float d) { m_depth = d; }
float getDepth() const { return depth; } float getDepth() const { return m_depth; }
/*************************************************************/ /*************************************************************/
/* 3D MESH PROPERTIES */ /* 3D MESH PROPERTIES */
@ -236,7 +243,7 @@ public:
* *
* default is GL_TRIANGLES * default is GL_TRIANGLES
*/ */
void setPrimitiveType(GLenum type) {primitive_type = type;} void setPrimitiveType(GLenum type) {m_primitive_type = type;}
/** /**
* @brief updateFlags forces the refresh of the mesh flags, * @brief updateFlags forces the refresh of the mesh flags,
@ -246,19 +253,19 @@ public:
unsigned int updateFlags(); unsigned int updateFlags();
protected: protected:
std::string m_name; // yup P_STRING(m_name)
Material* material; // nop Material* m_material;
bool isDoubleSided; // yup P_BOOL(m_isDoubleSided)
bool isBillboard; // yup P_BOOL(m_isBillboard)
bool isWireframe; // yup P_BOOL(m_isWireframe)
bool isShadowCaster; // yup P_BOOL(m_isShadowCaster)
float depth; // yup P_FLOAT(m_depth)
unsigned int m_flags; // nop GL unsigned int m_flags;
GLenum primitive_type; // yup P_UINT(m_primitive_type)
std::vector<Buffer*> buffers; // nop GL std::vector<Buffer*> buffers;
enum { enum {
// required buffer // required buffer
@ -279,12 +286,12 @@ protected:
NB_BUFFERS NB_BUFFERS
}; };
int buffersId[NB_BUFFERS]; // nop GL int buffersId[NB_BUFFERS];
void addBuffer(Buffer *b, int bufferType); void addBuffer(Buffer *b, int bufferType);
void clearBuffers(); void clearBuffers();
GLuint vao; // nop GL GLuint m_vao;
}; };
#endif // MESH_H #endif // MESH_H