3D game from scratch-8: texture

Source: Internet
Author: User

: Golden point time and space ::

Back 8: texture

People are always easy to ignore things around them. The more familiar and intimate they are, the more they will happen. Why? I do not know, but since it has already happened, there is no need to blame anything.
Taking actual actions immediately is the only thing we should do!
What is texture? Texture is what we usually call texture (also called skin ). The principle is to overwrite 2D images on 3D models to make our 3D models more realistic. Take a look at the two pictures below. There is a tiger model without textures on the left and a picture on the right. Obviously, the pictures seem more authentic.

  

Fortunately, d3d has provided us with a very convenient texture function, but before we start, let's take a look at several important concepts about texture.

I. Texture size
The texture is a 2D image, which can be saved as BMP, TGA, JPG, or any common image format, so it must have a size. Although theoretically, the new graphics card can support textures of any size, to ensure compatibility with the previous graphics card and program execution efficiency, you 'd better make the texture size to the Npower of 2. For example, 16x16, 32x32, 64x64,128x128,256x256.

Ii. texture coordinates
Texture coordinates are also called texture coordinates. In d3d, we use two floating point values (u, v) ranging from 0 to 1 to set the texture coordinates of a point. U is a horizontal axis and V is a vertical axis. The upper left corner of the texture is (), and the lower right corner is ). The following figure shows the definition of texture coordinates.

  

If a 3D model has a texture, you must specify a texture coordinate (u, v) for each point in the model to indicate the position of the point in the texture.

Well, the specific texture process is described below.
(1) first, we need to set the vertex format and fvf to the following.

// Customize the vertex Structure
Struct customvertex
{
D3dxvector3 position; // vertex Coordinate
D3dcolor color; // vertex color
Float tu, TV; // texture coordinate
};

// Our custom fvf defines the vertex structure,
// D3dfvf_xyz: vertex Coordinate
// D3dfvf_diffuse: Specifies the vertex color.
// D3dfvf_tex1: Indicates texture
# Define d3dfvf_customvertex (d3dfvf_xyz | d3dfvf_diffuse | d3dfvf_tex1)

(2) Define and create a texture. The texture is defined using the idirect3dtexture8 interface in d3d:

Lpdirect3dtexture8 g_ptexture = NULL; // texture

// Create our texture from a file
D3dxcreatetexturefromfile (g_pd3ddevice, "3dgame.bmp", & g_ptexture ));

(3) set the texture we just created before rendering the model:

// Specify the texture
G_pd3ddevice-> settexture (0, g_ptexture );
G_pd3ddevice-> settexturestagestate (0, d3dtss_colodrop, d3dtop_selectarg1 );

This example is based on the previous example. We have attached a texture to the back-to-life bucket, which is shown below.

  

 

Run the program and the execution result is immediately displayed as follows:

  

In general, texture is like this. After understanding the basic principles, you must carefully read our example program, because many details are included in the program.
Ah ~~~~, It's not too early. I have practiced my guitar for one afternoon. It's so tired. Go to bed.

[Download the complete source code of this back-to-source program]

Home: http://www.gpgame.com
Http://www.gpgame.net
Mail: softboy@sina.com
Soft on


Return

Golden point time and space 2000/8/18

Www.gpgame.net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.