exploring lighting in jitter [part 2] – using jit.gl.sketch
in part 1, we saw that the different elements that make up material and light properties, together with a global ambient element, contribute towards lighting and can be controlled directly by setting parameters within our Jitter GL objects. However, OpenGL has a few more lighting characteristics up its sleeve.

eight lights enabled via jit.gl.sketch
download the patches to explore lighting: jitter lighting [max 5]
If we want to go further and make use of these additional parameters we have to design the lighting using jit.gl.sketch and input the values via a command list. The extra control we then have enables us to:
• use more than one light (you will be able to have at least eight lights);
• have different properties for the front and back faces of our objects;
• create spotlight effects; and
• take advantage of the attenuation factors that determine how strong a light’s effect is depending on it’s distance from the objects being lit.
using command lists with jit.gl.sketch
the command list that follows is for two lights. one is positional, the other directional. the positional light (light1) has a blueish tint, is positioned relatively close to the centre (0, 0, 0) and is attenuated. the other light (light0) is further away and can’t be attenuated because it is directional.
note that the command list is formatted for a coll.
0, gldisable color_material; 1, glmaterial front ambient_and_diffuse 0.480769 0.480769 0.480769 1.; 2, glmaterial front specular 1. 1. 1. 1.; 3, glmaterial front emission 0. 0. 0. 1.; 4, glmaterial front shininess 25.; 5, glmaterial back ambient_and_diffuse 0.283019 0.264151 0.49359 1.; 6, glmaterial back specular 0.217949 0.207547 0.735849 1.; 7, glmaterial back emission 0. 0. 0. 1.; 8, glmaterial back shininess 12.; 9, gllightmodel ambient 0.224806 0.224806 0.224806 1.; 10, gllightmodel two_side 1; 11, gllightmodel local_viewer 1; 12, gllight light0 ambient 0.42515 0.42515 0.42515 1.; 13, gllight light0 diffuse 0.760479 0.760479 0.760479 1.; 14, gllight light0 specular 0.263473 0.263473 0.263473 1.; 15, gllight light0 position -40. 42. 57. 0.; 16, gllight light0 spot_cutoff 180.; 17, gllight light0 spot_direction 0. 0. -1.; 18, gllight light0 spot_exponent 0.; 19, gllight light0 constant_attenuation 1.; 20, gllight light0 linear_attenuation 0.; 21, gllight light0 quadratic_attenuation 0.; 22, glenable light0; 23, gllight light1 ambient 0.117054 0.117054 0.173142 1.; 24, gllight light1 diffuse 0.207547 0.207547 0.773585 1.; 25, gllight light1 specular 0.415483 0.415094 0.958939 1.; 26, gllight light1 position 3. 0. 4. 1.; 27, gllight light1 spot_cutoff 180.; 28, gllight light1 spot_direction 0. 0. -1.; 29, gllight light1 spot_exponent 0.; 30, gllight light1 constant_attenuation 1.; 31, gllight light1 linear_attenuation 0.2; 32, gllight light1 quadratic_attenuation 0.01; 33, glenable light1; 34, glenable lighting; 35, glenable normalize;
the picture below shows the lights cast onto a torus with the top left image showing the result from the command list i.e. the two lights, the two bottom images illustrate light0 and light1 separately and the top right image is the lighting on the back face (@cull-face 2).

lighting from the command list
• You see in the command list that the first call disables opengl’s colormaterial mode. Changing the colour of the object will not have any impact on what we see until colormaterial is enabled again, instead the next few lines set the material properties for our object;
• I have chosen to set values for both the ambient and diffuse elements together using only one command, although it is possible to have separate controls for each;
• There are different settings for the front and the back faces. as two sided lighting is enabled in the light model it will be possible to see the different characteristics for each of the two faces if we look ‘inside’ it using poly mode, cullface or using a clipping plane;
• lines 9, 10 and 11 provide values for the light model. one option not available in jitter objects is local viewer. if disabled the viewing position is assumed to be infinitely far away and therefore ‘cheaper’ to calculate. It is enabled in this example so opengl will calculate the angles between the viewing position and the objects being lit, which should produce a more realistic result;
• Next come the properties for the light sources. As we have two lights (light0 and light1) we need two sets of values. Note that each light has to be enabled to contribute to our scene and that there is a call at the end to enable lighting for all the lights;
• spotlights are positional lights and therefore have to have the ‘w’ component of the light position set to a value other than zero. In addition, to enable a spotlight the cutoff value must be set to a value between 0º and 90º. The special value of 180 turns off the spotlight;
• the attenuation factors reduce the amount of light that ‘reaches’ an object depending on its distance from the object, they only affect positional lights (remember that directional lights are infinitely far away).
trouble shooting spotlights
if you enable a spotlight and everything goes black, check a few settings that are probably affecting your scene:
• reduce the attenuation values;
• the cutoff value narrows the field that the light covers in the shape of a cone starting at the position of the light source and pointing in direction set by the spot direction, so increase its value;
• the spot_direction is the axis of the cone of light, check that it points towards your object;
• finally, spotlights also have a spot_exponent that alters the intensity of light towards the edges of the cone, lower its value to reduce the attenuation at the edges;
jit.gl.sketch and pushstate
It’s worth pointing out the pushstate attribute available in jit.gl.sketch. If we have more than one light source in our command list we can apply the additional lights to other jitter objects by setting pushstate to zero. As I understand this, (and I may be wrong so please correct me) the default behavior of sketch (pushstate 1) is where glpushaattrib() is used to save the state variables (for our purposes these are the lighting commands) and at each draw command glpopattrib() is called to restore these variables. If pushstate is set to zero then the state variables are not restored on the next call to draw our scene. this allows the commands that we send to sketch to ‘bleed’ across to other objects. Be warned, this can get confusing quite quickly. If we set pushstate to 1 again, the lighting commands have now been included on the attribute stack and will be restored at each draw command, so even if the lights are disabled in the command list to sketch they will still be enabled for the other jitter objects when they are drawn. To stop the other lights being drawn you need to send commands to sketch to disable the lights before resetting pushstate to 1.
normals
the astute will notice that i haven’t mentioned normals here whilst lighting calculations rely on a vertex’s normal to determine the amount of light it (the vertex) receives from each of the light sources. well i intend to say no more than that!
shaders
Finally, note that if you want to apply shaders to your objects then all the lighting commands will be disregarded unless you specifically call for the state lighting variables from within your shader.

No Comments, Comment or Ping
Reply to “exploring lighting in jitter [part 2] – using jit.gl.sketch”