opengl 不同颜色的三角扇
我想画一个圆圈,其中(内部)有彩虹颜色的三角形扇形。(每个三角形将容纳 6 种颜色中的一种)
这是我的代码:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
去掉标志变量,你想要的是 mod 运算符“%”。
以下内容对我有用:
}
Get rid of the flag variable, what you want is the mod operator '%'.
The following worked for me:
}