For this MP, you will be implementing several Two-Dimensional Geometric Transformations (translation, rotation, and scale). Your program will take input from the mouse for the points of a polygon, and the user will be able to enter the values by which to translate, rotate, and scale the polygon. The user will also need to enter an arbitrary point with the mouse about which these geometric operations take place.
Since you will be implementing your own transformations, you may not use the built-in OpenGL transformations (such as glTranslate, glRotate, and glScale). Also, you may not use any of the OpenGL matrix stack manipulations routines (such as glPushMatrix and glPopMatrix). Instead, you must implement your own matrix multiplication routine and use glLoadIdentity and glLoadMatrixd.
Upon completion of this MP, you should be able to do the following things:
Read Chapter 5 in the required text for this class - ``Computer Graphics, C Version, 2nd Edition'' by Hearn & Baker. It discusses the various two-dimensional transformations you will need for this MP. Pay close attention to how to combine matrices and the order of multiplication. You will need to understand this when multiplying your transformations together into one matrix.
You will probably need to program four main functions: Identity, Translate, Rotate, and Scale. Each of these routines will make the appropriate matrix, and multiply it with the current matrix in the appropriate manner. Figure out the matrices for these functions before you begin programming.
For this MP, you will need to use several mouse buttons. Their functionality is described below. You can use the code from MP1 to get mouse coordinates. In addition, you will have the user enter parameters from the keyboard. Your program will have to respond to certain keystrokes to allow the user to enter this information. Get your program working with just entering the points of the polygon and worry about the transformations later.
You are to write a program that allows the user to enter a polygon, a rotation point, and three transformations on that polygon: translate, rotate, and scale. You must implement the following inputs to the mouse and the keyboard.
The Left Mouse Button is used to enter vertices of your polygon. Each button press adds one more vertex to the polygon. The polygon should have a minimum of 3 vertices and a maximum of 20 vertices.
While the user is entering the vertices, your program must draw the points entered thus far connected by line segments. To do this, use GL_LINE_STRIP. Also, draw the first vertex entered as a ``large square point'' so that the user can see where to click to close off the polygon. (Use GL_POINTS with a point size of 3.0 for this first vertex.) Draw the line segments and the first vertex in YELLOW (glColor3f(1.0,1.0,0.0)). The line segments should have a width of 1.0.
To finish entering points of the polygon, click the mouse ``sufficiently close'' to the first vertex that was entered. To do this, check to see if the new point entered is within 3 pixels (in the x and y directions) of the first vertex. Note that you do not need to add this final point to your list of vertices, since the last and first vertices are automatically connected when drawing a filled/empty polygon. (Also, keep in mind that your polygon can have a maximum of 20 vertices. So, if the user has entered 20 vertices and not closed the polygon, you should automatically connect the last and first vertices to form the filled polygon.)
Once you have finished entering all of the points of your polygon, draw the polygon using GL_POLYGON. This will fill the polygon with a solid color. Note that OpenGL is designed to render only convex polygons, so when you use GL_POLYGON and enter points that form a concave polygon, the results are unpredicable. You will not be marked off for this. Just be aware that non-convex polygons may give strange results. Also, do not draw the first vertex as a ``large square point'' once the polygon is completly entered.
Until all of the points are entered for the polygon, do not allow the user to do any geometric transformations. If you do, the mouse points you input will not appear where you want them to! So don't let the user do translate, rotate, or scale while entering the polygon's vertices.
After all of the vertices are entered, you can then allow the user to do the various geometric transformations. You can also enter a new polygon by clicking with the left mouse button again. The old polygon disappears and you start all over entering vertices of the polygon. You should have only one polygon. Also, when the user starts entering new points for the polygon, be sure to clear all geometric transformations. You will get strange results if you don't.
The Right Mouse Button is used to set the ``Rotation Point''. This is a point about which all of your rotation and scaling transformations take place. Initially, set the position of this point to be at the center of the screen. The user should be able to click with the right mouse button at any time and move the rotation point to a new location.
Draw the rotation point in MAGENTA (glColor3f(1.0,0.0,1.0)), and make the point size equal to 3.0. Also, make sure that you draw the Rotation Point after you draw the polygon. This way, the point will be ``on top'' of the polygon if they occupy the same area. You also want the Rotation Point to be unaffected by any of the geometric transformations to be applied to the polygon, so you should probably do glLoadIdentity() just before drawing the point.
The Rotation Point is used as the reference point when you do rotation and scaling. Your polygon will be rotated around this point and scaled with respect to this point. The rotation point has no effect on translation operations.
When user types the letter ``i'' or ``I'', all transformations should be cleared and the polygon should appear in its original configuration.
When the user types the letter ``t'' or ``T'', the program should prompt the user for the number of pixels to move the polygon in both the x and y directions. You can do this with printf and scanf. Once these two numbers have been entered by the user, the polygon should be drawn in its new position.
You should realize that you will have to change the sign of the y value in your program since y values increase from the top of the screen down to the bottom. When the user says to move the polygon 50 pixels in the y direction, it should move 50 pixels up, not down. Change the sign of the y valve to get the desired effect.
When the user types the lettter ``r'' or ``R'', the program should prompt the user for the number of degrees to rotate the polygon counterclockwise. A negative value would rotate the polygon clockwise. Once the user has entered this number, the polygon should be drawn in its new position. You must use the Rotation Point as the point about which to rotate the polygon.
Here again, you will have to take into account the reversed y direction when rotating. You will probably need to reverse the sign of the number of degrees to get it to rotate counterclockwise.
When the user types the letter ``s'' or ``S'', the program should prompt the user for two scaling factors: in the x and y directions. Values between 0 and 1 will make the polygon smaller, and negative values will ``flip'' the polygon. You must use the Rotation Point as the fixed point about which to scale the polygon.
When the user types the letter ``q'' or ``Q'', the program should exit.
This section lists restrictions that are placed on your program. Please read this section carefully, or you may lose points by implementing something incorrectly.
| m0 m4 m8 m12 | Matrix = | m1 m5 m9 m13 | | m2 m6 m10 m14 | | m3 m7 m11 m15 |
Those graduate students enrolled for 1 unit of credit, and undergrad students registered for honors credit, must also implement the following additional functionality.
You must implement reflection about an arbitrary line. To do this, you will need to have two additional inputs available to the user.
The Spacebar is used to set the ``Reflection Point''. This point and the Rotation Point define a line about which you can reflect your polygon. Initially, set the position of this point to be at the top-middle of the screen. The user should be able to type a space character at any time and move the Reflection Point to a new location.
Draw the reflection point in CYAN (glColor3f(0.0,1.0,1.0)), and make the point size equal to 3.0. Also, make sure that you draw the Reflection Point after you draw the polygon. This way, the point will be ``on top'' of the polygon if they occupy the same area. You also want the Reflection Point to be unaffected by any of the geometric transformations to be applied to the polygon, so you should probably do glLoadIdentity() just before drawing the point.
The Reflection Point and Rotation Point are used to define a line for reflection. Your polygon will be reflected aross this line. The reflection point has no effect on other transformation operations.
Important: You must make sure that the Reflection Point and the Rotation Point never occupy the same position. You would not have a line and your reflection operation would be undefined. So, when the user changes the position of either point, you must make sure that they are not at the same location. If they are, shift one of the points by a pixel so that you still have a line defined.
When the user types the letter ``f'' or ``F'' - the polygon should be reflected about the line defined by the Reflection and Rotation Points.
For this and future MPs, we will be using the electronic handin program. Once you have completed your program, logon or telnet to one of the Sun Sparc machines (``handin'' does not work on the WindowsNT machines) and hand in your source code by typing ``handin cs318 mp2 mp2.c''. (For more information on handing in your code, consult the class web page at http://www-courses.cs.uiuc.edu/~cs318/handin.html.) This will send your program to the class directory for grading.
Do not submit your executable program. Also, put all your source code in one file. You must hand in your source code file before 10:00am of the due date, as handin will not accept late assignments. Make sure your name, login, MP#, and date appear at the top of your code listing. See the class web page for a sample file header.
If your program does not run as required, you will be given at most 50% of the total points for the MP. You are responsible for making sure your program compiles on the WindowsNT machines using the makewin batch file.
Late assignments will not be accepted. If you are having problems beyond your control, you must let a TA know at least 24 hours before the due date to arrange for a possible extension. There will be no exceptions!