c - Texture mapping a circle -


first off, used following resource generate circles.

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

now trying apply texture circle using following, cant seem 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 sine , cosine   float s = sinf(theta);   float t;   int i;    float x = r; //we start @ 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; < points; i++)    {       gltexcoord2f(tx, ty);     glvertex2f(x, y);//output vertex       //apply rotation matrix     t = x;     x = c * x - s * y;     y = s * t + c * y;     // not sure how update tx , ty   }   glvertex2f(-r, 0);     glend();   glpopmatrix();  } 

i have tried few different things have seemed fail in terms of updating tx , ty properly.

istead of calculate tx , ty c , s, calculate them x , y. this:

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

and in inner loop right before calling gltexcoord.

on side note, gl_triangle_fan makes more sense geometry. ease topology i'd start center vertex @ position 0,0,0. gets rid of last vertex after loop i.e.

  glbegin(gl_triangle_fan);    gltexcoord2f(0,0);   glvertex2f(0,0);   for(i = 0; < 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 rotation matrix     t = x;     x = c * x - s * y;     y = s * t + c * y;   }   glend(); 

note glbegin…glend immediate mode deprecated , should consider building vbo , uploading instead.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -