opengl 不同颜色的三角扇

发布于 2024-10-21 14:02:22 字数 1520 浏览 0 评论 0原文

我想画一个圆圈,其中(内部)有彩虹颜色的三角形扇形。(每个三角形将容纳 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-10-28 14:02:22

去掉标志变量,你想要的是 mod 运算符“%”。

以下内容对我有用:

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINES);    //drawing circle
GLfloat x=0.0f, y=0.0f, r=1.0f;
int j;
for ( j = 0; j < 180; j++)
{
    x = r * cos(j);
    y = r * sin(j);
    glVertex3f(x ,y ,0);
    x = r * cos(j + 0.1);
    y = r * sin(j + 0.1);
    glVertex3f(x,y,0);
}
glEnd();

int i;
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

for(i = 0; i <= sections;i++) {

    glVertex2f(radius*cos(i*twoPi / sections),
               radius*sin(i*twoPi / sections));

    if(i%6 == 0)
        glColor3f(1.0f, 0.0f, 0.0f);

    if (i%6 == 1)
        glColor3f(1.0f, 0.5f, 0.0f);

    if (i%6 == 2)
        glColor3f(1.0f, 1.0f, 0.0f);

    if (i%6 == 3)
        glColor3f(0.0f, 1.0f, 0.0f);

    if (i%6 == 4)
        glColor3f(0.0f, 0.0f, 1.0f);

    if (i%6 == 5)
        glColor3f(1.0f, 0.0f, 1.0f);
}

glEnd();

glFlush();

}

Get rid of the flag variable, what you want is the mod operator '%'.

The following worked for me:

{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINES);    //drawing circle
GLfloat x=0.0f, y=0.0f, r=1.0f;
int j;
for ( j = 0; j < 180; j++)
{
    x = r * cos(j);
    y = r * sin(j);
    glVertex3f(x ,y ,0);
    x = r * cos(j + 0.1);
    y = r * sin(j + 0.1);
    glVertex3f(x,y,0);
}
glEnd();

int i;
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

for(i = 0; i <= sections;i++) {

    glVertex2f(radius*cos(i*twoPi / sections),
               radius*sin(i*twoPi / sections));

    if(i%6 == 0)
        glColor3f(1.0f, 0.0f, 0.0f);

    if (i%6 == 1)
        glColor3f(1.0f, 0.5f, 0.0f);

    if (i%6 == 2)
        glColor3f(1.0f, 1.0f, 0.0f);

    if (i%6 == 3)
        glColor3f(0.0f, 1.0f, 0.0f);

    if (i%6 == 4)
        glColor3f(0.0f, 0.0f, 1.0f);

    if (i%6 == 5)
        glColor3f(1.0f, 0.0f, 1.0f);
}

glEnd();

glFlush();

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文