PDA

View Full Version : why this program don't give me an animation ?



airseb
07-15-2002, 01:50 PM
#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 () ;

}

Junkstyle
07-15-2002, 04:46 PM
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.

airseb
07-15-2002, 11:52 PM
what buffer swap please ?

Bob
07-16-2002, 02:27 AM
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.

coredump
07-16-2002, 03:01 AM
i think you should remove the for loop from your display function and place the counter in an idle function.




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

nexusone
07-16-2002, 05:54 AM
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.




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.





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

} <HR></BLOCKQUOTE>



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

airseb
07-16-2002, 12:14 PM
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 http://www.opengl.org/discussion_boards/ubb/frown.gif
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).]

Rob The Bloke
07-17-2002, 11:10 AM
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 http://www.opengl.org/discussion_boards/ubb/frown.gif
what's wrong ?




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

}