DirectX9 3D 快速上手 7

原创 2005年04月28日 23:21:00

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

 

DirectX9 初始化Direct3D通用框架

初始化Direct3D通用框架 The declaration of IDirect3DDevice9::Clearis: HRESULTIDirect3DDevice9::Clear( DWORD...

Directx3D9学习之二:Windows编程之最简单窗口程序

Directx3D9学习之二:Windows编程之最简单窗口程序 一、一些基本知识介绍 由于D3D的编程要借助于windows的窗口来体现,所以也需要懂一些windows的编程知识(以前做得比较多的是...
  • lhqsine
  • lhqsine
  • 2015年01月31日 23:05
  • 2649

DirectX 9 UI设计学习笔记之三:第4章Introducing DirectInput+第5章Wrapping Direct3D

此文由哈利_蜘蛛侠原创,转载请注明出处!有问题欢迎联系本人!        邮箱:2024958085@qq.com          上一期的地址: DX 9 UI设计学习笔记之二  ...

3d世界尽在掌握!—— directx 8.1/9 graphic 全解析

前言:  在我们日常游戏中,很多朋友经常挂在嘴边一个:directx。.几乎100%的喜欢玩游戏,特别是fps 3d射击游戏的朋友都要遇到directx安装和设置的问题。不过,很遗憾的是,有超过70%...

DirectX_9_3D游戏设计入门.pdf

  • 2014年07月15日 11:03
  • 4.4MB
  • 下载

DirectX 9.0c游戏开发手记之“龙书”第二版学习笔记之5: Chap7: Drawing in Direct3D ---- Part I

这一章终于开始讲绘制3D图形的内容了!这一章讲的是非shader方法,也是“龙书”第一版大部分时候所用的方法。 但是值得注意的是,这里的顶点声明是用D3DVERTEXELEMENT9数组实现的,而不...

深入理解DirectX D3D9

深入理解D3D9对图形程序员来说意义重大,我把以前的一些学习笔记都汇总起来,希望对朋友们有些所帮助,因为是零散笔记,思路很杂,还请包涵。其实只要你能完美理解D3DLOCK、D3DUSAGE、D3DPO...
  • djp505
  • djp505
  • 2011年07月17日 13:33
  • 462
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:DirectX9 3D 快速上手 7
举报原因:
原因补充:

(最多只允许输入30个字)