DirectX9 3D 快速上手 7

** DirectX9 3D ** ** 快速上手 ** ** 7

**

** By sssa2000

**

** 4/28/2005 ** **

**

这里我想继续写点和 Mesh 有关的东西,毕竟我们可能还需要对它有很多别的要求。在 3D 游戏的实际运用中,一般来说都是运用低多边形模型,简称低模。这样才能有更加好的速度来运行游戏,恰好 DX 中有提供给我们这样的函数让我们来控制读入的 Mesh 的复杂程度。

public void WeldVertices (


 Microsoft.DirectX.Direct3D.WeldEpsilonsFlags flags ,//标志


    Microsoft.DirectX.Direct3D.WeldEpsilons epsilons , 


    Microsoft.DirectX.Direct3D.GraphicsStream adjacencyIn ,


    Microsoft.DirectX.Direct3D.GraphicsStream adjacencyOut ,


    out int[] faceRemap ,


    Microsoft.DirectX.Direct3D.GraphicsStream vertexRemap )

这个方法能实现简化模型的目的,前 2 个参数用来确定怎么简化模型,

第一个标志一共包括以下几个:

** Member

**

|

** Value

**

|

** Description

**

---|---|---

DoNotSplit

|

8

|

Instructs the weld to allow vertices to be modified only, not removed. This flag is valid only if WeldPartialMatches is set. It is useful to modify vertices so that they are equal, but not to allow vertices to be removed.

只有当 WeldPartialMatches 参数指定时才能生效,不允许分离定点

DoNotRemoveVertices

|

4

|

Instructs the weld to allow vertices to be modified only, not removed. This flag is valid only if WeldPartialMatches is set. It is useful to modify vertices to be equal, but not to allow vertices to be removed.

只有当 WeldPartialMatches 参数指定时才能生效,不能移除定点,只能修改

WeldPartialMatches

|

2

|

If a given vertex component is within epsilon, instructs the weld to modify partially matched vertices so that both components are equal. If all components are equal, one of the vertices is removed.

修改符合在 WeldEpsilons 结构中给定的顶点的条件

WeldAll

|

1

|

Welds all vertices that are marked by adjacency as being overlapping.

焊接所有的 adjacency 指定的定点

例如我们可以这样简单的调用这个方法

mesh.WeldVertices(WeldEpsilonsFlags.WeldAll, new WeldEpsilons(), null, null);

当然 前提是你必须在这之前对变量 mesh 进行付值。

到程序里面看看,效果还是不错的,已经简化了很多顶点。

或许你还觉得不够简化,没关系这里我们还可以把复杂的模型拆成小块,用下面这个函数:

public static Microsoft.DirectX.Direct3D.Mesh[] Split (


Mesh mesh , //要拆分的mesh


int[] adjacencyIn , 


System.Int32 maxSize ,// 拆分后新的Mesh的最大顶点数量


MeshFlags options , //标志


    out GraphicsStream adjacencyArrayOut , //这三个out 参数返回新mesh的一些信息


    out GraphicsStream faceRemapArrayOut ,


    out GraphicsStream vertRemapArrayOut )

我们也可以使用简单的重载版本的函数。

在拆分前,我们需要建立保存拆分后的各个 mesh 的容器,数组不错。

我们这么写:

Mesh splitmeshes[]= meshes = Mesh.Split(mesh, null , 500, mesh.Options.Value);

这样我们就把 mesh 根据顶点的树木分成了很多个部分

在我们这个事例程序里面,通过控制对数组的遍历来实现对所有拆分出来的 mesh 的遍历。

完整代码如下:

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 Chapter7Code

{

///

1<summary>
2
3///  Summary description for Form1. 
4
5///  </summary>

public class Form1 : System.Windows.Forms.Form

{

private Device device = null ;

private Mesh mesh = null ;

private Material[] meshMaterials;

private Texture[] meshTextures;

// Split information

private Mesh[] meshes = null ;

private bool drawSplit = false ;

private bool drawAllSplit = false ;

private int index = 0;

private int lastIndexTick = System.Environment.TickCount;

///

1<summary>
2
3///  Required designer variable. 
4
5///  </summary>

private System.ComponentModel.Container components = null ;

private float angle = 0.0f ;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

this .SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true );

}

///

1<summary>
2
3///  We will initialize our graphics device here 
4
5///  </summary>

public void InitializeGraphics()

{

// Set our 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);

// Load our mesh

LoadMesh(@"..\..\tiny.x");

meshes = Mesh.Split(mesh, null , 10, mesh.Options.Value);

}

private void LoadMesh( string file)

{

ExtendedMaterial[] mtrl;

// Load our mesh

mesh = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);

// If we have any materials, store them

if ((mtrl != null ) && (mtrl.Length > 0))

{

meshMaterials = new Material[mtrl.Length];

meshTextures = new Texture[mtrl.Length];

// Store each material and texture

for ( int i = 0; i < mtrl.Length; i++)

<p class="MsoNormal" style="BACKGROUND: #e5

Published At
Categories with Web编程
Tagged with
comments powered by Disqus