Multiple Sprites/Meshes

Multiple Sprites/Meshes

Post by Jacky Lu » Mon, 27 May 2002 19:37:26

Hi guys and shelias,
I have looked all the way down in the DXSDK folder, but there are no samples
that illustrate the methods
of creating multiple sprites (Z-buffer stuff) or meshes... ya know any?
anyone knows how to fix it by the way... I need to achieve 'Multi-sprite'...

here is my code:

#define STRICT
#include <basetsd.h>
#include <stdio.h>
#include <math.h>
#include <D3DX8.h>
#include "D3DApp.h"
#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"

class CMyD3DApplication : public CD3DApplication
     CD3DFont* m_pFont;
     CD3DMesh* m_pForkLift;
     CD3DMesh* m_pPallet;
     CD3DMesh* m_pRack;
/////////////////// added
     TCHAR m_strMeshFilename[512];
     TCHAR m_strInitialDir[512];

     LPD3DXMESH m_pMeshSysMem;    // forklift
     LPD3DXMESH m_pMeshEnhanced;
     UINT m_dwNumSegs;

   DWORD m_dwNumMaterials;
     CD3DArcBall         m_ArcBall;

     LPDIRECT3DTEXTURE8 *m_ppTextures;
     LPD3DXBUFFER        m_pbufMaterials;
     LPD3DXBUFFER        m_pbufAdjacency;

     D3DXVECTOR3 m_vObjectCenter;
     FLOAT m_fObjectRadius;


     D3DXMATRIX m_matBillboardMatrix;

     HRESULT ConfirmDevice(D3DCAPS8*, DWORD, D3DFORMAT);
     HRESULT GenerateEnhancedMesh(UINT cNewNumSegs);
     HRESULT OneTimeSceneInit();
     HRESULT InitDeviceObjects();
     HRESULT RestoreDeviceObjects();
     HRESULT InvalidDeviceObjects();
     HRESULT DeleteDeviceObjects();
     HRESULT FinalCleanup();
     HRESULT Render();
     HRESULT FrameMove();



inline FLOAT HeightField( FLOAT x, FLOAT y)
     return 9*(cosf(x/20+0.2f)*cosf(y/15-0.2f)+1.0f);


CMyD3DApplication g_d3dApp;

     if (FAILED (g_d3dApp.Create (hInst)))
          return 0;

     return g_d3dApp.Run();


     m_strWindowTitle = _T("B&P Demostration");
     m_pFont = new CD3DFont (_T("Arial"),12, D3DFONT_BOLD);
     m_pForkLift = new CD3DMesh();
     m_pPallet = new CD3DMesh();
     m_pRack = new CD3DMesh();

     /////////////////// added
     m_bUseDepthBuffer = TRUE;
     m_bShowCursorWhenFullscreen = TRUE;

     m_dwNumSegs = 1;
     _tcscpy (m_strInitialDir, DXUtil_GetDXSDKMediaPath() );
     _tcscpy (m_strMeshFilename, _T("car.x"));



