C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

managed directx实现读写buffer (Read and Write VertexBuffer and IndexBuffer Data)

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-06-09 16:39 梦在天涯 阅读(1359) 评论(1)  编辑 收藏 引用 所属分类: DirectX

评论

# re: managed directx实现读写buffer (Read and Write VertexBuffer and IndexBuffer Data)[未登录] 2007-11-09 15:34 梦在天涯

除了上面提到的vertexbuffer来存model。

我们还可以使用indexbuffer+vertexbuffer来存,

还可以使用vertexelement+vertexbuffer来存。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

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

搜索

  •  

积分与排名

  • 积分 - 1784830
  • 排名 - 5

最新评论

阅读排行榜