opengl-intro: animacija.c

File animacija.c, 1.1 KB (added by msitar, 15 years ago)

Primer animacije vozil na avtocesti

Line 
1#include <GL/glut.h>
2
3float y[5]={0,50,120,170,200};
4float v[5]={50,30,45,31,33};
5float pas; 
6
7void ura()
8{
9  float dt;
10  int i;
11  dt=0.1;
12  for(i=1;i<6;i++)
13  {
14    y[i]=y[i]+v[i]*dt;
15  }
16  glutPostRedisplay();
17  glutTimerFunc(100, ura, 0);
18}
19
20float vozilo(float y, float pas)
21{
22  glPushMatrix();
23  glColor3f(1.0, 1.0, 1.0);
24  glTranslatef(pas, y, 0.5);
25  glRectf(-2.0, 0.0, 2.0, 6.0);
26  glPopMatrix();
27}
28
29void display()
30{
31  int i;
32  glClear(GL_COLOR_BUFFER_BIT);
33  glPushMatrix();
34  glRotatef(-45.0, 0.0, 0.0, 1.0);
35  glTranslatef(0.0, -1.0, 0.0);
36  glScalef(0.004, 0.004, 0.004);
37  glColor3f(0.0, 0.0, 0.0);
38  glRectf(-4.0, 0.0, 4.0, 500.0);
39  glTranslatef(0.0, -50.0, 0.0);
40  for(i=1;i<6;i++)
41  {
42    if (i<5 && (y[i+1]-y[i])>10.0)
43      pas=-2.0;
44    else
45      pas=2.0;
46
47    vozilo(y[i], pas);
48  }
49  glPopMatrix();
50  glutSwapBuffers();
51}
52
53int main(int argc, char *argv[])
54{
55  glutInit(&argc, argv);
56  glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
57  glutCreateWindow("Avtocesta");
58  glClearColor(0.0, 0.5, 0.0, 0.0);
59  glutDisplayFunc(display);
60  glutTimerFunc(100, ura, 0);
61  glutMainLoop();
62  return 0;
63} 
64