PDA

View Full Version : Simple texture wrapping question



chronophile
03-02-2010, 11:17 PM
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:

http://s803.photobucket.com/albums/yy315...pg&newest=1 (http://s803.photobucket.com/albums/yy315/Chronophile/?action=view&current=Capture-1.jpg&newest=1)


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

http://s803.photobucket.com/albums/yy315...pg&newest=1 (http://s803.photobucket.com/albums/yy315/Chronophile/?action=view&current=Capture-2.jpg&newest=1)



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 > nfaces) break;
double next_point = 0;
next_point = angle+face_div;

glBegin(GL_QUADS);
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;
}

dorbie
03-03-2010, 01:01 AM
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 = angle*M_PI*2.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, len*0.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.

dorbie
03-03-2010, 01:05 AM
P.S.

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