Graphics Reference
In-Depth Information
ment shader if you are computing the color at each pixel for Phong shading.
These two kinds of shading were discussed earlier in this chapter. Because we
have not yet talked about the GLSL programming API, we have stubbed in the
light and materials definitions in the function, indicating where they would
come from if this were part of a graphics application.
// Assumed context:
//uniform variables uLightsource[i] and uFrontMaterial are
//stubbed with constant values below. These would probably be
//passed into the shader function if used in an application.
//
//variables myNormal and myPosition are passed in; in a vertex
//shader these would be computed and used directly, while in a
//fragment shader these would be set by the associated vertex
// shader.
//
//the ADS color is returned from the function
vec3 ADSLightModel( in vec3 myNormal, in vec3 myPosition )
{
const vec3 myLightPosition = vec3( 1. , 0.5, 0. );
const vec3 myLightAmbient = vec3( 0.2, 0.2, 0.2 );
const vec3 myLightDiffuse = vec3( 1. , 1. , 1 . );
const vec3 myLightSpecular = vec3( 1. , 1. , 1. );
const vec3 myMaterialAmbient = vec3( 1. , 0.5, 0. );
const vec3 myMaterialDiffuse = vec3( 1. , 0.5, 0. );
const vec3 myMaterialSpecular = vec3( 0.6, 0.6, 0.6 );
const float myMaterialShininess = 80.;
//normal, light, view, and light reflection vectors
vec3 norm = normalize( myNormal );
vec3 lightv = normalize( myLightPosition - myPosition);
vec3 viewv = normalize( vec3(0.,0.,0.) - myPosition );
vec3 refl = reflect( vec3(0.,0.,0.) - lightv, norm );
//ambient light computation
vec3 ambient = myMaterialAmbient*myLightAmbient;
//diffuse light computation
vec3 diffuse = max(0.0, dot(lightv, norm)) * myMaterialDiffuse
*myLightDiffuse;
//Optionally you can add a diffuse attenuation term at this
//point
Search WWH ::




Custom Search