Advertisement
Guest User

Untitled

a guest
Jan 27th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////
  2. // Pixel shader for the final pass
  3. //
  4. // Copyright 2013 DigiPen Institute of Technology
  5. ////////////////////////////////////////////////////////////////////////
  6. #version 330
  7.  
  8. uniform int mode, WIDTH, HEIGHT,camNear,camFar;
  9. uniform bool useTexture;
  10.  
  11. uniform vec3 cameraPos,lightPos,lightForward;
  12.  
  13.  
  14. //shadow stuff
  15. uniform sampler2D ldepth;
  16. vec4 vShdwDepthAndTexCoord;
  17. uniform vec2 clipDistance;
  18. uniform mat4 ShadowViewMatrix;
  19. uniform mat4 ShadowProjMatrix;
  20.  
  21.  
  22. uniform sampler2D gBufferPos;
  23. uniform sampler2D gBufferNormal;
  24. uniform sampler2D gBufferDiffuse;
  25. uniform mat4 lightProj;
  26.  
  27. out vec4 fragColor;
  28.  
  29. in vec2 texCoord;
  30.  
  31. #define MAX_STEPS 2000
  32.  
  33.  
  34.  
  35. vec3 GetPos()
  36. {
  37.  
  38. vec2 coords;
  39. coords.x = texCoord.s * WIDTH;
  40. coords.y = texCoord.t * HEIGHT;
  41. ivec2 iCoords = ivec2(coords.x,coords.y);
  42. // index into the gBufferPos texture correctly to grab the appropiate texel for final rendering
  43. vec3 pos = texelFetch(gBufferPos,iCoords,0).xyz;
  44. return pos;
  45. }
  46.  
  47.  
  48.  
  49. float VolumeLight()
  50. {
  51. vec3 vertexPosWS = GetPos();
  52.  
  53. // gets initial vector for screen trace
  54. vec3 forwardVec = normalize(vertexPosWS - cameraPos);
  55. // scale vector to nearPlane(to start trace)
  56. float traceDistance = dot(vertexPosWS - (cameraPos + forwardVec * camNear),forwardVec);
  57. traceDistance = clamp(traceDistance, 0.0,2500.0); // clamp at max traceDistance
  58.  
  59. // get first trace position.
  60. vertexPosWS = cameraPos + forwardVec * camNear;
  61. float samplingRate = 1.0f;
  62. // NOTE: make sampling rate a modifable variable, pass it in from cpu, maybe make a gui
  63. forwardVec *= samplingRate * 2.0f;
  64. float numberOfSteps = min(traceDistance / length(forwardVec),MAX_STEPS);
  65.  
  66. //add jitter, this wil benefit from having a noise texture, but a rand might do
  67. float seed = 1234.0f;
  68. float jitter = noise1(seed) * 256.0f;
  69. //float jitter = 1.0f;
  70.  
  71. float step = length(forwardVec);
  72. float baseBrightness = 0.0009 ; // minimum brightness for samples???
  73. float scale = step * baseBrightness;
  74.  
  75.  
  76. vec3 curPos = vertexPosWS + forwardVec * jitter;
  77. vec4 coordinates = ShadowProjMatrix*ShadowViewMatrix*vec4(curPos.x,curPos.y,curPos.z,1.0f);
  78. coordinates.z = curPos.z;
  79.  
  80. curPos = vertexPosWS + forwardVec * (1.0f + jitter);
  81. vec4 coordinatesEnd = ShadowProjMatrix*ShadowViewMatrix*vec4(curPos.x,curPos.y,curPos.z,1.0f);
  82. coordinatesEnd.z = curPos.z;
  83.  
  84.  
  85. vec4 coordinateDelta = coordinatesEnd - coordinates;
  86.  
  87.  
  88. float sample;
  89. float light = 0;
  90.  
  91. for(float i = 0; i < numberOfSteps; ++i)
  92. {
  93.  
  94. vec2 tc = coordinates.st / coordinates.q;
  95. sample = float(texture2D(ldepth, tc).x < coordinates.z); // float() is a cast from bool to float
  96. if(sample > 0)
  97. light += scale * sample;
  98. coordinates += coordinateDelta;
  99. }
  100.  
  101.  
  102. return light;
  103. }
  104.  
  105. void main()
  106. {
  107.  
  108. vec3 worldVertex = GetPos();
  109.  
  110. float light = VolumeLight(); // calculates volume light value of this pixel by tracing the screen
  111.  
  112. fragColor.xyz = vec3(light,light,light);
  113. fragColor.w = 1.0;
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement