This commit is contained in:
Marianne Gagnon 2014-05-13 19:39:42 -04:00
commit 9410a0fd90
18 changed files with 143 additions and 87 deletions

View File

@ -21,62 +21,58 @@ void main()
vec2 offset = 10. / screen;
vec4 col = texture2D(tex, uv);
vec4 col = texture(tex, uv);
vec4 colOriginal = col;
// Weight from here http://artmartinsh.blogspot.fr/2010/02/glsl-lens-blur-filter-with-bokeh.html
col += texture2D(tex, uv + (vec2(0.0, 0.4) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.15, 0.37) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.29,0.29) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.37,0.15) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.4, 0.0) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.37, -0.15) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.29, -0.29) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.15, -0.37) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.0, -0.4) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.15, 0.37) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.29, 0.29) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.37, 0.15) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.4, 0.0) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.37, -0.15) * offset) * blur);
col += texture2D(tex, uv + (vec2(-0.29, -0.29) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.15, -0.37) * offset) * blur);
col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur);
col += texture(tex, uv + (vec2(0.15, 0.37) * offset) * blur);
col += texture(tex, uv + (vec2(0.29,0.29) * offset) * blur);
col += texture(tex, uv + (vec2(-0.37,0.15) * offset) * blur);
col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur);
col += texture(tex, uv + (vec2(0.37, -0.15) * offset) * blur);
col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur);
col += texture(tex, uv + (vec2(-0.15, -0.37) * offset) * blur);
col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur);
col += texture(tex, uv + (vec2(-0.15, 0.37) * offset) * blur);
col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur);
col += texture(tex, uv + (vec2(0.37, 0.15) * offset) * blur);
col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur);
col += texture(tex, uv + (vec2(-0.37, -0.15) * offset) * blur);
col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur);
col += texture(tex, uv + (vec2(0.15, -0.37) * offset) * blur);
col += texture2D(tex, uv + (vec2(0.15, 0.37) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(-0.37, 0.15) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(0.37, -0.15) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(-0.15, -0.37) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(-0.15, 0.37) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(0.37, 0.15) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(-0.37, -0.15) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(0.15, -0.37) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(0.15, 0.37) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(-0.37, 0.15) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(0.37, -0.15) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(-0.15, -0.37) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(-0.15, 0.37) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(0.37, 0.15) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(-0.37, -0.15) * offset) * blur * 0.9);
col += texture(tex, uv + (vec2(0.15, -0.37) * offset) * blur * 0.9);
col += texture2D(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.7);
col += texture2D(tex, uv + (vec2(0.0, 0.4) * offset) * blur *0.7);
col += texture(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.7);
col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur *0.7);
col += texture2D(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.4);
col += texture2D(tex, uv + (vec2(0.0, 0.4) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(0.29, 0.29) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(0.4, 0.0) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(0.29, -0.29) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(0.0, -0.4) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(-0.29, 0.29) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(-0.4, 0.0) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(-0.29, -0.29) * offset) * blur * 0.4);
col += texture(tex, uv + (vec2(0.0, 0.4) * offset) * blur * 0.4);
col = vec4(col.rgb / 41.0, col.a);
depth = clamp((FragPos.z/280), 0., 1.);
depth = (1 - depth);
vec3 final = colOriginal.rgb * depth + col.rgb * (1 - depth);
/*
FragColor.xyz = vec3(depth);
FragColor.a = 1.0;
*/
FragColor = vec4(final, 1.);
}

View File

@ -0,0 +1,13 @@
uniform sampler2D tex;
uniform float zn;
uniform float zf;
in vec2 uv;
out float Depth;
void main()
{
float d = texture(tex, uv).x;
float c0 = zn * zf, c1 = zn - zf, c2 = zf;
Depth = c0 / (d * c1 + c2);
}

View File

