Skip to content

Instantly share code, notes, and snippets.

@Maff-
Created May 16, 2011 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maff-/974475 to your computer and use it in GitHub Desktop.
Save Maff-/974475 to your computer and use it in GitHub Desktop.
A small update to Paul Houx's projectedTextureSample for Cinder
uniform sampler2D projectorTexture;
uniform sampler2DShadow projectorDepthMap;
uniform bool renderAmbient;
uniform bool renderDiffuse;
uniform bool renderSpecular;
uniform bool renderShadow;
uniform bool renderGobo;
uniform bool renderNormal;
varying vec3 normal;
varying vec3 eye;
varying vec3 light;
varying vec4 texCoord;
void main(void)
{
// set initial colors
vec4 ambient = vec4(0);
vec4 diffuse = vec4(0);
vec4 specular = vec4(0);
vec3 coord = 0.5 * (texCoord.xyz / texCoord.w + 1.0);
bool inside = (texCoord.w < 0.0 || coord.x < 0.0 || coord.x > 1.0 || coord.y < 0.0 || coord.y > 1.0 || coord.z < 0.0 || coord.z > 1.0) ? false : true;
float shadow = inside ? 1.0 : 0.0;
vec4 gobo = inside ? vec4(1) : vec4(0);
// ambient color
if(renderAmbient) {
ambient = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) +
(gl_LightSource[0].ambient * gl_FrontMaterial.ambient);
}
// calculate shadow modifier
if(renderShadow) {
shadow = inside ? shadow2D( projectorDepthMap, coord ).r : 1.0;
}
if(renderGobo) {
// project texture by using the shadow coords and flipping them
//coord.y = 1.0 - coord.y;
gobo = (texCoord.w < 0.0) ? vec4(0) : texture2D( projectorTexture, coord.xy );
}
if(renderDiffuse || renderSpecular) {
// calculate fall-off
vec3 N = normalize(normal);
vec3 L = normalize(light);
float lambert = dot(N,L);
if( lambert > 0.0 )
{
if(renderDiffuse)
diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambert;
if(renderSpecular) {
vec3 E = normalize(eye);
vec3 R = -normalize( reflect(L, N) );
float intensity = max(dot(R, E), 0.0);
//vec3 half = normalize( vec3(gl_LightSource[0].halfVector) );
//float intensity = max(dot(N,half), 0.0);
specular = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow( intensity, gl_FrontMaterial.shininess );
}
}
}
// final pixel color
gl_FragColor.a = 1.0;
if(renderNormal){
gl_FragColor.rgb = normalize(normal);
} else {
gl_FragColor.rgb = (ambient + ((diffuse * gobo) + specular) * shadow).rgb;
//gl_FragColor.rgb = gobo.rgb;
}
}
uniform mat4 projectorModelView;
varying vec3 normal;
varying vec3 eye;
varying vec3 light;
varying vec4 texCoord;
void main(void)
{
// calculate coordinate of vertex in eye space
vec4 eyeCoord = gl_ModelViewMatrix * gl_Vertex;
// calculate coordinate of vertex in shadow map and gobo
texCoord = projectorModelView * eyeCoord;
// calculate normal at vertex
normal = gl_NormalMatrix * gl_Normal;
// calculate light vectors
eye = vec3(0.0 - eyeCoord);
light = gl_NormalMatrix * vec3(gl_LightSource[0].position);
gl_Position = ftransform();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment