• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            Managed DirectX --- create Device

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

            ????????????}

            ????????}

            ????}

            }


            1 creating the Direct3D device
            ?? PresentParameters object that is used to set the windows display characteristics.????? presentParameters 用來設置windows的特征. 她的windowed設為? true? 表示以窗口顯示,沒有菜單和子窗口,但是有最大,最小和關閉button.
            2 Device的構造函數中,指定了hardware device是優先的,頂點的處理有軟件來處理.但是如果我們有好的顯卡且我們設置 ?CreateFlags.HardwareVertexProcessing參數,則我們有更好的性能.
            3 ?Application.DoEvents(); 不能沒有,否則窗口不接受消息,一直render().
            4 Device.Clear method使窗口顏色被設置為blue. Device.BeginScene則真正開始render. the EndScene and Present methods 表示完成render.

            包含檢查硬件的支持:
            //------------------------------------------------------------------------------
            //???????????Name:?dx9cs_initialization.cs
            //?????????Author:?Kevin?Harris
            //??Last?Modified:?06/15/05
            //????Description:?This?sample?demonstrates?how?to?initialize?Direct3D.
            //------------------------------------------------------------------------------

            using?System;
            using?System.Drawing;
            using?System.Windows.Forms;
            using?Microsoft.DirectX;
            using?Microsoft.DirectX.Direct3D;

            namespace?DX9Sample
            {
            ????
            public?class?DX9Form?:?System.Windows.Forms.Form
            ????
            {
            ????????
            private?Device?d3dDevice?=?null;

            ????????
            public?DX9Form()
            ????????
            {
            ????????????
            this.ClientSize?=?new?System.Drawing.Size(?640,?480?);
            ????????????
            this.Text?=?"Direct3D?(DX9/C#)?-?Initialization";
            ????????????
            this.SetStyle(?ControlStyles.AllPaintingInWmPaint?|?ControlStyles.Opaque,?true?);
            ????????}


            ????????
            protected?override?void?OnPaint(System.Windows.Forms.PaintEventArgs?e)
            ????????
            {
            ????????????
            this.Render();
            ????????????
            this.Invalidate();
            ????????}


            ????????
            protected?override?void?OnKeyDown(System.Windows.Forms.KeyEventArgs?e)
            ????????
            {
            ????????????
            switch(?e.KeyCode?)
            ????????????
            {
            ????????????????
            case?System.Windows.Forms.Keys.Escape:
            ????????????????????
            this.Dispose();
            ????????????????????
            break;
            ????????????}

            ????????}


            ????????
            protected?override?void?Dispose(bool?disposing)
            ????????
            {
            ????????????
            base.Dispose(?disposing?);
            ????????}


            ????????
            static?void?Main()?
            ????????
            {
            ????????????
            using(?DX9Form?frm?=?new?DX9Form()?)
            ????????????
            {
            ????????????????frm.Show();
            ????????????????frm.Init();
            ????????????????Application.Run(?frm?);
            ????????????}

            ????????}


            ????????
            ///?<summary>
            ????????
            ///?This?method?basically?creates?and?initialize?the?Direct3D?device?and
            ????????
            ///?anything?else?that?doens't?need?to?be?recreated?after?a?device?
            ????????
            ///?reset.
            ????????
            ///?</summary>

            ????????private?void?Init()?
            ????????
            {
            ????????????
            //?Does?the?hardware?support?a?16-bit?z-buffer?
            ????????????if(?!Manager.CheckDeviceFormat(?Manager.Adapters.Default.Adapter,?
            ???????????????????????????????????????????DeviceType.Hardware,
            ???????????????????????????????????????????Manager.Adapters.Default.CurrentDisplayMode.Format,?
            ???????????????????????????????????????????Usage.DepthStencil,
            ???????????????????????????????????????????ResourceType.Surface,
            ???????????????????????????????????????????DepthFormat.D16?)?)
            ????????????
            {
            ????????????????
            //?POTENTIAL?PROBLEM:?We?need?at?least?a?16-bit?z-buffer!
            ????????????????return;
            ????????????}


            ????????????
            //
            ????????????
            //?Do?we?support?hardware?vertex?processing??if?so,?use?it.?
            ????????????
            //?If?not,?downgrade?to?software.
            ????????????
            //

            ????????????Caps?caps?
            =?Manager.GetDeviceCaps(?Manager.Adapters.Default.Adapter,?
            ???????????????????????????????????????????????DeviceType.Hardware?);
            ????????????CreateFlags?flags;

            ????????????
            if(?caps.DeviceCaps.SupportsHardwareTransformAndLight?)
            ????????????????flags?
            =?CreateFlags.HardwareVertexProcessing;
            ????????????
            else
            ????????????????flags?
            =?CreateFlags.SoftwareVertexProcessing;

            ????????????
            //
            ????????????
            //?Everything?checks?out?-?create?a?simple,?windowed?device.
            ????????????
            //

            ????????????PresentParameters?d3dpp?
            =?new?PresentParameters();

            ????????????d3dpp.BackBufferFormat???????
            =?Format.Unknown;
            ????????????d3dpp.SwapEffect?????????????
            =?SwapEffect.Discard;
            ????????????d3dpp.Windowed???????????????
            =?true;
            ????????????d3dpp.EnableAutoDepthStencil?
            =?true;
            ????????????d3dpp.AutoDepthStencilFormat?
            =?DepthFormat.D16;
            ????????????d3dpp.PresentationInterval???
            =?PresentInterval.Immediate;?

            ????????????d3dDevice?
            =?new?Device(?0,?DeviceType.Hardware,?this,?flags,?d3dpp?);

            ????????????
            //?Register?an?event-handler?for?DeviceReset?and?call?it?to?continue
            ????????????
            //?our?setup.
            ????????????d3dDevice.DeviceReset?+=?new?System.EventHandler(?this.OnResetDevice?);
            ????????????OnResetDevice(?d3dDevice,?
            null?);
            ????????}


            ????????
            ///?<summary>
            ????????
            ///?This?event-handler?is?a?good?place?to?create?and?initialize?any?
            ????????
            ///?Direct3D?related?objects,?which?may?become?invalid?during?a?
            ????????
            ///?device?reset.
            ????????
            ///?</summary>

            ????????public?void?OnResetDevice(object?sender,?EventArgs?e)
            ????????
            {
            ????????????
            //?This?sample?doens't?create?anything?that?requires?recreation?
            ????????????
            //?after?the?DeviceReset?event.
            ????????}


            ????????
            private?void?Render()
            ????????
            {
            ????????????d3dDevice.Clear(?ClearFlags.Target?
            |?ClearFlags.ZBuffer,?
            ?????????????????????????????Color.FromArgb(
            255,?0,?255,?0),?1.0f,?0?);

            ????????????d3dDevice.BeginScene();
            ????????????
            ????????????
            //?Render?geometry?here
            ????
            ????????????d3dDevice.EndScene();

            ????????????d3dDevice.Present();
            ????????}

            ????}

            }


            posted on 2006-05-09 18:31 夢在天涯 閱讀(1164) 評論(1)  編輯 收藏 引用 所屬分類: DirectX

            評論

            # re: Managed DirectX --- create Device 2006-05-17 17:31 夢在天涯

            Managed DirectX是對DirectX大部分功能的托管封裝,可以用任何支持.NET的語言開發。MDX只對DirectX做了非常低層次的封裝,因此保持了用COM接口DirectX開發時的大部分原貌。MDX的性能是不用擔心的,因為它還是象COM DirectX一樣提供對硬件層次的訪問,大部分功能都是在你的顯卡/網卡/聲卡上起作用的,托管部分只是它的接口。事實證明MDX在DirectX Graphics中的性能與COM接口的DirectX不相上下。既然MDX的開發方式、API和性能都與COM接口的DirectX差不多,那為什么要用MDX呢?我自己對這個問題的回答是:

            1、托管代碼的對象模型更好。MDX基于類庫的組織結構,比用COM接口的處理方式更方便。
            2、用MDX,一般不用操心資源釋放的問題。很大一部分資源釋放的操作,都被封裝好了。
            3、與更多現代技術結合得更好。我們可以讓DirectX程序使用XML、WebService和智能客戶端等技術。

            Direct3D程序最基本的流程是:創建Windows窗口、創建設備、處理消息循環、物體圖形顯示、退出和清理。在MDX的世界里,窗口創建和處理消息循環我們交給Windows Forms模型來做,剩下最主要的任務就是創建設備和物體圖形顯示。Direct3D設備是Direct3D開發最基本的入口,它定義了Direct3D所有的繪圖組件,大部分的操作都需要從Direct3D設備開始。創建設備需要設定幾個參數,包括顯卡序號、設備類型、所屬窗口、3D運算方式等。除了這些信息外,還需要一個PresentParameters類型的參數,其中定義了Direct3D設備所需的相關信息。下面代碼中的InitializeDirect3D函數完整的演示了創建設備的步驟

            設備類型:Direct3D支持3種設備,其中HAL和REF最為重要。HAL通過硬件進行光柵化、坐標變換和光照處理等,速度最快。REF則是用軟件實現相關的操作,僅用于硬件不支持某種操作的情況。DeviceType枚舉定義了設備類型可能的選項。

            3D運算方式包括一些選項,如HardwareVertexProcessing,PureHardwareVertexProcessing等,用于指定頂點運算由硬件執行還是軟件執行等。

            PresentParameters還包括一些設置,如后臺緩沖區的高度、寬度和像素格式,以及從后臺緩沖區復制到前臺緩存屏幕顯示的方式等等。如果Direct3D采用窗口方式運行,像素格式必須查詢當前的顯示模式獲得。

            Direct3D設備創建成功以后,就可以進入圖形顯示階段。在下面的代碼中以Render()函數的形式出現。在繪制圖形前,需要調用Device::Clear()函數重制ViewPort的顏色緩沖區。ViewPort就是3D形狀投射到平面顯示器上供我們看到的那個區域。Clear函數的Flag參數指定了對顏色緩沖區、深度緩沖區還是模板緩沖區進行初始化。因為我用ATI顯卡,所以我選擇了將顏色緩沖區初始化為紅色:)。接下來是調用BeginScene()函數和EndScene()函數。實際的繪圖中,所有渲染的代碼都必須放在BeginScene函數和EndScene函數之間,否則就會出錯。最后調用Present()函數,將后臺緩沖區中的數據復制到前臺緩沖區,我們就能看見圖形了。

              回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产呻吟久久久久久久92| 久久毛片免费看一区二区三区| 国产午夜精品久久久久免费视| 国产精品美女久久久久AV福利| 亚洲va久久久噜噜噜久久男同 | 久久99亚洲网美利坚合众国| 无码八A片人妻少妇久久| 久久久久亚洲av成人无码电影| 婷婷综合久久狠狠色99h| 久久久久一区二区三区| 色综合久久久久| 91久久精品电影| 久久婷婷色综合一区二区| 久久久久亚洲国产| 亚洲国产另类久久久精品| 久久久久亚洲Av无码专| 97久久综合精品久久久综合| 久久狠狠色狠狠色综合| 伊人久久大香线蕉影院95| 久久天天躁狠狠躁夜夜不卡| 一级做a爰片久久毛片毛片| 欧美va久久久噜噜噜久久| 东京热TOKYO综合久久精品| 青青草原综合久久| 手机看片久久高清国产日韩| 久久精品国产清自在天天线| 99久久国产热无码精品免费| 亚洲国产精品久久66| 国产精品久久久久久久久久影院 | 亚洲综合久久综合激情久久| 久久精品无码一区二区日韩AV | 日韩人妻无码一区二区三区久久99| 无码国内精品久久综合88| 国产成人精品久久一区二区三区| 久久se精品一区二区| 精品久久久久成人码免费动漫| 久久久久亚洲av无码专区导航| 久久激情亚洲精品无码?V| 99久久婷婷国产综合亚洲| 欧美午夜A∨大片久久| 国产综合久久久久|