前面的一篇文章Direct 3D基础介绍了一些基本概念,叙述了如何在显示器上直接绘制具有立体感的2D图形。上面的方法是不现实的,因为预先根据透视原理人工计算出3D物体在显示屏幕上显示的坐标然后再绘制的这种方式如果涉及从不同角度观察的3D物体的话,需要计算的次数会很多。Direct 3D实现3D所采用的方法是首先设计一个仿真真实3D物体的立体模型,然后由计算机根据透视原理计算出每一个角度模型显示在计算机显示器屏幕上的坐标和每个面的颜色,然后显示出来。

把3D场景中的所有3D物体在2D显示器上显示出来的过程叫做渲染。这一过程与使用照相机照相类似。最终将3D物体的3D坐标转化为2D显示器平面坐标及颜色在平面显示器显示出来。渲染过程将对3D场景中的所有3D物体进行世界、观察和投影坐标变换,最终将3D物体的3D坐标变换为2D显示器平面坐标及颜色在平面显示器显示出来。三个坐标变换中的每个变换都可以用一个Matrix结构中的4行4列仿射矩阵来表示,记录3D场景中的所有3D物体的平移、缩放、旋转等操作。渲染前需要进行建模,即制作被渲染的3D物体模型,用来搭建3D场景。一个3D物体一般包括若干曲面,任意曲面都可以由若干三角形平面组成,一个三角形平面由三角形的三个顶点确定。所以3D物体模型就是用顶点定义的3D物体。在建模阶段需要定义3D物体所有顶点的位置以及属性。建模可以在程序中完成,也可以用专用软件比如说3D Max完成后再导进去。每个模型都有自己的坐标系统,称为建模坐标系统,坐标系统所代表空间称为建模空间。建模必须首先定义3D模型以及建模坐标所表示的顶点坐标,这些顶点经过三个变换,最终转换为平面显示器坐标,同时,还可能定义3D模型的其他元素,比如说颜色、面的法线等。

世界变换:使用建模创建的3D模型搭建3D场景。观察变换:也称为取景变换,从场景的世界空间中取得感兴趣的部分场景。投影变换:将摄像机空间中的3D模型转换为能够在显示器屏幕上显示的具有立体感的平面图形。

要在屏幕中显示一个三角形,三角形的三个顶点必须要经过三个变换——世界、观察和投影变换。当3D程序的运行状态发生变化时,比如说窗口状态和全屏状态切换,Device类对象的参数必然要发生变化,这些参数必须被重新设置。一般情况下,可以在OnResetDevice和Render方法中修改Device类对象的参数。在程序中不改变的Device类对象的参数一般在OnResetDevice方法中修改;在程序中需要改变的Device类对象的参数一般要在Render方法中修改,这样做可以加快渲染的速度。为了使三角形旋转,每次渲染三角形之前,必须根据三角形在世界空间中的新位置为其指定新的世界变换矩阵。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 显示三角形
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;float Angle = 0, ViewZ = -6.0f;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;               //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 3,dev, 0, CustomVertex.TransformedColored.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;       //取消背面剔除dev.RenderState.Lighting = false;          //取消灯光SetupMatrices();      //在程序运行期间,Device的3个变换不改变,因此放在此处}public void Render()      //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null) //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);device.BeginScene();  //开始渲染      SetupMatrices();        //在程序运行期间,Device的2个变换参数要改变,因此放在此处device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionColored.Format;device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);device.EndScene();     //渲染结束device.Present();     //更新显示区域,把后备缓存的图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionColored[] verts =             //建模(CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);verts[0].Position = new Vector3(-1.0f, -1.0f, 0.0f);   //顶点0位置,注意为Vector3verts[0].Color = System.Drawing.Color.Aqua.ToArgb();   //顶点0颜色verts[1].Position = new Vector3(1.0f, -1.0f, 0.0f);    //顶点1位置verts[1].Color = System.Drawing.Color.Brown.ToArgb();verts[2].Position = new Vector3(0.0f, 1.0f, 0.0f);    //顶点2位置verts[2].Color = System.Drawing.Color.LightPink.ToArgb();vertexBuffer.Unlock();}private void SetupMatrices()     //修改Device的3个变换{int iTime = Environment.TickCount % 1000;Angle = iTime * (2.0f * (float)Math.PI) / 1000.0f;device.Transform.World = Matrix.RotationY(Angle);device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, ViewZ),new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,1.0f, 1.0f, 100.0f);}private void Form1_KeyDown(object sender, KeyEventArgs e){switch (e.KeyCode)                //e.KeyCode是键盘每个键的编号{case Keys.Left:            //Keys.Left是左箭头键编号,三角形沿Y轴左转Angle += 0.1F;break;case Keys.Right:            //三角形沿Y轴右转Angle -= 0.1F;break;case Keys.Down:          //三角形离观察者越来越远ViewZ += 0.1F;break;case Keys.Up:                //三角形离观察者越来越近ViewZ -= 0.1F;break;}}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();Show();Render();}}
}

