Difference between revisions of "Project4F15"

From Immersive Visualization Lab Wiki
Jump to: navigation, search
(Created page with "=Project 4: Light and Shade= This project is on OpenGL lighting and shading, and adds support for the mouse. The project is due on <b>Friday, Oct 30th, 2015 at 2pm</b>. You...")
 
Line 11: Line 11:
 
In this project we will only use the bunny, dragon and bear data sets from the last two projects. And we will add a sphere. Your application should switch between the four models with the function keys. Please change the mapping to 'F1' through 'F4'. Nothing should auto-spin like the cube did in assignment 1.
 
In this project we will only use the bunny, dragon and bear data sets from the last two projects. And we will add a sphere. Your application should switch between the four models with the function keys. Please change the mapping to 'F1' through 'F4'. Nothing should auto-spin like the cube did in assignment 1.
  
==1. Mouse Control (30 Points)==
+
==1. Object Centering (20 Points)==
 +
 
 +
First add a sphere to your collection of 3D models. You can render it with [https://www.opengl.org/resources/libraries/glut/spec3/node81.html glutSolidSphere()]. Make it accessible with the 'F1' key.
 +
 
 +
For the mouse controls we're about to implement to work well, we're going to need to center our 3D models on the screen and normalize their sizes. Write function calls to do both of these things whenever a new model is selected with the function keys. All models should get scaled to the same size, and should fully occupy the OpenGL window, without exceeding it.
 +
 
 +
==2. Mouse Control (30 Points)==
  
 
It is time to support the mouse to control your 3D models. We will no longer use keyboard support to manipulate the models. Add functionality to allow the following operations on your 3D models:
 
It is time to support the mouse to control your 3D models. We will no longer use keyboard support to manipulate the models. Add functionality to allow the following operations on your 3D models:
Line 62: Line 68:
 
==2. Light Sources (50 Points)==
 
==2. Light Sources (50 Points)==
  
In this part we add more light sources and control them as if they were 3D models. Before you add the light sources, change the colors of your 3D models to be white - this is so that the effect of the light sources can clearly be seen.
+
In this part we add more light sources and control them with the mouse. Before you add the light sources, change the color of all your 3D models to white - this is so that the effect of the light sources is obvious.
  
Write classes to manage lights and their properties (<tt>Light</tt>). Instead of the default light source from the previous assignments, create three light sources: a directional light, a point light and a spot light. Give them initial colors, positions, and directions of your choice. Each needs to have a different color, clearly distinguishable from the other two. The spot light should always point to the center of the window coordinate system.
+
Write classes to manage lights and their properties (<tt>Light</tt>). Instead of the default light source from the previous assignments, create three separate light sources: a directional light, a point light and a spot light. Give them initial colors, positions, and directions of your choice. Each needs to have a different color, clearly distinguishable from the other two. The spot light should always point to the origin of the world coordinate system.
  
 
To visually indicate where the light sources are, draw a [https://www.opengl.org/resources/libraries/glut/spec3/node81.html glutSolidSphere] in the location of the point light, and a [https://www.opengl.org/resources/libraries/glut/spec3/node83.html glutSolidCone] in the location of the spot light - the wide end pointing in the direction of the spot, the cone angle should equal the opening angle of the spot.
 
To visually indicate where the light sources are, draw a [https://www.opengl.org/resources/libraries/glut/spec3/node81.html glutSolidSphere] in the location of the point light, and a [https://www.opengl.org/resources/libraries/glut/spec3/node83.html glutSolidCone] in the location of the spot light - the wide end pointing in the direction of the spot, the cone angle should equal the opening angle of the spot.

Revision as of 16:43, 23 October 2015

Contents

Project 4: Light and Shade

This project is on OpenGL lighting and shading, and adds support for the mouse.

The project is due on Friday, Oct 30th, 2015 at 2pm. You need to present your results in the CSE basement labs as usual, grading starts at 2:00pm.

The discussion for this project will be on Monday, Oct 26th at 3pm.

From this project on, we will no longer use the software rasterizer, so you don't have to keep the code for it around.

In this project we will only use the bunny, dragon and bear data sets from the last two projects. And we will add a sphere. Your application should switch between the four models with the function keys. Please change the mapping to 'F1' through 'F4'. Nothing should auto-spin like the cube did in assignment 1.

1. Object Centering (20 Points)

First add a sphere to your collection of 3D models. You can render it with glutSolidSphere(). Make it accessible with the 'F1' key.

For the mouse controls we're about to implement to work well, we're going to need to center our 3D models on the screen and normalize their sizes. Write function calls to do both of these things whenever a new model is selected with the function keys. All models should get scaled to the same size, and should fully occupy the OpenGL window, without exceeding it.

2. Mouse Control (30 Points)

It is time to support the mouse to control your 3D models. We will no longer use keyboard support to manipulate the models. Add functionality to allow the following operations on your 3D models:

  • While the left mouse button is pressed and the mouse is moved, rotate the model about the center of your graphics window. This is similar to the earlier orbit function, but it should allow rotations in all directions. We will refer to this function as "trackball rotation". This video shows how this should work.
  • When the right mouse button is pressed and the mouse is moved, move the model along the x and y axes of your window. If you don't have a right mouse button, use your mouse button along with a function key.
  • Use the mouse wheel to scale the model about its object space origin. Mouse wheel down should make it smaller, up should make it bigger. You can access the mouse wheel with the callback function glutMouseWheelFunc. This function is not available in the basic version of GLUT. If you don't have it please install FreeGLUT. If you don't have a mouse wheel, simulate it with the mouse cursor up/down keys. To keep the normals of your 3D models pointing in the right directions, put the following OpenGL command in the initialization part of your code: glEnable(GL_NORMALIZE).

Notes on Mouse Support in GLUT

To access the mouse x and y coordinates, you should use GLUT's callback function glutMouseFunc(), which gets called when you press a mouse button, and glutMotionFunc(), which gets called repeatedly while you hold the button down and move the mouse. Note that successive trackball rotations must build on previous ones; at no point should the model snap back to a previous or default position.

Notes on the Trackball Rotation

The figure below illustrates how to translate mouse movement into a rotation axis and angle. m0 and m1 are consecutive 2D mouse positions. These positions define two locations v and w on an invisible 3D sphere that fills the rendering window. Use their cross product as the rotation axis a = v x w, and the angle between v and w as the rotation angle.

Trackball.jpg

Horizontal mouse movement exactly in the middle of the window should result in a rotation just around the y-axis. Vertical mouse movement exactly in the middle of the window should result in a rotation just around the x-axis. Mouse movements in other areas and directions should result in rotations about an axis a which is not parallel to any single coordinate axis, and is determined by the direction the mouse is moved in.

Once you have calculated the trackball rotation matrix for a mouse drag, you will need to multiply it with the object-to-world transformation matrix of the object you are rotating.

For step by step instructions, take a look at this tutorial. Note that the tutorial was written for Windows messages, instead of GLUT mouse functions. This means that you'll need to replace the "CSierpinskiSolidsView::OnLButtonDown" function with "glutMouseFunc", "CSierpinskiSolidsView::OnMouseMove" with "glutMotionFunc", etc. To help you understand the code here is a line-by-line commented version of the trackBallMapping function:

Vec3f CSierpinskiSolidsView::trackBallMapping(CPoint point)    // The CPoint class is a specific Windows class. Either use separate x and y values for the mouse location, or use a Vector3 in which you ignore the z coordinate.
{
    Vec3f v;    // Vector v is the synthesized 3D position of the mouse location on the trackball
    float d;     // this is the depth of the mouse location: the delta between the plane through the center of the trackball and the z position of the mouse
    v.x = (2.0*point.x - windowSize.x) / windowSize.x;   // this calculates the mouse X position in trackball coordinates, which range from -1 to +1
    v.y = (windowSize.y - 2.0*point.y) / windowSize.y;   // this does the equivalent to the above for the mouse Y position
    v.z = 0.0;   // initially the mouse z position is set to zero, but this will change below
    d = v.Length();    // this is the distance from the trackball's origin to the mouse location, without considering depth (=in the plane of the trackball's origin)
    d = (d<1.0) ? d : 1.0;   // this limits d to values of 1.0 or less to avoid square roots of negative values in the following line
    v.z = sqrtf(1.001 - d*d);  // this calculates the Z coordinate of the mouse position on the trackball, based on Pythagoras: v.z*v.z + d*d = 1*1
    v.Normalize(); // Still need to normalize, since we only capped d, not v.
    return v;  // return the mouse location on the surface of the trackball
}

Grading:

  • -5 if rotation is not in world coordinates
  • -5 if object's size changes when rotating
  • -10 if there is no zoom
  • -5 if rotation is in opposite direction
  • -10 if rotation is not cumulative