UCSD Graphics group

Project 4: Lighting and Texturing

This project is on OpenGL lighting and texturing, including GLSL shader programs. In contrast to project 3, where you worked on your own software rasterizer, we return to the OpenGL renderer for this and all remaining remaining projects in this course. There are also not going to be any limitations anymore from now on as to which OpenGL functions you are allowed to use.

This project is due on Friday, October 29th.

UPDATE OCTOBER 26: Due to problems with shader support on the lab machines, problem #2 is not going to be due until next Friday, November 5th. Problem #4 (extra credit) is still due this Friday, but there is a new alternative problem which does not require shaders.

UPDATE OCTOBER 28: The shader problem with the Windows machines has been resolved.

0. OpenGL Extensions

For Windows and Linux users, in order to expose OpenGL extensions, you must download GLee and add the glee.h and glee.c files to your project files, and tell the linker to link with glee.lib/glee.dll (Windows) or libglee.a/libglee.so (Linux). The shader code (shader.h) references glee.h. For OSX users, OpenGL extensions are already available by default and shader.h will include OpenGL/gl.h

1. Lighting (30 points)

This problem is NOT affected by the shader problem in the lab and WILL be due this Friday, October 29.

We provide base code to help you get started with the project. This code displays two simple spheres on the screen, deliberately at a low tesselation level to make the lighting effects you are implementing more obvious.

You should start by replacing the matrix class in the base code with your own. Then you should write classes to manage light and material properties (Light and Material). You will also need to add your trackball functionality from project 2 to your application. If it never worked correctly that is fine, as long as it rotates about some axis.

To get full credit for this part of the homework project you need to do the following things:

  • Display the two spheres from the base code and and give them material properties: one of them should be shiny and diffuse, the other one only diffuse (5 points).
  • Create two light sources: one point light and one spot light. The spot light should point to one of the spheres and the spot diameter should be smaller than the sphere so that the outline of it can be seen, as well as the brightness gradient at the edge of it. (15 points)
  • You need to support two keys: 'p' and 's': The 'p' key turns the point light on and off, the 's' key turns the spot light on and off (both are toggle keys), so that you can demonstrate the functionality of each light individually, as well as their combination. (5 points)
  • You need to use your trackball routine to allow the user to rotate the point light (not the spot light) around the center point between the two spheres. The spot light can remain in place. (5 points)

This URL provides a good summary of OpenGL lighting and materials.

Note that OpenGL transforms the light position and direction with the current modelview matrix. Therefore, you need to set the modelview matrix to the correct transformations for rotating the point light source, and identity for the spot light source before setting the light position.

Note: To make sure that your normal vectors will be scaled to unit length automatically, you can use the following code:

glEnable(GL_NORMALIZE);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

2. Shader Programs (40 points)

UPDATE OCTOBER 26: Given the issue that the Windows computers in the lab do not currently have an adequate OpenGL version installed, this problem is not going to be due until NEXT Friday, November 5, along with homework assignment #5. You are free to submit it on the original due date if you find a way to demonstrate it in the lab.

We provide a sample shader class and three sample combinations of vertex and fragment shaders for you to get familiar with GLSL shader programming. For this part of the project, extend the diffuse_shading shader to perform per-pixel Phong shading. The shader should use the properties of your light sources and materials. These properties can be accessed in the shader through the predefined gl_LightSource and gl_FrontMaterial variables. You will need to create separate shaders for the point light (15 points) and the spot light (20 points). You will need to demonstrate the scene with the two spheres from Part 1. Add support for the 'g' key to toggle your GLSL shader on or off, to demonstrate the difference between OpenGL's built-in shading and your custom per-pixel shader. (5 points)

Notes

  • It is more efficient to compute the reflection vector in the vertex shader. Then you pass it to the fragment shader as a varying parameter. OpenGL will automatically interpolate the vector at each pixel. However, you will have to re-normalize it in the fragment shader to make sure it is a unit vector.
  • For this project it is sufficient to support just one concurrent light source. However, you will need to support the point light as well as the spot light in your scene, just assume that they will never be enabled at the same time.

