Game Development Reference
In-Depth Information
The vertex shader
The following code shows the steps to be performed to calculate the light ray on a
vertex for two positional lights (street lamps in our scene). We added two uniforms
for light positions (
uLightPositionLamp1
and
uLightPositionLamp2
). First, the
position (
uLightPositionLamp1
) of the light is transformed by multiplying it by the
ModelView matrix (
newLightPosition1
). Then, the ray is calculated by subtracting
the transformed light position (
newLightPosition1
) from the transformed vertex
position (
vertexPos
). The light ray is stored in a varying variable (
uLightRayLamp1
)
to be passed to the fragment shader:
<script id="shader-vs" type="x-shader/x-vertex">
.................................
varying vec3 vertexPos;
uniform vec3 uLightPositionLamp1;
uniform vec3 uLightPositionLamp2;
...........................
varying vec3 uLightRayLamp1;
varying vec3 uLightRayLamp2;
void main(void) {
vec4 vertexPos4 = mVMatrix * vec4(aVertexPosition, 1.0);
vertexPos = vertexPos4.xyz;
vec4 newLightPosition1=mVMatrix * vec4(uLightPositionLamp1,
1.0);
vec4 newLightPosition2=mVMatrix * vec4(uLightPositionLamp2,
1.0);
uLightRayLamp1=vertexPos-newLightPosition1.xyz;
uLightRayLamp2=vertexPos-newLightPosition2.xyz;
transformedNormal = vec3(nMatrix * vec4(aVertexNormal,1.0));
gl_Position= pMatrix *vertexPos4;
}
</script>
The rest of the calculation in the fragment shader is the same as that for directional
light, where the light ray represents the direction.