C# Direct3D模型概论

开发 后端
这里介绍C# Direct3D模型由World 空间,View 空间和光源组成。World空间就像我们现实生活中的空间一样,我们可以把物体放在这个空间的任何地方。

C#语言有很多值得学习的地方,这里我们主要介绍C# Direct3D模型,包括介绍改成PositionColored格式等方面。

什么是真正的3D世界呢?或者说C# Direct3D模型是什么样的呢?

C# Direct3D模型由World 空间,View 空间和光源组成。World空间就像我们现实生活中的空间一样,我们可以把物体放在这个空间的任何地方。但仅仅这样是不够的。因为现实中,我们是通过眼睛来看事物的,存在的东西不一定我们可以看得到,太远的东西看不到,被挡住的东西看不到,太近的东西也看不到,在我们视角以外的东西一样看不到。在 Direct3D中用camera来模拟我们的眼睛。

另外还有一个重要概念,transform,变换。在world空间中,我们可以把物体移动,旋转,这需要通过变换来实现。通过camera来决定我们最终能看到的那些东西时也是经过变换。在Direct3D中,变换是通过4x4的矩阵来实现的。

不是搞算法的人没有必要了解矩阵的具体内容,可以通过Maxtrix中的静态函数来创建矩阵:
◆Matrix.RotationX()
◆Matrix.RotationY()
◆Matrix,RotationZ()创建分别围绕X,Y,Z轴旋转的矩阵。
◆Matrix.Translation()用来创建移动物体的矩阵。

而与camera相关的变换有两个,View Transform,和Projection Transform.View Transform用来定义camera的信息。而Projection Transform用来定义我们的可见范围,也就是说那些物体会被绘制到屏幕上去。

另外一个很重要的概念就是光源,在真实世界中,没有光的情况下我们是看不到东西的,即使他存在。而且我们看到的物体的颜色/亮度,与光源的颜色/亮度,与他本身的材质/颜色都有关系。在Direct3D中也是如此,一旦一个物体在我们的可视范围内,并且没有被遮挡住,那么他能否可见,以及什么颜色/亮度,就由光源,物体的材质来决定了。

下面就开始我们的例子。首先把我们的代码中的顶点格式,改成PositionColored格式。注意坐标哦,这里的坐标可不是屏幕坐标系了,而是World坐标系,是左手笛卡尔3D坐标系。***步的代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Windows.Forms;  
  7. using Microsoft.DirectX;  
  8. using Microsoft.DirectX.Direct3D;  
  9. #endregion  
  10. namespace Exam2  
  11. {  
  12. partial class Form1 : Form  
  13. {  
  14. private Device device = null;  
  15. public Form1()  
  16. {  
  17. InitializeComponent();  
  18. this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true );  
  19. }  
  20. public void InitializeGraphics()  
  21. {  
  22. PresentParameters para = new PresentParameters();  
  23. para.Windowed = true;  
  24. para.SwapEffect = SwapEffect.Discard;  
  25. device = new Device( 0, DeviceType.Hardware, this, 
    CreateFlags.HardwareVertexProcessing, para );  
  26. }  
  27. private void Form1_Paint(object sender, PaintEventArgs e)  
  28. {  
  29. device.Clear( ClearFlags.Target, Color.Blue, 0.0f, 0 );  
  30. CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];  
  31. verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);  
  32. verts[0].Color = Color.Aqua.ToArgb();  
  33. verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);  
  34. verts[1].Color = Color.Black.ToArgb();  
  35. verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);  
  36. verts[2].Color = Color.Purple.ToArgb();  
  37. device.BeginScene();  
  38. device.VertexFormat = CustomVertex.PositionColored.Format;  
  39. device.DrawUserPrimitives( PrimitiveType.TriangleList, 1, verts );  
  40.  
  41. device.EndScene();  
  42. device.Present();  
  43. this.Invalidate();  
  44. }  
  45. }  

以上介绍C# Direct3D模型。

【编辑推荐】

  1. C#方法重写全面介绍
  2. C#流程控制语句简单描述
  3. Java和C#字符串类型概述
  4. C#访问修饰符详细剖析
  5. 选择C#构造函数描述
责任编辑:佚名 来源: 博客园
相关推荐

2013-09-02 15:46:06

OpenGLWindows

2018-01-23 10:07:13

LinuxWindowsWine 3.0

2023-06-28 08:29:29

Direct3D工作图功能

2010-09-30 10:31:43

J2ME3D

2009-08-25 16:03:51

C# SQLDMO对象

2015-10-28 10:21:54

WineUbuntu 15.1Linux

2022-05-20 07:30:38

微软Windows 11

2023-02-09 16:14:09

微软Windows

2013-04-25 10:03:07

unity3D手机游戏引擎

2009-08-28 09:25:59

C#查看Excel对象

2009-09-03 17:18:40

C#扩展性对象模型

2009-09-04 15:43:07

C#流模型

2020-08-26 10:37:21

阿里3D

2009-09-03 17:33:08

C#常规扩展性模型

2009-08-05 16:04:27

C# Actor模型

2023-08-18 08:00:00

游戏开发3D模型

2021-09-14 10:11:46

谷歌3D舞蹈生成模型FACT

2012-12-24 09:14:31

ios

2009-04-14 22:13:44

LinuxWine 1.1.19

2009-08-18 17:19:33

C#事件模型
点赞
收藏

51CTO技术栈公众号