显示立方体,首先创建立方体模型,设置8个顶点的坐标,首先创建立方体的一个面,其他的面用世界变换将第一个面变换到立方体的相应位置,最终实现立方体。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 显示立方体
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;float Angle = 0, ViewZ = -6.0f;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;               //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;       //阴影部分是所作修改,正方形有6个顶点vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 6,dev, 0, CustomVertex.TransformedColored.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;//背面剔除方式为只显示顺时针三角形,因为正方体应该只看到外表面dev.RenderState.CullMode = Cull.CounterClockwise;dev.RenderState.Lighting = false;                //取消灯光}public void Render()     //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null)                      //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.LightBlue, 1.0f, 0);device.BeginScene();                        //开始渲染SetupMatrices();device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionColored.Format;device.Transform.World = Matrix.Translation(0, 0, -1);//沿Z轴向观察者方向移动1个单位device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);    //绘制正前面//旋转180度是为了从外侧看,按顺时针方向绘制三角形,因背面剔除打开,内侧不被看到device.Transform.World = Matrix.RotationY((float)Math.PI) * Matrix.Translation(0, 0, 1);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);    //绘制正后面device.Transform.World =Matrix.RotationY(-(float)Math.PI / 2) * Matrix.Translation(1, 0, 0);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);     //绘制右侧面device.Transform.World =Matrix.RotationY((float)Math.PI / 2) * Matrix.Translation(-1, 0, 0);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);        //绘制左侧面device.Transform.World =Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(0, 1, 0);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);     //绘制下面device.Transform.World =Matrix.RotationX(-(float)Math.PI / 2) * Matrix.Translation(0, -1, 0);device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);    //绘制上面device.EndScene();                                        //渲染结束device.Present(); //更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionColored[] verts =(CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);verts[0].Position = new Vector3(-1.0f, -1.0f, 0.0f);  //顶点0位置,注意为Vector3verts[0].Color = System.Drawing.Color.Aqua.ToArgb();          //顶点0颜色verts[1].Position = new Vector3(1.0f, 1.0f, 0.0f);          //顶点1位置verts[1].Color = System.Drawing.Color.Brown.ToArgb();verts[2].Position = new Vector3(1.0f, -1.0f, 0.0f);       //顶点2位置verts[2].Color = System.Drawing.Color.LightPink.ToArgb();verts[3].Position = new Vector3(-1.0f, -1.0f, 0.0f);      //顶点3位置verts[3].Color = System.Drawing.Color.Aqua.ToArgb();        //顶点3颜色verts[4].Position = new Vector3(-1.0f, 1.0f, 0.0f);         //顶点4位置verts[4].Color = System.Drawing.Color.Red.ToArgb();verts[5].Position = new Vector3(1.0f, 1.0f, 0.0f);          //顶点5位置verts[5].Color = System.Drawing.Color.Brown.ToArgb();vertexBuffer.Unlock();}private void SetupMatrices()      //修改Device的3个变换{device.Transform.World = Matrix.RotationY(Angle);  //世界变换矩阵,沿Y轴旋转device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 3.0f, ViewZ),//观察变换矩阵new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,1.0f, 1.0f, 100.0f);       //投影变换语句仍可以放到OnResetDevice方法中}private void Form1_KeyDown(object sender, KeyEventArgs e){switch (e.KeyCode)              //e.KeyCode是键盘每个键的编号{case Keys.Left:            //Keys.Left是左箭头键编号,三角形沿Y轴左转Angle += 0.1F;break;case Keys.Right:            //三角形沿Y轴右转Angle -= 0.1F;break;case Keys.Down:          //三角形离观察者越来越远ViewZ += 0.1F;break;case Keys.Up:                //三角形离观察者越来越近ViewZ -= 0.1F;break;}}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

也可以使用顶点索引来绘制立方体,可以极大地简化程序设计。

