$29
The purpose of this project is for you to transform (rotate and scale) an object in three dimensional space using a mouse.
Part I: 3D Objects (40 Points)
For this project, you need to create a computer generated three-dimensional object where each surface (triangle) of the object has a random color. For best result, the center of these objects must be at the origin. The maximum score of this part is 40 points. The score of this part will be based on the di culty of the object you decide to create as follows:
Cone: 15 Points
Cylinder: 20 Points
Tube: 25 Points
Sphere: 30 Points
Torus: 35 Points
Spring: 40 Points Here are examples:
1
Zoom In and Zoom Out (10 Points)
For this project, we will use the scroll wheel of a mouse to scale an object. Scroll one way is an equivalent of enlarge an object in all direction about the origin by the factor of 1.02. Similarly, to shrink the object by the factor of 1/1.02 can be done by scrolling the other way.
Scrolling events are the same as mouse event. So, you need to call the glutMouseFunc() function to register your callback function:
glutMouseFunc(mouse);
where your mouse() function should look like the following:
void mouse(int button, int state, int x, int y)
{
:
}
If you scroll up, the mouse() function will be call with the variable button initialized to 3. Similarly, if you scroll down, the variable button will be initialized to 4. Simply apply the scaling matrix and call the glutPostRedisplay() function.
Note that if you use the track pad of your laptop, it may not registered as a scroll wheel. In this case, use a couple keys on your keyboard to perform zoom in and zoom out instead.
Trackball Style Rotation (50 Points)
To rotate an object in 3D, simply imagine that your object is located in the middle of a glass ball. This glass ball can be spun in any direction. Now, imagine that half of this glass ball pops out of your screen as shown below:
2
y
y
screen
x
z
screen
x
screen
z
Side View
Front ViewTop View
From the above picture, there is a blue cube sitting inside this glass ball. If the glass ball is rotated, this cube is rotated as well.
Note that we a user click a mouse on the screen which is a two-dimensional surface, you have to imagine that the mouse pointer is a nger that touch the glass and point directly to the center of the glass ball in three-dimension. Mouse function only provide your x and y but you have to come up with your imaginary z since it is in three-dimensional space. Here are some examples:
y
screen
x
y
z
screen
x
screen
y
screen
x
z
Side View Front ViewTop View
y
z
screen
x
screen
z
Side View
Front ViewTop View
3
y
screen
x
y
z
screen
x
screen
z
Side View
Front ViewTop View
To rotate the object inside this class ball, user needs to simply move his/her nger while touching the ball. For this project, assume that a user’s nger is always point directly to the origin while it is moving. Ideally, a user can twist his/her nger to rotate the glass ball. But since we cannot twist the mouse pointer, we assume that twisting the nger is not allow for this project. We will use left button of a mouse to simulate a user touches the glass ball. If the left button is down, user touches the ball at the current pointer position. If the left button is up, user released his/her nger from the ball. To capture the left button event, we use the same callback as in previous section. The variable button will be initialized to GLUT LEFT BUTTON and the variable state will be initialized to either GLUT UP or GLUT DOWN. The variables x and y will be set to the pointer position. Note that the pointer position is the screen position. The top-left corner of the screen is at (0; 0).
If the mouse pointer is moving while the left button is down, it simulates a user turning the glass ball. When the glass ball rotates, it rotates about a vector and the xed point of rotation is at the origin. Your job is to come up with the vector so that you can apply rotation matrices correctly. A method of calculating this vector will be discussed in class.
To capture mouse motion events, use the glutMotionFunc() function as shown below:
glutMotionFunc(motion);
and the motion() function should look like the following:
void motion(int x, int y)
{
:
}
The variables x and y will be set to the current pointer position.
Spinning an Object (Just for fun)
One special feature of this glass ball is that it can rotate inde nitely (no friction). If a user touches the glass ball, drags his/her nger, and releases the nger, the glass ball should spin in the same direction of the user’s nger inde nitely. Note that the zoom-in/zoom-out feature must work while the object is spinning inde nitely.
4
Submission
The due date of this project is stated on the CourseWeb. Late submissions will not be accepted. Zip all les related to this project into the le named project1.zip and submit it via CourseWeb. After the due date, you must demonstrate your project to either TA or me within a week after the due date.