Programming and Graphics

(Kiana) #1

266 Introduction to C++ Programming and Graphics


int N=4;
float Dtheta = 2*3.1415926/N;
float centerx = 0.0, centery = 0.0;
float radius1 = 0.5;
float radius2 = 1.0;

glClear(GLCOLORBUFFERBIT);
glBegin(GLQUADS);

for (int i=1; i<= N; i++)
{
float angle1 = (i-1)*Dtheta;
float angle2 = i*Dtheta;
if(i==1)
{
centerx = 0.1;
centery = 0.1;
}
else if(i==2)
{
centerx = -0.1;
centery = 0.1;
}
else if(i==3)
{
centerx = -0.1;
centery = -0.1;
}
else
{
centerx = 0.1;
centery = -0.1;
}
glColor3f(0.2,0.5,0.2);
glVertex2f(centerx+cos(angle1)*radius1 ,centery+sin(angle1)*radius1);
glColor3f(0.8,0.5,0.2);
glVertex2f(centerx+cos(angle1)*radius2 ,centery+sin(angle1)*radius2);
glColor3f(0.8,0.5,0.2);
glVertex2f(centerx+cos(angle2)*radius2 ,centery+sin(angle2)*radius2);
glColor3f(0.2,0.5,0.2);
glVertex2f(centerx+cos(angle2)*radius1 ,centery+sin(angle2)*radius1);
}
glEnd();

glFlush();
}

Note that OpenGL allows us to set the vertex color, which is then automatically
interpolated over the surface of a quadrilateral.

Free download pdf