Advertisement

Cylindrical Texture Mapping

Started by August 08, 2012 12:12 AM
0 comments, last by Graphic_Programmer 12 years, 3 months ago
Here is an example program of cylindrical texture mapping.
I was unable to find it on the web, so I've made my one.

OpenGL does not generate cylindrical mapping automatically;
you can create spherical mapping with
[source lang="cpp"]
glTexGend(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
[/source]
but not cylindrical, so we must do it by hand, coupling each vertex with corrisponding texture coordinate, using
[source lang="cpp"]glTexCoord2d(s, t); glVertex3d( x, y, z );[/source]


First think about parametrical rappresentation of a cylinder:

cylinder(u, v):
x = r * cos(u * 2*pi)
y = r * sin(u * 2*pi)
z = v
in our case u and v are the texture coordinates
u=s
v=t
so the idea is to iterate on texture coordinates, and generating corresponding vertices.
Suppose we want the cylinder to have 360 side faces; then we select 360 point in the texture (u coordinate) and calculate corresponding (x,y,z) vertex coordinate.
Coordinate v will be only 0.0 or 1.0;

In our reference system, Y is the height, so we modify formulas:
cylinder(u, v):
x = r * cos(u * 2*pi)
z = r * sin(u * 2*pi)
y = v

We use a GL_QUAD_STRIP fot the cylinder side.
Here we generate a cylinder with the height parallel at Y axis.

/* radius */
double r=5.0;
double height=4.0;
/* number of side faces */
int faces=360;
/* Choose neutral color (white)*/
glColor3d(1,1,1);
/* Enable 2D Texture*/
glEnable(GL_TEXTURE_2D);
/* set current working texture */
glBindTexture(GL_TEXTURE_2D, texture[0]);

/* Disabling these is not necessary in this example,
* BUT if you have previously enabled GL_TEXTURE_GEN_
* for other textures,then you need these lines
*/
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);

glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
}
glEnd();


[unverified]
Issue:
I have loaded images with SDL, but if you loaded the image with OpenGL probably you will have to modify code in this way:


glTexCoord2d(u, 0.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, y, z );
}
glEnd();


Attachment is a working code using OpenGL, glut, and SDL.
The attached code alse let you rotate the view with mouse to verify the result.

I really hope my hours spent to create this example will be usefull to someone.

---

Update 21 August 2012 - Face Orientation

The code above will create cylinder with faces where the front is toward the center of cylinder (this because the OpenGL default is glFrontFace(CCW)= faces have front orientation where its vertex are declared in counterclockwise order ). Keep an eye on this especially if you are using back face culling
glEnable(GL_CULL_FACE); glCullFace(GL_BACK);

If you want your cylinder to have front face towards the extern, just reorder the vertexes declaration in this way:
glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
}


or, instead of doing thath, you can put glFrontFace(GL_CW); before glBegin

glFrontFace(GL_CW);
glBegin(GL_QUAD_STRIP);
double x, y, z;
y=height;
for (int i =0; i <= faces; i++) {
double u = i/ (double) faces;
x = r*cos(2*M_PI*u);
z = r*sin(2*M_PI*u);
/* Bottom vertex*/
glTexCoord2d(u, 1.0); glVertex3d( x, 0, z );
/* Top vertex*/
glTexCoord2d(u, 0.0); glVertex3d( x, y, z );
}

this second method have an issue: from now every polygon you will draw will have front face if vertex are declared CounterClockWise, which is not OpenGL default.
This is as my own cylinder texture mapping but i did't use GL_QUAD_STRIP, used GL_QUADS. This topic can help in need of someone.
Thanks.

This topic is closed to new replies.

Advertisement