@ -1,9 +1,7 @@
// From paper http://graphics.cs.williams.edu/papers/AlchemyHPG11/
// and improvements here http://graphics.cs.williams.edu/papers/SAOHPG12/
uniform sampler2D ntex;
uniform sampler2D dtex;
uniform sampler2D noise_texture;
uniform vec4 samplePoints[16];
uniform vec2 screen;
@ -37,24 +35,25 @@ const float k = 1.;
const float invSamples = 1. / SAMPLES;
vec3 DecodeNormal(vec2 n);
vec4 getPosFromUVDepth(vec3 uvDepth, mat4 InverseProjectionMatrix);
vec3 getXcYcZc(int x, int y, float zC)
{
// We use perspective symetric projection matrix hence P(0,2) = P(1, 2) = 0
float xC= (1. - 2 * (float(x) + 0.5) / screen.x) * zC / ProjectionMatrix[0][0];
float yC= (1. + 2 * (float(y) + 0.5) / screen.y) * zC / ProjectionMatrix[1][1];
return vec3(xC, yC, zC);
}
void main(void)
{
vec4 cur = texture(ntex, uv);
float curdepth = texture(dtex, uv).x;
vec4 FragPos = getPosFromUVDepth(vec3(uv, curdepth), InverseProjectionMatrix);
float lineardepth = textureLod(dtex, uv, 0.).x;
int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y);
vec3 FragPos = getXcYcZc(x, y, lineardepth);
// get the normal of current fragment
vec3 ddx = dFdx(FragPos.xyz);
vec3 ddy = dFdy(FragPos.xyz);
vec3 norm = normalize(cross(ddy, ddx));
// Workaround for nvidia and skyboxes
float len = dot(vec3(1.0), abs(cur.xyz));
if (len < 0.2) discard;
vec3 ddx = dFdx(FragPos);
vec3 ddy = dFdy(FragPos);
vec3 norm = -normalize(cross(ddy, ddx));
int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y);
float r = radius / FragPos.z;
float phi = 30. * (x ^ y) + 10. * x * y;
float bl = 0.0;
@ -63,20 +62,18 @@ void main(void)
float alpha = (i + .5) * invSamples;
float theta = 2. * 3.14 * tau * alpha + phi;
float h = r * alpha;
vec2 occluder_uv = h * vec2(cos(theta), sin(theta)) * screen;
occluder_uv += gl_FragCoord.xy;
vec2 offset = h * vec2(cos(theta), sin(theta)) * screen;
float m = round(log2(h)) + 7;
ivec2 ioccluder_uv = ivec2(occluder_uv);
occluder_uv = (ioccluder_uv >> int(m)) << int(m);
occluder_uv /= screen;
float m = round(log2(h) + 7);
ivec2 ioccluder_uv = ivec2(x, y) + ivec2(offset);
ioccluder_uv = (ioccluder_uv << int(m)) >> int(m);
if (occluder_uv.x < 0. || occluder_uv.x > 1. || occluder_uv.y < 0. || occluder_uv.y > 1.) continue;
if (ioccluder_uv.x < 0 || ioccluder_uv.x > screen.x || ioccluder_uv.y < 0 || ioccluder_uv.y > screen.y) continue;
float occluderFragmentDepth = textureLod(dtex, occluder_uv, m).x;
vec4 OccluderPos = getPosFromUVDepth(vec3(occluder_uv, occluderFragmentDepth), InverseProjectionMatrix);
float LinearoccluderFragmentDepth = textureLod(dtex, vec2(ioccluder_uv) / screen, m).x;
vec3 OccluderPos = getXcYcZc(ioccluder_uv.x, ioccluder_uv.y, LinearoccluderFragmentDepth);
vec3 vi = (OccluderPos - FragPos).xyz;
vec3 vi = OccluderPos - FragPos;
bl += max(0, dot(vi, norm) - FragPos.z * beta) / (dot(vi, vi) + epsilon);
}

View File

