// // Vertex shader for procedural bricks // // Authors: Dave Baldwin, Steve Koren, Randi Rost // based on a shader by Darwyn Peachey // // Copyright (c) 2002-2004 3Dlabs Inc. Ltd. // // See 3Dlabs-License.txt for license information // // adapted a bit to deal with a cube and some comments added by andy uniform vec3 LightPosition; // in eye coordinates const float SpecularContribution = 0.3; const float DiffuseContribution = 1.0 - SpecularContribution; varying float LightIntensity; varying vec2 XYposition; varying vec2 XZposition; varying vec2 ZYposition; varying vec3 norm; // added to determine which side of cube we are drawing bricks on void main(void) { // convert vertex position into eye coordinates vec3 ecPosition = vec3 (gl_ModelViewMatrix * gl_Vertex); // convert and normalize the vertex normal vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal); // compute normalized vector from vertex to light source vec3 lightVec = normalize(LightPosition - ecPosition); // compute reflection vector of light at the vertex using the normal vec3 reflectVec = reflect(-lightVec, tnorm); // compute normalized vector from vertex to viewer (at 0,0,0) vec3 viewVec = normalize(-ecPosition); // dot product of light vector and vertex normal gives amount of diffuse reflection // when light vector and normal are the same diffuse = 100% // when light vector is at right angles to normal then diffuse = 0% float diffuse = max(dot(lightVec, tnorm), 0.0); // if diffuse component <= 0 then there is no specular component float spec = 0.0; norm = normalize(gl_Normal); // if diffuse component > 0 compute cosine between reflect vector and view vector // if they are almost identical then specular component should be large if (diffuse > 0.0) { spec = max(dot(reflectVec, viewVec), 0.0); // shrink / concentrate / sharpen the area of specular reflection spec = pow(spec, 16.0); } // use both components to compute final lighting LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec; // allow the fragment shader to interpolate between vertex positions XYposition = gl_Vertex.xy; ZYposition = gl_Vertex.zy; XZposition = gl_Vertex.xz; // pass the vertex through gl_Position = ftransform(); }