日期:2014-01-22  浏览次数:20402 次


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.Tran