I want to draw a circle which will have (inside) triangle fans with the colors of rainbow.(every triangle will hold one of the 6 colors)
Here is my code:
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES); //drawing circle
int j;
for ( j = 0; j < 180; j++)
{
circle.x = r * cos(j);
circle.y = r * sin(j);
glVertex3f(circle.x ,circle.y ,0);
circle.x = r * cos(j + 0.1);
circle.y = r * sin(j + 0.1);
glVertex3f(circle.x,circle.y,0);
}
glEnd();
int i,k;
int sections = 36;
GLfloat radius = 1.0;
GLfloat twoPi = 2.0 * 3.14159;
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0, 0.0); //center of triangles
int flag=1;
for(i = 0; i <= sections;i++) {
glVertex2f(radius*cos(i*twoPi / sections),
radius*sin(i*twoPi / sections));
if(flag==i+1)
glColor3f(1.0f, 0.0f, 0.0f);
if (flag==i+2)
glColor3f(1.0f, 0.5f, 0.0f);
if (flag==i+3)
glColor3f(1.0f, 1.0f, 0.0f);
if (flag==i+4)
glColor3f(0.0f, 1.0f, 0.0f);
if (flag==i+5)
glColor3f(0.0f, 0.0f, 1.0f);
if (flag==i+6)
glColor3f(1.0f, 0.0f, 1.0f);
flag++;
}
glEnd();
glutSwapBuffers();
}
I can’t get the colors to work. The triangles are drawn only with the last color.
Get rid of the flag variable, what you want is the mod operator ‘%’.
The following worked for me:
}