HRESULT CMyD3DApplication::InitDeviceObjects()
     BYTE *pVertices = NULL;
     LPD3DXMESH pTempMesh;
     TCHAR strMeshPath[512];

     m_pFont->InitDeviceObjects( m_pd3dDevice );
     m_pForkLift->Create( m_pd3dDevice, _T("car.x"));
     m_pRack->Create (m_pd3dDevice, _T("rack.x"));
     m_pPallet->Create (m_pd3dDevice, _T("pallet.x"));

     //////////////////// added
     DXUtil_FindMediaFile (strMeshPath, m_strMeshFilename );

     D3DXLoadMeshFromX (strMeshPath, D3DXMESH_SYSTEMMEM, m_pd3dDevice,
                            &m_pbufAdjacency, &m_pbufMaterials,
                            &m_pMeshSysMem );

     m_pMeshSysMem->GetVertexBuffer (&pVB);
     pVB->Lock (0,0,&pVertices,0);

     D3DXComputeBoundingSphere (pVertices, m_pMeshSysMem->GetNumVertices(),


     m_pMaterials = (D3DXMATERIAL*) m_pbufMaterials->GetBufferPointer();
     m_ppTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];

     for (UINT i=0; i < m_dwNumMaterials; i++)
          TCHAR strTexturePath[512] = _T("");
          DXUtil_FindMediaFile (strTexturePath,
          D3DXCreateTextureFromFile(m_pd3dDevice, strTexturePath,

     if (! (m_pMeshSysMem->GetFVF() & D3DFVF_NORMAL ))
          m_pMeshSysMem->CloneMeshFVF (m_pMeshSysMem->GetOptions(),
                                              m_pMeshSysMem->GetFVF() |
                                              m_pd3dDevice, &pTempMesh);

          D3DXComputeNormals (pTempMesh, NULL);
          SAFE_RELEASE (m_pMeshSysMem);
          m_pMeshSysMem = pTempMesh;

     return S_OK;


HRESULT CMyD3DApplication::OneTimeSceneInit()
     return S_OK;


HRESULT CMyD3DApplication::Render()
     m_pd3dDevice->Clear (0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0x000000ff, 1.0f, 0L);
     D3DXMATRIX matView, matViewSave;
     m_pd3dDevice->GetTransform (D3DTS_VIEW, &matViewSave);
     matView = matViewSave;
     matView._41 = 0.0f; matView._42 = -0.3f; matView._43 = 0.0f;
     m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView);

     ////////////////// added
     for (UINT i = 0; i < m_dwNumMaterials; i++) {
          m_pd3dDevice->SetTexture (0, m_ppTextures[i]);
          m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_ALWAYS);
          m_pMeshEnhanced->DrawSubset (i);
          m_pd3dDevice->SetRenderState (D3DRS_ZFUNC, D3DCMP_LESSEQUAL);



     return S_OK;


HRESULT CMyD3DApplication::FrameMove()
     D3DXMATRIX matWorld;

     // view for XMesh
     D3DXMatrixTranslation (&matWorld, -m_vObjectCenter.x,
     D3DXMatrixMultiply (&matWorld, &matWorld,
     D3DXMatrixMultiply (&matWorld, &matWorld,

     m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);
     D3DXMATRIX matView;
     D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0,0,-2*m_fObjectRadius),
                                       &D3DXVECTOR3 (0, 0, 0),
                                        &D3DXVECTOR3 (0, 1, 0) );
     m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

     // view for CDCMesh
/*     D3DXVECTOR3 vUpvec (0.0f, 1.0f, 0.0f);
     D3DXVECTOR3 vEyePt;
     D3DXVECTOR3 vLookatPt;

     vEyePt.x = 30.0f*cosf (0.8f * (m_fTime));
     vEyePt.z = 30.0f*sinf (0.8f * (m_fTime));
     vEyePt.x = 4 + HeightField (vEyePt.x, vEyePt.z );

     vLookatPt.x = 30.0f*cosf(0.8f * (m_fTime + 0.5f) );
     vLookatPt.z = 30.0f*sinf(0.8f * (m_fTime + 0.5f) );
     vLookatPt.y = vEyePt.y - 1.0f;

//     D3DXMATRIX matView;
     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpvec);
     m_pd3dDevice->SetTransform (D3DTS_VIEW, &matView); */

     return S_OK;


HRESULT CMyD3DApplication::GenerateEnhancedMesh(UINT dwNewNumSegs)
LPD3DXMESH pMeshEnhancedSysMem = NULL;

////////////////// temp cancelled
//         (m_pMeshSysMem->GetOptions() & D3DXMESH_32BIT),
//           m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );

 hr = D3DXTessellateNPatches( m_pMeshSysMem,
                                    (float)dwNewNumSegs, FALSE,
&pMeshEnhancedSysMem, NULL );
       if( FAILED(hr) )
           // If the tessellate failed, there might have been more triangles
or vertices
           // than can fit into a 16bit mesh, so try cloning to 32bit before

           hr = m_pMeshSysMem->CloneMeshFVF( D3DXMESH_SYSTEMMEM |
               m_pMeshSysMem->GetFVF(), m_pd3dDevice, &pMeshTemp );
           if (FAILED(hr))
               return hr;

           hr = D3DXTessellateNPatches( pMeshTemp,
                                        (float)dwNewNumSegs, FALSE,
&pMeshEnhancedSysMem, NULL );
           if( FAILED(hr) )
               return hr;


       // Make a vid mem version of the mesh
       // Only set WRITEONLY if it doesn't use 32bit indices, because those
       // often need to be emulated, which means that D3DX needs
       DWORD dwMeshEnhancedFlags = pMeshEnhancedSysMem->GetOptions() &
       if( (dwMeshEnhancedFlags & D3DXMESH_32BIT) == 0)
           dwMeshEnhancedFlags |= D3DXMESH_WRITEONLY;
       hr = pMeshEnhancedSysMem->CloneMeshFVF( dwMeshEnhancedFlags,
                                               m_pd3dDevice, &pMeshTemp );
       if( FAILED(hr) )
           SAFE_RELEASE( pMeshEnhancedSysMem );
           return hr;

       // Latch in the enhanced mesh
       SAFE_RELEASE( pMeshEnhancedSysMem );

          m_pMeshEnhanced = pMeshTemp;
          m_dwNumSegs = dwNewNumSegs;

 return S_OK;


HRESULT CMyD3DApplication::RestoreDeviceObjects()

     m_pForkLift->RestoreDeviceObjects( m_pd3dDevice );
     m_pPallet->RestoreDeviceObjects( m_pd3dDevice );
     m_pRack->RestoreDeviceObjects (m_pd3dDevice );

     HRESULT hr;

   // m_bUseHWNPatches = (m_d3dCaps.DevCaps & D3DDEVCAPS_NPATCHES);

   hr = GenerateEnhancedMesh( m_dwNumSegs );
   if( FAILED(hr) )
       return hr;

   // Setup render state
   m_pd3dDevice->SetRenderState( D3DRS_LIGHTING,     TRUE );
   m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
   m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,      TRUE );
   m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,      0x33333333 );
   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER,
   m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER,

   // Setup the light
   D3DLIGHT8 light;
   D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
   m_pd3dDevice->SetLight(0, &light );
   m_pd3dDevice->LightEnable(0, TRUE );

   // Setup the arcball parameters

