pass_point.glsl 2 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 82 83 84 85 86 87 88 89 90
#version 150

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

uniform sampler2D samplerPos;
uniform sampler2D samplerNormal;


uniform samplerBuffer samplerMaterial;

uniform vec3 lightPos;
uniform vec3 lightAtten;

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


uniform vec4 projParams;

in vec2 vTexCoord;
out vec4 outColor;



void main() 
{
  // read material buffer
  vec3 cEmissive;
  vec3 cAmbient;
  vec3 cDiffuse;
  vec4 cSpecular;
  
  float shininess;
  
  int matID = int(floor(texture2D(samplerPos, vTexCoord).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;
  
  // read G-buffer
  vec3 fragPos = ReadPositionVS(samplerPos, vTexCoord, projParams.x, projParams.y);
  vec3 fragNormal = ReadNormalVS(samplerNormal, vTexCoord);

  
  vec3 fragColor = cEmissive + cAmbient;
  
  // lighting 
 
  vec3 cLight;
 
  vec3 lightDir = lightPos - fragPos; 
  float lightDist2 = dot(lightDir, lightDir);
  float lightDist = sqrt(lightDist2);
  
  lightDir = lightDir / lightDist;
 
  // diffuse
  float ldotn = clamp(dot(lightDir, fragNormal), 0, 1);
  
  cLight = ldotn * cDiffuse;
  
  // specular
  vec3 h = normalize(lightDir - vec3(0,0,-1));
  float hdotn = max(dot(h, fragNormal), 0);
  
  hdotn = max(0, dot( reflect(lightDir, fragNormal), vec3(0,0,-1) ) );
  
  cLight += pow(hdotn, shininess) * cSpecular.xyz;
  
  // attenuate
  float att = lightAtten.x + lightAtten.y * lightDist + lightAtten.z * lightDist2;
  cLight /= att;
  
  // saturate
  cLight = clamp(cLight, 0, 1);
  fragColor += cLight;
  
  fragColor = clamp(fragColor, 0, 1);
  
  
  // output
  outColor = vec4(fragColor, 1);
  
  gl_FragDepth = ViewspaceToProjectedDepth(fragPos.z, projParams.z, projParams.w);
}