Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////
- // Pixel shader for the final pass
- //
- // Copyright 2013 DigiPen Institute of Technology
- ////////////////////////////////////////////////////////////////////////
- #version 330
- uniform int mode, WIDTH, HEIGHT,camNear,camFar;
- uniform bool useTexture;
- uniform vec3 cameraPos,lightPos,lightForward;
- //shadow stuff
- uniform sampler2D ldepth;
- vec4 vShdwDepthAndTexCoord;
- uniform vec2 clipDistance;
- uniform mat4 ShadowViewMatrix;
- uniform mat4 ShadowProjMatrix;
- uniform sampler2D gBufferPos;
- uniform sampler2D gBufferNormal;
- uniform sampler2D gBufferDiffuse;
- uniform mat4 lightProj;
- out vec4 fragColor;
- in vec2 texCoord;
- #define MAX_STEPS 2000
- vec3 GetPos()
- {
- vec2 coords;
- coords.x = texCoord.s * WIDTH;
- coords.y = texCoord.t * HEIGHT;
- ivec2 iCoords = ivec2(coords.x,coords.y);
- // index into the gBufferPos texture correctly to grab the appropiate texel for final rendering
- vec3 pos = texelFetch(gBufferPos,iCoords,0).xyz;
- return pos;
- }
- float VolumeLight()
- {
- vec3 vertexPosWS = GetPos();
- // gets initial vector for screen trace
- vec3 forwardVec = normalize(vertexPosWS - cameraPos);
- // scale vector to nearPlane(to start trace)
- float traceDistance = dot(vertexPosWS - (cameraPos + forwardVec * camNear),forwardVec);
- traceDistance = clamp(traceDistance, 0.0,2500.0); // clamp at max traceDistance
- // get first trace position.
- vertexPosWS = cameraPos + forwardVec * camNear;
- float samplingRate = 1.0f;
- // NOTE: make sampling rate a modifable variable, pass it in from cpu, maybe make a gui
- forwardVec *= samplingRate * 2.0f;
- float numberOfSteps = min(traceDistance / length(forwardVec),MAX_STEPS);
- //add jitter, this wil benefit from having a noise texture, but a rand might do
- float seed = 1234.0f;
- float jitter = noise1(seed) * 256.0f;
- //float jitter = 1.0f;
- float step = length(forwardVec);
- float baseBrightness = 0.0009 ; // minimum brightness for samples???
- float scale = step * baseBrightness;
- vec3 curPos = vertexPosWS + forwardVec * jitter;
- vec4 coordinates = ShadowProjMatrix*ShadowViewMatrix*vec4(curPos.x,curPos.y,curPos.z,1.0f);
- coordinates.z = curPos.z;
- curPos = vertexPosWS + forwardVec * (1.0f + jitter);
- vec4 coordinatesEnd = ShadowProjMatrix*ShadowViewMatrix*vec4(curPos.x,curPos.y,curPos.z,1.0f);
- coordinatesEnd.z = curPos.z;
- vec4 coordinateDelta = coordinatesEnd - coordinates;
- float sample;
- float light = 0;
- for(float i = 0; i < numberOfSteps; ++i)
- {
- vec2 tc = coordinates.st / coordinates.q;
- sample = float(texture2D(ldepth, tc).x < coordinates.z); // float() is a cast from bool to float
- if(sample > 0)
- light += scale * sample;
- coordinates += coordinateDelta;
- }
- return light;
- }
- void main()
- {
- vec3 worldVertex = GetPos();
- float light = VolumeLight(); // calculates volume light value of this pixel by tracing the screen
- fragColor.xyz = vec3(light,light,light);
- fragColor.w = 1.0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement