DirectX9 3D fast start 4

xiaoxiao2021-03-05  26

DirectX9 3D fast start 4

By SSSA2000

4/17/2005

Next, we must use Mesh to read the .x file, about .x file, actually to say, we can use external tools such as 3ds max to create .3DS file, then use Microsoft to provide our tool to our tools into .x files If you use 3DS at the same time, you will ask, what should I do? You don't have to worry, .x file automatically contains material, and animation, so using Mesh we can easily use our products in 3DS Max in the game.

The .x file allows custom extensions, of course, this will also speak in the future, let's see how to use it first, by the way, there is a more file format. MD3 format file, that is, Quake3 Use the file, if you have a chance, I will talk.

About 3DS files and .x conversions can be found in the Gameres website http://www.gameres.com/articles/program/visual/3D/3dinapp.htm

The next process is simple, you can't believe, please see:

Private mesh mesh = null; // Establish a Mesh object

Private Material [] Meshmaterials; // Used to save material

Private texture [] Meshtextures; // for saving texture

Private void loadmesh (string file)

{

ExtendedMaterial [] mtrl; // Save Mesh subset information, save material information

// loading ou Mesh

Mesh = Mesh.fromfile (File, Meshflags.Managed, Device, Out mtrl);

// if we have any matials, store them

IF ((mtrl! = null) && (mtrl.length> 0))))

{

Meshmaterials = new massial [mtrl.length];

Meshtextures = new texture [mtrl.length];

// Store Each Material and Texture

For (int i = 0; i

{

Meshmaterials [i] = mtrl [i] .MATERIAL3D;

IF ((mtrl [i] .texturefilename! = null) && (mtrl [i] .textureFileName! = String.empty)))))

{

// WE Have a Texture, Try to loading IT

Meshtextures [i] = textureloader.fromfile (device, @ "../../" mtrl [i] .textureFileName);

}

}

}

}

If you think that the code here is more chaotic, there is no relationship, we are reasonable, first of all, the Mesh class has two major static methods to load external models. These two methods are Mesh.Formfile and Mesh.Fromstream. The two methods are in nature, and the Stream method has more overloaded to accommodate different sizes. Mesh.formfile is the easiest, we only introduce this, this method has a lot of overloaded versions, we use the most parameter, so others are well understood:

Public Static Mesh fromfile (String FileName, // File Name Meshflags Options, // Control Go and How to Load Data Device Device, // Rendering Mesh Device Out GraphicsTream Adjacency, // Save 3 faces adjacent to each side OUT ExtendedMaterial Materials, // Saves normal Direct3D materials and a string loaded as textured, often used, the texture or resource file name Out EffectInstance Effects // describes the HLSL material files and values ​​for Mesh.); us Used is a method with 4 parameters:

Public Static Mesh fromfile (String FileName, Meshflags Options, Device Device, Out Extendedmaterial Materials);

Next, if you read this .x file with material or texture, an array is created according to the size of the material and texture to save these textures and materials. Because there may be many different subsets in Mesh, you need to create an array of materials and textures to meet the needs of each subset. Mesh will automatically put each subset and material texture of these subsets.

In this way, we have completed a loadmesh function. This function we can use anywhere in the future, it is very convenient to read .x files.

Let's take a look at our results, of course, we still need to add a few words in the drawing:

For (int i = 0; i

{

Device.material = meshmaterials [i]; // gives the preserved material to Device;

Device.Settexture (0, Meshtextures [i]); // Give the texture to Device, if not null

Mesh.drawsubset (i); / / Call DrawSubset method according to the idiogram

}

Ok, now we can compile it on the basis of the conventional framework, we can see a .x file that has been read, no matter how it is worth celebrating. After we look at the example of SDK Tutorial6, we found that the results of this program can be rotated. In the front, you can add a sentence:

Device.Transform.World = Matrix.RotationYawpitchroll (YAW, Pitch, Roll) * Matrix.Translation (X, Y, Z);

Ok, let's go to the goal, you can also set the transform matrix with your own meaning, is it COOL?

Ok, the following is a complete code, if you have any opinions, send it to my blog. Everyone makes progress together.

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using Microsoft.directX;

Using Microsoft.directx.direct3d;

Namespace Chapter5code

{

///

/// summary description for Form1.

///

Public class form1: system.windows.forms.form {

PRIVATE DEVICE DEVICE = NULL;

PRIVATE MESH MESH = NULL;

Private Material [] Meshmaterials;

PRIVATE TEXTURE [] Meshtextures;

///

/// Required Designer Variable.

///

Private system.componentmodel.Container Components = NULL;

Private float angle =

0.0F

;

Public Form1 ()

{

//

// Required for Windows Form Designer Support

//

InitializationComponent ();

This.SetStyle (ControlStyles.allpaintingInwmpaint | Controlstyles.opaque, true);

}

///

/// We Will Initialize Our Graphics Device Here

///

Public void initializegraphics ()

{

// setur presentation parameters

PresentParameters Presentparams = New Presentparameters ();

Presentparams.windowed = true;

Presentparams.swaPeffect = swapeffect.discard;

Presentparams.autodepthstencilFormat = depthformat.d16;

Presentparams.enableAutodepthstencil = true;

// Create Our Device

Device = New Device (0, DeviceType.hardware, this, createflags.softwarevertexprocessing, presentparams);

// loading ou Mesh

Loadmesh (@ "../../ tiny.x");

}

Private void loadmesh (string file)

{

ExtendedMaterial [] mtrl;

// loading ou Mesh

Mesh = Mesh.fromfile (File, Meshflags.Managed, Device, Out mtrl);

// if we have any matials, store them

IF ((mtrl! = null) && (mtrl.length> 0))))

{

Meshmaterials = new massial [mtrl.length];

Meshtextures = new texture [mtrl.length];

// Store Each Material and Texture

For (int i = 0; i

{

Meshmaterials [i] = mtrl [i] .MATERIAL3D;

IF ((mtrl [i] .texturefilename! = null) && (mtrl [i] .textureFileName! = String.empty)))))

{

// WE Have a Texture, Try to loading IT

Meshtextures [i] = textureloader.fromfile (device, @ "../../" mtrl [i] .textureFileName);

}

}

}

}

