Archived

This topic is now archived and is closed to further replies.

Crispy

Texture mapping theory

Recommended Posts

Crispy    556
this is a cross-post from NeHe''s OpenGL forum, and I know one shouldn''t cross-post, but I do need a solution to the following problem: I''ve calculated texture coordinates for each of the polys in the little virtual world of mine. Now I need to draw those polys with the textures mapped on them correctly. I''ve never done this in an ''automated'' fashion (autocalculating tex coords) and with randomly shaped polys. I gather that I''d need to know the orientation of each poly to map the texture correctly: for example a poly on the XY plane would require something like glTexCoord3f(u, v, w), but a poly on the YZ plane would need glTexCoord3f(v, w, w) or something (?!?!). I''ve no idea how to texture map an arbitrarily banked poly... Most likely these calls are wrong, but that''s why I''m posting this - how do I define how the textures need to be mapped on a poly? Erm... does this make sense at any point at all? thanks, crispy

Share this post


Link to post
Share on other sites
Chock    122
There are several theories about how to texture a 3D object with a 2D Texture (you wrote something about glTexCoord3f(), but do you really have a 3D texture?).
It would possibly help if you told us what kind of objects you have.
A simple way is the following. Suppose dx is the width of your object and dy the height. Then every vertex gets the texture coordinate glTexCoord2f(x/dx + dx/2, y/dy + dy/2), supposing that your object is centered around its origin. But in most cases this looks ugly. It really depends on what kinds of objects you have...

Share this post


Link to post
Share on other sites
Crispy    556
Here''s my reply in the other forum - it should explain a wee bit better what I''m trying to do.

Chock: I''m texture mapping polygons that are faces of random objects (rooms, random shaped meshes, etc.)

Is this topic really as taboo as it seems - I should think most of you (meaning above beginner coders) have faced this question at one time or another

Anyway - I still need help...

thanks,
Crispy

Share this post


Link to post
Share on other sites
H A L O    122
You have to use the vertex normals. I am trying to get this working as well. It amazes me that Worldcraft does this so easily. Go here to see what I have going. It uv maps meshes in real-time:
www.planetquake.com/lucid/cshop.zip

My method is to align a pivot's x axis to the vertex normal, and then assign UV's by the vertex x and y position relative to the pivot orientation (which I leave positioned at 0,0,0). Here is my code in Blitz3D. If you get this perfected, let me know!

It also helps a lot to make your objects from flat faces. Use 6 rectangle sub-objects for a cube, not just 4 vertices. Use a flat sub-object for each side of a cylinder. This makes it so the vertex normals aren't averaged between two sides' angles.

Function uvmap(mesh,scalex#=256,scaley#=256)
temp=CreatePivot()
For s=1 To CountSurfaces(mesh)
surf=GetSurface(mesh,s)
For v=0 To CountVertices(surf)-1
RotateEntity temp,0,0,0
TFormNormal VertexNX(surf,v),VertexNY(surf,v),VertexNZ(surf,v),mesh,0
AlignToVector temp,TFormedX(),TFormedY(),Abs(TFormedZ()),0
TFormPoint VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v),mesh,temp
tu#=TFormedX()
tv#=TFormedY()
VertexTexCoords surf,v,tu/scalex,-tv/scaley,0
Next
Next
FreeEntity temp
End Function

[edited by - H A L O on May 4, 2002 8:11:21 PM]

[edited by - H A L O on May 4, 2002 8:13:48 PM]

[edited by - H A L O on May 4, 2002 8:17:27 PM]

Share this post


Link to post
Share on other sites