国内最大的酷站演示中心!
这里我想继续写点和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);
}
}
}
}
当然我们这里并不能任意的控制模型的细节程度,明天要期中答辩,突然发现自己的周记没写完,所以今天先草草结束,细节控制的下一次我们再讲吧。