Sign in to follow this  
i1977

D3DXOptimize functions

Recommended Posts

I'm using DirectX9.0c and I'm trying to figure out how to use the D3DXOptimizeFaces() and D3DXOptimizeVertices() functions. I find the documentation very unhelpful on this particular topic. Has anyone ever used them? Can someone post a code sample to better illustrate how they are used.

Share this post


Link to post
Share on other sites
Dunno if this helps, its the complete code for a funtion i made that creates a cylinder mesh, the node business can be ignored, it was part of a linked list of different cylinders...


node = &start;

node = node->next;

while (node)
{
HRESULT hr = S_OK;
CUSTOMVERTEX* v = NULL;
DWORD* pdwAdjacency = NULL;

float Radius = node->CylinderRadius;
float Length = node->CylinderLength;

unsigned int SegmentsX= (unsigned int)node->CylinderSegmentsX+1;
unsigned int SegmentsY= (unsigned int)node->CylinderSegmentsY+1;

UINT nOT = (UINT) (SegmentsX) * (SegmentsY) * 2;


// Create a square grid m_n*m_m for rendering the Cylinder
if( FAILED( hr = D3DXCreateMeshFVF( nOT, nOT * 3, D3DXMESH_SYSTEMMEM, D3DFVF_CUSTOMVERTEX, g_pD3DDevice, &node->pCylinderMesh ) ) )
{

MessageBox(hWnd, "D3DXCreateMeshFVF Error!", "", MB_OK);
goto End;
}

// Fill in the grid vertex data
float dY;
UINT k;
UINT y;
if( FAILED( hr = node->pCylinderMesh->LockVertexBuffer(0, (LPVOID*)&v) ) )
{
MessageBox(hWnd, "LockVertexBuffer Error!", "", MB_OK);
goto End;
}
dY = Length/(SegmentsY-1);
//dZ = node->CylinderLength/(SegmentsY-1);
k = 0;

float tGuideU = node->tGuideU;
float tGuideV = node->tGuideV;

for (y=0; y < (SegmentsY-1); y++)
{
for (UINT x=0; x < (SegmentsX-1); x++)
{
FLOAT theta = (node->CylinderPortion*D3DX_PI*x) /(SegmentsX-1);
FLOAT thetaAhead = (node->CylinderPortion*D3DX_PI*(x+1)) /(SegmentsX-1);

v[k].p = D3DXVECTOR3(( sinf(theta) * Radius ), (dY * y), ( cosf(theta) * Radius ));
v[k].n = D3DXVECTOR3(sinf(theta), 0, cosf(theta));
v[k].tu = ((float)x/(SegmentsX-1))*tGuideU;
v[k].tv = ((float)y/(SegmentsY-1))*tGuideV;
k++;

v[k].p = D3DXVECTOR3(( sinf(thetaAhead) * Radius ), (dY * (y +1)), ( cosf(thetaAhead) * Radius ));
v[k].n = D3DXVECTOR3(sinf(thetaAhead), 0, cosf(thetaAhead));
v[k].tu = ((float)(x+1)/(SegmentsX-1))*tGuideU;
v[k].tv = ((float)(y+1)/(SegmentsY-1))*tGuideV;
k++;

v[k].p = D3DXVECTOR3(( sinf(theta) * Radius ), (dY * (y +1)), ( cosf(theta) * Radius ));
v[k].n = D3DXVECTOR3(sinf(theta), 0, cosf(theta));
v[k].tu = ((float)x/(SegmentsX-1))*tGuideU;
v[k].tv = ((float)(y+1)/(SegmentsY-1))*tGuideV;
k++;


v[k].p = D3DXVECTOR3(( sinf(theta) * Radius ), (dY * y), ( cosf(theta) * Radius ));
v[k].n = D3DXVECTOR3(sinf(theta), 0, cosf(theta));
v[k].tu = ((float)x/(SegmentsX-1))*tGuideU;
v[k].tv = ((float)y/(SegmentsY-1))*tGuideV;
k++;

v[k].p = D3DXVECTOR3(( sinf(thetaAhead) * Radius ), (dY * y ), ( cosf(thetaAhead) * Radius ));
v[k].n = D3DXVECTOR3(sinf(thetaAhead), 0, cosf(thetaAhead));
v[k].tu = ((float)(x+1)/(SegmentsX))*tGuideU;
v[k].tv = ((float)y/(SegmentsY-1))*tGuideV;
k++;

v[k].p = D3DXVECTOR3(( sinf(thetaAhead) * Radius ), (dY * (y +1)), ( cosf(thetaAhead) * Radius ));
v[k].n = D3DXVECTOR3(sinf(thetaAhead), 0, cosf(thetaAhead));
v[k].tu = ((float)(x+1)/(SegmentsX-1))*tGuideU;
v[k].tv = ((float)(y+1)/(SegmentsY-1))*tGuideV;
k++;


}
}



node->pCylinderMesh ->UnlockVertexBuffer();

// Fill in index data
WORD* pIndex;
if( FAILED( hr = node->pCylinderMesh ->LockIndexBuffer(0, (LPVOID*)&pIndex) ) )
{
MessageBox(hWnd, "LockIndexBuffer Error!", "", MB_OK);
goto End;
}

WORD iIndex;
for( iIndex = 0; iIndex < (nOT * 3); iIndex++ )
{
*(pIndex++) = iIndex;
}
node->pCylinderMesh ->UnlockIndexBuffer();

// Eliminate redundant vertices
pdwAdjacency = new DWORD[3 * nOT];
if( pdwAdjacency == NULL )
{
MessageBox(hWnd, "E_OUTOFMEMORY Error!", "", MB_OK);

hr = E_OUTOFMEMORY;
goto End;
}


if( FAILED( hr = D3DXWeldVertices( node->pCylinderMesh , D3DXWELDEPSILONS_WELDALL, NULL, NULL, pdwAdjacency, NULL, NULL ) ) )
{
goto End;
}

// Optimize the mesh
if( FAILED( hr = node->pCylinderMesh ->OptimizeInplace( D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
pdwAdjacency, NULL, NULL, NULL ) ) )
{
goto End;
}

// Copy the mesh into fast write-only memory
if( FAILED( hr = node->pCylinderMesh ->CloneMeshFVF( D3DXMESH_WRITEONLY, D3DFVF_CUSTOMVERTEX, g_pD3DDevice, &node->m_pCylinderMesh ) ) )
{
MessageBox(hWnd, "CloneMeshFVF Error!", "", MB_OK);
goto End;
}

/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////

node = node->next;
}
End:
fclose(fp);
return;




i use an optimiseinplace function, im sure these fucntions would be fine for waht u want to do.

ace

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this