Game Development Reference
In-Depth Information
The vertex shader
We declared a constant number of the positional light ( NUM_POSITIONAL_LIGHTS ).
We declared a uniform array ( uLightPosition[NUM_POSITIONAL_LIGHTS] ) to hold
the reference to positions of the lights. We declared a varying to hold a calculated
light ray ( uLightRay[NUM_POSITIONAL_LIGHTS] ) per vertex. Then, we iterated over
the array to calculate the light ray for each positional light. The code snippet for the
vertex shader is as follows:
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 mVMatrix;
uniform mat4 pMatrix;
uniform mat4 nMatrix;
varying vec3 transformedNormal;
varying vec3 vertexPos;
const int NUM_POSITIONAL_LIGHTS = 2;
uniform vec3 uLightPosition[NUM_POSITIONAL_LIGHTS];
varying vec3 uLightRay[NUM_POSITIONAL_LIGHTS];
void main(void) {
vec4 vertexPos4 = mVMatrix * vec4(aVertexPosition, 1.0);
vertexPos = vertexPos4.xyz;
for(int i = 0; i < NUM_POSITIONAL_LIGHTS; i++){
vec4 newLightPosition=mVMatrix * vec4(uLightPosition[i],
1.0);
uLightRay[i]=vertexPos-newLightPosition.xyz;
}
transformedNormal = vec3(nMatrix * vec4(aVertexNormal,1.0));
gl_Position= pMatrix *vertexPos4;
}
</script>
The fragment shader
The fragment shaders have two constants: one for the number of directional
lights ( NUM_DIRECTIONAL_LIGHTS ) and the other is the number of positional
lights ( NUM_POSITIONAL_LIGHTS ). Then, we declare three array uniforms
( uDirectionalDiffuseColor , uDirectionalSpecularColor , and uLightDirection )
for directional lights and two array uniforms ( uPositionalDiffuseColor and
uPositionalSpecularColor ) for positional lights. We also declare a varying
( uLightRay ) whose values are passed from the vertex shader.
 
Search WWH ::




Custom Search