• <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++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            Managed DirectX ---- Using Matrices

            // -----------------------------------------------------------------------------
            // ?File:?Matrices.cs
            //
            // ?Desc:?Now?that?we?know?how?to?create?a?device?and?render?some?2D?vertices,
            // ???????this?tutorial?goes?the?next?step?and?renders?3D?geometry.?To?deal?with
            // ???????3D?geometry?we?need?to?introduce?the?use?of?4x4?matrices?to?transform
            // ???????the?geometry?with?translations,?rotations,?scaling,?and?setting?up?our
            // ???????camera.
            //
            // ???????Geometry?is?defined?in?model?space.?We?can?move?it?(translation),
            // ???????rotate?it?(rotation),?or?stretch?it?(scaling)?using?a?world?transform.
            // ???????The?geometry?is?then?said?to?be?in?world?space.?Next,?we?need?to
            // ???????position?the?camera,?or?eye?point,?somewhere?to?look?at?the?geometry.
            // ???????Another?transform,?via?the?view?matrix,?is?used,?to?position?and
            // ???????rotate?our?view.?With?the?geometry?then?in?view?space,?our?last
            // ???????transform?is?the?projection?transform,?which?"projects"?the?3D?scene
            // ???????into?our?2D?viewport.
            //
            // ???????Note?that?in?this?tutorial,?we?are?introducing?the?use?of?D3DX,?which
            // ???????is?a?set?of?helper?utilities?for?D3D.?In?this?case,?we?are?using?some
            // ???????of?D3DX's?useful?matrix?initialization?functions.?To?use?D3DX,?simply
            // ???????include?the?D3DX?reference?in?your?project
            //
            // ?Copyright?(c)?Microsoft?Corporation.?All?rights?reserved.
            // -----------------------------------------------------------------------------
            using ?System;
            using ?System.Drawing;
            using ?System.Windows.Forms;
            using ?Microsoft.DirectX;
            using ?Microsoft.DirectX.Direct3D;
            using ?Direct3D = Microsoft.DirectX.Direct3D;

            namespace ?MatricesTutorial
            {
            ????
            public ? class ?Matrices?:?Form
            ????
            {
            ????????
            // ?Our?global?variables?for?this?project
            ????????Device?device? = ? null ;? // ?Our?rendering?device
            ????????VertexBuffer?vertexBuffer? = ? null ;
            ????????PresentParameters?presentParams?
            = ? new ?PresentParameters();
            ????????
            bool ?pause? = ? false ;

            ????????
            public ?Matrices()
            ????????
            {
            ????????????
            // ?Set?the?initial?size?of?our?form
            ???????????? this .ClientSize? = ? new ?System.Drawing.Size( 400 , 300 );
            ????????????
            // ?And?it's?caption
            ???????????? this .Text? = ? " Direct3D?Tutorial?3?-?Matrices " ;
            ????????}


            ????????
            public ? bool ?InitializeGraphics()
            ????????
            {
            ????????????
            try
            ????????????
            {
            ????????????????
            // ?Now?let's?setup?our?D3D?stuff
            ????????????????presentParams.Windowed = true ;
            ????????????????presentParams.SwapEffect?
            = ?SwapEffect.Discard;
            ????????????????device?
            = ? new ?Device( 0 ,?DeviceType.Hardware,? this ,?CreateFlags.SoftwareVertexProcessing,?presentParams);
            ????????????????device.DeviceReset?
            += ? new ?System.EventHandler( this .OnResetDevice);
            ????????????????
            this .OnCreateDevice(device,? null );
            ????????????????
            this .OnResetDevice(device,? null );
            ????????????????pause?
            = ? false ;
            ????????????????
            return ? true ;
            ????????????}

            ????????????
            catch ?(DirectXException)
            ????????????
            {?
            ????????????????
            return ? false ;?
            ????????????}

            ????????}

            ????????
            public ? void ?OnCreateDevice( object ?sender,?EventArgs?e)
            ????????
            {
            ????????????Device?dev?
            = ?(Device)sender;
            ????????????
            // ?Now?Create?the?VB
            ????????????vertexBuffer? = ? new ?VertexBuffer( typeof (CustomVertex.PositionColored),? 3 ,?dev,? 0 ,?CustomVertex.PositionColored.Format,?Pool.Default);
            ????????????vertexBuffer.Created?
            += ? new ?System.EventHandler( this .OnCreateVertexBuffer);
            ????????????
            this .OnCreateVertexBuffer(vertexBuffer,? null );
            ????????}

            ????????
            public ? void ?OnResetDevice( object ?sender,?EventArgs?e)
            ????????
            {
            ????????????Device?dev?
            = ?(Device)sender;
            ????????????
            // ?Turn?off?culling,?so?we?see?the?front?and?back?of?the?triangle
            ????????????dev.RenderState.CullMode? = ?Cull.None;
            ????????????
            // ?Turn?off?D3D?lighting,?since?we?are?providing?our?own?vertex?colors
            ????????????dev.RenderState.Lighting? = ? false ;
            ????????}

            ????????
            public ? void ?OnCreateVertexBuffer( object ?sender,?EventArgs?e)
            ????????
            {
            ????????????VertexBuffer?vb?
            = ?(VertexBuffer)sender;
            ????????????CustomVertex.PositionColored[]?verts?
            = ?(CustomVertex.PositionColored[])vb.Lock( 0 , 0 );
            ????????????verts[
            0 ].X =- 1.0f ;?verts[ 0 ].Y =- 1.0f ;?verts[ 0 ].Z = 0.0f ;?verts[ 0 ].Color? = ?System.Drawing.Color.DarkGoldenrod.ToArgb();
            ????????????verts[
            1 ].X = 1.0f ;?verts[ 1 ].Y =- 1.0f ?;verts[ 1 ].Z = 0.0f ;?verts[ 1 ].Color? = ?System.Drawing.Color.MediumOrchid.ToArgb();
            ????????????verts[
            2 ].X = 0.0f ;?verts[ 2 ].Y = 1.0f ;?verts[ 2 ].Z? = ? 0.0f ;?verts[ 2 ].Color? = ?System.Drawing.Color.Cornsilk.ToArgb();
            ????????????vb.Unlock();
            ????????}


            ????????
            private ? void ?Render()
            ????????
            {
            ????????????
            if ?(device? == ? null )?
            ????????????????
            return ;

            ????????????
            if ?(pause)
            ????????????????
            return ;

            ????????????
            // Clear?the?backbuffer?to?a?blue?color?
            ????????????device.Clear(ClearFlags.Target,?System.Drawing.Color.Blue,? 1.0f ,? 0 );
            ????????????
            // Begin?the?scene
            ????????????device.BeginScene();
            ????????????
            // ?Setup?the?world,?view,?and?projection?matrices
            ????????????SetupMatrices();
            ????
            ????????????device.SetStreamSource(
            0 ,?vertexBuffer,? 0 );
            ????????????device.VertexFormat?
            = ?CustomVertex.PositionColored.Format;
            ????????????device.DrawPrimitives(PrimitiveType.TriangleList,?
            0 ,? 1 );
            ????????????
            // End?the?scene
            ????????????device.EndScene();
            ????????????device.Present();
            ????????}


            ????????
            private ? void ?SetupMatrices()
            ????????
            {
            ????????????
            // ?For?our?world?matrix,?we?will?just?rotate?the?object?about?the?y-axis.

            ????????????
            // ?Set?up?the?rotation?matrix?to?generate?1?full?rotation?(2*PI?radians)?
            ????????????
            // ?every?1000?ms.?To?avoid?the?loss?of?precision?inherent?in?very?high?
            ????????????
            // ?floating?point?numbers,?the?system?time?is?modulated?by?the?rotation?
            ????????????
            // ?period?before?conversion?to?a?radian?angle.
            ???????????? int ??iTime?? = ?Environment.TickCount? % ? 1000 ;
            ????????????
            float ?fAngle? = ?iTime? * ?( 2.0f ? * ?( float )Math.PI)? / ? 1000.0f ;
            ????????????device.Transform.World?
            = ?Matrix.RotationY(?fAngle?);

            ????????????
            // ?Set?up?our?view?matrix.?A?view?matrix?can?be?defined?given?an?eye?point,
            ????????????
            // ?a?point?to?lookat,?and?a?direction?for?which?way?is?up.?Here,?we?set?the
            ????????????
            // ?eye?five?units?back?along?the?z-axis?and?up?three?units,?look?at?the
            ????????????
            // ?origin,?and?define?"up"?to?be?in?the?y-direction.
            ????????????device.Transform.View? = ?Matrix.LookAtLH(? new ?Vector3(? 0.0f ,? 3.0f , - 5.0f ?),? new ?Vector3(? 0.0f ,? 0.0f ,? 0.0f ?),? new ?Vector3(? 0.0f ,? 1.0f ,? 0.0f ?)?);

            ????????????
            // ?For?the?projection?matrix,?we?set?up?a?perspective?transform?(which
            ????????????
            // ?transforms?geometry?from?3D?view?space?to?2D?viewport?space,?with
            ????????????
            // ?a?perspective?divide?making?objects?smaller?in?the?distance).?To?build
            ????????????
            // ?a?perpsective?transform,?we?need?the?field?of?view?(1/4?pi?is?common),
            ????????????
            // ?the?aspect?ratio,?and?the?near?and?far?clipping?planes?(which?define?at
            ????????????
            // ?what?distances?geometry?should?be?no?longer?be?rendered).
            ????????????device.Transform.Projection? = ?Matrix.PerspectiveFovLH(?( float )Math.PI? / ? 4 ,? 1.0f ,? 1.0f ,? 100.0f ?);
            ????????}


            ????????
            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
            ????????}

            ????????
            protected ? override ? void ?OnResize(System.EventArgs?e)
            ????????
            {
            ????????????pause?
            = ?(( this .WindowState? == ?FormWindowState.Minimized)? || ? ! this .Visible);
            ????????}


            ????????
            /// ? <summary>
            ????????
            /// ?The?main?entry?point?for?the?application.
            ????????
            /// ? </summary>

            ???????? static ? void ?Main()?
            ????????
            {
            ????????????
            using ?(Matrices?frm? = ? new ?Matrices())
            ????????????
            {
            ????????????????
            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();
            ????????????????}

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

            ????????}

            ????}

            }



            ?// Set up the world, view, and projection matrices.


            private void SetupMatrices()
            ??{
            ???// For our world matrix, we will just rotate the object about the y-axis.
            ???// Set up the rotation matrix to generate 1 full rotation (2*PI radians)
            ???// every 1000 ms. To avoid the loss of precision inherent in very high
            ???// floating point numbers, the system time is modulated by the rotation
            ???// period before conversion to a radian angle.
            ???int? iTime? = Environment.TickCount % 1000;
            ???float fAngle = iTime * (2.0f * (float)Math.PI) / 1000.0f;
            ???device.Transform.World = Matrix.RotationY( fAngle );

            ???// Set up our view matrix. A view matrix can be defined given an eye point,
            ???// a point to lookat, and a direction for which way is up. Here, we set the
            ???// eye five units back along the z-axis and up three units, look at the
            ???// origin, and define "up" to be in the y-direction.
            ???device.Transform.View = Matrix.LookAtLH( new Vector3( 0.0f, 3.0f,-5.0f ), new Vector3( 0.0f, 0.0f, 0.0f ), new Vector3( 0.0f, 1.0f, 0.0f ) );

            ???// For the projection matrix, we set up a perspective transform (which
            ???// transforms geometry from 3D view space to 2D viewport space, with
            ???// a perspective divide making objects smaller in the distance). To build
            ???// a perpsective transform, we need the field of view (1/4 pi is common),
            ???// the aspect ratio, and the near and far clipping planes (which define at
            ???// what distances geometry should be no longer be rendered).

            ???device.Transform.Projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
            ??}

            posted on 2006-05-17 10:16 夢(mèng)在天涯 閱讀(678) 評(píng)論(0)  編輯 收藏 引用 所屬分類: DirectX

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            久久无码专区国产精品发布| 91亚洲国产成人久久精品| 国内精品综合久久久40p| 久久婷婷五月综合色奶水99啪| 久久综合亚洲鲁鲁五月天| 久久久久亚洲精品天堂久久久久久| 久久综合亚洲色HEZYO国产| 思思久久99热只有频精品66| 狠狠综合久久AV一区二区三区| 亚洲中文精品久久久久久不卡| 热re99久久精品国99热| 久久夜色tv网站| 久久精品国产亚洲AV忘忧草18| 波多野结衣中文字幕久久| 久久国产视屏| 国产99精品久久| 影音先锋女人AV鲁色资源网久久 | 国产精品日韩深夜福利久久 | 99久久国产综合精品成人影院| 久久人搡人人玩人妻精品首页| 色播久久人人爽人人爽人人片AV| 久久人妻少妇嫩草AV无码专区| 久久中文字幕一区二区| 久久综合九色综合网站| 天天综合久久久网| 性欧美大战久久久久久久久 | 久久久久久国产a免费观看不卡 | 99久久精品毛片免费播放| 久久99久久无码毛片一区二区| 国内精品久久久久久99蜜桃| 久久99热这里只有精品国产| 国产激情久久久久影院老熟女免费 | 欧美伊人久久大香线蕉综合| 一本大道加勒比久久综合| 久久久无码一区二区三区| 中文字幕无码精品亚洲资源网久久| 久久免费大片| 无码精品久久一区二区三区| 久久午夜综合久久| 亚洲国产一成久久精品国产成人综合 | 国产精品久久久久久久久鸭|