Difference between revisions of "Project4SP15"
(→1. Mouse Control (40 Points)) |
|||
Line 19: | Line 19: | ||
* Use the mouse wheel to move the model along the screen space z axis. If you don't have a mouse wheel, use a different function key and mouse cursor up/down. | * Use the mouse wheel to move the model along the screen space z axis. If you don't have a mouse wheel, use a different function key and mouse cursor up/down. | ||
* Retain the functionality of the 's'/'S' keys to scale the model about its object space origin. The other keyboard functions for the control of the 3D models are no longer needed, but it does not hurt to keep them supported. 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: <tt>glEnable(GL_NORMALIZE)</tt>. | * Retain the functionality of the 's'/'S' keys to scale the model about its object space origin. The other keyboard functions for the control of the 3D models are no longer needed, but it does not hurt to keep them supported. 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: <tt>glEnable(GL_NORMALIZE)</tt>. | ||
+ | |||
+ | ===Notes on Mouse Support=== | ||
+ | |||
+ | To access the mouse x and y coordinates, you should use GLUT's callback function [http://www.opengl.org/resources/libraries/glut/spec3/node50.html glutMouseFunc()], which gets called when you press a mouse button, and [http://www.opengl.org/resources/libraries/glut/spec3/node51.html 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=== | ===Notes on the Trackball Rotation=== | ||
Line 29: | Line 33: | ||
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. | 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 [http://web.cse.ohio-state.edu/~crawfis/Graphics/VirtualTrackball.html 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. | For step by step instructions, take a look at [http://web.cse.ohio-state.edu/~crawfis/Graphics/VirtualTrackball.html 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. |
Revision as of 04:27, 25 April 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, May 1st, 2015 at 1pm. You need to present your results in the CSE basement labs as usual, grading starts at 12:15pm.
The homework discussion for this project will be on Monday, April 27th.
From this project on, we will no longer use our software rasterizer. Feel free to start the project with your code for project 2.
In this project we will use the cube, bunny, dragon and bear data sets from the last two projects, but you will no longer need to support the house. Your application should switch between the four models with the function keys: use the previous mapping of 'F1', 'F4', 'F5' and 'F6' for these models, or consolidate them into the range of 'F1' through 'F4' if you prefer.
1. Mouse Control (40 Points)
It is time to support the mouse to control your 3D models. Add functionality to allow the following operations on the displayed 3D model:
- While the left mouse button is pressed and the mouse is moved, rotate the model about the center of your graphics window. This is an extension of the orbit function ('o' key), but it should orbit the model around a sphere, not just a circle. We will refer to this operation 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 in the plane of the screen (similar to what the 'x' and 'y' keys did). Scale this operation so that when the model is in the screen space z=0 plane, the model follows your mouse pointer as closely as possible. If you don't have a right mouse button, use your mouse button along with a function key (Shift, Control, Alt, etc.) to enter this mode.
- Use the mouse wheel to move the model along the screen space z axis. If you don't have a mouse wheel, use a different function key and mouse cursor up/down.
- Retain the functionality of the 's'/'S' keys to scale the model about its object space origin. The other keyboard functions for the control of the 3D models are no longer needed, but it does not hurt to keep them supported. 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
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.
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
2. Light Sources (30 Points)
In this part we add more light sources and control them as if they were 3D models.
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 other lighting parameters of your choice. Draw a glutSolidSphere in the location of the point light, and a glutSolidCone in the location of the spot light, the wide end pointing in the direction of the spot.
The lights should be stationary in world space: when the 3D model is rotated, the lights should not rotate with it.
Notes:
- By default, OpenGL uses a simplified model for the calculation of the highlights. For a more realistic model add this command to your main() function: glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE).
Grading:
- -10 if the spot light is indistinguishable from a point light (spot too wide)
- -5 if point and spot light have no visual representations as described
3. Materials (30 Points)
Write classes to manage light and material properties (Light and Material). As a starting point, refer to the relevant sections in Chapter 5 of the OpenGL Programming Guide, as well as the OpenGL Lighting FAQ.
Associate different material properties with each of the 3D model files: make one more shiny, another one more diffuse. Use colors other than white. (15 points)