新聞中心
C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C# Direct3D模型,包括介紹改成PositionColored格式等方面。

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,網(wǎng)站建設(shè)、做網(wǎng)站負(fù)責(zé)任的成都網(wǎng)站制作公司!
什么是真正的3D世界呢?或者說C# Direct3D模型是什么樣的呢?
C# Direct3D模型由World 空間,View 空間和光源組成。World空間就像我們現(xiàn)實(shí)生活中的空間一樣,我們可以把物體放在這個(gè)空間的任何地方。但僅僅這樣是不夠的。因?yàn)楝F(xiàn)實(shí)中,我們是通過眼睛來看事物的,存在的東西不一定我們可以看得到,太遠(yuǎn)的東西看不到,被擋住的東西看不到,太近的東西也看不到,在我們視角以外的東西一樣看不到。在 Direct3D中用camera來模擬我們的眼睛。
另外還有一個(gè)重要概念,transform,變換。在world空間中,我們可以把物體移動(dòng),旋轉(zhuǎn),這需要通過變換來實(shí)現(xiàn)。通過camera來決定我們最終能看到的那些東西時(shí)也是經(jīng)過變換。在Direct3D中,變換是通過4x4的矩陣來實(shí)現(xiàn)的。
不是搞算法的人沒有必要了解矩陣的具體內(nèi)容,可以通過Maxtrix中的靜態(tài)函數(shù)來創(chuàng)建矩陣:
◆Matrix.RotationX()
◆Matrix.RotationY()
◆Matrix,RotationZ()創(chuàng)建分別圍繞X,Y,Z軸旋轉(zhuǎn)的矩陣。
◆Matrix.Translation()用來創(chuàng)建移動(dòng)物體的矩陣。
而與camera相關(guān)的變換有兩個(gè),View Transform,和Projection Transform.View Transform用來定義camera的信息。而Projection Transform用來定義我們的可見范圍,也就是說那些物體會(huì)被繪制到屏幕上去。
另外一個(gè)很重要的概念就是光源,在真實(shí)世界中,沒有光的情況下我們是看不到東西的,即使他存在。而且我們看到的物體的顏色/亮度,與光源的顏色/亮度,與他本身的材質(zhì)/顏色都有關(guān)系。在Direct3D中也是如此,一旦一個(gè)物體在我們的可視范圍內(nèi),并且沒有被遮擋住,那么他能否可見,以及什么顏色/亮度,就由光源,物體的材質(zhì)來決定了。
下面就開始我們的例子。首先把我們的代碼中的頂點(diǎn)格式,改成PositionColored格式。注意坐標(biāo)哦,這里的坐標(biāo)可不是屏幕坐標(biāo)系了,而是World坐標(biāo)系,是左手笛卡爾3D坐標(biāo)系。***步的代碼如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- #endregion
- namespace Exam2
- {
- partial class Form1 : Form
- {
- private Device device = null;
- public Form1()
- {
- InitializeComponent();
- this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true );
- }
- public void InitializeGraphics()
- {
- PresentParameters para = new PresentParameters();
- para.Windowed = true;
- para.SwapEffect = SwapEffect.Discard;
- device = new Device( 0, DeviceType.Hardware, this,
CreateFlags.HardwareVertexProcessing, para );- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- device.Clear( ClearFlags.Target, Color.Blue, 0.0f, 0 );
- CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
- verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
- verts[0].Color = Color.Aqua.ToArgb();
- verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
- verts[1].Color = Color.Black.ToArgb();
- verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
- verts[2].Color = Color.Purple.ToArgb();
- device.BeginScene();
- device.VertexFormat = CustomVertex.PositionColored.Format;
- device.DrawUserPrimitives( PrimitiveType.TriangleList, 1, verts );
- device.EndScene();
- device.Present();
- this.Invalidate();
- }
- }
- }
以上介紹C# Direct3D模型。
本文題目:C#Direct3D模型概論
本文網(wǎng)址:http://fisionsoft.com.cn/article/cdoeiig.html


咨詢
建站咨詢
