王朝网络
分享
 
 
 

DirectX9 3D 快速上手 7

王朝other·作者佚名  2006-01-31
宽屏版  字体: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有