Skip to content

Commit

Permalink
Put skybox in its own pass apart from deferred shading and phong
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy C. Li committed Mar 20, 2012
1 parent cd92aec commit cfbd931
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 49 deletions.
66 changes: 35 additions & 31 deletions src/main/resources/shaders/final.frag.glsl
Expand Up @@ -3,15 +3,13 @@ uniform sampler2D difGbuf;
uniform sampler2D spcGbuf;
uniform sampler2D ssaoBuf;

uniform float farClip;

varying vec2 texcoord;

#define MAX_LIGHTS 1

// Using custom z buffer version
float zSample(vec2 texc) {
return texture2D(nmlGbuf, texc).w-farClip;
return texture2D(nmlGbuf, texc).w-50.0;
}

void main()
Expand All @@ -32,35 +30,41 @@ void main()
vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0);

vec4 difMat = texture2D(difGbuf, texcoord);
vec4 aoFactor = texture2D(ssaoBuf, texcoord);

for (int i=0;i<MAX_LIGHTS;i++)
{
vec3 L = normalize(gl_LightSource[i].position.xyz - v);
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
vec3 R = normalize(-reflect(L,N));

//calculate Ambient Term:
vec4 Iamb =
gl_FrontLightProduct[i].ambient*difMat*aoFactor;

//calculate Diffuse Term:
vec4 Idiff =
gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0)*difMat;
Idiff = clamp(Idiff, 0.0, 1.0);

// calculate Specular Term:
vec4 Ispec = gl_FrontLightProduct[i].specular
* pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
Ispec = clamp(Ispec, 0.0, 1.0);

//finalColor += Idiff;
finalColor += Iamb + Idiff + Ispec;
//finalColor += vec4(L, 1);

if(difMat.xyz != vec3(0., 0., 0.)) {
vec4 aoFactor = texture2D(ssaoBuf, texcoord);

for (int i=0;i<MAX_LIGHTS;i++)
{
vec3 L = normalize(gl_LightSource[i].position.xyz - v);
vec3 E = normalize(-v); // we are in Eye Coordinates, so EyePos is (0,0,0)
vec3 R = normalize(-reflect(L,N));

//calculate Ambient Term:
vec4 Iamb =
gl_FrontLightProduct[i].ambient*difMat*aoFactor;

//calculate Diffuse Term:
vec4 Idiff =
gl_FrontLightProduct[i].diffuse * max(dot(N,L), 0.0)*difMat;
Idiff = clamp(Idiff, 0.0, 1.0);

// calculate Specular Term:
vec4 Ispec = gl_FrontLightProduct[i].specular
* pow(max(dot(R,E),0.0),0.3*gl_FrontMaterial.shininess);
Ispec = clamp(Ispec, 0.0, 1.0);

//finalColor += Idiff;
finalColor += Iamb + Idiff + Ispec;
//finalColor += vec4(L, 1);
}

// write Total Color:
gl_FragColor = clamp(finalColor, 0.0, 1.0);
}
else {
//gl_FragColor = vec4(1., 0., 0., 0.0);
}

// write Total Color:
gl_FragColor = clamp(finalColor, 0.0, 1.0);
//gl_FragColor = vec4(texcoord.s, texcoord.t, 0, 1.0);
//gl_FragColor = vec4(texture2D(difTex, texcoord).rgb, 1.0);
}
4 changes: 1 addition & 3 deletions src/main/resources/shaders/gbufs.frag.glsl
@@ -1,7 +1,5 @@
#version 120

uniform float farClip; // to store linear normalized z depths

uniform sampler2D texDif;
uniform sampler2D texSpc;

Expand All @@ -19,7 +17,7 @@ void main()
float zEye = vEyeHomo.z/vEyeHomo.w;
//gl_FragData[0] = vec4(vec3(1,1,1)*(-vEyeHomo.x/vEyeHomo.w)*0.05,
//gl_FragData[0] = vec4(vec3(1,1,1)*(-vEyeHomo.z/vEyeHomo.w)*0.05, 1.0);
gl_FragData[0] = vec4(N, zEye+farClip);
gl_FragData[0] = vec4(N, zEye+50.0);

// Diffuse color
gl_FragData[1] = texture2D(texDif, texcoord);
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/shaders/ssao.frag.glsl
Expand Up @@ -3,7 +3,6 @@ uniform sampler2D difGbuf;
uniform sampler2D spcGbuf;
//uniform sampler2D zBuf;

uniform float farClip;
varying vec2 texcoord;

float rand(vec2 co){
Expand All @@ -12,7 +11,7 @@ float rand(vec2 co){

// Using custom z buffer version
float zSample(vec2 texc) {
return texture2D(nmlGbuf, texc).w-farClip;
return texture2D(nmlGbuf, texc).w-50.0;
}

void main()
Expand Down
4 changes: 0 additions & 4 deletions src/main/scala/Camera.scala
Expand Up @@ -146,10 +146,6 @@ class Camera extends Entity {
Project.gluPerspective(90, 16.0f/9.0f, nearClip, farClip)
}

def passInUniforms(shader: Shader) {
glUniform1f(glGetUniformLocation(shader.id, "farClip"), farClip)
}

def updateCamPos() {
camX = (camR*cos(camPhi)*sin(camTheta) + centerX).asInstanceOf[Float]
camY = (camR*sin(camPhi)*sin(camTheta) + centerY).asInstanceOf[Float]
Expand Down
26 changes: 17 additions & 9 deletions src/main/scala/Game.scala
Expand Up @@ -158,6 +158,8 @@ object Main {
}

def renderGame() = {
glDisable(GL_BLEND)

// Render G - buffers
gbufFbo.bind()
gbufShader.use()
Expand All @@ -166,17 +168,14 @@ object Main {
//TODO move into skybox.render()

camera.loadGLMatrices()
camera.passInUniforms(gbufShader)

skybox.render(camera, gbufShader)

manager.renderAll(gbufShader)
partmanager.renderAll(camera, gbufShader)

// Render SSAO pass
ssaoFbo.bind()
ssaoShader.use()

camera.passInUniforms(ssaoShader)
camera.loadIntoTextureMatrices()
ViewMode.bindGBufs(ssaoShader)

Expand All @@ -198,18 +197,25 @@ object Main {

// Render final shader
finalFbo.bind()

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

testShader.use()
camera.loadGLMatrices()

skybox.render(camera, testShader)

finalShader.use()
ViewMode.bindGBufs(finalShader)
//ssaoFbo.tex.bindAndSetShader(3, finalShader, "ssaoBuf");
blurYFbo.tex.bindAndSetShader(3, finalShader, "ssaoBuf");

camera.passInUniforms(finalShader)
camera.loadIntoTextureMatrices()

camera.loadGLMatrices()
curLevel.setLights()

drawQuad(finalShader)
drawQuad(finalShader, false)/**/

// Render Screen
screenFbo.bind()
Expand All @@ -219,8 +225,10 @@ object Main {
drawQuad(testShader)
}

def drawQuad(shader: Shader) = {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def drawQuad(shader: Shader, clear: Boolean = true) = {
if(clear)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, 1, 0, 1, 1, -1)
Expand Down

0 comments on commit cfbd931

Please sign in to comment.