CSE 480/580 PROJECT #1 ====================== Due Date: Midnight, Friday, Feb 16th Maximum Points: 150 ============================================================================== This project is designed to introduce you to (1) basic graphics primitives in OpenGL or Mesa, (2) the basic set-up necessary to write graphic applications, (3) Popup menu, Windows event handling with GLUT, and GLX libraries. This project requires you to implement, what we call, "Turtle Graphics". TURTLE GRAPHICS =============== Turtle graphics is a simple and quick way to learn some of the basics of computer graphics. Imagine a turtle whose current situation is described by its position (expressed by a coordinate (x,y)) and orientation (expressed as an angle measured counter-clockwise from the x-axis). The turtle can rotate to change its orientation, and move forward in straight lines only to change its position. When moving, it may or may not trace its path. Thus, all figures are to be drawn using the following three commands: LineForward(distance) Move and trace path by 'distance' amount from current position in the current direction. Right(angle) Turn right by angle at current position. Note that each of the turtle's moves is specified in polar coordinates (direction and distance). Thus the turtle is operating in relative polar coordinates since every command modifies its current position or current direction. (By the way, the programming language LOGO, meant to teach young children programming basics, is based on turtle graphics.) THE PROBLEM =========== We are providing you with a C program that declares the necessary OpenGL environment set-up that is required, and some basic routines of the aforementioned turtle-graphics. Further more, the program contains the procedures TurtleHouse and RepeatTurtleHouse [see ALGORITHMS section below]. The code uses the mouse position at which the user clicks the mouse button as the current position of the turtle. The initial orientation angle is set in the program. This program is available in the file "turtle.c" in the directory /visualization/agarg/courses/cse580/2001/proj1. Please study this program, and play with it. Study its output. (An executable image of this program is available in the file "turtle" in the same directory). With the mouse cursor in the "turtle" window, click the middle button to popup menu items "Turtle House" and "Repeated Turtle House", select one of them, click the left button to draw the object in the window, click the third button to popup "Quit" and "Clear". We are also providing you with an executable file "turtle-sol" that contains solution to project 1. This file is also located in the directory /visualization/agarg/courses/cse580/2001/proj1. Note that it also draws two other kinds of objects, namely, Koch Snowflake (SF), and Tree. The maximum complexity of SF supported by the program is 6. However, you are NOT required to implement drawing of SF and Tree. Here is what you have to do: (1) Code the procedure Polyspiral (see ALGORITHMS section below) and add to the given program. In addition, add four menu items "PolySpiral Style 1" through "PolySpiral Style 4" right after the menu item "RepeatTurtleHouse" for the Middle button popup menu. When user chooses these new menu items, four different calls are made to PolySpiral with the following parameters (see ALGORITHMS section below for the usage of these parameters): dist angle(in degrees) incr num 2 144 2 200 1 170 2 200 1 60 1 200 1 89.5 1 200 to produce four different polyspirals. You can play with the num parameter to make it fit the window better if necessary. Playing with the other parameters can give you a large variety of shapes and sizes. (2) Next, add two more middle buttonmenu items, "PolySpriral Style 5" and "PolySpiral Style 6", that call PolySpiral, and produce polyspirals that are reasonably different from the previous four. Experiment with all four parameters until you get the results that please you. (3) Next, write a function called RegularPolygons(...) that draws a set of clustered polygons starting with a triangle and upto an n-gon, all sharing a common side. Prompt the user in the text window to input the maximum number of sides (n) wanted and draw upto that n-sided regular polygon. Your program can fix the length of the common side. Please do checks to make sure the value entered is a number and is greater than 2. Add this procedure's execution as the next middle buttom menu item "Regular n-gon". (4) Lastly, as it is currently written, the given turtle.c program does not redraw the window if it is iconified or lowered and then raised, or if the window size is changed by the user using the window manager; it simply loses whatever was drawn. Also, whenever a new object is chosen from the menu, and drawn, the previous object is lost. (Try doing some of this with the executable "turtle".) As the last enhancement to "turtle.c", add the ability to redraw the window when it is raised after having been iconified or hidden, or if the window is resized by the user. Also, your program should display all the objects created so far, not just the last one (unless they have been cleared using the "clear" button). Use some data-structure to store the objects drawn (such as a linked-list, and/or display lists): do not simply store the bitmap of the window. Your data-structure should be space-efficient, and should store only the objects that need to be drawn; it should not store the objects that have been cleared. Also note that when the window is resized, the contents of the window need not be resized. IMPLEMENTATION HINTS ==================== (1) Spend some time with "turtle.c" as given, and understand how it works. (2) Run the program "turtle-sol", which contains solution to Project 1. Study the behavior of the program. Also read the file "readme.txt" that gives information on how to run turtle-sol. (3) Hint for procedure RegularPolygons above: Recall, the sum of the exterior angles of a regular polygon = 360. (4) Please comment your code well. C needs it. Points will be allocated not only to how well you code in C, but also how well you comment your C code. (5) Start implementating early! Don't wait for the last minute. HOW TO COMPILE ============== For undergraduate students, having access mostly to X terminals, please use Mesa. The one-line command to compile and link turtle.c is gcc -I/util/X11/include -L/util/X11R6/lib/ -R/util/X11/lib turtle.c -lglut -lMesaGL -lMesaGLU -lX11 -lXext -lXmu -lXi -lm -lpthread -o turtle The binary file "turtle" created with this method will run on both Sun workstations and X terminals, therefore the above is strongly suggested for everyone. For convenience, you can add following alias to your ".cshrc", alias mesacc 'gcc -I/util/X11/include -L/util/X11R6/lib/ -R/util/X11/lib -lglut -lMesaGL -lMesaGLU -lXi -lXmu -lXext -lX11 -lm -lpthread' Then you will just need to type "mesacc turtle.c -o turtle" to compile and link turtle.c to obtain the binary file turtle. Similarly, the command to compile and link using OpenGL is: 1. gcc -c -I/usr/openwin/include -I/util/X11/glut/include turtle.c 2. gcc -L/usr/openwin/lib -L/util/X11/glut/lib -R/usr/openwin/lib turtle.o -lGL -lGLU -lglut -lXmu -lX11 -lXext -lm You can either choose to use the above comand lines to compile and link your programs, or use the Makefiles available in /visualization/agarg/courses/cse580/2001/proj1. If you chose the latter, please do the following: (1) For compiling and linking with Mesa: 1. copy the file Makefile-mesa to your directory. 2. type the command: make -f Makefile-mesa (2) For compiling and linking with OpenGL: 1. copy the file Makefile-ogl to your directory. 2. type the command: make -f Makefile-ogl LEARNING OPENGL =============== The following Project Related and Other Useful Links are available on the course webpage. OpenGL Home Page A Tutorial on OpenGL OpenGL Man Pages GLUT Man Pages Mesa The best way to learn OpenGL is to get your hand wet by playing with some examples. You can download OpenGL-Examples.tar.gz, which contains all examples from the book OpenGL Programming Guide from the directory /visualization/agarg/courses/cse580/2001, and then run its example programs. To run the example programs in OpenGL-Examples.tar.gz, please type the following five commands. The 5th step runs one of programs called "scene". 1. gunzip OpenGL_Examples.tar.gz 2. tar xvf OpenGL_Examples.tar 3. cd openglbk 4. make 5. scene However, please keep the following in mind, while running the examples of OpenGL-Examples.tar.gz: 1. The "Makefile" used in Step 4 (make) is already updated, it should work on our system. 2. OpenGL does not work on Ultra 5 Sun Workstations and Xterminals. For running, on these machines, you should individually compile and link each example .c file of openglbk using Mesa. HOW TO SUBMIT ============= HEADER BOXES: ------------ For all projects, the following information is REQUIRED to be in a comment header box within the source code of your submitted program. (1) title "cse480 project1" or "cse580 project1", (2) Your Full Name, (3) Person Number. (4) Where Compiled, tell us what architecture you compiled your program under, or else on what machine. (For example: SPARC, sun4, lictor, SunCluster, MIPS, hadar, armstrong.) (5) Workstations Used, tell us EXACTLY the type AND location of all the workstation set-ups that you KNOW your program will run correctly on. (For example: Sparcstations in Bell 338, X Terminals in Bell 338, Sun workstations in Bell 226 etc). If you do the project at home on Linux, you should make sure your project runs on CS machines. ONLINE SUBMISSIONS: ------------------ As mentioned in the project descriptions, you must, by midnight of the due date, submit online your source code file(s). If you are in CSE480, you can submit by running: submit_cse480 ... If you are in CSE580, you can submit by running: submit_cse580 ... We will compile and test-run your programs. Any special information we need to know should be provided, for example, if you use different compile command than the one described above, you should specify clearly within the header box. Please ensure that the version of your program, which you submit, does actually compile and run. If you add comments after you get the program working, recompile the commented code before you submit it. The submit_cse480 and submit_cse580 programs will accept new versions of file. So if you submit a file, and then make changes to it, you may resubmit it, and the new version will replace the old. ALGORITHMS (expressed in pseudocode) ========== (1) procedure TurtleHouse { /* Turtle is facing up */ LineForward(100); /* Draw up left wall */ Right(30); /* Turn right 30 degrees */ LineForward(100); /* Go up left side of roof */ Right(120); LineForward(100); /* Go down right side of roof */ Right(30); LineForward(100); /* Draw down right wall */ Right(90); LineForward(100); /* Draw bottom floor, right to left */ Right(90); /* Restore turtle's orientation */ } (2) procedure RepeatTurtleHouse { for i:=1 to 6 do { TurtleHouse; Right(60); } } (3) procedure PolySpiral(double dist, double angle, double incr, int num) { int i; for i := 1 to num do { LineForward(dist); Right(angle); dist = dist + incr; } } End of Project #1 description.