Archived

This topic is now archived and is closed to further replies.

Santharn

using Dynamic Buffering with static data?

Recommended Posts

Santharn    122
I have a problem with my static index data, that i have precalculated (i thought copying them each frame would be a little faster than building up the indices each frame again) - but it doesn't work. The index and vertex-data is accordingly stored in common system memory-arrays (called m_pIndices and m_pVertices). The indices are related absolutely from the first vertex in my precalculated data - and there also is my problem: Copying this data from these arrays in the index-and vertex buffers at runtime, the indices are still related to the originally vertex-array and are NOT related to the vertex buffer, from which i am rendering. Is there any possibility to adjust this problem with my index-buffer (maybe with the ->SetIndices-Function or ->DrawIndexedPrimitives-parameters) or is there absolutely no chance and will i have to calculate the indices for each looppass? My engine follows this principles out of DX-Docu:
      
for loop()
{
    pBuffer->Lock(...D3DLOCK_DISCARD...); //Ensures that hardware 

                                          //doesn't stall by returning 

                                          //a new pointer.

    Fill data (optimally 1000s of vertices/indices, no fewer) in pBuffer.
    pBuffer->Unlock()
    Change state(s).
    DrawPrimitive() or DrawIndexedPrimitive()
}
  
My vertex and index buffers are only as big as they need to be for one loop-cycle:
             
// Create the vertex buffer

m_pd3dDevice->CreateVertexBuffer( 
				sizeof( CFunctionVertex ) * (m_uiNumOfBatchVertices+1), 
				D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC,
				MY_VERTEX_FORMAT, 
				D3DPOOL_DEFAULT, 
				&m_pVB );

//Create the index buffer:

m_pd3dDevice->CreateIndexBuffer( 
				sizeof( *m_pIndices ) * (m_uiNumOfBatchIndices+1),
				D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC, 
				D3DFMT_INDEX16, 
				D3DPOOL_DEFAULT, 
				&m_pIB );
  
The rendering-code looks like this:
     
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof( CFunctionVertex ) );		
m_pd3dDevice->SetVertexShader( CFUNCTION_VERTEX_FORMAT );	

	
BYTE* pData = 0; //Used as Pointer to the vertexbuffer and to the indexbuffer

   //in the loop while rendering



m_pd3dDevice->SetIndices( m_pIB, 0 );


//Calculate here the pointer-data for the first loopcycle

//...



//Here is the core-loop:

for( iNumOfLoops ) {
   //Update indices, i'm aware that this is not correct, but perhaps i'm using it wrong?

   m_pd3dDevice->SetIndices( m_pIB, m_CurrentCycle.uiStartIndex );
	

   //Lock the vertex buffer

   m_pVB->Lock( 0, 0, &pData, D3DLOCK_DISCARD );


   //Copy the face vertices into the buffer

   CopyMemory( pData, 
      (void*)&m_pVertices[m_CurrentCycle.uiMinVertex], 
      sizeof( MyVertex ) * m_CurrentCycle.uiNumOfVertices );


   //Unlock the Vertex Buffer:

   m_pVB->Unlock();

	

   //Lock the index buffer

   m_pIB->Lock( 0, 0, &pData, D3DLOCK_DISCARD );
		
   //Copy the indices into the buffer

   CopyMemory( pData, 
      (void*)&m_pIndices[ m_CurrentCycle.uiStartIndex ], 
      sizeof( *m_pIndices ) * m_CurrentCycle.uiNumOfIndices );


   //Unlock the index Buffer:

   m_pIB->Unlock();
 


   //And render the indexed triangles:

//******MAYBE_HERE_IS_ADJUSTING_FOR_MY_PROBLEM_POSSIBLE?**************

   hr = m_pd3dDevice->DrawIndexedPrimitive(	
      D3DPT_TRIANGLELIST, 
      0, 
      m_CurrentCycle.uiNumOfVertices, 
      0,
      m_CurrentCycle.uiNumOfIndices/3 );
//******MAYBE_HERE_IS_ADJUSTING_FOR_MY_PROBLEM_POSSIBLE?**************

 


   //Calculate the pointer data for the next loopcycle

   //...



   } //for( iNumOfLoops )

    
This works only well when all the indices can be rendered within one loop-cycle. thanks for your help! [edited by - Santharn on September 30, 2002 12:49:47 PM]

Share this post


Link to post
Share on other sites