GameDev.netApplying the Texture Mapping Technique

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 , and v vectors described in the texture mapping equation. n describes the vector that points in the direction of the texture image's x-axis. u describes the vector that points in the direction of the texture images y-axis. v describes a vector that is normal to both n and u. As an optimization based on the final form of the equation, we decided that by scaling the v and u vectors, we could scale the texture on the 3D polygon. This means v and u 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.v
In my code, I also rotate is still orthogonal to both v and n.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 [. There's also a constant scalar, and constant scalings of V,C], u, and v. The vectors, n, u, and v, 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.n
## 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.
© 1999-2011 Gamedev.net. All rights reserved. |