This part is intended to give you practice using lights and materials. There is nothing to turn in. Create a simple JOGL project with a camera pointed at a teapot or other glut object (glut objects include normals so you don't have to worry about adding them yourself). Experiment by trying the following:
Add a light (in init) and run the code. Note, the default location of the light is at the camera (i.e. (0,0,0) in the camera coordinates).
gl.glEnable(gl.GL_COLOR_MATERIAL); // uses glColor to set the color gl.glEnable(GL.GL_LIGHT0); gl.glEnable(GL.GL_LIGHTING);
Next, explicitly set the location of the light, for example, using
float[] lightPosition = {10.0f, 0.0f, 5.0f, 1.0f}; gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPosition,0);Try different lightPosition values. Try placing the above two lines in different places in the code, e.g. in init(), in display() before or after the gluLookAt, etc. Try to understand why the light appears as it does. Remember that when gl.glLightfv is called, the light position is transformed by the current value of the ModelView matrix.
Get the light to orbit around the teapot.
Get the light to stay still and have the teapot rotate.
Set light parameters (e.g. in init or write separate method that gets called from init) such as
float[] lightAmbient = {0.4f, 0.4f, 0.4f, 1.0f}; float[] lightDiffuse = {0.8f, 0.8f, 0.8f, 1.0f}; float[] lightSpecular = {1.0f, 1.0f, 1.0f, 1.0f}; gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmbient,0); gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDiffuse,0); gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightSpecular,0);Think about the meaning of ambient, diffuse, and specular; vary these parameters to see how the affect the image. Use these parameters to change the color of the light as well. Recall, if an RGB component of light is zero, then it zeros out that component of the surface color.
Look at how varying the shading model affects the image (place in init):
gl.glShadeModel(GL.GL_FLAT);vs
gl.glShadeModel(GL.GL_SMOOTH);
gl.glEnable(gl.GL_COLOR_MATERIAL)and instead create a material using
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, amb,0); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, diff,0); gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, spec,0); gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, shine);where amb, diff, and spec are arrays of length 4 and shine is a single float. Experiment with different values. Can you create materials that represent surfaces such as plastic or brass or whatever. Note, without real reflection (e.g. ray tracing), it is hard to create objects that really look shiny.
All objects created after the above material properties are set, will have this material. If you want to use a second material, then you need to reset the parameters. In such a case, it helps to create your own Material class that stores values for a particular type of surface. Include a method that resets all of the above parameters (amb, diff, etc). This will make it easy to switch between materials.
You can do the same with lights, i.e. create your own Light class that stores the parameters of a particular light. This helps to keep your code clean so that it is easy to read, modify, and reuse in a later program.
Copy over the Netbeans project FractalLandscapeSimple located on enfuzion. The DrawFractal method should look
like the familiar JOGL event handler Code. The FractalLandscape code generates vertices for a fractal landscape. The vertices are
stored in the array vertices[][][]
. The landscape is basically a rectangular grid in the x-z plane with varying height values (y-values).
if you run the code, you will find that the landscape is just white as shown here:
It looks this way because there are no lights or colors, and the normals are not set properly Your job is to do the following:
initLighting()
method in the class DrawFractal
)
calcNormals()
method in the class FractalLandscape
)
calcColor()
method in the class DrawFractal
).
For example, your color ramp should transition
from blue for the water up to white for snow.
Below are what the above changes should give you.
Lights added | Normals fixed | Color Ramp added |
Note, if you are interested in the diamond-square algorithm used for generating the terrain, go look at the web page Generating Random Fractal Terrain.
Deliverables: By class time on Tuesday, Nov 4: Place the jar file of the final version (with lights, normals and color ramp) on enfuzion. Email the corresponding zipped project code to gorr. Be prepared to discuss in class what you discovered.