Results 1 to 8 of 8

Thread: why this program don't give me an animation ?

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

    why this program don't give me an animation ?

    #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) ;


    glPushMatrix () ;
    glTranslatef ( 0.0, 0.0, 3.0 ) ;

    for (int i=1; i<1000 ; i++)
    {

    glRotatef (45 + i , 0.0, 1.0, 0.0) ;
    glPushMatrix () ;
    glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    glClear (GL_COLOR_BUFFER_BIT) ;
    }

    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, 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
    Member Newbie
    Join Date
    Jul 2002
    Posts
    42

    Re: why this program don't give me an animation ?

    You have to update your scene to something different after a buffer swap. I read your code and I didn't see anything that was changing. It looks just like the same scene was being drawn over and over.

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

    Re: why this program don't give me an animation ?

    what buffer swap please ?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: why this program don't give me an animation ?

    Your display function is really wierd.

    Since you have a double frame buffer, you are drawing into the back buffer by default. Your drawing function will draw the objwect into the back buffer and then clear it. Problem here is that you don't swap the back buffer to the front buffer before clearing it. So basically your animation is only "shown" in the back buffer. You canb begin with putting glutSwapBuffers in the for loop, before glClear.

    Also, all your calls to glPushMatrix without corresponding glPopMatrix is going to overflow the matrix stack after a few iterations in the for loop.

  5. #5
    Senior Member Regular Contributor
    Join Date
    Mar 2001
    Posts
    184

    Re: why this program don't give me an animation ?

    i think you should remove the for loop from your display function and place the counter in an idle function.

    Code :
    void idle()
    {
       if (counter < 1000)
       {
          counter++
       }
       else
       {
          counter = 0;
       }
    }
     
    void display()
    {
       ...
       glPushMatrix();
       glTranslatef(0.0, 0.0, 3.0);
       glRotatef(45 + counter, 0.0, 1.0, 0.0);
       ...
       glutSwapBuffers();
    }
     
    int main(...)
    {
       ...
       glutIdleFunc(idle);
       ...
    }
    this may get the effect you want. don't forget to match up your push/pop functions. abd maybe try clearing the color buffer at the start of the display function.

    b

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

    Re: why this program don't give me an animation ?

    First I think you don't have a good understanding of how to program, I think taking some time to learn more C would be helpful.

    I have to guess you wanted to have an object spin around in the center of the screen?
    But from how you have coded the program you draw a 1000 objects in the center of the screen.

    There are a few errors in your program.

    Example in your display(), you use glpushmatrix, but do not use glpopmatrix.

    Also glRotate is in degrees, the value should be 0 to 360. not 1000.

    glClear should be place at the start of the display() routine.

    Code :
    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();
    As suggested by other poster:
    add a glutIdelFunc()
    and put in it something like this;
    angle = angle + 10;
    if (angle > 360) angle = 0;
    this will make your object spin.

    glutSwapBuffers() is used to make the animation smooth, by drawing on a screen that is not seen by the user, when the swapbuffers is called the old screen in which the user was looking at is replaced by a new screen which we have updated.

    Now animation works, first we draw our object with 0 rotation to our buffer.
    then we display the object with a swapbuffer call.
    Next we rotate the object say 15 degrees and it is drawn to the buffer. but is not displayed until we call swapbuffers again.
    Then our new rotated object is shown.
    We repeat these steps everytime something in our scene has changed.


    Code :
    [B]#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) ;
     
     
    	glPushMatrix () ;
    	glTranslatef ( 0.0, 0.0, 3.0 ) ;
     
    	for (int i=1; i<1000 ; i++)
    	{
     
    		glRotatef (45 + i , 0.0, 1.0, 0.0) ;
    		glPushMatrix () ;
    		glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
    		glClear (GL_COLOR_BUFFER_BIT) ;
    	}
     
    	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) ;
     
     
    glutMainLoop () ;
     
    }[/B]
    <HR></BLOCKQUOTE>



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

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

    Re: why this program don't give me an animation ?

    please look at this new program
    i haven't used glutIdlefunc but it nearly work. And 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 airseb (edited 07-16-2002).]

  8. #8
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Oxford, England
    Posts
    472

    Re: why this program don't give me an animation ?

    Originally posted by airseb:
    please look at this new program
    i haven't used glutIdlefunc but it nearly work. And the problem is that we don't manage to see PERFECTLY the "sphere" and it diseappears after about few seconds
    what's wrong ?
    Code :
    #include <GL/glut.h>
    #include <stdlib.h>
     
    #define X .525731112119133606 
    #define Z .850650808352039932 
     
    /* make this global */
    int angle = 1;
     
    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};
     
    	/* clear the buffers at the start of the frame */
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    	gluLookAt (0.0, 0.0, 10.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0) ;
     
    	/* you might want to have a look at this a wee bit.... */
    	glTranslatef ( 0.0, 0.0, 3.0 ) ;
    	glRotatef (45, 0.0, 1.0, 0.0) ;
    	glPushMatrix();
     
     
    		glRotatef (angle, 0.0, 1.0, 0.0 ) ;
    		glDrawElements (GL_TRIANGLES, 60, GL_UNSIGNED_INT, indices) ;
     
    	glPopMatrix () ;	
     
    	/* swap the buffers when finished drawing */
    	glutSwapBuffers();
    }
     
    /* added an idle function callback */
    void idle()
    {
    	/* increment the angle and force a redraw */
    	angle++;
    	glutPostRedisplay();
    }
     
    /* added a reshape function callback */
    void reshape(int w, int h)
    {
    	glViewport(0,0,w,h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
     
    	glFrustum(-10.0 , 10.0, -10.0, 10.0, 5.0, 100.0); 
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
     
    }
    /* added a function to do your openGL initialisation */
    void Init()
    {
    	glClearColor (0.0, 0.0, 0.0, 0.0) ;
    	glEnable( GL_DEPTH_TEST );
    	glPolygonMode (GL_FRONT, GL_LINE) ;
    	glFrontFace (GL_CCW) ;
    	glEnable (GL_CULL_FACE) ;
    	glCullFace (GL_BACK) ;
    }
     
     
    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]) ;
     
    	glutDisplayFunc (display) ;
    	glutIdleFunc(idle);
    	glutReshape(reshape);
     
    	/* do your opengl init */
    	Init();
     
    	glutMainLoop () ;
     
    }

Similar Threads

  1. How to give time delay for animation ....
    By qawded in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 07-01-2013, 11:38 AM
  2. Replies: 3
    Last Post: 11-09-2012, 09:37 AM
  3. Can you give me a example?
    By wen in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 09-16-2002, 04:08 PM
  4. Can you give me a example:
    By wen in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-13-2002, 04:34 AM
  5. Ok I give up...
    By rebel10000 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-18-2000, 09:24 AM

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