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:
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
float sr = sin(radians);
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.