由於項目需要,不得不介入到以前從沒有接觸過的3D編程,我選擇了DX9,當然這也是項目需要,既然是要求快速上手,那麼就最好選擇RAD的開發工具了,顯然用MS的東西比較保險一些,我這個人怕麻煩,最怕出一些莫名其妙的錯誤了,所以這裡C#是比較好的選擇了,當然如果你用VB的話也不錯,反正這兩個差不多,個人偏愛C#一些。
廢話少說,切入正題。
要使用DX,那麼第一件事情就是建立裝置。比起DirectDraw,3D的裝置建立還是稍微簡單一些的:
public Device (
System.Int32 adapter , //表示我們將要使用哪個物理圖形卡。一般用0
Microsoft.DirectX.Direct3D.DeviceType deviceType ,// 建立那種類型的device。
System.Windows.Forms.Control renderWindow , // rendrWindow表示把裝置綁定到的視窗。
Microsoft.DirectX.Direct3D.CreateFlags behaviorFlags, //描述裝置建立之後的行為Microsoft.DirectX.Direct3D.PresentParameters presentationParameters )// 裝置把資料呈現到顯示器的方式
一共5個參數,看一下程式碼片段:
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true; //視窗模式
presentParams.SwapEffect = SwapEffect.Discard;// SwapEffect成員用於控制緩衝交換的行為。如果選擇了SwapEffect.Flip,運行時會建立額外的後備緩衝
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
到這裡裝置的建立就已經完成了,對於其中相關參數的含義,可以參考DirectX的協助。
現在看看DX SDK內建的Tutorial1,是不是很容易了?託管環境為我們作了很多事情,不過在這個例子裡面沒有體現出來,感謝上帝。在這個例子裡面Render()是關鍵。
private void Render()
{
if (device == null)
return;
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
// Rendering of scene objects can happen here
//End the scene
device.EndScene();
device.Present();
}
Render是渲染的進入點,每一幀渲染都要調用這個函數。BeginScene()/EndScene()函數對,分別在渲染前後調用。BeginScene()會使系統檢查內部資料結構和渲染表面的可用性有效性。這裡在BeginScene()/EndScene() 中間沒有做任何事情,所以就沒有什麼被渲染,這個例子可以被當作模板使用。
下面是Tutorial1的全部代碼:
//-----------------------------------------------------------------------------
// File: CreateDevice.cs
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DeviceTutorial
{
public class CreateDevice : Form
{
// Our global variables for this project
Device device = null; // Our rendering device
public CreateDevice()
{
// Set the initial size of our form
this.ClientSize = new System.Drawing.Size(400,300);
// And it's caption
this.Text = "D3D Tutorial 01: CreateDevice";
}
public bool InitializeGraphics()
{
try
{
// Now let's setup our D3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed=true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
return true;
}
catch (DirectXException)
{
return false;
}
}
private void Render()
{
if (device == null)
return;
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
//Begin the scene
device.BeginScene();
// Rendering of scene objects can happen here
//End the scene
device.EndScene();
device.Present();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
this.Render(); // Render on painting
}
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
this.Close(); // Esc was pressed
}
///<summary>
/// The main entry point for the application.
///</summary>
static void Main()
{
using (CreateDevice frm = new CreateDevice())
{
if (!frm.InitializeGraphics()) // Initialize Direct3D
{
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.");
return;
}
frm.Show();
// While the form is still valid, render and process messages
while(frm.Created)
{
frm.Render();
Application.DoEvents();
}
}
}
}
}
.net