@ -2305,7 +2305,7 @@ void IrrDriver::applyObjectPassShader()
// ----------------------------------------------------------------------------
scene::ISceneNode *IrrDriver::addLight(const core::vector3df &pos, float energy,
scene::ISceneNode *IrrDriver::addLight(const core::vector3df &pos, float energy, float radius,
float r, float g, float b, bool sun, scene::ISceneNode* parent)
{
if (m_glsl)
@ -2314,7 +2314,7 @@ scene::ISceneNode *IrrDriver::addLight(const core::vector3df &pos, float energy,
LightNode *light = NULL;
if (!sun)
light = new LightNode(m_scene_manager, parent, energy, r, g, b);
light = new LightNode(m_scene_manager, parent, energy, radius, r, g, b);
else
light = new SunNode(m_scene_manager, parent, r, g, b);

View File

@ -563,8 +563,8 @@ public:
void applyObjectPassShader();
void applyObjectPassShader(scene::ISceneNode * const node, bool rimlit = false);
// ------------------------------------------------------------------------
scene::ISceneNode *addLight(const core::vector3df &pos, float energy = 1., float r = 1.0f,
float g = 1.0f, float b = 1.0f, bool sun = false, scene::ISceneNode* parent = NULL);
scene::ISceneNode *addLight(const core::vector3df &pos, float energy, float radius, float r,
float g, float b, bool sun = false, scene::ISceneNode* parent = NULL);
// ------------------------------------------------------------------------
void clearLights();
// ------------------------------------------------------------------------

View File

@ -34,10 +34,11 @@ using namespace core;
aabbox3df LightNode::box;
LightNode::LightNode(scene::ISceneManager* mgr, scene::ISceneNode* parent, float e, float r, float g, float b):
LightNode::LightNode(scene::ISceneManager* mgr, scene::ISceneNode* parent, float e, float d, float r, float g, float b):
ISceneNode(parent == NULL ? mgr->getRootSceneNode() : parent, mgr, -1)
{
m_energy = e;
m_radius = d;
m_energy_multiplier = 1.0f;
m_color[0] = r;
m_color[1] = g;

View File

@ -40,7 +40,7 @@ class LightNode: public scene::ISceneNode
#endif
public:
LightNode(scene::ISceneManager* mgr, scene::ISceneNode* parent, float energy, float r, float g, float b);
LightNode(scene::ISceneManager* mgr, scene::ISceneNode* parent, float energy, float d, float r, float g, float b);
virtual ~LightNode();
virtual void render() OVERRIDE;
@ -55,7 +55,7 @@ public:
virtual u32 getMaterialCount() const OVERRIDE { return 1; }
virtual bool isPointLight() { return true; }
//float getRadius() const { return m_radius; }
float getRadius() const { return m_radius; }
float getEnergy() const { return m_energy; }
float getEffectiveEnergy() const { return m_energy_multiplier * m_energy; }
core::vector3df getColor() const { return core::vector3df(m_color[0], m_color[1], m_color[2]); }
@ -66,7 +66,7 @@ public:
protected:
static core::aabbox3df box;
//float m_radius;
float m_radius;
float m_color[3];
float m_energy;

View File

@ -451,12 +451,22 @@ void PostProcessing::renderSSAO()
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
// Generate linear depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, irr_driver->getFBO(FBO_LINEAR_DEPTH));
glUseProgram(FullScreenShader::LinearizeDepthShader::Program);
glBindVertexArray(FullScreenShader::LinearizeDepthShader::vao);
setTexture(0, irr_driver->getDepthStencilTexture(), GL_LINEAR, GL_LINEAR);
FullScreenShader::LinearizeDepthShader::setUniforms(irr_driver->getSceneManager()->getActiveCamera()->getNearValue(), irr_driver->getSceneManager()->getActiveCamera()->getFarValue(), 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindFramebuffer(GL_FRAMEBUFFER, irr_driver->getFBO(FBO_SSAO));
if (!noise_tex)
noise_tex = irr_driver->getTexture(file_manager->getAsset("textures/noise.png").c_str());
glUseProgram(FullScreenShader::SSAOShader::Program);
glBindVertexArray(FullScreenShader::SSAOShader::vao);
setTexture(0, irr_driver->getDepthStencilTexture(), GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_LINEAR_DEPTH), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
setTexture(1, getTextureGLuint(noise_tex), GL_LINEAR, GL_LINEAR);

View File

@ -874,7 +874,7 @@ void IrrDriver::renderLights(scene::ICameraSceneNode * const camnode, float dt)
PointLightsInfo[lightnum].blue = col.Z;
// Light radius
PointLightsInfo[lightnum].radius = 20 * light_node->getEffectiveEnergy();
PointLightsInfo[lightnum].radius = light_node->getRadius();
}
}
if (lightnum > MAXLIGHT)

View File

@ -83,6 +83,7 @@ RTT::RTT()
RenderTargetTextures[RTT_TMP2] = generateRTT(res, GL_RGBA16F, GL_BGRA, GL_FLOAT);
RenderTargetTextures[RTT_TMP3] = generateRTT(res, GL_RGBA16F, GL_BGRA, GL_FLOAT);
RenderTargetTextures[RTT_TMP4] = generateRTT(res, GL_R16F, GL_RED, GL_FLOAT);
RenderTargetTextures[RTT_LINEAR_DEPTH] = generateRTT(res, GL_R32F, GL_RED, GL_FLOAT);
RenderTargetTextures[RTT_NORMAL_AND_DEPTH] = generateRTT(res, GL_RGBA16F, GL_RGBA, GL_FLOAT);
RenderTargetTextures[RTT_COLOR] = generateRTT(res, GL_RGBA16F, GL_BGRA, GL_FLOAT);
RenderTargetTextures[RTT_MLAA_COLORS] = generateRTT(res, GL_SRGB, GL_BGR, GL_UNSIGNED_BYTE);
@ -118,6 +119,7 @@ RTT::RTT()
FrameBuffers[FBO_TMP1_WITH_DS] = generateFBO(RenderTargetTextures[RTT_TMP1], DepthStencilTexture);
FrameBuffers[FBO_TMP2_WITH_DS] = generateFBO(RenderTargetTextures[RTT_TMP2], DepthStencilTexture);
FrameBuffers[FBO_TMP4] = generateFBO(RenderTargetTextures[RTT_TMP4], DepthStencilTexture);
FrameBuffers[FBO_LINEAR_DEPTH] = generateFBO(RenderTargetTextures[RTT_LINEAR_DEPTH]);
FrameBuffers[FBO_DISPLACE] = generateFBO(RenderTargetTextures[RTT_DISPLACE], DepthStencilTexture);
FrameBuffers[FBO_HALF1] = generateFBO(RenderTargetTextures[RTT_HALF1]);
FrameBuffers[FBO_HALF2] = generateFBO(RenderTargetTextures[RTT_HALF2]);

View File

@ -31,6 +31,7 @@ enum TypeRTT
RTT_TMP2,
RTT_TMP3,
RTT_TMP4,
RTT_LINEAR_DEPTH,
RTT_NORMAL_AND_DEPTH,
RTT_COLOR,
RTT_LOG_LUMINANCE,
@ -87,6 +88,7 @@ enum TypeFBO
FBO_TMP1_WITH_DS,
FBO_TMP2_WITH_DS,
FBO_TMP4,
FBO_LINEAR_DEPTH,
FBO_HALF1,
FBO_HALF2,
FBO_QUARTER1,

View File

@ -311,6 +311,7 @@ void Shaders::loadShaders()
FullScreenShader::PenumbraVShader::init();
FullScreenShader::GlowShader::init();
FullScreenShader::PassThroughShader::init();
FullScreenShader::LinearizeDepthShader::init();
FullScreenShader::SSAOShader::init();
FullScreenShader::SunLightShader::init();
FullScreenShader::DiffuseEnvMapShader::init();
@ -2510,6 +2511,29 @@ namespace FullScreenShader
vao = createVAO(Program);
}
GLuint LinearizeDepthShader::Program;
GLuint LinearizeDepthShader::uniform_zn;
GLuint LinearizeDepthShader::uniform_zf;
GLuint LinearizeDepthShader::uniform_texture;
GLuint LinearizeDepthShader::vao;
void LinearizeDepthShader::init()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/linearizedepth.frag").c_str());
uniform_texture = glGetUniformLocation(Program, "texture");
uniform_zf = glGetUniformLocation(Program, "zf");
uniform_zn = glGetUniformLocation(Program, "zn");
vao = createVAO(Program);
}
void LinearizeDepthShader::setUniforms(float zn, float zf, unsigned TU_tex)
{
glUniform1f(uniform_zn, zn);
glUniform1f(uniform_zf, zf);
glUniform1i(uniform_texture, TU_tex);
}
GLuint GlowShader::Program;
GLuint GlowShader::uniform_tex;
GLuint GlowShader::vao;

View File

@ -713,6 +713,17 @@ public:
static void init();
};
class LinearizeDepthShader
{
public:
static GLuint Program;
static GLuint uniform_zn, uniform_zf, uniform_texture;
static GLuint vao;
static void init();
static void setUniforms(float zn, float zf, unsigned TU_tex);
};
class GlowShader
{
public:

View File

@ -35,7 +35,7 @@ using namespace scene;
using namespace core;
SunNode::SunNode(scene::ISceneManager* mgr, scene::ISceneNode* parent, float r, float g, float b):
LightNode(mgr, parent, 0., r, g, b)
LightNode(mgr, parent, 0., 0., r, g, b)
{
m_color[0] = r;
m_color[1] = g;

View File

@ -302,7 +302,7 @@ void FeatureUnlockedCutScene::init()
120, 120));
const core::vector3df &sun_pos = core::vector3df( 0, 200, 100.0f );
m_light = irr_driver->addLight(sun_pos, 10000.0f, 1, 1, 1);
m_light = irr_driver->addLight(sun_pos, 10000.0f, 1., 1, 1, 1, true);
#ifdef DEBUG
m_light->setName("light");
#endif

