DirectX9 3D 快速上手 1

由于项目需要,不得不介入到以前从没有接触过的3D编程,我选择了DX9,当然这也是项目需要,既然是要求快速上手,那么就最好选择RAD的开发工具了,显然用MS的东西比较保险一些,我这个人怕麻烦,最怕出一些莫名其妙的错误了,所以这里C#是比较好的选择了,当然如果你用VB.net的话也不错,反正这两个差不多,个人偏爱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
        }
        ///
        /// The main entry point for the application.
        ///
        static void MainA()
        {
            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();
                }
            }
        }
    }
}

 转自:http://www.chinagcn.com/blog/?uid-145-action-viewspace-itemid-1780

posted on 2008-11-17 11:03  DRT3D  阅读(272)  评论(0编辑  收藏  举报