pass_directionalMS_G.glsl 2.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
// version with multisampling of G-buffer (no lighting MSAA)

#version 150

#include "GBufferAccess.glsl"
#include "multisampling.glsl"
#include "depthbuffer.glsl"

uniform sampler2DMS samplerPos;
uniform sampler2DMS samplerNormal;

uniform samplerBuffer samplerMaterial;

uniform vec3 lightDir;

uniform vec3 lightAmbient;
uniform vec3 lightDiffuse;
uniform vec3 lightSpecular;

uniform vec4 projParams;

uniform int numSamples;
uniform samplerBuffer samplerFilterWeights;

in vec2 vTexCoord;
out vec4 outColor;



void main() 
{

  ivec2 texSize = textureSize(samplerPos);
  
  // absolute pixel index
  ivec2 absIndex = ivec2(floor( texSize * vTexCoord ));

  // read G-buffer
  vec3 cEmissive;
  vec3 cAmbient;
  vec3 cDiffuse;
  vec4 cSpecular;
  
  float shininess;
  
  int matID = int(floor(texelFetch(samplerPos, absIndex, 0).y));
  cEmissive = texelFetch(samplerMaterial, matID * 5).xyz;
  cAmbient = texelFetch(samplerMaterial, matID * 5 + 1).xyz * lightAmbient;
  cDiffuse = texelFetch(samplerMaterial, matID * 5 + 2).xyz * lightDiffuse;
  cSpecular.xyz = texelFetch(samplerMaterial, matID * 5 + 3).xyz * lightSpecular;

  shininess = texelFetch(samplerMaterial, matID * 5 + 4).y;
  
  float depthVS = ReadMultisampledTexture(samplerPos, absIndex, numSamples, samplerFilterWeights).x;
  vec3 fragPos = ComputePosVS(depthVS, vTexCoord, projParams.x, projParams.y);
  vec3 fragNormal = ReadMultisampledTexture(samplerNormal, absIndex, numSamples, samplerFilterWeights).xyz;

  fragNormal = normalize( fragNormal * 2 - vec3(1,1,1) );
  
  vec3 fragColor = cEmissive + cAmbient;
  
  // lighting
 
  // diffuse
  float ldotn = clamp(dot(lightDir, fragNormal), 0, 1);
  
  fragColor += ldotn * cDiffuse;
  
  // specular
  vec3 h = normalize(lightDir - vec3(0,0,-1));
  float hdotn = max(dot(h, fragNormal), 0);
  
  fragColor += pow(hdotn, shininess) * cSpecular.xyz;
  
  // saturate
  fragColor = clamp(fragColor, 0, 1);
  
  outColor = vec4(fragColor, 1);
  
  gl_FragDepth = ViewspaceToProjectedDepth(fragPos.z, projParams.z, projParams.w);
}