GameDev.netA Texture Mapping Technique

I've decided to write this page because of the lack of information on texture mapping on the WWW. When I did find information, it was usually a collection of formulae without any explanation of how they were developed. The problem with this approach is that no one can check the method if they cannot reproduce the equations. I haven't been able to reproduce the equations I found on the WWW yet, so I did my own calculations which appear to work, and I feel safer knowing that you can actually derive my result using vector math and linear algebra. ## Equation of a planeIn this derivation, we require the equation of the plane that our 3D polygon will be in. This is so we can find out where the screen x and y coordinates are on this plane. Once we know what point on the plane we have, we then transform this point into texture space, which I'll show how to do later. Some simple facts about vectors that should be known to those studying 3D graphics are the results of a dot product and cross product. A dot product, which I'll symbolize as || where u||*||v||*cos(theta)|| represents the magnitude of the vector u|| and utheta is the angle between the vectors and u. A cross product, which I'll symbolize as v results in a vector that is orthogonal (perpendicular) to the vectors involved in the operation. Because any vector orthogonal to another vector is at 90 degrees to the other angle, we can make the following statement. If uXv and u are on the plane and are not parallel, v(, if uXv).w = 0 is a vector on the plane.wThe reason for the given result, ( has to be orthogonal to both, which means it is also orthogonal to the plane they are in. Any vector orthogonal to the resultant vector would be at a 90 degree angle, and the cosine of 90 degrees is zero.uXv)Now using this result, we can find the vector p = (px, py, pz), and a point that we want to test and see if it is on the plane, (x, y, z). If the chosen point is on the plane, the result of the dot product will be zero, otherwise it will be non-zero. The displacement vector is .w = [x-px, y-py, z-zy]Doing the math (which I'd show if they had a decent equation writer for a WWW browser), we end up with the equation:
## Transforming the plane space to texture spaceTransforming the plane space to texture space takes more linear algebra than finding the equation of a plane. If you breezed through the "equation of a plane" discussion because you knew it already or understand it well, you are in good shape, otherwise you should probably try finding more basic discussions of vectors, and 2D/3D space calculations. The first step we do in this process is to translate a point on our polygon which is going to be have texture position
Now that the center of the texture map is at the origin of the 3D coordinate system, we need to rotate the polygon so that it is on the x-y plane (taking it out of 3D space). This can be done by taking three orthogonal vectors that represent the way the texture is aligned on the polygon. One is the normal to the polygon's plane, a second is the vector that points along the direction that the x-coordinates in the texture space increases, and the third is the vector that points along the direction that the y-coordinate increases. Using just two of these we can calculate the third. These also should be normalized vectors (unit vectors).
A way to convince yourself that this actually works is by plugging in the points
Now we put them together. Translate to the origin and then rotate to get the final result of moving from polygon space to texture space. (don't forget that we are moving the polygon to the x-y plane, so the z coordinate should either be zero, or should approach zero, and it can be ignored).
## Transforming from screen space to polygon spaceNow let's go from screen space to polygon space. To do this, we use the equation of the plane and the equations that translate a point from 3D space to screen space.
We can now ignore the z-coordinate because it is in orthogonal space, and say that
Now we can plug in a screen coordinate (x', y') and get the point of intersection with the polygon's plane at (x, y, z). Neat. ## Transforming from screen space to texture space The last step to this process is now to put all of our pieces together and try to simplify our equation as much as possible. By using the coordinate found by intersecting the ray from the focus through the polygon's plane, we can translate the intersected point into texture space, which gives us the texture position that is being seen. I'll be using
This is the basic answer and equation for going from screen to texture coordinates. Something you may want to notice is that you don't have to normalize ## Advanced analysis of the resultIt would be good to see many of the nice features that appear in the result. The constant in each expression, As of now, my method is just a simple linear interpolation between two calculated texture points on a linear scanline. As long as the amount of interpolated points stays small, it is hard to notice the difference between a non-interpolated texture and an interpolated one. Part of this is caused by the fact that everything is straight when you peer directly at the plane of a polygon and when the polygon's face isn't directly pointed at you, the points become more crammed. This is advantageous because you are more likely to notice discrepancies in objects that are much easier to see (those head on), and those are the objects less effected by the division by z. One colleague, Andrew Jewett, suggests that it may be possible to have general line scanlines that follow the contour of changing z on a 3D plane. This would allow you to do only one division by z per scanline. If the lines could be lined up properly, not to overlap, then it may beable to provide a quick method of having near-perfectly drawn textured polygons. (I'll take a look at this method when I have time.) ## Applying the equations in a programEven with equations, there still has to be a method of using the practically. Not only can this possibly help one better understand the usefulness of the results, but also feel the joy of tangible success! To complement this study, I have produced a WWW page on ## Special thanks to:Professor Forsyth, for teaching CS184 where much of what I learned went into the derivation of this technique. All of the Matrix techniques came from what I learned during lecture.
© 1999-2011 Gamedev.net. All rights reserved. |