3D game from scratch-9th: 3D model (. X)

Source: Internet
Author: User

: Golden point time and space ::

9: 3D model (. X)

Without knowing it, This 3D Game's original tutorial has been serialized for two months, just as expected. I have persisted in the course of one Monday, and I don't even believe it myself, but this is true. I think everything is almost the same. As long as you have a clear goal and then move forward to the goal step by step, there will always be a day to reach the goal no matter what the goal is.
After today's completion, this tutorial has come to an end. If you have been following my tutorial, you can be proud to say that after today: "I will also do 3D games! ".
But do not think that 3D games are so simple. These are just the most basic functions, just like the point-of-painting functions in 2D games. If you want to make a real 3D game, it also requires your persistent efforts. I will also work hard, so I will come out from time to discuss some advanced 3D skills with you. Let's start with today's topic.
In 2D games, Genie and architecture are pictures pre-painted by artists. So what is 3D? The answer is the 'model', which is a 3D model made by 3dmax or Maya. The model stores the position and composition of vertices, texture coordinates, materials, textures, and other information we need. Generally, 3D games have their own model formats. d3d also has a model format, that is, the. X file.
D3d supports the. X file for us, so it is very convenient to use. You only need a few simple functions to render a model. If you do not want to write the 3D export plug-in, we may never need to know the. X file format, but we must know how. X is made:
1. Create a model in 3dmax.
2. Export to *. 3DS format.
3. Use the conv3ds.exe tool of d3d1_to convert a file in *. 3DS format to A. X file.
(Conv3ds.exe is provided in dx8sdk, and there is also a copy of pai_^ In the example package in this example)
OK. Now the. X file is available. Start to write the program.

First, define a model pointer using the model interface provided by d3d.

Lpd3dxmesh g_pmesh = NULL; // model object pointer
D3dmaterial8 * g_pmeshmaterials = NULL; // model material array (a model can have multiple materials)
Lpdirect3dtexture8 * g_pmeshtextures = NULL; // texture array of the model (one model can have multiple textures)
DWORD g_dwnummaterials = 0l; // number of materials (equal to the number of textures)

1. Read the model from the. X file.

// Material buffer
Lpd3dxbuffer pd3dxmtrlbuffer;

// Read Tiger. X to our model
If (failed (d3dxloadmeshfromx (
"Tiger. X", // [input]. X file name
D3dxmesh_systemmem, // [input] using system memory
G_pd3ddevice, // [input] d3d device pointer
Null, // ignore
& Pd3dxmtrlbuffer, // [Output] gets the model material Buffer
& G_dwnummaterials, // number of [Output] Materials
& G_pmesh) // [Output] model object pointer
{
Return e_fail;
}

3. Unfortunately, d3dxmesh does not provide material and texture management for the model, so you must do this on your own.

// Obtain the material pointer
D3dxmaterial * d3dxmaterials = (d3dxmaterial *) pd3dxmtrlbuffer-> getbufferpointer ();
G_pmeshmaterials = new d3dmaterial8 [g_dwnummaterials];
G_pmeshtextures = new lpdirect3dtexture8 [g_dwnummaterials];

For (DWORD I = 0; I <g_dwnummaterials; I ++)
{
// Copy the required material to our material object.
G_pmeshmaterials [I] = d3dxmaterials [I]. matd3d;

// Set the environment color for the material
G_pmeshmaterials [I]. Ambient = g_pmeshmaterials [I]. Diffuse;

// Create the corresponding texture
If (failed (d3dxcreatetexturefromfile (g_pd3ddevice,
D3dxmaterials [I]. ptexturefilename,
& G_pmeshtextures [I])
{
G_pmeshtextures [I] = NULL;
}
}

// Release the buffer
Pd3dxmtrlbuffer-> release ();

4. Everything is ready for rendering. Note that when the. X file is read, the model is split and each material corresponds to a part of the model.

// Our model is split according to the number of materials. Each material corresponds to a part.
// Therefore, the rendering process must be divided into several parts.
For (DWORD I = 0; I <g_dwnummaterials; I ++)
{
// Set materials for this part
G_pd3ddevice-> setmaterial (& g_pmeshmaterials [I]);
G_pd3ddevice-> settexture (0, g_pmeshtextures [I]);

// Render this part
G_pmesh-> drawsubset (I );
}

Soon, the program running results also came out:

  

Is it incredible that everything is done? I think so too, but in fact, few commercial games are directly used. most of the X Files are self-written plug-ins. I have also written one, but for beginners ,. X is enough. There is no need for everyone to write a game engine. Are you right? After learning about the basic implementation principles of 3D, I don't think you will have to be able to play a real 3D game in the future.
"There is no banquet in the world, and everything will pass away ..."
Let's say goodbye in the Song of Zheng Jun.

[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

<Full text>


Return

Golden point time and space 2000/8/24

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.