Feb
2
GM6 D3D from the Ground Up - Chapter 7
2007 @ 05:41 PMChapter 7 - Texture Mapping
What is a Texture?
A
texture is simply any image. You apply it to a 3D polygon for a realistic rendering, much more so than basic lighting and colors can depict.
Loading Textures
The best way to organize textures in your Game Maker project is to create a Group under the Backgrounds resource tree named 'Textures.' From there, you can add each individual texture you will use in your game.
There are a few restrictions to the sizes of textures. The dimensions MUST be powers of 2. For example, 64x32, 128x64, etc.
Mapping Textures to Polygons
Now that you have your textures inside Game Maker, we must tell it how to apply the textures to our objects. We do this by specifying a
texture coordinate for each vertex of the triangle. This texture coordinate, or
texel is a value from 0.0 to 1.0. Texels are in two dimensions - x and y. The x texels go from 0 to 1 from left to right, while the y texels go from 0 to 1 from top to bottom.

To allow a primitive to have a texture, we have to create it with that accessibility. So we now use
d3d_primitive_begin_texture(kind, texture_id). kind is the same argument that was used in
d3d_primitive_begin. texture_id is the ID of the texture you want to use for this primitive. To get this ID, you can use the function
background_get_texture(back) where back is the name of the texture you want.
Now, when specifying a vertex for our textured primitive, we'll call
d3d_vertex_texture(x,y,z,xtex,ytex) where xtex and ytex are the x and y texels of our texture map. Please note that you can also use
d3d_vertex_normal_texture_color(x,y,z,nx,ny,nz,xtex,ytex,color,alpha) if you want to include all of those characteristics for that vertex.
Texture Filtering
When the texture is mapped onto the polygon, there is a loss of quality as it is stretched and shrunk to fit the vertices. To combat this, filtering is used. By default, Game Maker uses
nearest pixel filtering. Nearest-pixel filtering is the simplest and fastest filtering method. However, when the texture is stretched to a large size, it is plagued by large, block pixels.
Linear filtering requires more processing power but it results in a large improvement in quality. Rather than taking a single pixel next to the texel, it averages the surrounding pixels. This results in a fuzzy look when stretched to large sizes. This fuzziness is a better looking artifact than the pixelated look of nearest-pixel filtering, however. You can set this filtering mode by calling
texture_set_interpolation(true). You can go back to nearest-pixel filtering with a call to
texture_set_interpolation(false).
Texturing Built-In Primitives
Each of the built in basic shapes has three arguments that I have yet to explain:
texid,
hrepeat, and
vrepeat.
texid is the ID of the texture, just as it was earlier explained.
hrepeat is the amount of times the texture is repeated horizontally along each face.
vrepeat is the amount of times the texture is repeated vertically along each face.
<< Previous Chapter Next Chapter >>
This article hasn't been commented yet.