Starting from:

$24.99

CS3241 Lab 2 Solution


LEARNING OBJECTIVES
OpenGL viewing, OpenGL transformations, hierarchical modeling, and animation. After completing the programming assignment, you should have learned
• how to set up the view transformation (camera position and orientation) in OpenGL,
• how to set up perspective viewing in OpenGL,
• how to use the OpenGL transformations for modeling, and • how to use the OpenGL transformations for animation.
TASKS
Please download the ZIP file Lab2_todo_(*).zip from the Assignments folder in LumiNUS Files.

You are provided with an incomplete C++ application program main.cpp, and your job is to complete it according to the requirements.


Please read the instructions shown in the console/terminal window to learn how to operate the program. When you run the program, you should see a spherical planet at the center of the window. There are a dozen cars moving on the planet surface, and each car moves in a different great circle (you can search the web to find out what a great circle is), and they have different speeds and colors.

• resize the window and see what happens,
• press the Up, Down, Left or Right arrow key to change the camera’s position, • press the Page Up or Page Down key to change the camera’s distance from the planet,
• press the ‘P’ key to pause/resume the animation of the cars.

You will notice that the camera is always looking at the center of the planet. With respect to the planet, the camera’s position can be expressed as latitude and longitude, and its distance from the planet’s center. When the Left or Right arrow key is pressed, the camera’s longitude decreases or increases, respectively; and when the Down or Up arrow key is pressed, the camera’s latitude decreases or increases, respectively. Note that the camera’s up-vector is always pointing north.



Figure 1



Follow the following instructions to complete main.cpp as required. You must not add or change any other files.

1) Study the source program very carefully.

2) Complete the DrawOneCar() function. The function must draw the car using only GLUT functions such as glutSolidCube(), glutSolidTorus(), glutSolidCone(), and glutSolidSphere(). You should not directly use any OpenGL geometric primitive. You should make use of the OpenGL 3D transformation functions to help you resize, orientate and position the parts. The functions glPushMatrix() and glPopMatrix() are very helpful for you to save and restore the current transformation before and after drawing each part. You can design your cars any way you like as long as they look like cars. More details about the GLUT functions can be found at https://www.opengl.org/resources/libraries/glut/spec3/spec3.html.

3) Complete the DrawAllCars() function. This function draws each car at the correct position on its great circle. Note that any great circle on a sphere centered at the origin can be defined as follows. Let C be the great circle in the y = 0 plane, and let v be a vector in the x-z plane, and let  be an angle. If we rotate C about v by , we get another great circle of the sphere. All great circles of the sphere can be obtained by varying v and .

4) Set up the correct perspective viewing volume in the MyDisplay() function. You should use the gluPerspective() function. The near and far planes should be set near the planet’s surface, yet still do not clip off any part of the planet and cars. The near and far planes should vary with the eye’s distance from the planet’s center. You should make use of the value of the predefined constant CLIP_PLANE_DIST to position your near and far planes.


6) Complete the MyTimer() function. You should use the GLUT timer callback to control the speed of the animation by maintaining a constant frame rate (DESIRED_FPS). Refer to https://www.opengl.org/resources/libraries/glut/spec3/node64.html to find out more about the GLUT function glutTimerFunc().



DO NOT HARD-CODE VALUES. You should write your code in such a way that when the values of the named constants (defined in the beginning of the program) are changed to other valid values, your program should function accordingly. For example, if the car’s size is changed, the tyre size should vary proportionally.



GRADING



Good coding style. Comment your code adequately, use meaningful names for functions and variables, and indent your code properly. You must fill in your name, and NUS User ID in the header comment.

SUBMISSION
For this assignment, you need to submit only your completed main.cpp.

You must put it/them in a ZIP file and name your ZIP file nus-user-id_lab2.zip. For example, if your NUS User ID is e0123456, you should name your file e0123456_lab2.zip.




——— End of Document ———

More products