-
Notifications
You must be signed in to change notification settings - Fork 18
/
motion.c
67 lines (49 loc) · 1.35 KB
/
motion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// a constantly moving rectangle
#include <stdlib.h>
#include <GL/glut.h>
int lastFrameTime = 0;
float boxX = 0.0f;
void display(void){
if(lastFrameTime == 0)
lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMilliseconds = now - lastFrameTime;
float elapsedTime = elapsedMilliseconds / 1000.0f;
lastFrameTime = now;
int windowWidth = glutGet(GLUT_WINDOW_WIDTH);
boxX += 512.0f * elapsedTime;
if(boxX > windowWidth)
boxX -= windowWidth;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(boxX, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2f( 0.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glVertex2f(128.0f, 128.0f);
glVertex2f( 0.0f, 128.0f);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int width, int height){
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}
void idle(void){
glutPostRedisplay();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("GLUT Program");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
return EXIT_SUCCESS;
}