Game Development Reference
In-Depth Information
Also, the ambient color from all lights need not be calculated in the shaders as the
ambient component is independent of distance and orientation of the object. We can
sum the ambient color from all lights and pass the sum to the shader. Even if we use
a single ambient color uniform in our shader for all lights, we would need two to
three uniforms (position/direction, diffuseColor , specularColor ) per light.
Adding multiple lamps
Open the 03-Loading-Scene-With-Lamps-Arrays.html file in your favorite editor.
The solution to the problem is adding arrays to our shaders. There are two important
things to note about the use of arrays in the shading language. The first is that many
WebGL implementations will not allow an array to be indexed with a variable or
with an unknown value at compile time. That is, WebGL only mandates that array
indexing is to be supported by constant integral expressions.
The other note about arrays is that there is no syntax in the shading language to
initialize an array at the creation time. The elements of the array need to be initialized
one by one, and also, arrays cannot be qualified as const . So, even if you declare an
array as int a[]; , you need to specify the size as int a[5]; before indexing it.
When we discuss arrays, loops automatically come into the picture. There are
a variety of restrictions placed on the types of loops supported in the shading
language. To boil it down to its simplest form, the for loops in WebGL must have an
iteration count that is known at the compile time.
You should generally be cautious when using loops in WebGL. The basic restrictions
are as follows:
There must be only one loop iteration variable and it must be incremented
or decremented using a simple statement ( i++ , i-- , i+=constant ,
i-=constant )
The stop condition must be a comparison between the loop index and the
constant expression
We must not change the value of the iterator in the loop
So, let's take a look at our shaders now.
 
Search WWH ::




Custom Search