PDA

View Full Version : my animation is strange, why ?



airseb
07-17-2002, 02:41 AM
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 () ;

}

dabeav
07-17-2002, 04:59 AM
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).]

airseb
07-17-2002, 05:35 AM
the translation is good but for the for loop, how can i do ?

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

Miguel_dup1
07-17-2002, 06:18 AM
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... http://www.opengl.org/discussion_boards/ubb/smile.gif

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

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

airseb
07-17-2002, 06:52 AM
the glloadIdentity is in the main function, and when i do glPushMatrix then THE LOOP then glPopMatrix it makes the object turning faster http://www.opengl.org/discussion_boards/ubb/frown.gif but it also disappears after few seconds :-(

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

Miguel_dup1
07-17-2002, 07:41 AM
Yes, you are right, the loadidentity is in the main function...

Did you try it though?

Miguel_dup1
07-17-2002, 08:01 AM
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.

nexusone
07-17-2002, 08:12 AM
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).]

nexusone
07-17-2002, 09:22 AM
Here is a working version of your program.




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

}

airseb
07-17-2002, 09:33 AM
thanks mancha, thanks everybody, it works http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Miguel_dup1
07-17-2002, 09:34 AM
I guess I missed what the problem was...

airseb
07-17-2002, 11:19 AM
it was the GL_DEPTH_BUFFER_BIT http://www.opengl.org/discussion_boards/ubb/biggrin.gif

nexusone
07-17-2002, 12:02 PM
Did you also try my suggestons on how to control the rotation?


Originally posted by airseb:
it was the GL_DEPTH_BUFFER_BIT http://www.opengl.org/discussion_boards/ubb/biggrin.gif

airseb
07-17-2002, 12:18 PM
yes it works but i don't understand very well the working of glIdleFunc and what do i put to adjust the speed of my object http://www.opengl.org/discussion_boards/ubb/frown.gif

nexusone
07-17-2002, 01:10 PM
That is very easy to do.

static int rotation_speed = 10; // add a speed varible
static int rotation_count = 0; // store current delay count


add to the control routine the following:

rotation_count++;
if ( rotation_count > rotation_speed ) // increase count until speed varible is reached
{
angle = angle + 10;
if (angle > 360) angle = 0;
rotation_count = 0; reset count
}

by adjusting the rotation_speed varible you can control the speed of the object.


Originally posted by airseb:
yes it works but i don't understand very well the working of glIdleFunc and what do i put to adjust the speed of my object http://www.opengl.org/discussion_boards/ubb/frown.gif

Relic
07-17-2002, 11:55 PM
Originally posted by airseb:
it was the GL_DEPTH_BUFFER_BIT http://www.opengl.org/discussion_boards/ubb/biggrin.gif

Ugh, the same error again three weeks later? http://www.opengl.org/discussion_boards/ubb/confused.gif
Concentrate!
http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/009053.html