1

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.

user1118321
  • 25,567
  • 4
  • 55
  • 86
Pete L
  • 61
  • 1
  • 3
  • If nobody can answer you, you might want to check the blender source, uv unwrapping has got to be *somewhere* in there. https://svn.blender.org/svnroot/bf-blender/trunk/blender/ – Benjamin Lindley Jan 30 '11 at 20:21
  • Well I was looking at these kind of actions in XSI but none of them even achieved what I was trying to do They would wrap around and I would like it just to be flat on the material. Thanks for the suggestion though. – Pete L Jan 30 '11 at 20:31
  • I am not sure if you are making the assumption that a given vertex maps to a unique point in xy. Given a model which is topologically a sphere, surely mapping it into the plane in such a way is not possible ( e.g. consider a simple cube.) Relatedly, if you want to avoid stretching, even the three sides of a cube at a single vertex cannot all map to have that vertex common on the plane. So I think you either have to have to treat each triangle separately or have stretching, and in the latter case at least some vertices will need multiple mappings. – Keith Jan 31 '11 at 22:27
  • In general you can't map a 3D surface to 2D without some distortion. See, for example, http://en.wikipedia.org/wiki/Map_projection. If you are willing to allow some distortion, there are zillions of mapping/unwrapping algorithms. – brainjam Feb 01 '11 at 01:57
  • Transferring a triangle in 3d space to 2d space shouldn't really have any distortion effects its 3 points, as long as the 2d triangle has the same area, same angles, and same edge lengths it should always be the same. I just don't know how to do it :) – Pete L Feb 02 '11 at 01:34
  • 1
    A single triangle yes, but you want a model which has many triangles. Do you want to keep the spatial relationships intact (ie: keep a triangle connected to its neighbors)? If so there isn't a generalized mapping from 3D to 2D that will preserve the geometries of all triangles concerned. – Ron Warholic Feb 08 '11 at 22:56
  • @Ron Warholic, right, and general UV unwrapping is done by setting seams in your mesh, such that you reduce distortion by "cutting" your triangle mesh where the surface is very curved. – Aaron H. Feb 09 '11 at 16:48

1 Answers1

0

I would think of the vertices as vectors.

So, you could normalize each vector, then remove the Z coord, 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 coord, find a vector perpendicular to that, and you have the y coord 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 reprsented 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.