2D elements are correctly blended and rendered according to depth

This commit is contained in:
Anselme FRANÇOIS 2016-07-24 14:53:31 +02:00
parent e94f2a3c98
commit 7ca0d3d003
11 changed files with 65 additions and 47 deletions

View File

@ -12,6 +12,8 @@ uniform vec3 materialKd;
#ifdef ALPHA_MASK
uniform sampler2D alphaMask;
#else
uniform float opacity;
#endif
// OUTPUT COLOR
@ -31,7 +33,7 @@ void main(void)
#ifdef ALPHA_MASK
float alpha = texture(alphaMask, texCoord).r;
#else
float alpha = 1.0;
float alpha = opacity;
#endif
outColor = vec4(color, alpha);

View File

@ -8,11 +8,10 @@ out vec2 texCoord;
uniform mat4 transformMatrix;
uniform mat4 orthoMatrix;
uniform float depth;
void main(void) {
#ifdef TEXTURABLE
texCoord = inTexCoord;
#endif
gl_Position = orthoMatrix * transformMatrix * vec4(inPosition, depth, 1.0);
gl_Position = orthoMatrix * transformMatrix * vec4(inPosition, 0.0, 1.0);
}

View File

@ -17,7 +17,7 @@ uniform vec3 lightColor;
#ifdef SHADOWMAP
#ifdef POINT_LIGHT
uniform samplerCubeShadow shadowMap;
uniform samplerCube shadowMap;
uniform mat3 inverseViewMatrix;
#else
uniform sampler2DShadow shadowMap;
@ -118,8 +118,10 @@ void main(void) {
#ifdef POINT_LIGHT
vec3 dirLight = pointLight - fragPos.xyz;
vec4 pointShadowParam = vec4(inverseViewMatrix * dirLight, length(dirLight));
att = texture(shadowMap, pointShadowParam);
//outColor = vec4(vec3(texture(shadowMap, vec4(inverseViewMatrix * dirLight, length(dirLight))).r), 1);
//att = texture(shadowMap, pointShadowParam);
//outColor = vec4(vec3(texture(shadowMap, vec4(inverseViewMatrix * dirLight, length(dirLight)/range))), 1);
//outColor = vec4(vec3(texture(shadowMap, vec3(inverseViewMatrix * dirLight)).r), 1);
//outColor = vec4(vec3(length(dirLight)/range), 1);
//return;
att = clamp(att, 0, 1);
dirLight = normalize(dirLight);

View File

@ -3,12 +3,10 @@ uniform sampler2D alphaMask;
in vec2 varTexCoord;
#endif
out float fragmentdepth;
#ifdef POINT_LIGHT
in vec4 FragPos;
in vec4 fragPos;
flat in int fcolor_idx;
uniform vec3 pointLight;
uniform float far_plane;
#endif
@ -18,10 +16,12 @@ void main()
if(texture(alphaMask, varTexCoord).r < 0.5)
discard;
#endif
/*
#ifdef POINT_LIGHT
fragmentdepth = length(FragPos.xyz - pointLight)/far_plane;
//gl_FragDepth = float(fcolor_idx)/6;
gl_FragDepth = length(fragPos.xyz)/far_plane;
#else
fragmentdepth = gl_FragCoord.z;
#endif
gl_FragDepth = gl_FragCoord.z;
#endif*/
gl_FragDepth = 0.5;
}

View File

