Project4F15

From Immersive Visualization Lab Wiki
Revision as of 15:35, 23 October 2015 by Jschulze (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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. 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