7

I am trying to map a texture to a circle using GL_POLYGON using this code:

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);
}

it looks like this when running.

img1

And should look like this:

img2

Community
  • 1
  • 1
Mike Tarrant
  • 291
  • 2
  • 7
  • 19
  • 1
    x and y you are passing into glTexCoord2f are being scaled by c.r and translated by c.pos, is this intended? Typical values in glTexCoord2f are 0.0->1.0 (although it can wrap, I'm making sure you meant to have potentially large values). – ClickerMonkey Jan 06 '12 at 18:50
  • Just a suggestion: Instead of GL_POLYGON I'd use a GL_TRIANGLE_FAN, , the starting vertex being at (0,0) and texture coordinate (0.5, 0.5) – this will give you a (slightly) robuster texture perspective correction. Plus it's not deprecated. Also I suggest switching to vertex arrays. – datenwolf Jan 07 '12 at 13:24

1 Answers1

9

Try:

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);
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
ClickerMonkey
  • 1,881
  • 16
  • 13