@ -7,10 +7,9 @@ layout(triangles) in;
layout(triangle_strip, max_vertices = 18) out;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrices[6];
uniform mat4 layerTransform[6];
out vec4 FragPos;
out vec4 fragPos;
// debug parameter
flat out int fcolor_idx;
@ -18,12 +17,11 @@ flat out int fcolor_idx;
void main(void){
for (int layerId = 0; layerId < 6; ++layerId)
{
mat4 MVP = projectionMatrix * viewMatrices[layerId];
gl_Layer = layerId;
fcolor_idx = layerId;
for (int i = 0; i < 3; ++i)
{
gl_Position = MVP * gl_in[i].gl_Position;
gl_Position = layerTransform[layerId] * gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
@ -42,4 +40,3 @@ uniform mat4 backShadowMVP;
#endif

View File

@ -9,6 +9,7 @@
#include "phongmaterial.h"
#include "camera.h"
#include <resource.h>
#include <algorithm>
RESOURCE_PACK(shaders)
@ -95,6 +96,10 @@ DeferredPipeline::DeferredPipeline() :
m_postEffectsShader = new Shader(vertSource, fragSource);
}
bool depthCompare(const GeometryNode* firstElem, const GeometryNode* secondElem) {
return firstElem->mesh->getDepth() < secondElem->mesh->getDepth();
}
void DeferredPipeline::renderGL(Scene *scene)
{
if(m_renderTarget == NULL)
@ -118,7 +123,7 @@ void DeferredPipeline::renderGL(Scene *scene)
unsigned int type = node->mesh->getFlags();
if(type & (1 << Mesh::MESH_2D))
{
mesh2D.push_back(node);
mesh2D.push_back(node); // TODO : not every frame
continue;
}
Shader *shader = m_mesh3DShaders[node->mesh->getFlags()];
@ -191,6 +196,7 @@ void DeferredPipeline::renderGL(Scene *scene)
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
std::sort(mesh2D.begin(), mesh2D.end(), depthCompare);
for(GeometryNode* node : mesh2D)
{
Shader *shader = m_mesh2DShaders[node->mesh->getFlags()];

View File

@ -27,16 +27,6 @@ const glm::mat4 Light::biasMatrix(
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
const glm::mat4 PointLight::m_viewMatrices[6] =
{
glm::lookAt(glm::vec3(0), glm::vec3(1, 0, 0 ), glm::vec3(0,-1, 0 )),
glm::lookAt(glm::vec3(0), glm::vec3(-1,0, 0 ), glm::vec3(0,-1, 0 )),
glm::lookAt(glm::vec3(0), glm::vec3(0, 1, 0 ), glm::vec3(0, 0, 1 )),
glm::lookAt(glm::vec3(0), glm::vec3(0, -1,0 ), glm::vec3(0, 0, -1)),
glm::lookAt(glm::vec3(0), glm::vec3(0, 0, 1 ), glm::vec3(0,-1, 0 )),
glm::lookAt(glm::vec3(0), glm::vec3(0, 0, -1), glm::vec3(0,-1, 0 ))
};
void AmbientLight::bindAttributes(Shader *shader, Camera *camera)
{
shader->bindVec3(shader->getLocation("lightColor"), m_color);
@ -271,6 +261,7 @@ unsigned int PointLight::getFlags()
void PointLight::initShadowMap(int resolution)
{
m_shadowMapResolution = resolution;
setPos(m_position);
// shader compilation
if(m_shaderRefCounter == 0)
@ -299,8 +290,27 @@ void PointLight::initShadowMap(int resolution)
m_shadowMap = new FrameBuffer();
m_shadowMap->addTexture(tex, GL_DEPTH_ATTACHMENT);
m_shadowMap->initColorAttachments();
}
m_projectionMatrix = glm::perspective(90.f, 1.f, 0.01f, m_range);
void PointLight::updateTransform()
{
if(m_shadowCaster)
{
m_shadowTransforms.clear();
glm::mat4 shadowProj = glm::perspective(45.0f, 1.f, 0.01f, m_range);
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(1.0,0.0,0.0), glm::vec3(0.0,-1.0,0.0)));
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(-1.0,0.0,0.0), glm::vec3(0.0,-1.0,0.0)));
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,1.0,0.0), glm::vec3(0.0,0.0,1.0)));
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,-1.0,0.0), glm::vec3(0.0,0.0,-1.0)));
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,0.0,1.0), glm::vec3(0.0,-1.0,0.0)));
m_shadowTransforms.push_back(shadowProj *
glm::lookAt(m_position, m_position + glm::vec3(0.0,0.0,-1.0), glm::vec3(0.0,-1.0,0.0)));
}
}
void PointLight::destroyShadowMap()
@ -339,9 +349,7 @@ void PointLight::updateShadowMap(Scene* scene)
m_shaders[shaderId]->bind();
m_shaders[shaderId]->bindMat4(m_shaders[shaderId]->getLocation("modelMatrix"), node->modelMatrix);
m_shaders[shaderId]->bindMat4(m_shaders[shaderId]->getLocation("projectionMatrix"), m_projectionMatrix);
m_shaders[shaderId]->bindMat4Array(m_shaders[shaderId]->getLocation("viewMatrices"), m_viewMatrices, 6);
m_shaders[shaderId]->bindVec3(m_shaders[shaderId]->getLocation("pointLight"), m_position);
m_shaders[shaderId]->bindMat4Array(m_shaders[shaderId]->getLocation("layerTransform"), m_shadowTransforms.data(), m_shadowTransforms.size());
m_shaders[shaderId]->bindFloat(m_shaders[shaderId]->getLocation("far_plane"), m_range);
node->mesh->draw(m_shaders[shaderId], false, shaderId, false);
}

View File

@ -97,8 +97,8 @@ public:
glm::vec3 getPos() { return m_position; }
float getAttenuation() { return m_range; }
void setPos(glm::vec3 pos) { m_position = pos; /* TODO : update projection matrix */ }
void setAttenuation(float attenuation) { m_range = attenuation; }
void setPos(glm::vec3 pos) { m_position = pos; updateTransform(); }
void setRange(float range) { m_range = range; updateTransform(); }
virtual LightType getType() { return POINT; }
virtual void bindAttributes(Shader *shader, Camera *camera);
@ -109,14 +109,15 @@ public:
void updateShadowMap(Scene* scene);
private:
void updateTransform();
glm::vec3 m_position;
float m_range;
bool m_shadowCaster;
int m_shadowMapResolution;
FrameBuffer* m_shadowMap;
static const glm::mat4 m_viewMatrices[6];
glm::mat4 m_projectionMatrix;
std::vector<glm::mat4> m_shadowTransforms;
static int m_shaderRefCounter;
static Shader* m_shaders[2];

View File

@ -152,7 +152,7 @@ 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
*/
void setDepth(float d) { depth = d; }
float getDepth() { return depth; }
float getDepth() { return depth; } const
/*************************************************************/
/* 3D MESH PROPERTIES */

View File

@ -10,6 +10,7 @@ void PhongMaterial::bindAttributes(Shader* myShader)
{
// TODO store the attributes location
myShader->bindFloat(myShader->getLocation("materialNs"), shininess);
myShader->bindFloat(myShader->getLocation("opacity"), m_opacity);
if(textures[NORMALS_SLOT] != NULL)
{

View File

@ -23,6 +23,7 @@ struct PhongMaterial : public Material
glm::vec3 diffuse;
glm::vec3 specular;
float shininess;
float m_opacity;
Texture* textures[NB_PHONG_SLOTS];
std::string textureNames[NB_PHONG_SLOTS];
@ -30,7 +31,8 @@ struct PhongMaterial : public Material
emission(0),
diffuse(0.5f),
specular(0.5f),
shininess(10)
shininess(10),
m_opacity(1.f)
{
for(int i=0; i<NB_PHONG_SLOTS; ++i)
textures[i] = NULL;