PDA

View Full Version : problem in show Color square in my code



mornwk
05-11-2014, 01:46 AM
hello, i write a code that show 4 square with rotates around a point with Different speed. But while running the code, the color 4 square isn't good show. please help me.very thanks.
Code:

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

static float ypoz = 0, zpoz = 0 ,xpoz=0 , rpoz=0 , wpoz=0 , apoz=0 , qpoz=0 , tpoz=0;

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D (100, 100, 0, 200);
glShadeModel (GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}


void display(void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef(0,0,-6);
glRotatef(ypoz,0,0,1);
glRotatef(zpoz,0,1,0);
glColor3f (1, 0,0);
glBegin (GL_QUADS);
glVertex2f (-0.8, 0);
glVertex2f (-0.4, 0.4);
glVertex2f (0.0, 0.0);
glVertex2f (-0.4, -0.4);
glEnd ();
glFlush ();
glutSwapBuffers();

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef(0,0,-6);
glRotatef(qpoz,0,0,1);
glRotatef(tpoz,0,1,0);
glColor3f (0, 1,0);
glBegin (GL_QUADS);
glVertex2f (-0.8, 0);
glVertex2f (-0.4, 0.4);
glVertex2f (0.0, 0.0);
glVertex2f (-0.4, -0.4);
glEnd ();
glFlush ();
glutSwapBuffers();

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef(0,0,-6);
glRotatef(wpoz,0,0,1);
glRotatef(apoz,0,1,0);
glColor3f (0, 0,1);
glBegin (GL_QUADS);
glVertex2f (-0.8, 0);
glVertex2f (-0.4, 0.4);
glVertex2f (0.0, 0.0);
glVertex2f (-0.4, -0.4);
glEnd ();
glFlush ();
glutSwapBuffers();


glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();
glTranslatef(0,0,-6);
glRotatef(xpoz,0,0,1);
glRotatef(rpoz,0,1,0);
glColor3f (1, 1,1);
glBegin (GL_QUADS);
glVertex2f (-0.8, 0);
glVertex2f (-0.4, 0.4);
glVertex2f (0.0, 0.0);
glVertex2f (-0.4, -0.4);
glEnd ();
glFlush ();
glutSwapBuffers();
}




void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // ESC key
exit(0);
break;
}
}

void animate()
{
ypoz=ypoz+1;
if (ypoz>360) ypoz=0;

xpoz=xpoz+0.75;
if (xpoz>270) xpoz=0;

wpoz=wpoz+0.5;
if (wpoz>180) wpoz=0;

qpoz=qpoz+0.25;
if (qpoz>90) qpoz=0;
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (700, 500);
glutInitWindowPosition (300, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(animate);
glutMainLoop();
return 0;
}

reto.koradi
05-11-2014, 12:15 PM
You clear your rendering surface multiple times in your display() function, before rendering each square. So you wipe out the previous squares every time. You also call glutSwapBuffers(), which signals that you finished rendering your frame, multiple times.

Call glClear() once at the start of your display() function, and glutSwapBuffers() once at the end of the display() function.