Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 5.54 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to manage code to display animation only once?
  2. void display()
  3. {
  4. /*
  5. code to draw a field
  6.  
  7. */
  8. loop:1 to 5
  9. loop:1 to 6
  10. /*
  11. here comes the code to animate the ball over the field
  12.  
  13. */
  14. }
  15.        
  16. /* This is ANSI-C - don't try to compile with a C++ compiler, it will fail! */
  17. #include <GL/glut.h>
  18.  
  19. #include <stdlib.h>
  20.  
  21. #include <sys/time.h>
  22. #include <math.h>
  23.  
  24. #define M_PI    3.1415926535897932384626433832795029L
  25. #define M_PI_2  1.5707963267948966192313216916397514L
  26.  
  27. # define timersub(a, b, result)                                              
  28.   do {                                                                        
  29.     (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                            
  30.     (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;                          
  31.     if ((result)->tv_usec < 0) {                                              
  32.       --(result)->tv_sec;                                                    
  33.       (result)->tv_usec += 1000000;                                          
  34.     }                                                                        
  35.   } while (0)
  36.  
  37. void idle(void);
  38. void animate(float dT);
  39. void display(void);
  40. void keyboard(unsigned char key, int x, int y);
  41. void init_sphere(unsigned int rings, unsigned int sectors);
  42. void draw_sphere(void);
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     glutInit(&argc, argv);
  47.     glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
  48.     glutCreateWindow("SinSphere");
  49.     glutIdleFunc(idle);
  50.     glutKeyboardFunc(keyboard);
  51.     glutDisplayFunc(display);
  52.  
  53.     init_sphere(10, 30);
  54.  
  55.     glutMainLoop();
  56.  
  57.     return 0;
  58. }
  59.        
  60. struct AnimationState
  61. {
  62.     float time;
  63.     float duration;
  64.     float sphere_speed;
  65.     float sphere_path_radius;
  66.     float sphere_path_bobbing;
  67.     float sphere_position[3];
  68. };
  69.  
  70. static struct AnimationState animation = {
  71.     0.,
  72.     5., /* play for 5 seconds */
  73.     0.1, 3., 1.,
  74.     {1., 0., 0.}
  75. };
  76.        
  77. void animate(float dT)
  78. {
  79.     if(animation.time < animation.duration) {
  80.         animation.time += dT;
  81.  
  82.         animation.sphere_position[0] = animation.sphere_path_radius * cos(2*M_PI * animation.time * animation.sphere_speed);
  83.         animation.sphere_position[1] = animation.sphere_path_bobbing * sin(2*M_PI * animation.time * 5 * animation.sphere_speed);
  84.         animation.sphere_position[2] = animation.sphere_path_radius * sin(2*M_PI * animation.time * animation.sphere_speed);
  85.     }
  86. }
  87.  
  88. struct ViewState {
  89.     float rotation;
  90.     float rotation_step;
  91. };
  92.  
  93. static struct ViewState view = {
  94.     0.,
  95.     0.1
  96. };
  97.        
  98. void keyboard(unsigned char key, int x, int y)
  99. {
  100.     switch(key) {
  101.     case 'R':
  102.     case 'r': /* restart animation */
  103.         animation.time = 0.;
  104.         break;
  105.  
  106.     case '+':
  107.         view.rotation += view.rotation_step;
  108.         break;
  109.  
  110.     case '-':
  111.         view.rotation -= view.rotation_step;
  112.         break;
  113.     }
  114.     glutPostRedisplay();
  115. }
  116.        
  117. GLfloat *sphere_vertices_normals;
  118. unsigned int sphere_quads = 0;
  119. GLushort *sphere_indices;
  120.  
  121. void init_sphere(unsigned int rings, unsigned int sectors)
  122. {
  123.     float const R = 1./(float)(rings-1);
  124.     float const S = 1./(float)(sectors-1);
  125.     int r, s;
  126.  
  127.     sphere_vertices_normals = malloc(sizeof(GLfloat)*3 * rings*sectors);
  128.  
  129.     GLfloat *v = sphere_vertices_normals;
  130.     for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
  131.         float const y = sin( -M_PI_2 + M_PI * r * R );
  132.  
  133.         float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );
  134.  
  135.         float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );
  136.  
  137.         v[0] = x;
  138.         v[1] = y;
  139.         v[2] = z;
  140.  
  141.         v+=3;
  142.     }
  143.  
  144.     sphere_indices = malloc(sizeof(GLushort) *  rings * sectors * 4);
  145.     GLushort *i = sphere_indices;
  146.     for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
  147.         *i++ = r * sectors + s;
  148.         *i++ = r * sectors + (s+1);
  149.         *i++ = (r+1) * sectors + (s+1);
  150.         *i++ = (r+1) * sectors + s;
  151.         sphere_quads++;
  152.     }
  153. }
  154.        
  155. void draw_sphere()
  156. {
  157.     glTranslatef(animation.sphere_position[0], animation.sphere_position[1], animation.sphere_position[2]);
  158.  
  159.     glEnableClientState(GL_VERTEX_ARRAY);
  160.     glEnableClientState(GL_NORMAL_ARRAY);
  161.  
  162.     glVertexPointer(3, GL_FLOAT, 0, sphere_vertices_normals);
  163.     glNormalPointer(GL_FLOAT, 0, sphere_vertices_normals);
  164.     glDrawElements(GL_QUADS, sphere_quads*4, GL_UNSIGNED_SHORT, sphere_indices);
  165. }
  166.  
  167. void idle()
  168. {
  169.     glutPostRedisplay();
  170. }
  171.        
  172. static GLfloat const light_pos[4] = {-1., 1., 1., 0.};
  173. static GLfloat const light_color[4] = {1., 1., 1., 1.};
  174.  
  175. void display()
  176. {
  177.     static struct timeval delta_T = {0., 0.};
  178.     struct timeval time_frame_begin, time_frame_end;
  179.  
  180.     int win_width, win_height;
  181.     float win_aspect;
  182.  
  183.     gettimeofday(&time_frame_begin, 0);
  184.  
  185.     animate(delta_T.tv_sec + delta_T.tv_usec * 1.e-6);
  186.  
  187.     win_width = glutGet(GLUT_WINDOW_WIDTH);
  188.     win_height = glutGet(GLUT_WINDOW_HEIGHT);
  189.     win_aspect = (float)win_width/(float)win_height;
  190.  
  191.     glViewport(0, 0, win_width, win_height);
  192.     glClearColor(0.6, 0.6, 1.0, 1.0);
  193.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  194.  
  195.     glMatrixMode(GL_PROJECTION);
  196.     glLoadIdentity();
  197.     glFrustum(-win_aspect, win_aspect, -1., 1., 1., 10.);
  198.  
  199.     glMatrixMode(GL_MODELVIEW);
  200.     glLoadIdentity();
  201.     glTranslatef(0,0,-5.5);
  202.  
  203.     glRotatef(view.rotation * 180./M_PI, 0, 1, 0);
  204.  
  205.     glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
  206.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color);
  207.  
  208.     glPushMatrix();
  209.  
  210.     glEnable(GL_DEPTH_TEST);
  211.  
  212.     glEnable(GL_LIGHTING);
  213.     glEnable(GL_LIGHT0);
  214.     draw_sphere();
  215.  
  216.     glPopMatrix();
  217.  
  218.     glutSwapBuffers();
  219.  
  220.     gettimeofday(&time_frame_end, 0);
  221.     timersub(&time_frame_end, &time_frame_begin, &delta_T);
  222.  
  223. }