# Simple texture wrapping question

Hi, I’m trying to wrap an image around a cylinder (30 sided for this example). I have the image correctly loaded and the parameters/settings are correct, but I’m making a mistake somewhere with my textcoords. The code snippet is the loop that makes the sides of the cylinder that I’m trying to wrap around. Right now it displays the image separately on each rectangle like so:

I also tried to divide the u coordinate (of the u,v pair) by 30, the number of sides but that gave me this:

So I guess I can’t figure how to make the image partition between the rectangular sides of the cylinder. Any ideas?

for(double angle = 0; angle < 360; angle+=12){

`````` if (count1 &gt; nfaces) break;
double next_point = 0;
next_point = angle+face_div;
``````

normals
if (smooth == false){

``````	if(texture == true) glTexCoord2f(count1, 0);
glVertex3d(cos((double)((next_point)*PI/180))*rt, len/2, sin((double)((next_point)*PI/180))*rt);
if(texture == true) glTexCoord2f((count1+1), 0);
glVertex3d(cos((double)(angle*PI/180))*rt, len/2, sin((double)(angle*PI/180))*rt);
if(texture == true) glTexCoord2f((count1+1), 1);
glVertex3d(cos((double)(angle*PI/180))*rt, -len/2, sin((double)(angle*PI/180))*rt);
if(texture == true) glTexCoord2f(count1, 1);
glVertex3d(cos((double)((next_point)*PI/180))*rt, -len/2, sin((double)((next_point)*PI/180))*rt);
``````

}
glEnd();

++count1;
}

Scaling every image by 30 is not going to do it because each face will still have 0

Generate your cylinder using a parametric float angle where s (u) = 0 to 1 around the circumference.

And use vertex arrays, this glVertex crap along with interleaved conditionals is insanely inefficient. It’s all deprecated and not portable to mobile platforms as is the display list mechanism you’d use to rescue the performance.

Anyway, here’s your code fixed and improved a bit drawing a triangle strip. You may have to switch the winding (just switch the -0.5 and 0.5 for y)

#define epsilon 0.0001f
float angle;

glBegin(GL_TRIANGLE_STRIP);
for(angle = 0.0f, angle < = 1.0f+epsilon; angle+= 1.0f/nfaces)
{
float radians = angleM_PI2.0f; // sure there’s a 2PI in math.h
float cr = cos(radians); // may nead cast to double for win math
glTexCoord2f(1.0f, angle);
glVertex3f(cr, len0.5f, sr);
glTexCoord2f(0.0f, angle);
glVertex3f(cr, len
-0.5f, sr);
}
glEnd();

Note this is designed to loop around and relies on epsilon being smaller that 1.0f/nfaces. There will also be cumulative rounding errors on the start/end seam, but I’m not going to do everything for you, it’ll probably look fine. If you use ArrayElements you can reindex 0, you could also ensure ~0=0, but please, not before you use attribute arrays and fast dispatch. No more branches per vertex.

It is untested, there may be bugs typos and brain farts in here, most of all write the attributes to a packed array and draw with a single call.

P.S.

I may have misinterpreted your meaning of nfaces, I mean it to be number of faces around the cylinder.