5 * Created by AppleMeiMei on 8/25/09.
6 * Copyright 2009 __MyCompanyName__. All rights reserved.
16 static double x_offset
= -50;
17 static double x_speed
= 1;
18 static double y_offset
;
19 static double angle
, new_y_offset
;
26 glClearColor(0,0,0,1.0);
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)
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
);
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
)
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
);
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 ^ |
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
115 /* Display Function */
118 static int boxArray
[100];
120 // Clear Color = black
122 glClearColor(0,0,0,0);
123 glClear(GL_COLOR_BUFFER_BIT
);
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
)
157 case ' ': /* Call display function */
164 shape
= 1; // ninja star
189 case 27: /* Escape Key */
194 if(pause
==0) glutPostRedisplay();
202 if(pause
==0) glutPostRedisplay();
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");
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