王朝网络
分享
 
 
 

DirectX9 3D快速上手 3

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

DirectX9 3D快速上手 3

By sssa2000

4/15/2005

我们这里暂时先跳过,乏味的索引缓冲和深度缓冲的内容,先看看怎么在3D空间中实现一个东西,给自己来点成就感。

正好SDK的向导也是这么安排的,呵呵,那我们就继续从向导出发吧,以Tutorial 3为例子。

这个例子主要讲解运用变换矩阵来实现物体的变换,学过图形学或者线性代数的肯定就很容易理解,没学过的,找点这方面的资料看看就可以了。

先看看这个例子实现了什么,编译运行,噢,相比前面两个例子,有耳目一新的感觉,一个三角形绕着Y轴旋转。

相比Tutorial 2,多了2个函数:OnResetDevice和SetupMatrices。其中有注释,中文由我翻译,E文太差,翻译得不好不要B4我啊

public void OnResetDevice(object sender, EventArgs e)

{

Device dev = (Device)sender;

// Turn off culling, so we see the front and back of the triangle

//关闭剔除,所以我们可以看见前面的和背面的三角形

dev.RenderState.CullMode = Cull.None;

// Turn off D3D lighting, since we are providing our own vertex colors

//关闭灯光,因为我们已经提供了自己的颜色

dev.RenderState.Lighting = false;

}

private void SetupMatrices()

{

// For our world matrix, we will just rotate the object about the y-axis.

// Set up the rotation matrix to generate 1 full rotation (2*PI radians)

// every 1000 ms. To avoid the loss of precision inherent in very high

// floating point numbers, the system time is modulated by the rotation

// period before conversion to a radian angle.

// 在世界坐标中,我们要绕Y旋转,建立旋转矩阵产生每秒360度的旋转,为了避免浮点数中的位数造成的时间上的损失,我们在转变为弧度前,强制调整系统时间

int iTime = Environment.TickCount % 1000;

float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;

device.Transform.World = Matrix.RotationY( fAngle );

// Set up our view matrix. A view matrix can be defined given an eye point,

// a point to lookat, and a direction for which way is up. Here, we set the

// eye five units back along the z-axis and up three units, look at the

// origin, and define "up" to be in the y-direction.

//建立观察矩阵,它可以被定义为视点,就像一个摄像机一样。这里我们设置在Z轴-5,Y轴3的位置

device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );

// For the projection matrix, we set up a perspective transform (which

// transforms geometry from 3D view space to 2D viewport space, with

// a perspective divide making objects smaller in the distance). To build

// a perpsective transform, we need the field of view (1/4 pi is common),

// the aspect ratio, and the near and far clipping planes (which define at

// what distances geometry should be no longer be rendered).

//射影矩阵,我们建立一个透视变换(透视变换把3D空间变换到2D视口,造成透视的效果),为了建立透视变换,我们需要视觉空间,(通常是1/4 pi)纵横比 和 远距离剔除(远处物体不渲染)

device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );

}

所有的新东西都在这里了,一共不到10条语句,简单吧?有几点要解释一下。

dev.RenderState.CullMode = Cull.None;

这一句,或许很多人会疑惑,首先了解一下DX的背面剔除功能,和现实中一样,我们同一时间只能够看到一个物体的一面,看不到它的背面,这就是背面剔除。Cull是一个枚举,一共3个值:

CounterClockwise

3

Cull back faces with counterclockwise vertices.

Clockwise

2

Cull back faces with clockwise vertices.

None

1

Do not cull back faces.

如果我们指定了背面剔除的方式,那么我们就看不到物体的背面,这样当三角形转过来的时候我们就看不到转过来的东西,所以设置为none,如果不理解,可以在程序中修改一下这一句话,就能很直观的理解了。

这个例子就这么多内容,当然你也可以把RotateY改为,RotateX, RotateZ等等,可以更加好的理解一下这个例子。

接下来我们来了解一下Mesh。

对我们初学者来说Mesh应该是一个让人激动的东西,让我们来看看介绍:Mesh可以用来储存任何类型的图形数据,但主要用来封装复杂的模型。Mesh类同样也有一些用来提高渲染物体性能的方法。用Mesh你可以从外部文件读入3D的模型例如.3DS文件,这样我们就可以在3D Max中做好模型,然后读入程序,想想看,一个游戏的雏形是不是已经在你脑海了呢?

