王朝网络
分享
 
 
 

DirectX9 3D 快速上手 1

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

DirectX9 3D 快速上手 1

by sssa2000

4/13/2005

由于项目需要,不得不介入到以前从没有接触过的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

}

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

}

}

}

}

}

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