Skip to content

Commit 330c3de

Browse files
committed
added a free fall animation example
1 parent ff7781a commit 330c3de

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

OpenGL-Free-Fall-Animation.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <gl/glut.h>
4+
5+
void init() {
6+
glClearColor(1.0, 1.0, 1.0, 0.0);
7+
glMatrixMode(GL_PROJECTION);
8+
glLoadIdentity();
9+
glPointSize(4.0);
10+
glColor3f(0.0f, 0.0f, 0.0f);
11+
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
12+
}
13+
14+
typedef struct {
15+
double x;
16+
double y;
17+
} Point;
18+
19+
Point p1 = {320.0, 480.0};
20+
Point p2 = {340.0, 480.0};
21+
22+
double time1 = 0.0;
23+
double time2 = 0.0;
24+
25+
void calc() {
26+
time1 += 0.1;
27+
time2 += 0.1;
28+
29+
p1.y = (-0.5*9.8)*(time1*time1)+0+480;
30+
p2.y = (-0.5*1.6)*(time2*time2)+0+480;
31+
32+
if(p1.y < 0.0) {
33+
p1.y = 480;
34+
time1 = 0;
35+
}
36+
if(p2.y < 0.0) {
37+
p2.y = 480;
38+
time2 = 0;
39+
}
40+
}
41+
42+
void display() {
43+
calc();
44+
glClear(GL_COLOR_BUFFER_BIT);
45+
glBegin(GL_POINTS);
46+
glColor3f(0.0f, 0.0f, 1.0f);
47+
glVertex2d(p1.x, p1.y);
48+
49+
glColor3f(0.0f, 1.0f, 0.0f);
50+
glVertex2d(p2.x, p2.y);
51+
glEnd();
52+
glFlush();
53+
}
54+
55+
void Timer(int value) {
56+
glutTimerFunc(1000 / 60, Timer, value);
57+
glutPostRedisplay();
58+
}
59+
60+
int main(int argc, char** argv) {
61+
glutInit(&argc, argv);
62+
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
63+
glutInitWindowPosition(200, 200);
64+
glutInitWindowSize(640, 480);
65+
glutCreateWindow("OpenGL");
66+
init();
67+
Timer(0);
68+
glutDisplayFunc(display);
69+
glutMainLoop();
70+
return 0;
71+
}

0 commit comments

Comments
 (0)