首先定义八个顶点,由于每个面要用两个三角形进行渲染,所以应该用12个三角形进行渲染立方体,在渲染每个面的正方形时,应该保证从每个面的外侧看,渲染这个面的两个三角形应该是顺时针方向。比如说,立方体前面的正方形,A、B、C、D四个点,渲染的顺序是ABC和CBD。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 顶点索引
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;float Angle = 0, ViewZ = -6.0f;IndexBuffer indexBuffer = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;               //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;       //阴影部分是所作修改,正方体有8个顶点vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 8,dev, 0, CustomVertex.TransformedColored.Format, Pool.Default);indexBuffer = new IndexBuffer(typeof(int), 36, dev, 0, Pool.Default);  //顶点索引vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);indexBuffer.Created += new EventHandler(indexBuffer_Created);this.OnCreateVertexBuffer(vertexBuffer, null);this.indexBuffer_Created(indexBuffer, null);}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;//背面剔除方式为只显示顺时针三角形,因为正方体应该只看到外表面dev.RenderState.CullMode = Cull.CounterClockwise;dev.RenderState.Lighting = false;               //取消灯光}public void Render()     //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null)          //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);device.BeginScene();         //开始渲染SetupMatrices();device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionColored.Format;device.Indices = indexBuffer;device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);device.EndScene();               //渲染结束device.Present();//更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionColored[] verts =
(CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);verts[0].Position = new Vector3(-1.0f, 1.0f, 1.0f);     //顶点0位置,注意为Vector3verts[0].Color = System.Drawing.Color.Aqua.ToArgb();         //顶点0颜色verts[1].Position = new Vector3(1.0f, 1.0f, 1.0f);              //顶点1位置verts[1].Color = System.Drawing.Color.Brown.ToArgb();verts[2].Position = new Vector3(-1.0f, -1.0f, 1.0f);          //顶点2位置verts[2].Color = System.Drawing.Color.LightPink.ToArgb();verts[3].Position = new Vector3(1.0f, -1.0f, 1.0f);           //顶点3位置verts[3].Color = System.Drawing.Color.Red.ToArgb();             //顶点3颜色verts[4].Position = new Vector3(-1.0f, 1.0f, -1.0f);            //顶点4位置verts[4].Color = System.Drawing.Color.Green.ToArgb();verts[5].Position = new Vector3(1.0f, 1.0f, -1.0f);           //顶点5位置verts[5].Color = System.Drawing.Color.Black.ToArgb();verts[6].Position = new Vector3(-1.0f, -1.0f, -1.0f);         //顶点6位置verts[6].Color = System.Drawing.Color.LightPink.ToArgb();verts[7].Position = new Vector3(1.0f, -1.0f, -1.0f);          //顶点7位置verts[7].Color = System.Drawing.Color.Red.ToArgb();vertexBuffer.Unlock();}private void SetupMatrices()      //修改Device的3个变换{device.Transform.World = Matrix.RotationY(0);  //世界变换Vector3 v1 = new Vector3(0.0f, 0.0f, -5.0f);     //下句使v1点分别沿Y轴和X轴旋转v1.TransformCoordinate(Matrix.RotationYawPitchRoll(Angle, ViewZ, 0));device.Transform.View = Matrix.LookAtLH(v1, new Vector3(0.0f, 0.0f, 0.0f),new Vector3(0.0f, 1.0f, 0.0f));   //观察变换device.Transform.Projection =                //透视变换Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);}private void Form1_KeyDown(object sender, KeyEventArgs e){switch (e.KeyCode)             //e.KeyCode是键盘每个键的编号{case Keys.Left:            //Keys.Left是左箭头键编号,三角形沿Y轴左转Angle += 0.1F;break;case Keys.Right:            //三角形沿Y轴右转Angle -= 0.1F;break;case Keys.Down:          //三角形离观察者越来越远ViewZ += 0.1F;break;case Keys.Up:                //三角形离观察者越来越近ViewZ -= 0.1F;break;}}void indexBuffer_Created(object sender, EventArgs e){    //下面数组每3个数表示一个三角形的索引,每2个三角形绘制1个面,int[] index =      //按顺序分别绘制前面、右面、上面、左面、后面和下面{ 4, 5, 6, 5, 7, 6, 5, 1, 7, 7, 1, 3, 4, 0, 1, 4, 1, 5, 2, 0, 4, 2, 4, 6, 3, 1, 0, 3, 0, 2, 2, 6, 7, 2, 7, 3 };int[] indexV = (int[])indexBuffer.Lock(0, 0);for (int i = 0; i < 36; i++){indexV[i] = index[i];}indexBuffer.Unlock();}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();this.Show();Render();}}
}

