2

First off, I used the following resource to generate my circles.

http://slabode.exofire.net/circle_draw.shtml

Now I am trying to apply a texture to a circle using the following, but I just cant seem to get the math right.

void drawCircle(float cx, float cy, float cz,
  float r, int points,
  float red, float green, float blue) 
{ 

  float theta;

  theta = 2 * PI / (float)points; 


  float c = cosf(theta);//precalculate the sine and cosine
  float s = sinf(theta);
  float t;
  int i;

  float x = r; //we start at angle = 0 
  float y = 0;

  float tx = c * 0.5 + 0.5;
  float ty = s * 0.5 + 0.5;

  glPushMatrix();
  glTranslatef(cx, cy, cz);

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, newBelgTex);

  glBegin(GL_POLYGON); 
  // glColor3f(red/255.0, green/255.0, blue/255.0);
  for(i = 0; i < points; i++) 
  { 

    glTexCoord2f(tx, ty);
    glVertex2f(x, y);//output vertex 

    //apply the rotation matrix
    t = x;
    x = c * x - s * y;
    y = s * t + c * y;

   // Not sure how to update tx and ty
  }
  glVertex2f(-r, 0);


  glEnd();
  glPopMatrix(); 
}

I have tried a few different things but all have seemed to fail in terms of updating tx and ty properly.

Scalahansolo
  • 2,615
  • 6
  • 26
  • 43
  • See here: http://stackoverflow.com/questions/26536570/how-do-i-texture-a-cylinder-in-opengl-created-with-triangle-strip/26541334#26541334. The question was about a cylinder, but the first part (top cap) shows how to draw a circle. – Reto Koradi Dec 02 '14 at 05:14

1 Answers1

4

Istead of calculate tx and ty from c and s, calculate them from x and y. Like this:

float tx = (x/r + 1)*0.5;
float ty = (y/r + 1)*0.5;

And do that in the inner loop right before calling glTexCoord.

On a side note, a GL_TRIANGLE_FAN makes more sense for your geometry. To ease up the topology I'd start it with a center vertex at position 0,0,0. This also gets rid of that last vertex after your loop i.e.

  glBegin(GL_TRIANGLE_FAN); 
  glTexCoord2f(0,0);
  glVertex2f(0,0);
  for(i = 0; i < points; i++) 
  { 
    float const tx = (x/r + 1)*0.5;
    float const ty = (y/r + 1)*0.5;

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

    //apply the rotation matrix
    t = x;
    x = c * x - s * y;
    y = s * t + c * y;
  }
  glEnd();

Note that glBegin…glEnd immediate mode is deprecated and you should consider building a VBO and uploading that instead.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • The equations for tx and ty did the trick! I attempted to use TRIANGLE_FAN but for some reason there was some undesired effects with my colors and the image didn't map properly. – Scalahansolo Dec 01 '14 at 23:08
  • The texture coordinate for the center looks wrong. I think you want (0.5f, 0.5f) for the center. I would also multiply by `r` for the coordinates instead of dividing for the texture coordinates, since multiplications are normally much more efficient than divisions. And calculate everything in float instead of partly using double. You will also need to repeat the first vertex as the last to create a closed circle. – Reto Koradi Dec 02 '14 at 05:09