read more »


Multiple Sprites/Meshes

Post by Kev » Tue, 28 May 2002 16:46:00

Youve got three pointers to meshes right there already :-)

You only really need one mesh in memory for each unique object. To have 10
forklift/pallet/rack combos (for example) use a struct or class... each
with its own position/rotation/size info, etc etc etc... and pointers back
to the meshes. Check out the billboarding sample or the point sprites
sample (for D3D) or sprite animate (DD). Different situations but the jist
of it is still the same. Also... go to and places like that to
get tutorials and reviews of good books to get.

Quote:>      CD3DMesh* m_pForkLift;
>      CD3DMesh* m_pPallet;
>      CD3DMesh* m_pRack;


1. Multiple meshes from a particle system?

I'm trying to create an animation of an asteroid belt
around a planet.

So I create a rock and assign it to a particle system
using follow path around the planet.

The asteroids can slowly float by and rotate and are
scaled differently but they are the same.

So maybe can mix it up by creating, say, 10 different
asteroids. But if I assign them to 10 different particle
systems than the asteroids from the different systems
can move through each other. Not realistic.

There doesn't seem to be a way to assign 10 different
meshes to the same system.

Using scatter makes everything static across the distribution
object. Is there some way to have random rotations on the
scattered objects along the tube?
If so I can scatter along 10 instances of a tube distribution
object and offset the tubes so the asteroids
don't overlap and animate the tubes to slowly rotate.

Suggestions for how I can have different asteroids floating
around my planet??

Thanks for any help/suggestions.

2. palette installation?

3. Max, Mesh Paint, and Multiple Surfaces

4. Layer Planes?

5. Single mesh..multiple surfaces?

6. Rotating Picture in DirectDraw

7. Applying Multiple Giz Files on a Single Mesh Character

8. Did someone have the .max format description. Thanks a lot.

9. Merging multiple meshes

10. Fitting textures/sprites into a texture/sprite page

11. Multiple textures on a single mesh

12. Triangle Sprite vs quad sprite

13. URGENT HELP multiple mesh at once