You should call glutSwapBuffers only once in the drawing function.
For an animation you have to call glutPostRedisplay, to ensure that the display function will be called again.
The glBegin and glEnd calls only have to be called once per circle. In the for loop you just supply vertices with calls to glVertex.
To simplify debugging I installed a keyboard handler that closes the window when the escape key is pressed.
// compile in linux: gcc ogl.c -lglut -lGLU
#include <GL/glut.h>
#include <math.h>
#include <stdlib.h>
static void redraw(void);
#define PI 3.14159265
enum{
EDGES=90,
};
static void
circle(float radius)
{
int i;
glBegin(GL_LINE_LOOP);
for (i = 0; i < EDGES; i++){
glVertex2f(radius*cos((2*PI*i)/EDGES),
radius*sin((2*PI*i)/EDGES));
glVertex2f(radius*cos((2*PI*(i+1))/EDGES),
radius*sin((2*PI*(i+1))/EDGES));
}
glEnd();
}
int count=0;
void display (void)
{
float r=10;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotated(count,0,0,1);
count++;
if(count>360)
count=0;
glPushMatrix();
// ring1
glColor3f (0.0, 0.0 ,1.0);
glTranslatef(-30.0,10.0,-100.0);
circle(r);
glPopMatrix();
glPushMatrix();
// ring2
glColor3f (0.0, 0.0, 0.0);
glTranslatef(-8.0,10.0,-100.0);
circle(r);
glPopMatrix();
glPushMatrix();
//ring3
glColor3f (1.0, 0.0 ,0.0);
glTranslatef(14.0,10.0,-100.0);
circle(r);
glPopMatrix();
glPushMatrix();
//ring4
glColor3f (1.0, 1.0, 0.0);
glTranslatef(-19.0,-2.0,-100.0);
circle(r);
glPopMatrix();
glPushMatrix();
//ring5
glColor3f (0.0, 1.0, 0.0);
glTranslatef(4.0,-2.0,-100.0);
circle(r);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
static void
keyb(unsigned char key, int x, int y)
{
switch (key) {
case 27: /* Escape key */
exit(0);
}
}
void
init()
{
glPointSize(3);
glShadeModel (GL_FLAT);
glMatrixMode(GL_PROJECTION);
gluPerspective(45,1.0,10.0,200.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(1.0,1.0,1.0,0.0);
glLineWidth(2);
}
int
main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*3, 110*3);
glutCreateWindow("draw circle");
glutDisplayFunc(display);
glutKeyboardFunc(keyb);
init();
glutMainLoop();
return 0;
}
6
solved rotate 5 circle problem [duplicate]