added some comments on the code
[myFirstGlutProject.git] / src / melissa.c
blob279b26f874f86561fdbb2bcc5adaf928dc056d90
1 /*
2 * Ng_Melissa_a1.c
3 * melng
5 * Created by AppleMeiMei on 8/25/09.
6 * Copyright 2009 __MyCompanyName__. All rights reserved.
8 */
10 #include <stdlib.h>
11 #include <GL/glut.h>
12 #include <math.h>
13 #define N 10
14 #define PI 3.14159
16 static double x_offset = -50;
17 static double x_speed = 1;
18 static double y_offset;
19 static double angle, new_y_offset;
20 static int pause = 0;
21 static int shape = 1;
23 void init(void)
25 // setup opengl state
26 glClearColor(0,0,0,1.0);
27 glColor3f(1,1,1);
31 // Setup the viewing transform
32 // This setup up a display with coordiantes x axis -50 to 50, y axis -50 to 50
33 // This callback is called anytime the window size changes (for example, you resize)
34 // in Windows
35 void reshape(int w, int h)
37 glViewport(0, 0, w, h); // maps to window 0,0, with * height
38 glMatrixMode(GL_PROJECTION); // set the projection matrix (will talk about this later)
39 glLoadIdentity(); // This clear any existing matrix
41 // This is a common approach in OpenGL -- the world does is always the same, no matter
42 // the window dim. So, we need to normalize in case the width and height
43 if (w <= h) // normalize by which dimensin is longer
44 gluOrtho2D (-50.0, 50.0,
45 -50.0*(GLfloat)h/(GLfloat)w, 50.0*(GLfloat)h/(GLfloat)w);
46 else
47 gluOrtho2D (-50.0*(GLfloat)w/(GLfloat)h,
48 50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0);
50 glMatrixMode(GL_MODELVIEW); // set model transformation
51 glLoadIdentity(); // to be empty (will talk about this later
54 // Draw a square about the origin
55 void drawBox(double x_offset, double y_offset)
57 switch (shape)
59 case 1:
60 // yellow ninja
61 glBegin(GL_POLYGON);
62 glColor3f(1,0,1);
63 glVertex2f(x_offset, 1.0+y_offset);
64 glVertex2f(0.3+x_offset, 0.3+y_offset);
65 glVertex2f(1.0+x_offset, y_offset);
66 glVertex2f(0.3+x_offset, -0.3+y_offset);
67 glVertex2f(x_offset, -1.0+y_offset);
68 glVertex2f(-0.3+x_offset, -0.3+y_offset);
69 glVertex2f(-1.0+x_offset, y_offset);
70 glVertex2f(-0.3+x_offset, 0.3+y_offset);
72 glEnd();
74 glFlush();
75 break;
77 case 2:
78 //red heart
79 glBegin(GL_POLYGON);
80 glColor3f(1,0,0);
81 glVertex2f(x_offset, 0.5+y_offset); // v1 v4 --> v1
82 glVertex2f(0.5+x_offset, 0.75+y_offset); // v2 ^ |
83 glVertex2f(1.0+x_offset,y_offset); // v3 | v
84 glVertex2f(x_offset, -1.0+y_offset); // v4 v3 --- v2
85 glVertex2f(-1.0+x_offset, y_offset); // v1 v4 --> v1
86 glVertex2f(-0.5+x_offset, 0.75+y_offset); // v2 ^ |
88 glEnd();
90 glFlush();
91 break;
93 case 3:
94 // blue cross
95 glBegin(GL_POLYGON);
96 glColor3f(0,1,1);
97 glVertex2f(0.25+x_offset, 0.75+y_offset); // v1 v4 --> v1
98 glVertex2f(0.25+x_offset, 0.25+y_offset); // v2 ^ |
99 glVertex2f(1.0+x_offset,y_offset); // v3 | v
100 glVertex2f(0.25+x_offset, -0.25+y_offset); // v4 v3 --- v2
101 glVertex2f(0.25+x_offset, -0.75+y_offset); // v1 v4 --> v1
102 glVertex2f(-0.25+x_offset, -0.75+y_offset); // v2 ^ |
103 glVertex2f(-0.25+x_offset, -0.25+y_offset); // v1 v4 --> v1
104 glVertex2f(-0.75+x_offset, -0.25+y_offset); // v3 | v
105 glVertex2f(-0.75+x_offset, 0.25+y_offset); // v2 ^ |
106 glVertex2f(-0.25+x_offset, 0.25+y_offset); // v1 v4 --> v1
107 glVertex2f(-0.25+x_offset, 0.75+y_offset); // v3 | v
109 glEnd();
111 glFlush();
115 /* Display Function */
116 void display(void)
118 static int boxArray[100];
120 // Clear Color = black
121 // Clear the buffer
122 glClearColor(0,0,0,0);
123 glClear(GL_COLOR_BUFFER_BIT);
125 int j;
126 for(j=0;j<N;j++)
129 drawBox(x_offset, y_offset); // Draw with offset
132 glClearColor(0,0,0,0);
133 glClear(GL_COLOR_BUFFER_BIT);
135 angle = (double)((rand()%3001)/100.00) - 15; // to generate angles with 2 dp from -15 to 15
136 new_y_offset = (double) tan ((double)(angle * PI / 180)); //no. of units moving along the y axis
138 y_offset += new_y_offset; // Advance offset
139 x_offset += x_speed; // x_offset will move 5 units at a time
141 drawBox(x_offset, y_offset);
143 if (x_offset >= 50) //out of the boundary
145 x_offset = -50; // move back to the left side of the screen
146 y_offset = 0;// return back to original y_offset
149 glutSwapBuffers(); // Using double-buffer, swap the buffer to display
152 // SETUP KEYBOARD -- this program updates the square when the user presses ' '(space))
153 void keyboard(unsigned char key, int x, int y)
155 switch (key) {
157 case ' ': /* Call display function */
158 if(pause == 0)
159 pause = 1;
160 else pause = 0;
161 break;
163 case '1':
164 shape = 1; // ninja star
165 break;
167 case '2':
168 shape = 2; // heart
169 break;
171 case '3':
172 shape = 3; // cross
173 break;
175 case 's' :
176 if(x_speed >0.5)
177 x_speed *= 0.5;
178 else
179 x_speed = 0.5;
180 break;
182 case 'f':
183 if(x_speed < 10)
184 x_speed *= 2;
185 else
186 x_speed = 10;
187 break;
189 case 27: /* Escape Key */
190 exit(0);
191 break;
193 default:
194 if(pause==0) glutPostRedisplay();
195 break;
199 // Idle call back
200 void idle()
202 if(pause==0) glutPostRedisplay();
205 /* Main Loop
206 * Open window with initial window size, title bar,
207 * RGB display mode, and handle input events.
209 * We have registered a keyboard function. The animation of the
210 * square is updated each time you press ' '.
212 int main(int argc, char** argv)
214 glutInit(&argc, argv); // not necessary unless on Unix
215 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
216 glutInitWindowSize (512, 512);
217 glutCreateWindow ("Melissa_ng_a1");
218 init();
220 glutReshapeFunc (reshape); // register respace (anytime window changes)
221 glutKeyboardFunc (keyboard); // register keyboard (anytime keypressed)
222 glutDisplayFunc (display); // register display function
223 glutIdleFunc (idle); // reister idle function
224 x_speed = rand()% 5;
225 glutMainLoop();
227 return 0;