Mesh对象内部也包含了很多几何体的模型,我们来看看怎么使用它,因为使用它比使用顶点要快捷方便得多,一会你就会深刻体会到。

首先我们要先建立一个Mesh对象 private Mesh mesh = null;

然后mesh = Mesh.Box(device,2.0f,2.0f,2.0f); 这样我们就建立了一个长宽高都为2的立方体,很简单吧?如果你用顶点来建立的话,最快的方法,即使用索引缓冲器,深度缓冲器也要写8个顶点得值,还要SetStreamSource等等繁琐的工作,这一切在Mesh中都替我们完成了。当然,Mesh内置的几何形体还有很多,比如圆柱,茶壶等等,你可以一个一个的试一下。

我们来看看核心的DrawBox函数:

this is equivalent

private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z)

{

angle += 0.01f;

device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);

Material boxMaterial = new Material();

boxMaterial.Ambient = Color.White; //环境色

boxMaterial.Diffuse = Color.White; //漫反射

device.Material = boxMaterial;

mesh.DrawSubset(0);

}

先介绍Matrix.RotationYawPitchRoll方法,看看他的原形:

public static Matrix RotationYawPitchRoll(

float yaw, //偏移量,即绕Y轴的转动角度

float pitch,//斜度,即绕X轴的角度

float roll//滚动,即绕Z的角度

);

材质(materials)描述了这样的一种属性。你可以指定物体如何反射环境光以及散射(diffuse)光线,镜面高光(Specular Highlights)(少后会讨论它)看起来是什么样,以及物体是否完全反射(emit)光线。这里创建了一个新的材质,它的环境颜色(ambient color)(注:环境颜色和环境光的颜色是不同的^_^)和散射颜色值都被设置为白色。使用白色表示它会反射所有的光线。接下来,我们把材质赋予了device的Material属性,这样Direct3D就知道渲染时使用那种材质数据。

这里介绍一点关于光线和色彩的知识:环境色(ambient color),当其为黑色时,表示(环境光)不会影响材质的颜色,当环境色变浅时,它就会照亮材质,并将两种颜色混和起来,从而影响材质的颜色。如何场景中有环境光,那么这些光的颜色和亮度就会控制环境色对于最终材质颜色的影响程度)。把材质改为没有红色成分的颜色(比如绿色)会使物体再次变为黑色(注:因为此时物体不会反射红色,红色的光线被物体全部吸收了),改为含一些红色成分的颜色(比如灰色gray)会使物体呈现深灰色。

Mesh会被分为一系列的子集(subsets)(依据属性缓冲的大小来分配),同时使用一个叫做“DrawSubset”的方法来渲染。使用Mesh类创建的普通图元总是只有一个基于0的子集。所以使用了mesh.DrawSubset(0)

下面附上这个例子的全部代码:

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 Chapter5Code

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private Device device = null;

private Mesh mesh = null;

/// <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);

mesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);

}

private void SetupCamera()

{

device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);

device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 18.0f), new Vector3(), new Vector3(0,1,0));

device.RenderState.Ambient = Color.DarkBlue;

device.Lights[0].Type = LightType.Directional;

device.Lights[0].Diffuse = Color.DarkBlue;

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.CornflowerBlue, 1.0f, 0);

SetupCamera();

device.BeginScene();

// Draw our boxes

DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f);

//DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, 0.0f, 0.0f);

// DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, 0.0f, 0.0f);

// DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, -5.0f, 0.0f);

// DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, -5.0f, 0.0f);

// DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, -5.0f, 0.0f);

// DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 5.0f, 0.0f);

DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, 5.0f, 0.0f);

DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, 5.0f, 0.0f);

device.EndScene();

device.Present();

this.Invalidate();

}

private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z)

{

angle += 0.01f;

device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);

Material boxMaterial = new Material();

boxMaterial.Ambient = Color.White;

boxMaterial.Diffuse = Color.White;

device.Material = boxMaterial;

mesh.DrawSubset(0);

}

/// <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);

}

}

}

}

不知道大家注意到没有,我注释掉了很多DrawBox函数的调用,你也可以把它改过来,运行看看,你会发现一个问题,就是如果你画9个Box和你画一个Box,旋转的速度是不一样的,因为这里并没有像上个例子那样对时间有强制的调整。这个例子里面还有一些是关于灯光的,我们马上就会讲到,其实从字面也可以看出来是什么意思。接下来我们就要去读入我们自己的模型了,激动啊。

2005-4-15

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝网络 版权所有