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

            Managed Directx --rendering vertices (使用VertexBuffer 進(jìn)行render)

            ?

            // -----------------------------------------------------------------------------
            // ?File:?Vertices.cs
            //
            // ?Desc:?In?this?tutorial,?we?are?rendering?some?vertices.?This?introduces?the
            // ???????concept?of?the?vertex?buffer,?a?Direct3D?object?used?to?store
            // ???????vertices.?Vertices?can?be?defined?any?way?we?want?by?defining?a
            // ???????custom?structure?and?a?custom?FVF?(flexible?vertex?format).?In?this
            // ???????tutorial,?we?are?using?vertices?that?are?transformed?(meaning?they
            // ???????are?already?in?2D?window?coordinates)?and?lit?(meaning?we?are?not
            // ???????using?Direct3D?lighting,?but?are?supplying?our?own?colors).
            //
            // ?Copyright?(c)?Microsoft?Corporation.?All?rights?reserved.
            // -----------------------------------------------------------------------------
            using ?System;
            using ?System.Drawing;
            using ?System.Windows.Forms;
            using ?Microsoft.DirectX;
            using ?Microsoft.DirectX.Direct3D;

            namespace ?VerticesTutorial
            {
            ????
            public ? class ?Vertices?:?Form
            ????
            {
            ????????
            // ?Our?global?variables?for?this?project
            ????????Device?device? = ? null ;? // ?Our?rendering?device
            ????????VertexBuffer?vertexBuffer? = ? null ;

            ????????
            public ?Vertices()
            ????????
            {
            ????????????
            // ?Set?the?initial?size?of?our?form
            ???????????? this .ClientSize? = ? new ?System.Drawing.Size( 300 , 300 );
            ????????????
            // ?And?its?caption
            ???????????? this .Text? = ? " Direct3D?Tutorial?2?-?Vertices " ;
            ????????}

            ????????
            ????????
            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);
            ????????????????
            this .OnCreateDevice(device,? null );
            ????????????????
            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.TransformedColored),? 3 ,?dev,? 0 ,?CustomVertex.TransformedColored.Format,?Pool.Default);
            ????????????vertexBuffer.Created?
            += ? new ?System.EventHandler( this .OnCreateVertexBuffer);
            ????????????
            this .OnCreateVertexBuffer(vertexBuffer,? null );
            ????????}

            ????????
            public ? void ?OnCreateVertexBuffer( object ?sender,?EventArgs?e)
            ????????
            {
            ????????????VertexBuffer?vb?
            = ?(VertexBuffer)sender;
            ????????????GraphicsStream?stm?
            = ?vb.Lock( 0 ,? 0 ,? 0 );
            ????????????CustomVertex.TransformedColored[]?verts?
            = ? new ?CustomVertex.TransformedColored[ 3 ];

            ????????????verts[
            0 ].X = 150 ;verts[ 0 ].Y = 50 ;verts[ 0 ].Z = 0.5f ;?verts[ 0 ].Rhw = 1 ;?verts[ 0 ].Color? = ?System.Drawing.Color.Red.ToArgb();
            ????????????verts[
            1 ].X = 250 ;verts[ 1 ].Y = 250 ;verts[ 1 ].Z = 0.5f ;?verts[ 1 ].Rhw = 1 ;?verts[ 1 ].Color? = ?System.Drawing.Color.Yellow.ToArgb();
            ????????????verts[
            2 ].X = 50 ;verts[ 2 ].Y = 250 ;verts[ 2 ].Z = 0.5f ;?verts[ 2 ].Rhw = 1 ;?verts[ 2 ].Color? = ?System.Drawing.Color.SkyBlue.ToArgb();
            ????????????stm.Write(verts);
            ????????????vb.Unlock();
            ????????}

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

            ????????????
            // Clear?the?backbuffer?to?a?blue?color?(ARGB?=?000000ff)
            ????????????device.Clear(ClearFlags.Target,?System.Drawing.Color.Blue,? 1.0f ,? 0 );
            ????????????
            // Begin?the?scene
            ????????????device.BeginScene();
            ????????????
            ????????????device.SetStreamSource(?
            0 ,?vertexBuffer,? 0 );
            ????????????device.VertexFormat?
            = ?CustomVertex.TransformedColored.Format;
            ????????????device.DrawPrimitives(PrimitiveType.TriangleList,?
            0 ,? 1 );
            ????????????
            // 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 ?(Vertices?frm? = ? new ?Vertices())
            ????????????
            {
            ????????????????
            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 定義 VertexBuffer vb = null;
            2 創(chuàng)建 VertexBuffer?
            ??? public void OnCreateDevice(object sender, EventArgs e)
            ??? {
            ??? Device dev = (Device)sender;
            ???
            ??? // Now create the vertex buffer
            ??? vertexBuffer = new VertexBuffer(
            ??????? typeof(CustomVertex.TransformedColored), 3, dev, 0,
            ??????? CustomVertex.TransformedColored.Format, Pool.Default);
            ??? vertexBuffer.Created +=
            ??????? new System.EventHandler(this.OnCreateVertexBuffer);
            ??? this.OnCreateVertexBuffer(vb, null);
            ??? }

            ??? public void OnCreateVertexBuffer(object sender, EventArgs e)
            ??? {
            ??? VertexBuffer vb = (VertexBuffer)sender;
            ??? GraphicsStream stm = vb.Lock(0, 0, 0);?? //Lock() 保證cpu能夠直接訪問 VertexBuffer ,必須和unlock()一起使用
            ??? CustomVertex.TransformedColored[] verts =
            ??????? new CustomVertex.TransformedColored[3];
            ??? .
            ??? .
            ??? .
            ??? vb.Unlock();
            ??? }
            3? GraphicsStream對象直接訪問vertexbuffer, 或者說把vertexbuffer的內(nèi)容放到Graphicsstream中以合適的大小.
            verts[0].X=150; verts[0].Y=50; verts[0].Z=0.5f; verts[0].Rhw=1;
            verts[0].Color = System.Drawing.Color.Aqua.ToArgb();

            verts[1].X=250; verts[1].Y=250; verts[1].Z=0.5f; verts[1].Rhw=1;
            verts[1].Color = System.Drawing.Color.Brown.ToArgb();

            verts[2].X=50; verts[2].Y=250; verts[2].Z=0.5f; verts[2].Rhw=1;
            verts[2].Color = System.Drawing.Color.LightPink.ToArgb();

            stm.Write(verts);
            4 render device
            private void Render()
            {
            ??? if (device == null)
            ??? return;
            ???
            ??? // Clear the back buffer to a blue color (ARGB = 000000ff)
            ??? device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
            ??? // Begin the scene
            ??? device.BeginScene();
            ???
            ??? // New for Tutorial 2
            ??? device.SetStreamSource(0, vertexBuffer, 0);??? //設(shè)置stream源用vertexbuffer.
            ??? device.VertexFormat = CustomVertex.TransformedColored.Format;? //設(shè)置render格式
            ??? device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);?
            ???
            ??? // End the scene
            ??? device.EndScene();
            ??? device.Present();
            }

            Read and Write VertexBuffer and IndexBuffer Data With GraphicsStreams


            using?System;
            using?Microsoft.DirectX;
            using?Microsoft.DirectX.Direct3D;

            public?struct?PositionNormalTexVertex
            {
            ????
            public?Vector3?Position;
            ????
            public?Vector3?Normal;
            ????
            public??float?Tu0,?Tv0;
            ????
            public?static?readonly?VertexFormats?FVF?=?VertexFormats.Position?|?VertexFormats.Texture1;
            }

            public?class?Example
            {
            ????
            public?unsafe?void??GraphicsStreamReadWrite()
            ????
            {
            ????????
            //Create?a?vertex?buffer?in?the?managed?pool
            ????????VertexBuffer?vb?=?new?VertexBuffer(typeof(PositionNormalTexVertex),?100,?device,?Usage.None,?PositionNormalTexVertex.FVF,?Pool.Managed);

            ????????
            //First,?fill?an?array?of?PositionNormalTexVertex?elements?with?data.
            ????????PositionNormalTexVertex[]?vertices?=?new?PositionNormalTexVertex[50];
            ????????
            for(int?i=0;?i<50;?i++)
            ????????
            {
            ????????????
            //fill?the?vertices?with?some?data
            ????????????vertices[i].Position?=?new?Vector3(3f,4f,5f);
            ????????}


            ????????
            //The?size?of?the?verticies?are?32-bytes?each?(float3?(12)?+?float3?(12)?+?float(4)?+?float(4))
            ????????
            //To?lock?50?verticies,?the?size?of?the?lock?would?be?1600?(32?*?50)
            ????????GraphicsStream?vbData?=??vb.Lock(0,1600,?LockFlags.None);

            ????????
            //copy?the?vertex?data?into?the?vertex?buffer
            ????????vbData.Write(vertices);

            ????????
            //Unlock?the?VB
            ????????vb.Unlock();


            ????????
            //This?time,?lock?the?entire?VertexBuffer
            ????????vbData?=??vb.Lock(0,?3200,?LockFlags.None);

            ????????
            //Cast?the?InternalDataPointer?(a?void?pointer)?to?an?array?of?verticies
            ????????PositionNormalTexVertex*?vbArray?=?(PositionNormalTexVertex*)?vbData.InternalDataPointer;

            ????????
            for(int?i=0;?i<100;?i++)
            ????????
            {
            ????????????
            //perform?some?operations?on?the?data
            ????????????vbArray[i].Tu0?=?i;
            ????????????vbArray[i].Tv0?
            =?vbArray[i].Tu0?*?2;

            ????????????Console.WriteLine(vbArray[i].Tv0.ToString());
            ????????}


            ????????
            //Unlock?the?buffer
            ????????vb.Unlock();
            ????????vb.Dispose();
            ????}

            }

            Read and Write VertexBuffer Data With Arrays

            using?System;
            using?Microsoft.DirectX;
            using?Microsoft.DirectX.Direct3D;

            public?struct?PositionNormalTexVertex
            {
            ????
            public?Vector3?Position;
            ????
            public?Vector3?Normal;
            ????
            public?float?Tu0,?Tv0;
            ????
            public?static?readonly?VertexFormats?FVF?=?VertexFormats.Position?|?VertexFormats.Texture1;
            }


            public?class?Example
            {
            ????
            public?void?ArrayBasedReadWrite()
            ????
            {
            ????????
            //Create?a?vertex?buffer?in?the?managed?pool
            ????????VertexBuffer?vb?=?new?VertexBuffer(typeof(PositionNormalTexVertex),?100,?device,?Usage.None,?PositionNormalTex1Vertex.FVF,?Pool.Managed);

            ????????
            //Fill?an?array?of?the?appropriate?type?with?the?VB?data?using?Lock()
            ????????PositionNormalTexVertex[]?vbData?=?(PositionNormalTexVertex[])?vb.Lock(0,?typeof(PositionNormalTexVertex),?LockFlags.None,?50);
            ????????
            for(int?i=0;?i<50;?i++)
            ????????
            {
            ????????????
            //set?your?vertices?to?something
            ????????????vbData[i].Position?=?new?Vector3(2f,2f,2f);??
            ????????????vbData[i].Normal?
            =?new?Vector3(1f,0f,0f);
            ????????????vbData[i].Tu0?
            =?i;
            ????????????vbData[i].Tv0?
            =?i;
            ????????}

            ????????
            //Unlock?the?vb?before?you?can?use?it?elsewhere
            ????????vb.Unlock();

            ????????
            //This?lock?overload?simply?locks?the?entire?VB?--?setting?ReadOnly?can?improve?perf?when?reading?a?vertexbuffer
            ????????vbData?=?(PositionNormalTexVertex[])?vb.Lock(0,?LockFlags.ReadOnly);
            ????????
            for(int?i=0;?i<100;?i++)
            ????????
            {
            ????????????
            //read?some?vertex?data
            ????????????Console.WriteLine("Vertex?"?+?i?+?"Tu:?"?+??vbData[i].Tu0?+?"?,?Tv:?"?+?vbData[i].Tv0);
            ????????}


            ????????
            //Unlock?the?buffer
            ????????vb.Unlock();


            ????????vb.Dispose();
            ????}

            }

            posted on 2006-05-12 13:55 夢在天涯 閱讀(962) 評論(0)  編輯 收藏 引用 所屬分類: DirectX

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

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

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            亚洲日本va午夜中文字幕久久| 久久久国产精品网站| 欧美综合天天夜夜久久| 69久久夜色精品国产69 | 亚洲精品国产综合久久一线| 理论片午午伦夜理片久久| 91亚洲国产成人久久精品网址| 久久乐国产综合亚洲精品| 伊人久久大香线蕉精品| 久久久久国产精品人妻| 狠狠色丁香婷婷久久综合五月| 狠狠色伊人久久精品综合网| 99久久精品国产一区二区蜜芽| 中文精品99久久国产| 国产国产成人精品久久| 亚洲精品无码久久不卡| 国产精品九九九久久九九 | 麻豆一区二区99久久久久| 综合久久一区二区三区| 狠狠狠色丁香婷婷综合久久五月| 久久综合久久自在自线精品自| 97精品久久天干天天天按摩| 久久99热这里只有精品66| 亚洲国产成人久久综合碰碰动漫3d| 7777精品伊人久久久大香线蕉 | 亚洲精品97久久中文字幕无码| 狠色狠色狠狠色综合久久| 国产美女亚洲精品久久久综合| 亚洲AV日韩AV天堂久久| 国产精品美女久久久久久2018| 丰满少妇高潮惨叫久久久| 久久亚洲精品无码VA大香大香| 久久婷婷五月综合成人D啪| 7国产欧美日韩综合天堂中文久久久久 | 人人妻久久人人澡人人爽人人精品| 国产精品亚洲综合专区片高清久久久| 国产精品久久久久一区二区三区| 国内精品久久久久久99蜜桃| 粉嫩小泬无遮挡久久久久久| 久久久久免费看成人影片| 久久精品无码一区二区三区免费|