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
{
/// <summary>
/// Summary description for Form1.
/// </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;
/// <summary>
/// Required designer variable.
/// </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);
}
/// <summary>
/// We will initialize our graphics device here
/// </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++)
{
meshMaterials[i] = mtrl[i].Material3D;
if ((mtrl[i].TextureFilename != null) && (mtrl[i].TextureFilename != string.Empty))
{
// We have a texture, try to load 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.FillMode = FillMode.WireFrame;
device.RenderState.Ambient = Color.DarkBlue;
device.Lights[0].Type = LightType.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.Black, 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;
if ((System.Environment.TickCount - lastIndexTick) > 500)
{
index++;
if (index >= meshes.Length)
index = 0;
lastIndexTick = System.Environment.TickCount;
}
device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
for (int i = 0; i < meshMaterials.Length; i++)
{
device.Material = meshMaterials[i];
device.SetTexture(0, meshTextures[i]);
if (drawSplit)
{
if (drawAllSplit)
{
foreach(Mesh m in meshes)
m.DrawSubset(i);
}
else
{
meshes[index].DrawSubset(i);
}
}
else
{
mesh.DrawSubset(i);
}
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
{
drawSplit = !drawSplit;
}
else if (e.KeyChar == 'm')
{
drawAllSplit = !drawAllSplit;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new Size(800,600);
this.Text = "Form1";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main
()
{
using (Form1 frm = new Form1())
{
// Show our form and initialize our graphics engine
frm.Show();
frm.InitializeGraphics();
Application.Run(frm);
}
}
}
}
当然我们这里并不能任意的控制模型的细节程度,明天要期中答辩,突然发现自己的周记没写完,所以今天先草草结束,细节控制的下一次我们再讲吧。
By sssa2000
4/28/2005