Hi,I have encountered a problem while doing the animation for my program. I wanted keep adding a another shape on top of another. But for evry second image the background changes to black. Can you point out what I've done wrong. Thanks.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <GL/glut.h>
#include <windows.h>
void myinit(void)
{
//glEnable(GL_BLEND);
glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
/* set up viewing */
glColor3f(0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-110.0, 110.0, -110.0, 110.0);
}
void display( void )
{
glutSwapBuffers();
glFlush(); /* clear buffers */
}
void displayAnim( void )
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT); /*clear the window */
int i = 0;
/* define a point data type */
typedef GLfloat point2[2];
point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square
glMatrixMode(GL_MODELVIEW);
for(i=1;i<=30;i++)
{
glMatrixMode(GL_MODELVIEW);
glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color
glBegin(GL_LINE_LOOP);
glVertex2f(vertices[0][0],vertices[0][1]);
glVertex2f(vertices[1][0],vertices[1][1]);
glVertex2f(vertices[2][0],vertices[2][1]);
glVertex2f(vertices[3][0],vertices[3][1]);
glEnd();
glRotatef(9,0,0, 1);
glScalef(1.09,1.09,1);
glMatrixMode(GL_PROJECTION);
glutSwapBuffers();
Sleep(500);
}
glMatrixMode(GL_PROJECTION);
glutSwapBuffers();
glFlush(); /* clear buffers */
}
void displayNoAnim( void )
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT); /*clear the window */
int i = 0;
/* define a point data type */
typedef GLfloat point2[2];
point2 vertices[4]={{20,20},{20,-20},{-20,-20},{-20,20}}; // A square
glMatrixMode(GL_MODELVIEW);
for(i=1;i<=30;i++)
{
glColor3f(((float)rand())/RAND_MAX, ((float)rand())/RAND_MAX,((float)rand())/RAND_MAX); // Random color
glBegin(GL_LINE_LOOP);
glVertex2f(vertices[0][0],vertices[0][1]);
glVertex2f(vertices[1][0],vertices[1][1]);
glVertex2f(vertices[2][0],vertices[2][1]);
glVertex2f(vertices[3][0],vertices[3][1]);
glEnd();
glRotatef(9,0,0, 1);
glScalef(1.09,1.09,1);
}
glMatrixMode(GL_PROJECTION);
glutSwapBuffers();
glFlush(); /* clear buffers */
}
void popupmenu(int id)
{
glMatrixMode(GL_MODELVIEW);
if(id==3){
exit(0);
}
else if(id==2){
displayNoAnim();
}
else if(id==1){
displayAnim();
}
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* default, not needed */
glutInitWindowSize(500,500); /* 650 x 650 pixel window */
glutInitWindowPosition(100,100); /* place window top left on display */
glutCreateWindow("Images"); /* window title */
glutDisplayFunc(display);/* display callback invoked when window opened */
srand(time(NULL));
myinit(); /* set attributes */
glutCreateMenu(popupmenu);
glutAddMenuEntry("animated display mode",1);
glutAddMenuEntry("non-animated display mode",2);
glutAddMenuEntry("exit",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop(); /* enter event loop */
return 1;
}