纹理映射使用GL_POLYGON制作的圆

时间:2012-01-06 18:46:29

标签: opengl

我正在尝试使用此代码使用GL_POLYGON将纹理映射到圆圈:

void drawCircleOutline(Circle c, int textureindex)
{
    float angle, radian, x, y;       // values needed by drawCircleOutline

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureLib[textureindex]);

    glBegin(GL_POLYGON);

    for (angle=0.0; angle<360.0; angle+=2.0)
    {
        radian = angle * (pi/180.0f);

        x = (float)cos(radian) * c.r  + c.pos.x;
        y = (float)sin(radian) * c.r  + c.pos.y;

        glTexCoord2f(x, y);
        glVertex2f(x, y);
    }

    glEnd();
    glDisable(GL_TEXTURE_2D);
}

运行时看起来像这样。

img1

应该是这样的:

img2

1 个答案:

答案 0 :(得分:8)

尝试:

radian = angle * (pi/180.0f);

xcos = (float)cos(radian);
ysin = (float)sin(radian);
x = xcos * c.r  + c.pos.x;
y = ysin * c.r  + c.pos.y;
tx = xcos * 0.5 + 0.5;
ty = ysin * 0.5 + 0.5;

glTexCoord2f(tx, ty);
glVertex2f(x, y);