Applying textures

Started by
4 comments, last by swiftcoder 16 years, 5 months ago
I'm experimenting with applying textures to objects. I have a 256x256 .bmp file that I'm using as a texture. I can apply this texture to a simple quad using: glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 256.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(256.0, 256.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(256.0, 0.0, 0.0); And I can make a cube by repeating this 6 times for each face. But my question is how do I go about applying this texture to more complex shapes such as a pyramid, sphere or torus? (these would be drawn using the GLUT function rather than by hand). I want to 'stretch' the texture to wrap around the entire object rather than repeat the texture. Is there a simple function that will do this or would I have to do it manually by stepping through the 2D array storing the texture and altering it by hand? If there is no simple function I will be sticking to a 2D shape (because I'm running out of time!), so I would want to just apply the texture to various 2D shapes; triangles, circles or random polygons for example. Again, is there a simple function that can acheive this or do I have to alter the data manually? Thanks for any advice.
Advertisement
Hi,

as far as I know all "glut" primitives like glutSolidSphere ... don´t have predefined texture coordinates but I might be wrong.
The only exception as far as I know is the glutSolidTeapot.
So I the only way to texture more complex objects is to apply them by hand which might end up in a real pain, or to use a (simple) file format and an editor to place textures onto your objects.
A rather simple format for static objects might be the wavefront *.obj format.
Hope that helps,

Ralph
Thanks Ekast.

So I wont be using and of the pre-defined glut primitives! What about the posibility of using the texture to "stretch around" a simple 3D object, like a cube or pyramid that I have defined? How can that be acheived?

I cant really use any special file formats or editors because this assignment was all to do with creating a .bmp file and using that as a texture, and texture mapping it onto an object and I'm already going a bit off course!!

Thanks again.
Hi,

For your defined primitives like a sphere or easier a cylinder it´s quite easy to define texcoords. Think of a cylinder defined by 3 variables (r, theta, z)
Where r is the radius of your cylinder, theta is the angle of the current point on the cylinder and z is the current height of the point.
For further explanation on cylindrical coordinates look here:

Cylindrical Coordinates
For describing coordinates on a sphere another coordinate system might be more suitable:
Spherical Coordinates

For mapping the texture onto the cylinder just the angle theta and the height z have to be taken into account.
If you want to map the texture around the cylinder exaclty on time you have to calculate the u-coordinate like that:

u = theta / 360

Assuming the cylinder "stands" vertically with theta given in degrees.
Retrieving v is rather easier, v is zero at the bottom and 1 at the top of the cylinder. For any vertices between bottom and top just interpolate the values.

A Cube can be seen as a cylinder with "just" 4 faces on the side so just treat it as a cylinder.

For mapping a pyramid just think of the pyramid as a "special" kind of cylinder with all top vertices merged into one vertex.

For any other arbitrary surfaces it is probably the easiest to use an editor though.

I hope that answer was sufficient and helped you a bit.

Regards

Ralph
Thanks again Ekast.

The problem I have is more to do with the actual coding, rather than the maths behind it. As in, how do I use the functions like:
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, 256.0, 0.0); etc

I think I understand how I could calculate the vertex data for the glVertex3f(0.0, 0.0, 0.0); part - I could just replace the 3 variables with r, z and theta (in the cylinder example), but how does the TexCoord part vary? I dont really understand how this function works.

Would it be easier to use the physical pixel data from the 2D array or is the above method best?

Thanks
Quote:Original post by Ekast
Hi,

as far as I know all "glut" primitives like glutSolidSphere ... don´t have predefined texture coordinates but I might be wrong. The only exception as far as I know is the glutSolidTeapot.

Ralph


I don't know about the glut primitives, but the glu quadrics can all generate texture co-ordinates.

OP: You need to think about how texture co-ordinates work. Textures exist in a 2-dimensional space (usually notated as the 's' and 't' axes), and you object resides in a 3-dimensional space ('x', 'y', and 'z').

The texCoord function defines a mapping between the two at the next specified point, so for a quad, you set the corner points. For a more complex image, you do the same thing - for each vertex of the model, you specify the location in the image that you want to map there. OpenGL automatically blends between these texture coordinates, to retrieve the pixels inbetween.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement