diff --git a/shaders/gbuffer.frag.glsl b/shaders/gbuffer.frag.glsl index 4061260..42be0d7 100644 --- a/shaders/gbuffer.frag.glsl +++ b/shaders/gbuffer.frag.glsl @@ -38,50 +38,63 @@ uniform sampler2D normalMap; in vec3 varTangent; in vec3 varBinormal; #endif + +#ifdef WIREFRAME +in vec3 varColor; +#else in vec3 varNormal; in vec2 varTexCoord; +#endif void main() { +#ifdef WIREFRAME + outColor.rgb = varColor; + outNormal = vec3(0., 0., 1.); + outSpecular = vec4(0., 0., 0., 1.); +#else + #ifdef ALPHA_MASK - if(texture(alphaMask, varTexCoord).r < 0.5) - discard; + if(texture(alphaMask, varTexCoord).r < 0.5) + discard; #endif #ifdef NORMAL_MAP - vec3 normalTexel = normalize(texture(normalMap, varTexCoord).xyz -0.5); - vec3 tangent = normalize(varTangent); - vec3 binormal = normalize(varBinormal); - vec3 normal = normalize(varNormal); - mat3 tangentSpace = mat3(vec3(tangent.x, binormal.x, normal.x), - vec3(tangent.y, binormal.y, normal.y), - vec3(tangent.z, binormal.z, normal.z)); - - outNormal = normalize(normalTexel * tangentSpace); + vec3 normalTexel = normalize(texture(normalMap, varTexCoord).xyz -0.5); + vec3 tangent = normalize(varTangent); + vec3 binormal = normalize(varBinormal); + vec3 normal = normalize(varNormal); + mat3 tangentSpace = mat3(vec3(tangent.x, binormal.x, normal.x), + vec3(tangent.y, binormal.y, normal.y), + vec3(tangent.z, binormal.z, normal.z)); + + outNormal = normalize(normalTexel * tangentSpace); #else - outNormal = normalize(varNormal); + outNormal = normalize(varNormal); #endif #ifdef DIFFUSE_TEXTURE - outColor.rgb = texture(diffuseTexture, varTexCoord).rgb; + outColor.rgb = texture(diffuseTexture, varTexCoord).rgb; #else - outColor.rgb = materialKd; + outColor.rgb = materialKd; #endif +#ifdef SPECULAR_TEXTURE + outSpecular.rgb = texture(specularTexture, varTexCoord).rgb; +#else + outSpecular.rgb = materialKs; +#endif + + outSpecular.w = float(materialNs)/255; + +#endif // WIREFRAME + #ifdef INSTANCED outColor.a = float(object_identifier+instanceId)/255; #else outColor.a = float(object_identifier)/255; #endif -#ifdef SPECULAR_TEXTURE - outSpecular.rgb = texture(specularTexture, varTexCoord).rgb; -#else - outSpecular.rgb = materialKs; -#endif - - outSpecular.w = float(materialNs)/255; - - outPosition = posInView; + outPosition = posInView; } diff --git a/shaders/gbuffer.vert.glsl b/shaders/gbuffer.vert.glsl index 9ca3967..82dc487 100644 --- a/shaders/gbuffer.vert.glsl +++ b/shaders/gbuffer.vert.glsl @@ -7,9 +7,14 @@ out vec4 posInView; out vec3 varTangent; out vec3 varBinormal; #endif + +#ifdef WIREFRAME +out vec3 varColor; +#else out vec3 varNormal; out vec2 varTexCoord; +#endif layout(location = 0)in vec3 inPosition; layout(location = 2)in vec2 inTexCoord; @@ -31,10 +36,15 @@ void main(void) { varTangent = normalize(normalMatrix*inTangent); varBinormal = normalize(normalMatrix*inBinormal); #endif + +#ifdef WIREFRAME + varColor = inNormal; +#else varNormal = normalize(normalMatrix*inNormal); //computing UVs varTexCoord = inTexCoord.xy; +#endif // computing positions #ifdef INSTANCED @@ -44,6 +54,6 @@ void main(void) { vec4 pos = vec4(inPosition, 1.0); #endif - posInView = modelViewMatrix*pos; + posInView = modelViewMatrix * pos; gl_Position = projectionMatrix * posInView; } diff --git a/src/deferredpipeline.cpp b/src/deferredpipeline.cpp index 4466c49..c011f24 100644 --- a/src/deferredpipeline.cpp +++ b/src/deferredpipeline.cpp @@ -249,10 +249,13 @@ void DeferredPipeline::setSources(ShaderSource *gBufferSource, ShaderSource *lig void DeferredPipeline::refreshScene(Scene *scene) { + // delete previous shaders for(auto it : m_mesh3DShaders) delete it.second; m_mesh3DShaders.clear(); m_meshTypes.clear(); + + // gather all unique mesh flags scene->getMeshTypes(m_meshTypes); for(unsigned int type : m_meshTypes) { diff --git a/src/mesh.cpp b/src/mesh.cpp index 3ec5282..cb37e75 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -18,6 +18,7 @@ const char* const Mesh::flagStr[Mesh::NB_FLAGS] = "TANGENT_SPACE", "DOUBLE_SIDED", "BILLBOARD", + "WIREFRAME", "SHADOWED", "COLOR_TEXTURE", @@ -37,6 +38,7 @@ Mesh::Mesh(const std::string &name) : material(NULL), isDoubleSided(false), isBillboard(false), + isWireframe(false), isShadowCaster(true), depth(0), m_flags(0), @@ -87,6 +89,8 @@ unsigned int Mesh::updateFlags() m_flags |= 1 << MESH_DOUBLE_SIDED; if(isBillboard) m_flags |= 1 << MESH_BILLBOARD; + if(isWireframe) + m_flags |= 1 << MESH_WIREFRAME; if(isShadowCaster) m_flags |= 1 << MESH_SHADOWED; } @@ -274,6 +278,15 @@ void Mesh::setIsBillboard(bool val) m_flags &= ~(1 << MESH_BILLBOARD); } +void Mesh::setWireframe(bool val) +{ + isWireframe = val; + if(val) + m_flags |= 1 << MESH_WIREFRAME; + else + m_flags &= ~(1 << MESH_WIREFRAME); +} + void Mesh::setIsShadowCaster(bool val) { isShadowCaster = val; diff --git a/src/mesh.h b/src/mesh.h index fa05893..e50df83 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -34,6 +34,7 @@ public: MESH_TANGENT_SPACE, MESH_DOUBLE_SIDED, MESH_BILLBOARD, + MESH_WIREFRAME, MESH_SHADOWED, // simple material (no lighting) @@ -169,6 +170,13 @@ public: * a billboard mesh will always follow the camera orientation */ void setIsBillboard(bool val); + + /** + * @brief setIsWireframe must be called for a mesh of GL_LINE primitives, + * lighting will probably not be computed on lines, + * normals value can be interpreted as a color, depending on the shader. + */ + void setWireframe(bool val); /** * @brief setIsShadowCaster allows to enable or disable, @@ -249,6 +257,7 @@ protected: Material* material; bool isDoubleSided; bool isBillboard; + bool isWireframe; bool isShadowCaster; float depth; unsigned int m_flags; diff --git a/src/parametricmesh.h b/src/parametricmesh.h index ac0d021..82ec080 100644 --- a/src/parametricmesh.h +++ b/src/parametricmesh.h @@ -8,12 +8,12 @@ class MeshGenerator { public: virtual glm::vec3 evalUV(float u, float v) = 0; - + /** * @brief generateParametricMesh creates a mesh from a rectangular grid of size width:height */ Mesh* generateParametricMesh(Material* mat, int width = 10, int height = 10, float size = 1, bool alternate = true); - + /** * @brief generateGeodesicMesh creates a closed mesh from an icosahedron subdivided n times */ @@ -41,7 +41,7 @@ private: int getEdge(int a, int b); void createVertex(float u, float v); void subdivide(); - + // parametric methods int getVertexId(int i, int j, int height); };