Private void setupcamera ()

{

Device.Transform.Projection = Matrix.Perspectivefovlh (FLOAT) Math.pi / 4, this.width / this.height,

1.0f

,

10000.0F

);

Device.Transform.View = Matrix.lookatlh (New Vector3 (0, 0,

580.0F

), new vector3 (), new vector3 (0, 1, 0));

//Device.renderstate.Ambient = color.darkBlue;

Device.lights [0] .type = lightttype.directional;

Device.lights [0] .diffuse = color.white;

Device.lights [0] .direction = new vector3 (0, -1, -1);

Device.lights [0] .update ();

Device.lights [0] .enabled = true;

}

Protected Override Void Onpaint (System.Windows.Forms.Painteventargs E)

{

Device.clear (Clearflags.target | Clearflags.zbuffer, Color.CornflowerBlue,

1.0F

, 0);

Setupcamera ();

DEVICE.BEGINSCENE ();

// Draw Our Mesh

Drawmesh (angle / (float) Math.pi, Angle / (FLOAT) Math.pi *

2.0F

Angle / (float) Math.pi /

4.0F

,

0.0F

,

0.0F

,

0.0F

);

Device.endscene ();

Device.Present ();

THIS.INVALIDATE ();

}

Private Void Drawmesh (Float Yaw, Float Pitch, Float Roll, Float X, Float Y, Float Z)

{

Angle =

0.01F

;

Device.Transform.World = Matrix.RotationYawpitchroll (YAW, Pitch, Roll) * Matrix.Translation (X, Y, Z);

For (int i = 0; i

{

Device.material = meshmaterials [i];

Device.Settexture (0, Meshtextures [i]);

Mesh.drawsubset (i);

}

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

#Region Windows Form Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.Components = new system.componentmodel.container ();

This.size = new size (800, 600);

THIS.TEXT = "Form1";

}

#ndregion

///

/// The main entry point for the application.

///

Static void

Main

()

{

Using (Form1 FRM = New Form1 ())

{

// Show Our Form and Initialize Our Graphics Engine

Frm.show ();

frm.initializegraphics ();

Application.run (frm);

}

}

}

}

In fact, we have already found that our procedures so far, we have followed a fixed framework. Sometimes you find the procedure very long. In fact, from the perspective of the framework, it is not so long. If you are afraid that people who want to watch the Chang code can pay attention At the same time, many code are common. How to control the Mesh next time.

By SSSA2000

4/17/2005

转载请注明原文地址:https://www.9cbs.com/read-34872.html

New Post(0)