Pete L
Pete L

Reputation: 61

Unwrapping a polygon from 3d to 2d space (using triangles) for texture coordinates

So I have created a model in Ogre3D and this model is made up of a number of triangles of arbitrary rotation and position. I would like to "unwrap" the model like many modelling programs do so that all of the triangles are mapped to 2d (x,y) but the triangle sizes are maintained. This is for applying decals. The reason the triangle sizes must be maintained so that when the texture is applied there isn't any stretching.

This was the direction I was thinking of going in but I am having trouble visualizing it and achieving the correct algorithms:

//Verticies will have a converted bool;

func( triangle x):
     for each of x's vertices:
           map to x,y coordinates if not converted;
           check other triangles for common vertex if so call func(common_tri);

Once this returns there will be a converted version of all of the triangles so that they are all unwrapped and placeable on the texture, where I am having trouble is the mapping to x,y space. I'm not sure how to get a triangle in 3d space to 2d space so that it maintains all of its attributes (like going from an angled view to a perpedicular view of the surface) Any help would be greatly appreciated.

Upvotes: 1

Views: 3112

Answers (1)

vantreeseba
vantreeseba

Reputation: 1

I would think of the vertices as vectors.

So, you could normalize each vector, then remove the Z coordinate, and then apply the multiplication again.

i.e.

tri = vec1,vec2,vec3, 
vec1Length = vec1.getLength()
newVec1 = vec1.normalize() * vec1Length

As this will preserve the size of the vectors, but map them to a 2d plane. (or it should, I'm not 100% that this is mathematically correct.)

The other way you could do this, is by thinking of the triangle itself as a 2d plane, and then transforming the vectors from that local space to the 2d plane of world space.

So for example, world origin is (0,0,0).

The triangle itself is a plane defined by the three points, you use one vector as the X coordinate, find a vector perpendicular to that, and you have the y coordinate defined. You can also define the Z by X cross product Y, this will give you an "offset" from the world origin, you can then map those back onto the 2d plane represented by the X, Y vectors from the world origin (i.e., (1,0) and (0,1)). There should be math to do this in lots of basic computer graphics books.

Upvotes: 0

Related Questions