View File

@ -1719,7 +1719,7 @@ void Track::loadTrackModel(bool reverse_track, unsigned int mode_id)
}
const video::SColorf tmpf(m_sun_diffuse_color);
m_sun = irr_driver->addLight(m_sun_position, 0., tmpf.r, tmpf.g, tmpf.b, true);
m_sun = irr_driver->addLight(m_sun_position, 0., 0., tmpf.r, tmpf.g, tmpf.b, true);
if (!irr_driver->isGLSL())
{

View File

@ -675,15 +675,15 @@ TrackObjectPresentationLight::TrackObjectPresentationLight(const XMLNode& xml_no
xml_node.get("color", &m_color);
const video::SColorf colorf(m_color);
//m_distance = 25.0f;
//xml_node.get("distance", &m_distance);
m_energy = 1.0f;
xml_node.get("energy", &m_energy);
m_distance = 20. * m_energy;
xml_node.get("distance", &m_distance);
if (irr_driver->isGLSL())
{
m_node = irr_driver->addLight(m_init_xyz, m_energy, colorf.r, colorf.g, colorf.b, false, parent);
m_node = irr_driver->addLight(m_init_xyz, m_energy, m_distance, colorf.r, colorf.g, colorf.b, false, parent);
}
else
{

View File

@ -307,7 +307,7 @@ class TrackObjectPresentationLight : public TrackObjectPresentationSceneNode
{
private:
video::SColor m_color;
//float m_distance;
float m_distance;
float m_energy;
public: