Archived

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

absolute position in a plane

This topic is 5243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

given a 3D vector, and a plane, how can you find 2D coordinates in the plane that if you did it for every vertex of the triangle in the plane, the texture map would be square? i doubt you would need the distance part of the plane, only the normal. it seems like a relatively simple thing to do, but i''ve been unable to figure it out for a while now.

Share this post


Link to post
Share on other sites
sorry, I know its wierd because there is no sense of ''position'' in planes. basically, i''m trying to set up texturing of bsp polygons in an editor. i know the plane of the polygon, and all the vertices. what i need is to generate some texture coordinates that result in the texture map being square, as in no skewing. say for example, the polygon is perfectly horizontal. then the function i need to calculate the texture coordinates would simply select the x and z of the vector (assuming y is up), and give those as the texture coordinates. it should obviously be able to accept any plane, as not all surfaces are axis-planes.

Share this post


Link to post
Share on other sites
It sounds like you want to pick an "x" vector in the plane of the triangle, perhaps the vector between two of the vertices, perhaps some other vector. Then get another vector in the plane (any nonparallel vector), and find the projection of this new vector onto the first one. Subtact the projection from the new vector and thus get a new vector "y" in the plane of the triangle that''s perpendicular to the first vector.

Pick a texture coordinate for one of the vertices (need a reference point). Find vectors from this vertex to the other vertices, and find the magnitudes of the projections of these vectors onto the (normalized) x and y vectors you found earlier. Since these vector are orthonormal and in the plane of the triangle, they make an orthornormal basis for the plane of the triangle. You now have "x" and "y" relative coordinates for the other two vertices (other than the one you chose earlier to have known teture coordinates). You can now scale these coordinates as desired and then add them to the known texture coordinate.

... Note that I''ve never done this, but it seems logical...

Share this post


Link to post
Share on other sites
seems like overkill, after much trial and error, this is the closest i could get:

UV.x = (Vertex.Position.x * (1.0f - Plane.n.x) + Vertex.Position.z * (Plane.n.x));
UV.y = (Vertex.Position.y * (1.0f - Plane.n.y) + Vertex.Position.z * (Plane.n.y));

the textures are nearly square (at most an aspect ratio of 2), but it rotates textures on non-horizontal surfaces.
it seems like there should be a way to just scale by the normal of the plane to compensate for the angle

Share this post


Link to post
Share on other sites
Think of it in terms of two different coordinates spaces - world space, and texture space. World space is the actual position of the vertices in the world. This space is defined using the normal three principal axis (what you're used to). Now imagine texture space, which is aligned to the face of your polygon. Two of the axis, X and Y, are at right angles to each other, yet both coplanar with the polygon. The third axis (Z) is perpendicular to the polygon (and the X and Y axis), and is basically the normal for the polygon (although in texture space you really don't need it). For the X coordinate of a point in texture space, take the dot product between the normalized X axis and the world-space point, and for the Y coordinate take the dot product between the normalized Y axis and world-space point. Repeat for each vertex in the polygon.

Calculating right and up vectors (quick lingo for "X axis" and "Y axis" respectively) is easy. As Geoff mentioned, any arbitrary coplanar vector will do, and they can be created by subtracting any two vertices on the polygon. Once you have this right vector, and the existing normal, take the cross product of the two to create an up vector (keep in mind that order counts! I usually do the cross product normal first, then right vector, so that I can keep my naming consistent. Otherwise, you really started with an up vector and are creating a right vector ). In the end, you have three vectors perpendicular to each other, and two (right and up) coplanar to the polygon. Normalize and you're good to go

Then it's just a matter of taking dot products (or making projections, if you will):

float tex_x = DotProduct(right, point);
float tex_y = DotProduct(up, point);


You may need to do a few things differently along the way, such as choosing a specific vector for up/right instead of an arbitrarily calculated one. And maybe your editor also includes the ability to "offset" the texture along the ST vectors (like in Worldcraft). However given the context, I can almost certainly guarantee that you're going to want to align the texture vectors with the polygon face, so you don't frustrate the crap out of the users! Many of the effects of skewing can be accomplished by scaling along a texture axis, a feature of which that may also be handy for users.

[edited by - Zipster on August 6, 2003 1:24:27 AM]

Share this post


Link to post
Share on other sites
worked like a dream, thank you very much.

i do have texture transformation capabilities (scale, rotation, translation), i just want it so if nothing is transformed, its flat on the face, and that the transformations' reference points (the texture coordinates) are 'predictable'

edit: now that i get it, i see Geoff posted basically the same thing, i just didn't get it then, sorry

[edited by - billybob on August 6, 2003 1:53:21 AM]

Share this post


Link to post
Share on other sites
Yes, Geoff said the same thing, although he preferred to use vector projections instead of dot products (terminology-wise), and instead of doing so for each vertex, he simply assigned one of the points as the texture-space origin and defined the other vertices relative to that one.

The way I see it, it doesn't matter where you start, as long as the user can change it

[edited by - Zipster on August 6, 2003 2:05:36 AM]

Share this post


Link to post
Share on other sites