Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture mapping a circle made using GL_POLYGON

Tags:

opengl

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

like image 890
Mike Tarrant Avatar asked Jan 06 '12 18:01

Mike Tarrant


1 Answers

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);
like image 167
ClickerMonkey Avatar answered Sep 20 '22 13:09

ClickerMonkey