Here are a few URLs with tutorials on how to write GLSL shader code. The tutorial at the first URL contains detailed instructions for how to do per-pixel shading for point and spot lights, including sample shader code.

3. Texturing (30 points)

This problem is NOT affected by the shader problem in the lab and WILL be due this Friday, October 29.

In this part of the project you will learn how to render textured surfaces and build a demo program. First, get familiar with OpenGL texturing by reading the relevant chapter in the OpenGL programming guide, available online here. An additional tutorial is available here.

We provide a C++ function to load a PPM image file. We also provide a sample image file. The image file is 512x256 pixels, so that it can readily be used as a texture. Use this image as a texture to map on a cylinder. You will need to write your own algorithm to create the cylinder mesh. If you already did that in a previous homework project, you are welcome to use this code here. For each vertex, calculate the correct u and v coordinates for the texture and draw the texture on the cylinder (20 points). Note that the cylinder's triangles have to be white in order to not change the colors of the texture. Make sure that the cylinder's circumference equals twice its height so that the texture does not have to be stretched (5 points). Add code to slowly spin the cylinder about its axis of symmetry (5 points).

4. Extra Credit (10 points)

UPDATE OCTOBER 26: Option a) is affected by the shader problem in the lab. You are free to still solve it and demonstrate it on the original due date, October 29, if you find a way to demonstrate it in the lab. As a shader-free alternative, you can choose to solve the new problem in part b) instead. As opposed to problem #2, we are NOT going to accept submissions of solutions to either of these problems next week, on November 5, except if you are submitting your homework late.

a) Add support for per-pixel Phong shading to part 3 of this homework project. This will require writing an additional shader program (5 points). Then add support to do correct per-pixel Phong shading with both light sources at the same time. (5 points)

b) This alternative extra credit problem is an extension of problem #3, the textured cylinder. Instead of a static texture, play back a short video clip on the cylinder and run it in a loop so that it starts over when it reaches its end. In order to load the textures for the video, use glTexImage2D only the first time you bind the texture object, and use glTexSubImage2D thereafter, updating only the texture data but not recreating the texture object, which improves rendering performance. Use either this sequence of PPM images of a galloping horse (by Eadweard Muybridge), which gets you 5 points, or create your own video clip which gets you 10 points of extra credit.

Tips to create your own video clip: Record a clip with your digital camera or cell phone, download an AVI, WMV, MOV, etc. video clip from the internet, or create a stop-motion clip from photographs or images you create yourself or find on the internet. The clip must consist of at least 10 frames. Note that OpenGL requires power of two textures for the glTexImage commands so you will most likely have to resize your frames. We suggest resizing to 512x256 pixels. To do this we recommend IrfanView for Windows or ImageMagick for Linux. If you have a video file and need to turn it into a series of PPM image files, we recommend the VideoMach utility for Windows, or ffmpeg for Linux. All the above mentioned software tools are free of charge.

  Attachment Action Size Date Who Comment
zip project4-base.zip props, move 2.3 K 15 Oct 2010 - 00:01 JuergenSchulze  
else ucsd_aerial.ppm props, move 384.0 K 15 Oct 2010 - 00:02 JuergenSchulze  
zip shaders.zip props, move 2.3 K 15 Oct 2010 - 00:02 JuergenSchulze  
zip shader-class.zip props, move 2.1 K 15 Oct 2010 - 00:02 JuergenSchulze  
c loadppm.cpp props, move 1.6 K 15 Oct 2010 - 00:02 JuergenSchulze  
zip horse.zip props, move 1464.4 K 29 Oct 2010 - 10:41 JuergenSchulze  
r5 - 29 Oct 2010 - 10:50:41 - JuergenSchulze
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding UCSD Graphics TWiki? Send feedback
Syndicate this site RSSATOM