Skip to content

Commit

Permalink
Gloom: Binding lighting uniforms
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent d4e7e4f commit 195356c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 43 deletions.
27 changes: 21 additions & 6 deletions doomsday/tests/test_gloom/gloom/render/context.cpp
Expand Up @@ -27,23 +27,38 @@ namespace gloom {

Context &Context::bindCamera(GLProgram &program)
{
program << uCurrentTime << view.uCameraPos << view.uCameraMvpMatrix << view.uWorldToViewRotate
<< view.uViewToWorldRotate << view.uModelViewMatrix << view.uProjMatrix;
program << uCurrentTime
<< view.uCameraPos
<< view.uCameraMvpMatrix
<< view.uModelViewMatrix
<< view.uViewToWorldMatrix
<< view.uProjMatrix
<< view.uInverseProjMatrix
<< view.uViewToWorldRotate
<< view.uWorldToViewRotate;
return *this;
}

Context &Context::bindGBuffer(GLProgram &program)
{
program << gbuffer->uGBufferDiffuse() << gbuffer->uGBufferNormal() << gbuffer->uGBufferDepth()
<< gbuffer->uGBufferSpecGloss() << gbuffer->uGBufferEmissive()
program << gbuffer->uGBufferDiffuse()
<< gbuffer->uGBufferNormal()
<< gbuffer->uGBufferDepth()
<< gbuffer->uGBufferSpecGloss()
<< gbuffer->uGBufferEmissive()
<< gbuffer->uViewportSize();
return *this;
}

Context &Context::bindMaterials(GLProgram &program)
{
program << uDiffuseAtlas << uEmissiveAtlas << uSpecGlossAtlas << uNormalDisplAtlas << uEnvMap
<< uEnvIntensity << mapRender->materialLibrary().uTextureMetrics();
program << uDiffuseAtlas
<< uEmissiveAtlas
<< uSpecGlossAtlas
<< uNormalDisplAtlas
<< uEnvMap
<< uEnvIntensity
<< mapRender->materialLibrary().uTextureMetrics();
return *this;
}

Expand Down
68 changes: 41 additions & 27 deletions doomsday/tests/test_gloom/gloom/render/lightrender.cpp
Expand Up @@ -177,35 +177,11 @@ DENG2_PIMPL(LightRender)
ctx.bindGBuffer(shadingProgram);

giQuad.glInit(self().context());
ctx.shaders->build(giQuad.program(), "gloom.light.global")
<< ctx.view.uInverseProjMatrix
<< ctx.view.uViewToWorldRotate
<< ctx.uEnvMap
<< ctx.uEnvIntensity
<< ctx.ssao->uSSAOBuf()
<< uShadowMap
<< uViewSpaceLightOrigin
<< uViewSpaceLightDir
<< uLightIntensity
<< uViewToLightMatrix
<< ctx.uLightMatrix
<< uOmniLightCount
<< uShadowMaps[0]
<< uShadowMaps[1]
<< uShadowMaps[2]
<< uShadowMaps[3]
<< uShadowMaps[4]
<< uShadowMaps[5];
ctx.shaders->build(giQuad.program(), "gloom.light.global");
ctx.bindCamera(giQuad.program());
ctx.bindGBuffer(giQuad.program());

for (int i = 0; i < MAX_OMNI_LIGHTS; ++i)
{
giQuad.program() << uOmniLightCount
<< uOmniLights[i].origin
<< uOmniLights[i].intensity
<< uOmniLights[i].falloffRadius
<< uOmniLights[i].shadowIndex;
}
bindLighting(giQuad.program());

// Generate a sphere for light bounds.
{
Expand Down Expand Up @@ -267,6 +243,39 @@ DENG2_PIMPL(LightRender)
giQuad.glDeinit();
}

void bindLighting(GLProgram &program)
{
auto &ctx = self().context();

program
// Ambient:
<< ctx.uEnvMap
<< ctx.uEnvIntensity
<< ctx.ssao->uSSAOBuf()
// Directional:
<< uShadowMap
<< uViewSpaceLightOrigin
<< uViewSpaceLightDir
<< uLightIntensity
<< uViewToLightMatrix
<< ctx.uLightMatrix
// Omni:
<< uOmniLightCount;

for (auto &u : uShadowMaps)
{
program << u;
}

for (int i = 0; i < MAX_OMNI_LIGHTS; ++i)
{
program << uOmniLights[i].origin
<< uOmniLights[i].intensity
<< uOmniLights[i].falloffRadius
<< uOmniLights[i].shadowIndex;
}
}

void selectShadowCasters()
{
shadowCasters.clear();
Expand Down Expand Up @@ -421,6 +430,11 @@ void LightRender::advanceTime(TimeSpan elapsed)
}
}

