Skip to content

Instantly share code, notes, and snippets.

@joshuajnoble
Created August 13, 2013 08:26
Show Gist options
  • Save joshuajnoble/6219026 to your computer and use it in GitHub Desktop.
Save joshuajnoble/6219026 to your computer and use it in GitHub Desktop.
// vert
#version 150
out vec4 outColor; // this is the ultimate color for this vertex
out vec2 outtexcoord; // pass the texCoord if needed
out vec3 transformedNormal;
////////////////////////////////////////////////////////////
// Eye-coordinate position of vertex
out vec4 eyePosition;
// vec4 localColor;
out vec3 eyePosition3;
out vec3 eye;
// these are passed in from OF programmable renderer
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 textureMatrix;
uniform mat4 modelViewProjectionMatrix;
in vec4 position;
in vec4 color;
in vec4 normal;
in vec2 texcoord;
in mat4 material;
out mat4 currentMaterial;
//// keeping track of everything cumulatively
//vec4 Ambient;
//vec4 Diffuse;
//vec4 Specular;
// probably not use this one
//void ads( int lightIndex, out vec3 ambAndDiff, out vec3 spec )
//{
//
// vec3 s = vec3(lights[lightIndex].Position - posOut) ;
// vec3 v = normalize( posOut.xyz );
// vec3 n = normalize(normOut);
// vec3 halfVector = normalize(v+s); // direction of maximum highlights
//
// vec3 diffuse = ((Ka+ lights[lightIndex].Ld) * Kd * max( 0.0,dot(n, v) )) ;
// spec = Ks * pow( max(0.0, dot(n, halfVector) ), Shininess ) ;
//
// ambAndDiff = diffuse ;
//}
//vec3 DirectIllumination(vec3 position, vec3 normal, vec3 lightCentre, float lightRadius, vec3 lightColour, float cutoff)
//{
// // calculate normalized light vector and distance to sphere light surface
// float r = lightRadius;
// vec3 L = lightCentre - position;
// float distance = length(L);
// float d = max(distance - r, 0.);
// L /= distance;
//
// // calculate basic attenuation
// float denom = d/r + 1.;
// float attenuation = 1. / (denom*denom);
//
// // scale and bias attenuation such that:
// // attenuation == 0 at extent of max influence
// // attenuation == 1 when d == 0
// attenuation = (attenuation - cutoff) / (1. - cutoff);
// attenuation = max(attenuation, 0.);
//
// float dot = max(dot(L, normal), 0.);
// return lightColour * dot * attenuation;
//}
//////////////////////////////////////////////////////
// here's the main method
//////////////////////////////////////////////////////
void main (void)
{
float alphaFade = 1.0;
////////////////////////////////////////////////////////////
// Eye-coordinate position of vertex
vec4 eyePosition = modelViewMatrix * position;
mat4 normalMatrix = transpose(inverse(modelViewMatrix));
vec3 tempNormal = (normalMatrix * normal).xyz;
transformedNormal = normalize(tempNormal);
////////////////////////////////////////////////////////////
// let's do some eye positioning
eyePosition3 = (eyePosition.xyz) / eyePosition.w;
eye = vec3 (0.0, 0.0, 1.0);
////////////////////////////////////////////////////////////
// vertex transform
gl_Position = modelViewProjectionMatrix * position;
outtexcoord = (textureMatrix*vec4(texcoord.x,texcoord.y,0,1)).xy;
}
// frag
#version 150
#define MAX_LIGHTS 8
struct material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};
// note: these *should* be passed in via uniform buffer objects
// but it isn't supported in GLES2.0
struct light
{
vec4 ambient;
float type; // 0 = pointlight 1 = directionlight
vec4 position; // where are we
vec4 diffuse; // how diffuse
vec4 specular; // what kinda specular stuff we got going on?
// attenuation?
float constantAttenuation, linearAttenuation, quadraticAttenuation;
// only for spot
//float spotCutoff, spotExponent;
//vec3 spotDirection;
vec3 halfVector; // only for dir
};
uniform sampler2DRect tex0;
//uniform material currentMaterial;
uniform vec4 mat_ambient;
uniform vec4 mat_diffuse;
uniform vec4 mat_specular;
uniform float mat_shininess;
// this should be generated?
uniform int numberOfLights;
out vec4 finalColor;
in vec4 outColor; // this is the ultimate color for this vertex
in vec2 outtexcoord; // pass the texCoord if needed
in vec3 transformedNormal;
//in mat4 currentMaterial; // this is all our material data
////////////////////////////////////////////////////////////
// Eye-coordinate position of vertex
in vec4 eyePosition;
in vec3 eyePosition3;
in vec3 eye;
// these are passed in from OF programmable renderer
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 textureMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform light lights[MAX_LIGHTS];
vec4 Ambient;
vec4 Diffuse;
vec4 Specular;
mat3x4 pointLight( in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3 )
{
float nDotVP; // normal . light direction
float nDotHV; // normal . light half vector
float pf; // power factor
float attenuation; // computed attenuation factor
float d; // distance from surface to light source
vec3 VP; // direction from surface to light position
vec3 halfVector; // direction of maximum highlights
// Compute vector from surface to light position
VP = vec3 (lights[i].position.xyz) - ecPosition3;
// Compute distance between surface and light position
d = length(VP);
// Normalize the vector from surface to light position
VP = normalize(VP);
// Compute attenuation
attenuation = 1.0 / (lights[i].constantAttenuation + lights[i].linearAttenuation * d + lights[i].quadraticAttenuation * d * d);
halfVector = normalize(VP + eye);
nDotVP = max(0.0, dot(normal, VP));
nDotHV = max(0.0, dot(normal, halfVector));
// ha! no branching :)
pf = mix(0.0, pow(nDotHV, mat_shininess), step(0.0000001, nDotVP));
// Ambient += lights[i].ambient * attenuation;
// Diffuse += lights[i].diffuse * nDotVP * attenuation;
// Specular += lights[i].specular * pf * attenuation;
mat3x4 lightResult = mat3x4(lights[i].ambient * attenuation, lights[i].diffuse * nDotVP * attenuation, lights[i].specular * pf * attenuation);
return lightResult;
}
mat3x4 directionalLight(in int i, in vec3 normal)
{
float nDotVP; // normal . light direction
float nDotHV; // normal . light half vector
float pf; // power factor
nDotVP = max(0.0, dot(normal, normalize(vec3 (lights[i].position))));
nDotHV = max(0.0, dot(normal, vec3 (lights[i].halfVector)));
pf = mix(0.0, pow(nDotHV, mat_shininess), step(0.0000001, nDotVP));
mat3x4 lightResult = mat3x4(lights[i].ambient, (lights[i].diffuse * nDotVP), lights[i].specular * pf);
return lightResult;
}
//////////////////////////////////////////////////////
// here's the main method
//////////////////////////////////////////////////////
void main (void)
{
vec4 localColor;
Ambient = vec4(0., 0., 0., 0.);
Diffuse = vec4(0., 0., 0., 0.);
Specular = vec4(0., 0., 0., 0.);
////////////////////////////////////////////////////////////
// here's the loop over each light
// the count will be generated in the shader, em,
for( int i = 0; i < 2; i++ )
{
mat3x4 pointCalc = pointLight(i, transformedNormal, eye, eyePosition3);
mat3x4 dirCalc = directionalLight(i, transformedNormal);
vec4 amb = mix(pointCalc[0].xyzw, dirCalc[0].xyzw, step(1., lights[i].type));
vec4 dif = mix(pointCalc[1].xyzw, dirCalc[1].xyzw, step(1., lights[i].type));
vec4 spec = mix(pointCalc[2].xyzw, dirCalc[2].xyzw, step(1., lights[i].type));
Ambient += amb;
Diffuse += dif;
Specular += spec;
//Ambient += pointCalc[0];
//Diffuse += pointCalc[1];
//Specular += pointCalc[2];
}
////////////////////////////////////////////////////////////
// now add the material info
localColor = Ambient * max(mat_ambient, 0.00001) + Diffuse * max(mat_diffuse, 0.00001) + texture(tex0, outtexcoord);
localColor += Specular * max(mat_specular, 0.00001);
////////////////////////////////////////////////////////////
// now get the color ready
//finalColor = lights[1].ambient * mat_ambient;
finalColor = clamp( localColor, 0.0, 1.0 );
//
//outtexcoord = (textureMatrix*vec4(texcoord.x,texcoord.y,0,1)).xy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment