One day, when considering the reuse of my old code, I had a disturbing discovery! I didn't remember how I derived one of the equation in my code by using the general texture mapping equation! In my quest to minimize steps and reduce repeated calculations, I had obscured the general form of the equation, and had neglected to write comments on everything I had done! In this case, I had comments for everything except this particularly puzzling step. As a safe protect, I've decided to briefly describe how I organized and designed my part of the program. Describing the textured 3D polygonThere are intrinsic features of a textured 3D polygon that never change. In order to be efficient, we'll want to calculate these numbers only once, when first defining the textured 3D polygon. These features are the image to be used on the 3D polygon (not something that can never change, but something that will in most cases be static), the direction vectors that describe the orientation of the texture on the 3D polygon, and an offset vector for moving the texture around on the 3D polygon. The textured image is defined in whatever way you wish. Typically it consists of a 2D array of pixels. The texture coordinates calculated in the previous paper on texture mapping equations point inside this array and return the corresponding color. In my code, I define type as TexImage (for texture image). The direction vectors are the u, v, and n vectors described in the texture mapping equation. u describes the vector that points in the direction of the texture image's x-axis. v describes the vector that points in the direction of the texture images y-axis. n describes a vector that is normal to both u and v. As an optimization based on the final form of the equation, we decided that by scaling the u and v vectors, we could scale the texture on the 3D polygon. This means u and v should have magnitudes equal to the number of units per pixel. The offset vector can be represented as precalculated texture coordinates for each point of the 3D polygon. These precalculated points come in handy when setting up the actual drawing of the polygon.
In my code, I also rotate u by some angle, but that's done using a simple rotation matrix. After its rotated, I again make sure that v is still orthogonal to both n and u. Setup for drawing the polygonJust before you start drawing the polygon, there are some constants that need to be found. Two of the constants are [U,C] and [V,C]. There's also a constant scalar, and constant scalings of u, v, and n. The vectors, u, v, and n, need to be rotated at the beginning of the calculations and used in their rotated form. The must be rotated using the same rotation matrix which rotated the points of the 3D polygon.
Finding the pixel in the texture given a point on the screenNow using the constants defined in the previous section, it's easy to determine the position of the pixel in a texture given the screen coordinates. To make the value more useful, the remainder of the texture coordinate divided by the width of the texture is often used to produce a looping effect of the texture.
Where is this technique used?This information was stripped from the code I used in my cs184 project. I put it into a simpler, more condensed form, so that people might get an idea of how to use my resultant equation without having to look at the code. If you wish to see the code and program used in my cs184 project, just follow this link to The Improved Maze Runner's Homepage. Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|