PDA

View Full Version : bị bug với vertex buffer



ngocquang19877
26-03-2004, 21:07
tôi có tạo 1 class vertex buffer.nhưng tôi không thể nào render nó lên màn hình được,nếu ai có khả năng xin debug dùm tôi cám ơn nhiều.

ở dòng dứoi là dòng tôi init shape cho cái class này.tôi viết code cũng dễ hiểu.mong mọi người chỉ dùm

//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT CVertexBuffer::initialized(int Num_Sprites,TCHAR *Name_Texture,LPDIRECT3DDEVICE9 pd3dDevice,void (*pFunc)(void),CUSTOMVERTEX *Vertex,BOOL textures/*only support for bmp,jpg,tga,gif file */)
{
if(textures==TRUE)
{D3DFVF_CUSTOMVERTEX= (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1);}
else
{D3DFVF_CUSTOMVERTEX= (D3DFVF_XYZ|D3DFVF_DIFFUSE);}
Nums=Num_Sprites;
CUSTOMVERTEX *VERTEX=new CUSTOMVERTEX[Nums];
pFunc();
VERTEX=Vertex;

// Use D3DX to create a texture from a file based image
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice,Name_Texture, &m_pTexture ) ) )
{
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, "..\\Tut05_Textures.bmp", &m_pTexture ) ) )
{
MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
return E_FAIL;
}
}
// Create the vertex buffer.
if( FAILED( pd3dDevice->CreateVertexBuffer( Nums*2*sizeof(CUSTOMVERTEX),
D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&m_pVB, NULL ) ) )
{
MessageBox(NULL, "Could not Create buffer", "Textures.exe", MB_OK);
return E_FAIL;
}
// Fill the vertex buffer. We are setting the tu and tv texture
// coordinates, which range from 0.0 to 1.0
VOID* pVertices;
if( FAILED(m_pVB->Lock( 0,sizeof(VERTEX), (void**)&pVertices, 0 ) ) )
{
MessageBox(NULL, "Could not LOCK VB", "Textures.exe", MB_OK);
return E_FAIL;
}
memcpy( pVertices,VERTEX, sizeof(VERTEX) );
m_pVB->Unlock();

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
CVertexBuffer::~CVertexBuffer()
{
if( m_pTexture != NULL )
m_pTexture->Release();

if( m_pVB != NULL )
m_pVB->Release();
}

CVertexBuffer::CVertexBuffer()
{
m_pVB=NULL ;
m_pTexture=NULL ;
}


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID CVertexBuffer::Render(LPDIRECT3DDEVICE9 pd3dDevice,D3DPRIMITIVETYPE Flag)
{
pd3dDevice->SetTexture( 0, m_pTexture );
// Render the vertex buffer contents
pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof(CUSTOMVERTEX) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX);
pd3dDevice->DrawPrimitive( Flag, 0, 1 );
}



anh here the code i init shape for class vertex

quang[0].position=D3DXVECTOR3(150.0f, 50.0f, 0.5f);
quang[0].color=RGB(255,0,0);
quang[0].tu=0.0f;
quang[0].tv=1.0f;

quang[1].position=D3DXVECTOR3(250.0f, 250.0f, 0.5f);
quang[1].color=RGB(255,0,0);
quang[1].tu=1.0f;
quang[1].tv=0.0f;

quang[2].position=D3DXVECTOR3(50.0f, 250.0f, 0.5f);
quang[2].color=RGB(255,0,0);
quang[2].tu=0.0f;
quang[2].tv=1.0f;

quang[3].position=D3DXVECTOR3(131.0f,2.0f,3.0f);
quang[3].color=RGB(255,0,0);
quang[3].tu=1.0f;
quang[3].tv=0.0f;