任意复杂的曲面都可以由若干三角形定义的平面组成。创建一个空心圆柱体,可以考虑这样分解,将上下两个圆形底面的周长进行n等分,上下相对应的等分点用线连接,将空心圆柱体外表面划分为n个小矩形,每个小矩形由两个三角形平面组成。其实,可以利用建模工具比如说3D Max,创建复杂的立体模型,然后转换为Direct 3D能够识别的.x文件,并将其显示在显示器屏幕上。假设空心圆柱体的两个圆形底面和建模坐标系的XZ平面平行,两个圆形底面的圆心连线和建模坐标的Y轴重合,圆心连线的中点在建模坐标原点,两个圆形底面的Y坐标分别为1和-1。空心圆柱体的柱表面可以近似认为是由若干正方形组成,将两个圆形底面的圆心角平分为50份,那么就可以用50个正方形来近似这个立体空心圆柱体。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;namespace 旋转空心园柱
{public partial class Form1 : Form{private Device device = null;bool pause = false;VertexBuffer vertexBuffer = null;public Form1(){InitializeComponent();}public bool InitializeGraphics(){try{PresentParameters presentParams = new PresentParameters();presentParams.Windowed = true;                //不是全屏显示,在一个窗口显示presentParams.SwapEffect = SwapEffect.Discard;       //后备缓存交换的方式presentParams.EnableAutoDepthStencil = true;            //允许使用自动深度模板测试//深度缓冲区单元为16位二进制数presentParams.AutoDepthStencilFormat = DepthFormat.D16;device = new Device(0, DeviceType.Hardware, this,   //建立设备类对象CreateFlags.SoftwareVertexProcessing, presentParams);//设置设备重置事件(device.DeviceReset)事件函数为this.OnResetDevicedevice.DeviceReset += new System.EventHandler(this.OnResetDevice);this.OnCreateDevice(device, null);//自定义方法,初始化Device的工作放到这个方法中this.OnResetDevice(device, null);//调用设备重置事件(device.DeviceReset)事件函数}        //设备重置事件函数要设置Device参数,初始函数中必须调用该函数catch (DirectXException){return false;}return true;}public void OnCreateDevice(object sender, EventArgs e){Device dev = (Device)sender;vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionColored), 100, dev,Usage.WriteOnly, CustomVertex.TransformedColored.Format, Pool.Default);vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);this.OnCreateVertexBuffer(vertexBuffer, null);}public void OnResetDevice(object sender, EventArgs e){Device dev = (Device)sender;dev.RenderState.CullMode = Cull.None;       //取消背面剔除dev.RenderState.Lighting = false;          //取消灯光}public void Render()     //渲染方法,本方法没有任何渲染代码,可认为是渲染方法的框架{if (device == null) //如果未建立设备对象,退出return;if (pause)return;device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.WhiteSmoke, 1.0f, 0);device.BeginScene();//开始渲染SetupMatrices();          //device.SetStreamSource(0, vertexBuffer, 0);device.VertexFormat = CustomVertex.PositionColored.Format;device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, (4 * 25) - 2);device.EndScene();//渲染结束device.Present();//更新显示区域,把后备缓存的D图形送到图形卡的显存中显示}private void Form1_Paint(object sender, PaintEventArgs e){this.Render();}private void Form1_Resize(object sender, EventArgs e){pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);}public void OnCreateVertexBuffer(object sender, EventArgs e){CustomVertex.PositionColored[] verts =(CustomVertex.PositionColored[])vertexBuffer.Lock(0, 0);for (int i = 0; i < 50; i++){float theta = (float)(2 * Math.PI * i) / 49;verts[2 * i].Position = new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta));verts[2 * i].Color = System.Drawing.Color.LightPink.ToArgb();verts[2 * i + 1].Position = new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta));verts[2 * i + 1].Color = System.Drawing.Color.LightPink.ToArgb();}vertexBuffer.Unlock();}private void SetupMatrices(){device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 3000.0f);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));device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 100.0f);}private void Form1_Load(object sender, EventArgs e){InitializeGraphics();Show();Render();}}
}

DirectX 3D图形相关推荐

  1. DirectX 3D学习笔记(一)

    DirectX 3D学习笔记(一) Ⅰ.渲染状态 设备的渲染状态控制Direct3D设备的光栅化组件的行为.通过改变光栅渲染状态属性,可以设置使用何种方式来进行渲染着色,以及如何进行雾化等. 在Dir ...

  2. DirectX 3D编程及其最简单例子

    原文地址:http://blog.csdn.net/handsome0916/article/details/4636825 界面编程一直是程序员的一个比较高的门槛,Windows上最强大的3D库Di ...

  3. VB6编程:DirectX 2D图形学习日志20伽玛校正

    VB6编程:DirectX 2D图形学习日志20伽玛校正 教程下载地址:https://download.csdn.net/download/gosub60/13696651 作用:通过按↑和下方向键 ...

  4. 图形性能测试软件,3DMark05 Build 1.2.0『最为普及的3D图形卡性能测试工具』

    自1998年发布第一款3DMARK图形测试软件至今,3DMARK已经逐渐成长为一款最为普及的3D图形卡性能基准测试软件.3DMARK的一系列版本以简单清晰的操作界面和公正准确的3D图形测试流程赢得了越 ...

  5. 实时3D图形技术的进化历史2

    那么,在上次最后介绍的是可编程着色器架构(Programmable Shader Architecture),最早支持它的DirectX是2000年末发表的DirectX 8. 作为对应DirectX ...

  6. 【西川善司的3D图形技术连载】GPU和Shader技术的基础知识(1~8回)

    本连载的主要目的,是介绍最新的PC和GAME所使用的最新3D图形技术的发展趋势. 暂时的方针是,首先是考虑介绍比较新的PC Game和PS3,XBOX360等新时代游戏机的游戏所采用的技术. 那么首先 ...

  7. Direct9到Direct10 --- 玩的就是3D图形

    我先让大家来比较一下Direct9和Direct10在3D图形上的不同表现: 下面是DXD9: 下面是DXD10: 我们可以看到天上的云彩更逼真了,而水的处理简直就是传神,跟真的一样啊!! 下面是DX ...

  8. 3D图形的概念和渲染管线(Render Pipeline)

    3D图形的概念和渲染管线(Render Pipeline) 前面介绍了3D图形历史,接下来要解说的是3D图形的处理流程. 3D图形管线的流程图 图1是3D图形的流程模型.这个虽然是对应DirectX ...

  9. 西川善司的3D图形技术概念和渲染管线的处理

    from: http://psv.tgbus.com/news/ynzx/201305/20130528094843.shtml 3D图形技术概念和渲染管线的处理 一:3D图形的概念 图1是3D图形的 ...

  10. [西川善司]3D图形技术概念和渲染管线的处理

    翻译 Trace校对&注解 千里马肝: http://www.opengpu.org/forum.php?mod=viewthread&tid=7376&extra=page% ...

最新文章

  1. 从linux小白到进大厂,我是怎么做到的?
  2. dhcp snooping+IPSG的一些理解
  3. ionic ui框架及creator使用帮助
  4. 关注CIO:IT运维如何实现“向管理要效益”(转载)
  5. 三级菜单 python_python三级菜单
  6. url安全处理函数+php,php常用的url处理函数汇总
  7. ES6(Module 模块化)
  8. python中赋值与c语言区别,python中赋值与c语言区别
  9. android ukey,Tenorshare 4uKey for Android
  10. JavaCV最小依赖
  11. distpicker.js 三级联动,修改地址时设置默认值
  12. 微信ubuntu版服务器,Ubuntu 18.04 安装微信(Linux通用)
  13. 如何甄别应聘者简历的包装程度?
  14. PCA(主成分分析法)的理解笔记及算法的实现
  15. 人教版初中计算机学什么,人教版初中信息技术教案全集.pdf
  16. Neo4j-Graph Database Internals
  17. 直流电机驱动模块开发,为电子设备提供动力之源
  18. ASP.NET MVC 音乐商店完整项目示例
  19. python 课程体系
  20. 对菜单进行快捷键设置

热门文章

  1. Windows下打开.jar文件的方式
  2. 时钟芯片S35390A
  3. 中国象棋-单机游戏-微信小程序的项目开发流程详解
  4. C/C++编程:仿函数
  5. openCV利用航拍相机从底部向上扫描物体拼接全景图
  6. word论文页码从任意页开始编号
  7. matlab ga工具箱 使用教程,MATLAB7.0 GA工具箱详细讲解及实例演示.pdf
  8. 911 S5代理设置
  9. win7 开WiFi共享
  10. 加性高斯白噪声 AWGN