void LightRender::bindLighting(GLProgram &program)
{
d->bindLighting(program);
}

void LightRender::renderLighting()
{
auto & ctx = context();
Expand Down
1 change: 1 addition & 0 deletions doomsday/tests/test_gloom/gloom/render/lightrender.h
Expand Up @@ -42,6 +42,7 @@ class LightRender : public Render
void render() override;
void advanceTime(TimeSpan) override;

void bindLighting(GLProgram &);
void renderLighting();

typedef std::function<void (const Light &)> RenderFunc;
Expand Down
Expand Up @@ -11,12 +11,12 @@
in vec2 vUV;

void main(void) {
vec4 vsPos = GBuffer_FragViewSpacePos();
vec3 vsPos = GBuffer_FragViewSpacePos().xyz;
vec3 normal = GBuffer_FragViewSpaceNormal();
vec3 diffuse = GBuffer_FragDiffuse();
vec4 specGloss = GBuffer_FragSpecGloss();

SurfacePoint sp = SurfacePoint(vsPos.xyz, normal, diffuse, specGloss);
SurfacePoint sp = SurfacePoint(vsPos, normal, diffuse, specGloss);

// Ambient light.
vec3 outColor = Gloom_AmbientLight(sp, vUV);
Expand Down
Expand Up @@ -24,10 +24,10 @@ void main(void) {
vec3 diffuse = GBuffer_FragDiffuse();
vec4 specGloss = GBuffer_FragSpecGloss();

SurfacePoint surf = SurfacePoint(pos, normal, diffuse, specGloss);
SurfacePoint sp = SurfacePoint(pos, normal, diffuse, specGloss);

// Radius is scaled: volume is not a perfect sphere, avoid reaching edges.
// light.falloffRadius *= 0.95;

out_FragColor = vec4(lit * Gloom_BlinnPhong(light, surf), 0.0);
out_FragColor = vec4(lit * Gloom_BlinnPhong(light, sp), 0.0);
}
Expand Up @@ -3,6 +3,7 @@
#include "common/gbuffer.glsl"
#include "common/material.glsl"
#include "common/tangentspace.glsl"
#include "common/lightmodel.glsl"

uniform mat4 uProjMatrix;
uniform float uCurrentTime;
Expand Down Expand Up @@ -34,6 +35,7 @@ void main(void) {
);

// Parallax mapping.
// TODO: Better to sample from two textures at the lower level, not here...
float displacementDepths[2];
vec3 viewDir = normalize(vTSViewDir);
vec2 texCoord = Gloom_Parallax(matIndex, vUV + waterOff[0], viewDir, displacementDepths[0]);
Expand All @@ -43,23 +45,21 @@ void main(void) {
Gloom_FetchTexture(matIndex, Texture_NormalDisplacement, texCoord));
vec3 normal2 = GBuffer_UnpackNormal(
Gloom_FetchTexture(matIndex, Texture_NormalDisplacement, texCoord2));
// vec3 vsNormal = Gloom_TangentMatrix(ts) * normal;
// vec3 vsNormal2 = Gloom_TangentMatrix(ts) * normal2;

float displacementDepth = (displacementDepths[0] + displacementDepths[1]) / 2.0;

normal = normalize(normal + normal2);
//vsNormal = normalize(vsNormal + vsNormal2);

vec4 diffuse = Gloom_FetchTexture(matIndex, Texture_Diffuse, texCoord);
vec3 emissive = Gloom_FetchTexture(matIndex, Texture_Emissive, texCoord).rgb;
vec4 diffuse = Gloom_FetchTexture(matIndex, Texture_Diffuse, texCoord);
vec3 emissive = Gloom_FetchTexture(matIndex, Texture_Emissive, texCoord).rgb;
vec4 specGloss = Gloom_FetchTexture(matIndex, Texture_SpecularGloss, texCoord);

out_FragColor = vec4(normal, 0.5);

vec3 vsPos = vVSPos.xyz / vVSPos.w;

// Write a displaced depth. // TODO: Add a routine for doing this.
if (displacementDepth > 0.0) {
vec3 vsPos = vVSPos.xyz / vVSPos.w;
vsPos += normalize(vsPos) * displacementDepth / abs(dot(Axis_Z, viewDir));
vec4 dispPos = uProjMatrix * vec4(vsPos, 1.0);
float ndc = dispPos.z / dispPos.w;
Expand All @@ -68,4 +68,9 @@ void main(void) {
else {
gl_FragDepth = gl_FragCoord.z;
}

// Now we can apply lighting to the surface.
SurfacePoint sp = SurfacePoint(vsPos, normal, diffuse.rgb, specGloss);


}

0 comments on commit 195356c

Please sign in to comment.