首页 >> 民生法规

CDirect3D90开发实例之月亮绕着节能

民生法规  2020-10-26 15:17 字号: 大 中 小

一、建立空窗体

新建一个工程,添加引用,并导入名称空间。

加入一个设备对象变量:

private vice device = null;

<相对“十一五”期间增加156%p>添加初始化图形函数,并在这里面对设备对象进行实例化:

public void InitializeGraphics()

{

PresentParameters presentParams = new PresentParameters();

ndowed = true;

apEffect = ip;

toDepthStencilFormat = DepthFormat.D16;

ableAutoDepthStencil = true;

device = new vice(0, rdware, this,  rdwareVertexProcessing, presentParams);

}

当程序执行时,需要绘制场景,代码在这个函数里:

public void Render()

{

// 清空设备,并准备显示下一帧。

ear(rget | uffer, ack , 1.0f, 0);

// 设置照相机的位置

SetupCamera();

//开始场景

ginScene();

if(meshLoaded)

{

nder(meshLoc);

}

dScene();

//显示设备内容。

esent();

}

设置照相机的位置:

private void SetupCamera()

{

ojection = rspectiveFovLH((float) / 4, dth / ight, 1.0f, 1000.00f);

ew = okAtLH(new Vector3(0.0f ,0.0f, 20.0f), new Vector3(0.0f,0.0f, 0.0f), new Vector3(0,1,0));

}

现在改变主函数,调用我们写的初始化函数,并显示场景:

[STAThread]

static void Main()

{

using (Form1 EarthForm = new Form1())

{

itializeGraphics();

ow();

while(eated)

{

nder();

Events();

}

spose();

}

运行程序,会显示一个空的窗体。

二、加入地球:

在这一步里需创建一个3D格对象,来作为要显示的地球,为此,在工程中新加入一个类Earth,此类可以包含所创建的格对象的信息。

加入一些相关变量,含义见注释:

public class Earth : BaseEarth

{

private Material[] mMaterials; //保存材质

private Texture[] mTextures; //保存纹理

private Matrix locationOffset; //用来保存格对象的相对位置

private Mesh mMesh = null; //三角形格对象

private Device meshDevice; //需要显示在哪个设备上。

}

在构造函数中,把Device设备拷贝到私有成员变量,这样就可以在这个类的其它方法内使用它,另外就是把位置变量进行赋值:

public Earth(ref Device device, Matrix location): base(ref device)

{

meshDevice = device;

locationOffset = location;

}

下面这个函数是装入.X文件。

public bool LoadMesh(string meshfile)

{

ExtendedMaterial[] mtrl;

try

{

// 装载文件

mMesh = omFile(meshfile, naged, meshDevice, out mtrl);

// 如果有材质的话,装入它们

if ((mtrl != null) (ngth 0))

{

mMaterials = new Material[ngth];

mTextures = new Texture[ngth];

// 得到材质和纹理

for (int i = 0; i ngth; i++)

{

mMaterials[i] = mtrl[i].Material3D;

if ((mtrl[i].TextureFilename != null) (mtrl[i].TextureFilename != pty))

{

//前面得到的纹理的路径是相对路径,需要保存的是绝对路径,通过应用程序路径可以获得

mTextures[i] = omFile(meshDevice, @"..\..\" + mtrl[i].TextureFilename);

}

}

}

return true;

}

catch

{

return false;

}

}

在这个方法内,使用omFile()这个方法,从给定的文件名中找到.X文件,并装入相关数据,一旦数据格式设置完成,可以从此文件中找到材质和贴图信息,并把它存放在数组中,并通过文件路径,得到纹理文件文件的路径,最后返回真值,如果整个过程出现错误,返回假值。

下面这个Render()方法,是把此对象,即地球显示在设备对象上,此方法较简单,通过变形操作来得到格对象的X,Y,Z坐标,接着设置格对象的材质和纹理,最后,将每个材质和纹理应用到每个格。

public void Render(Matrix worldTransform)

{

/把位置变为世界坐标

rld = ltiply(locationOffset, worldTransform);

//绘制格

for (int i = 0; i ngth; i++)

{

terial = mMaterials[i];

tTexture(0, mTextures[i]);

awSubset(i);

}

}

现在回到窗体代码中,添加引用格对象的相关变量:

private Earth mesh = null;

private Matrix meshLoc;

private bool meshLoaded = false;

在图形初始化函数中,需要对格对象进行初始化。加入下面的代码:

meshLoc = entity;

meshLoc.M41 = 2.0f;

mesh = new Earth(ref device, meshLoc);

if (adMesh(@"..\..\earth.x"))

{

meshLoaded = true;

}

代码第一句把格对象的位置定为原点,接着偏移X轴2个单位,接下来从文件中得到此.X文件。如果成功设置,meshLoaded置为真。注意,这里有一个.X文件,在源代码中有此文件。

在设置相机的函数中,加入一盏灯光:

ghts[0].Type = rectional;

ghts[0].Diffuse = ite;

ghts[0].Direction = new Vector3(0, -1, -1);

ghts[0].Update();

ghts[0].Enabled = true;

此灯光较简单,仅为一个直射型白光灯。

最后,在Render()方法中,调用格对象的Rend及时提高优抚对象补助标准并及时兑现er()方法,以显示地球。

宝宝着凉肚子胀气怎么办
经验宝妈在线解惑奶粉过敏
健康医药资讯
推荐资讯