Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: my animation is strange, why ?

  1. #1
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    France
    Posts
    286

    my animation is strange, why ?

    please look at this program
    the problem is that we don't manage to see PERFECTLY the "sphere" and it diseappears after about few seconds
    what's wrong ?


    #include <GL/glut.h>
    #include <stdlib.h>

    #define X .525731112119133606
    #define Z .850650808352039932


    void display ()
    {
    static GLfloat tableau_entrelace [] = {1.0, 1.0, 1.0, -X, 0.0, Z, 1.0, 1.0, 1.0, X, 0.0, Z,
    1.0, 1.0, 1.0, -X, 0.0, -Z, 1.0, 1.0, 1.0, X, 0.0, -Z, 1.0, 1.0, 1.0, 0.0, Z, X,
    1.0, 1.0, 1.0, 0.0, Z, -X, 1.0, 1.0, 1.0, 0.0, -Z, X, 1.0, 1.0, 1.0, 0.0, -Z, -X,
    1.0, 1.0, 1.0, Z, X, 0.0, 1.0, 1.0, 1.0, -Z, X, 0.0,1.0, 1.0, 1.0, Z, -X, 0.0,
    1.0, 1.0, 1.0, -Z, -X, 0.0} ;

    glInterleavedArrays (GL_C3F_V3F, 0, tableau_entrelace) ;

    static GLuint indices [20][3]= {1,4,0,4,9,0,4,5,9,8,5,4,1,8,4,1,10,8,10,3,8,8,3,5 ,3,2,5,
    3,7,2,3,10,7,10,6,7,6,11,7,6,0,11,6,1,0,10,1,6,11, 0,9,2,11,9,5,2,9,11,2,7};

    glPolygonMode (GL_FRONT, GL_LINE) ;
    glFrontFace (GL_CCW) ;
    glEnable (GL_CULL_FACE) ;
    glCullFace (GL_BACK) ;

    glTranslatef ( 0.0, 0.0, 3.0 ) ;

    glRotatef (45, 0.0, 1.0, 0.0) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;

    for (int i =1; i<=360; i++)
    {
    glPushMatrix () ;
    int angle = 1;
    if (angle > 360) angle = 0;
    glRotatef (angle +i, 0.0, 1.0, 0.0 ) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;
    glClear (GL_COLOR_BUFFER_BIT) ;
    glPopMatrix () ;

    }

    glFlush () ;
    }


    void main (int argc, char** argv)

    {
    glutInit (&argc, argv) ;
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
    glutInitWindowSize (640, 480) ;
    glutInitWindowPosition (250,250) ;
    glutCreateWindow (argv [0]) ;

    glClearColor (0.0, 0.0, 0.0, 0.0) ;
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-10.0 , 10.0, -10.0, 10.0, 5.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt (0.0, 0.0, 10.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ;

    glutDisplayFunc (display) ;


    glutMainLoop () ;

    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Sep 2001
    Location
    macedon, ny
    Posts
    288

    Re: my animation is strange, why ?

    Ok on a quick look of your code, i noticed that you are calling glTranslate every frame, which will push your sphere out of possition eventualy, try turning that off, and see if the sphere stays where you want it. also you are running a for loop to rotate the sphere, but you are doing more than once per frame, so you never see it.

    [This message has been edited by dabeav (edited 07-17-2002).]

  3. #3
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    France
    Posts
    286

    Re: my animation is strange, why ?

    the translation is good but for the for loop, how can i do ?

    [This message has been edited by airseb (edited 07-17-2002).]

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: my animation is strange, why ?

    The glTranslate is fine because it has a constant value...


    Load the identity matrix everytime you display your scene...

    So, in the beginning of you display function call glLoadIdentity();

    Something else is that the glPushMatrix and glPopMatrix in you loop is useless the way you have it set up... You might want to try placing it outside of the loop...

    glPushMatrix();
    loop:
    glPopMatrix();

    For the loop, you should be fine because you are calling glClear(BUF....);, which will clear the old sphere and draw the new one.
    I am afraid that you will have to swap buffers too. If you have problems, swap you buffers inside the loop after iteration to see what happens...

    I hope that helps...

    [This message has been edited by mancha (edited 07-17-2002).]

    [This message has been edited by mancha (edited 07-17-2002).]

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    France
    Posts
    286

    Re: my animation is strange, why ?

    the glloadIdentity is in the main function, and when i do glPushMatrix then THE LOOP then glPopMatrix it makes the object turning faster but it also disappears after few seconds

    [This message has been edited by airseb (edited 07-17-2002).]

  6. #6
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: my animation is strange, why ?

    Yes, you are right, the loadidentity is in the main function...

    Did you try it though?

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jan 2002
    Posts
    292

    Re: my animation is strange, why ?

    Alright, I commented your code where I fixed it...

    Also, because you call you glLoadIdentity in te main function, it does not mean that you wont have to do it every again...

    #include "glut.h"
    //#include <stdlib.h>

    #define X .525731112119133606
    #define Z .850650808352039932


    void display ()
    {
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Doing it here to because it is good to
    //clear everything out before you draw...
    glLoadIdentity(); // this must be in every frame.

    static GLfloat tableau_entrelace [] = {1.0, 1.0, 1.0, -X, 0.0, Z, 1.0, 1.0, 1.0, X, 0.0, Z,
    1.0, 1.0, 1.0, -X, 0.0, -Z, 1.0, 1.0, 1.0, X, 0.0, -Z, 1.0, 1.0, 1.0, 0.0, Z, X,
    1.0, 1.0, 1.0, 0.0, Z, -X, 1.0, 1.0, 1.0, 0.0, -Z, X, 1.0, 1.0, 1.0, 0.0, -Z, -X,
    1.0, 1.0, 1.0, Z, X, 0.0, 1.0, 1.0, 1.0, -Z, X, 0.0,1.0, 1.0, 1.0, Z, -X, 0.0,
    1.0, 1.0, 1.0, -Z, -X, 0.0} ;

    glInterleavedArrays (GL_C3F_V3F, 0, tableau_entrelace) ;

    static GLuint indices [20][3]= {1,4,0,4,9,0,4,5,9,8,5,4,1,8,4,1,10,8,10,3,8,8,3,5 ,3,2,5,
    3,7,2,3,10,7,10,6,7,6,11,7,6,0,11,6,1,0,10,1,6,11, 0,9,2,11,9,5,2,9,11,2,7};

    glPolygonMode (GL_FRONT, GL_LINE) ;
    glFrontFace (GL_CCW) ;
    glEnable (GL_CULL_FACE) ;
    glCullFace (GL_BACK) ;

    //glTranslatef ( 0.0, 0.0, -3.0 ) ;// Not needed with the changes to you glLookAt and Frustum.

    glRotatef (45, 0.0, 1.0, 0.0) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;

    for (int i =1; i<=360; i++)
    {
    //glClear (GL_COLOR_BUFFER_BIT) ;// Not good 'cause you also need your depth_buffer cleared...
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // have to revalidate your depth buffer too.
    int angle = 1;
    if (angle > 360) angle = 0;
    glRotatef (angle +i, 0.0, 1.0, 0.0 ) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;
    }

    glFlush () ;
    }


    void main (int argc, char** argv)

    {
    glutInit (&argc, argv) ;
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
    glutInitWindowSize (640, 480) ;
    glutInitWindowPosition (250,250) ;
    glutCreateWindow (argv [0]) ;

    glClearColor (0.0, 0.0, 0.0, 0.0) ;
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-10.0 , 10.0, -10.0, 10.0, 0.0, 100.0); // Changed the value for the near plane to 0.

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt (0.0, 0.0, 0.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ; // Changed a z value for the position to 0.

    glutDisplayFunc (display) ;


    glutMainLoop () ;
    }

    Good luck.

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: my animation is strange, why ?

    If you had followed our instructions from your last post, your program would function correctly.

    You need to get rid of your animation loop in the display() routine!!!!

    You need to make a seperate routine to control your animation.

    example add the follow routine to your main() loop:

    glutIdelFunc(my_animation);

    also create a new routine called:

    void my_animation(void)
    {

    angle = angle + 10;
    if (angle > 360) angle = 0;

    glutPostRedisplay(); This tells openGL to call the display routine..

    }

    Your display routine will only be called once, unless you have a routine that tells opengl to run the display routine again and again. The display routine should only be called when you need to change the objects on the screen.

    Also you need to have structure to your programming. I think you would have a better time understanding how your program works if you work on structure.

    example:

    main()
    // make call's that setup your window.

    display()
    // draw your scene here

    control()
    // get user input, object control
    // an example of object control would be to control it movement.
    // if scene data has changed call our display routine


    Originally posted by airseb:
    please look at this program
    the problem is that we don't manage to see PERFECTLY the "sphere" and it diseappears after about few seconds
    what's wrong ?


    #include <GL/glut.h>
    #include <stdlib.h>

    #define X .525731112119133606
    #define Z .850650808352039932


    void display ()
    {
    static GLfloat tableau_entrelace [] = {1.0, 1.0, 1.0, -X, 0.0, Z, 1.0, 1.0, 1.0, X, 0.0, Z,
    1.0, 1.0, 1.0, -X, 0.0, -Z, 1.0, 1.0, 1.0, X, 0.0, -Z, 1.0, 1.0, 1.0, 0.0, Z, X,
    1.0, 1.0, 1.0, 0.0, Z, -X, 1.0, 1.0, 1.0, 0.0, -Z, X, 1.0, 1.0, 1.0, 0.0, -Z, -X,
    1.0, 1.0, 1.0, Z, X, 0.0, 1.0, 1.0, 1.0, -Z, X, 0.0,1.0, 1.0, 1.0, Z, -X, 0.0,
    1.0, 1.0, 1.0, -Z, -X, 0.0} ;

    glInterleavedArrays (GL_C3F_V3F, 0, tableau_entrelace) ;

    static GLuint indices [20][3]= {1,4,0,4,9,0,4,5,9,8,5,4,1,8,4,1,10,8,10,3,8,8,3,5 ,3,2,5,
    3,7,2,3,10,7,10,6,7,6,11,7,6,0,11,6,1,0,10,1,6,11, 0,9,2,11,9,5,2,9,11,2,7};

    glPolygonMode (GL_FRONT, GL_LINE) ;
    glFrontFace (GL_CCW) ;
    glEnable (GL_CULL_FACE) ;
    glCullFace (GL_BACK) ;

    glTranslatef ( 0.0, 0.0, 3.0 ) ;

    glRotatef (45, 0.0, 1.0, 0.0) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;

    for (int i =1; i<=360; i++)
    {
    glPushMatrix () ;
    int angle = 1;
    if (angle > 360) angle = 0;
    glRotatef (angle +i, 0.0, 1.0, 0.0 ) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glutSwapBuffers() ;
    glClear (GL_COLOR_BUFFER_BIT) ;
    glPopMatrix () ;

    }

    glFlush () ;
    }


    void main (int argc, char** argv)

    {
    glutInit (&argc, argv) ;
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
    glutInitWindowSize (640, 480) ;
    glutInitWindowPosition (250,250) ;
    glutCreateWindow (argv [0]) ;

    glClearColor (0.0, 0.0, 0.0, 0.0) ;
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-10.0 , 10.0, -10.0, 10.0, 5.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt (0.0, 0.0, 10.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ;

    glutDisplayFunc (display) ;


    glutMainLoop () ;

    }



    [This message has been edited by nexusone (edited 07-17-2002).]

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: my animation is strange, why ?

    Here is a working version of your program.

    Code :
    #include <GL/glut.h>
    #include <stdlib.h>
     
    #define X .525731112119133606
    #define Z .850650808352039932
     
    static int angle;
     
    void my_animation( void )
    {
     
    angle = angle + 10;
    if (angle > 360) angle = 0;
     
    glutPostRedisplay();
     
    }
     
    void display ()
    {
    static GLfloat tableau_entrelace [] = {1.0, 1.0, 1.0, -X, 0.0, Z, 1.0, 1.0, 1.0, X, 0.0, Z,
    1.0, 1.0, 1.0, -X, 0.0, -Z, 1.0, 1.0, 1.0, X, 0.0, -Z, 1.0, 1.0, 1.0, 0.0, Z, X,
    1.0, 1.0, 1.0, 0.0, Z, -X, 1.0, 1.0, 1.0, 0.0, -Z, X, 1.0, 1.0, 1.0, 0.0, -Z, -X,
    1.0, 1.0, 1.0, Z, X, 0.0, 1.0, 1.0, 1.0, -Z, X, 0.0,1.0, 1.0, 1.0, Z, -X, 0.0,
    1.0, 1.0, 1.0, -Z, -X, 0.0} ;
     
    glInterleavedArrays (GL_C3F_V3F, 0, tableau_entrelace) ;
     
    static GLuint indices [20][3]= {1,4,0,4,9,0,4,5,9,8,5,4,1,8,4,1,10,8,10,3,8,8,3,5,3,2,5,
    3,7,2,3,10,7,10,6,7,6,11,7,6,0,11,6,1,0,10,1,6,11,0,9,2,11,9,5,2,9,11,2,7};
     
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    glPolygonMode (GL_FRONT, GL_LINE) ;
    glFrontFace (GL_CCW) ;
    glEnable (GL_CULL_FACE) ;
    glCullFace (GL_BACK) ;
     
     
    glPushMatrix () ;
    glTranslatef ( 0.0, 0.0, 3.0 ) ;
    glRotatef (angle , 0.0, 1.0, 0.0 ) ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glPopMatrix () ;
     
    glutSwapBuffers() ;
    glFlush () ;
    }
     
     
    void main (int argc, char** argv)
     
    {
    glutInit (&amp;argc, argv) ;
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) ;
    glutInitWindowSize (640, 480) ;
    glutInitWindowPosition (250,250) ;
    glutCreateWindow (argv [0]) ;
     
    glClearColor (0.0, 0.0, 0.0, 0.0) ;
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glEnable( GL_DEPTH_TEST );
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     
    glFrustum(-10.0 , 10.0, -10.0, 10.0, 5.0, 100.0);
     
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
     
    gluLookAt (0.0, 0.0, 10.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ;
     
    glutDisplayFunc (display) ;
    glutIdleFunc(my_animation);
     
    glutMainLoop () ;
     
    }

  10. #10
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    France
    Posts
    286

    Re: my animation is strange, why ?

    thanks mancha, thanks everybody, it works

Page 1 of 2 12 LastLast

Similar Threads

  1. Very Strange possible Bug
    By Proterus in forum Vulkan
    Replies: 5
    Last Post: 06-21-2018, 04:15 PM
  2. Replies: 0
    Last Post: 03-05-2018, 09:16 AM
  3. Replies: 5
    Last Post: 08-18-2016, 03:03 AM
  4. Strange Performance Drop With Skeletal Animation
    By Zaladen in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 01-22-2016, 02:34 AM
  5. Sprite Animation / Texture Animation
    By Rodrix in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 03-11-2007, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean