Sign in to follow this  
lucky6969b

How do I "physically" scale up a ID3DXMESH?

Recommended Posts

What I mean here is if say I have a mesh who has these values

FrameTransformMatrix relative {
  1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,-11.184723,0.160751,30.519821,1.000000;;
 }
 
 ObjectMatrixComment object {
              1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000;;
 }
 
Mesh mesh_T {
  5114;
  2.035625;0.317421;13.026318;,
  1.984682;0.317421;13.027741;,
  1.984682;0.000000;13.027741;,
  2.035625;0.000000;13.026318;,
  2.082130;0.317421;13.022042;,
  2.082130;0.000000;13.022042;,
  2.124199;0.317421;13.014919;,
  2.124199;0.000000;13.014919;,

Where I want to make it 100 times larger.

I am not to say that to enlarge it at render times.

I mean to have the mesh absolute values reflected in the "actual" ID3DXMESH structure.

so that the matrix would have different values at load times and when it gets stored in memory.

How can I achieve that?

Thanks

Jack

void SetScale(float x, float y, float z) { m_Scale.x = x; m_Scale.y = y; m_Scale.z = z; }
    void Update() {
        D3DXMATRIX mat;
        D3DXMatrixIdentity(&mat);
        D3DXMatrixScaling(&mat, m_Scale.x, m_Scale.y, m_Scale.z);
        //??

    }
Edited by lucky6969b

Share this post


Link to post
Share on other sites

Hi Spiro,

How can I do that?

Here is my code for Mesh.

Note that I used both D3DXLoadMeshFromX and D3DXLoadMeshHierarchyFromX...

So I think the mesh(es) should be handled in both places.

 
#pragma once

#include <d3d9.h>
#include <d3dx9core.h>    

#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

struct FRAME : public D3DXFRAME
{
    D3DXMATRIX matCombined;

    FRAME() {
        Name = NULL;
        pMeshContainer = NULL;
        D3DXMatrixIdentity(&matCombined);
        D3DXMatrixIdentity(&TransformationMatrix);
        pFrameFirstChild = NULL;
        pFrameSibling = NULL;
    }
};

struct MESHCONTAINER : public D3DXMESHCONTAINER
{    

    ~MESHCONTAINER()
    {
    }
    LPDIRECT3DTEXTURE9*  ppTextures;       // array of textures, entries are NULL if no texture specified    

    // SkinMesh info             
    LPD3DXMESH                pOrigMesh;
    LPD3DXATTRIBUTERANGE    pAttributeTable;
    DWORD                    dwNumAttributeGroups;
    DWORD                    dwNumInfl;
    DWORD                    dwNumBones;
    LPD3DXBUFFER            pBoneCombinationBuf;
    D3DXMATRIX**            ppBoneMatrixPtrs;
    D3DXMATRIX*                pBoneOffsetMatrices;
    
    // for software skinning
    D3DXMATRIX*                pBoneMatrices;
    DWORD                    dwNumPaletteEntries;

    bool                    bUseSoftwareVP;
    DWORD                    dwAttributeSW;     // used to denote the split between SW and HW if necessary for non-indexed skinning
};

class CAllocateHierarchy: public ID3DXAllocateHierarchy
{
private:
    STDMETHOD(CreateFrame)(THIS_ LPCSTR Name, LPD3DXFRAME *ppNewFrame);
    STDMETHOD(CreateMeshContainer)(THIS_ LPCSTR Name, CONST D3DXMESHDATA * pMeshData, CONST D3DXMATERIAL * pMaterials, CONST D3DXEFFECTINSTANCE * pEffectInstances, DWORD NumMaterials, CONST DWORD * pAdjacency, LPD3DXSKININFO pSkinInfo, LPD3DXMESHCONTAINER * ppNewMeshContainer);
    STDMETHOD(DestroyFrame)(THIS_ LPD3DXFRAME pFrameToFree);
    STDMETHOD(DestroyMeshContainer)(THIS_ LPD3DXMESHCONTAINER pMeshContainerBase);    
};

class CMesh
{
public:
    CMesh(void);
    ~CMesh(void);

    bool Load(LPDIRECT3DDEVICE9 pDevice, char *pFilename, bool bVertexBlending = false);
    void SaveHierarchyToFile();
    bool IsVertexBlending()        { return m_bEnableVertexBlending; }    

    LPD3DXMESH GetStaticMesh()     { return m_pStaticMesh; }

    // frame root and animation controller
    LPD3DXFRAME                    GetFrameRoot()        { return m_pFrameRoot; }
    LPD3DXANIMATIONCONTROLLER   GetAnimController() { return m_pAnimController; }

    // bounding volume
    D3DXVECTOR3 GetCenter() { return m_vSphereCenter; }
    float        GetRadius() { return m_fSphereRadius; }
    void        GetBox(D3DXVECTOR3 *vMin, D3DXVECTOR3 *vMax) { *vMin = m_vBoxMin; *vMax = m_vBoxMax; }

    LPD3DXMESH GetBoxMesh()        { return m_pBoundingBox; }
    LPD3DXMESH GetSphereMesh()    { return m_pBoundingSphere; }

    void SetScale(float x, float y, float z) { m_Scale.x = x; m_Scale.y = y; m_Scale.z = z; }
    void Update() {
        D3DXMATRIX mat;
        D3DXMatrixIdentity(&mat);
        D3DXMatrixScaling(&mat, m_Scale.x, m_Scale.y, m_Scale.z);
        
    }

private:
    void PrintFrame(D3DXFRAME *pCurFrame);    
    bool GenerateSkinnedMesh(LPD3DXFRAME pCurFrame);
    bool MapFramesToBones(LPD3DXFRAME pCurFrame);    
    bool InitBounds();

private:
    LPDIRECT3DDEVICE9 m_pDevice;
    
    bool m_bEnableVertexBlending;

    LPD3DXFRAME                    m_pFrameRoot;
    LPD3DXANIMATIONCONTROLLER    m_pAnimController;
    
    LPD3DXMESH                    m_pStaticMesh;

    DWORD                        m_dwNumMeshes;

    // bounding volume properties
    LPD3DXMESH    m_pBoundingBox;
    LPD3DXMESH    m_pBoundingSphere;    
    D3DXVECTOR3 m_vBoxMin, m_vMind;
    D3DXVECTOR3 m_vBoxMax, m_vMaxd;
    D3DXVECTOR3 m_vSphereCenter, m_vCenterd;  // Center of bounding sphere of object
    float       m_fSphereRadius, m_fRadiusd;  // Radius of bounding sphere of object

    // miscellaneous
    string     m_DebugString;
    char     *m_pFilename;

    D3DXVECTOR3 m_Scale;
    

public:
    D3DXVECTOR3 getPosition(std::string name) {    
        
        D3DXMATRIX m = D3DXFrameFind(m_pFrameRoot, name.c_str())->TransformationMatrix;
        return D3DXVECTOR3(m.m[3][0], m.m[3][1], m.m[3][2]);
        
    }
};
#include "CMesh.h"

HRESULT CAllocateHierarchy::CreateFrame(LPCSTR Name, LPD3DXFRAME *ppNewFrame)
{    
    *ppNewFrame = NULL;

    FRAME *pFrame = new FRAME;

    if(Name != NULL)
    {
        pFrame->Name = new TCHAR[strlen(Name) + 1];
        strcpy((char*)pFrame->Name, (const char*)Name);
    }
    else
    {
        pFrame->Name = new TCHAR[10];
        strcpy((char*)pFrame->Name, "Unnamed");
    }

    // initialize other data members of the frame
    D3DXMatrixIdentity(&pFrame->TransformationMatrix);
    D3DXMatrixIdentity(&pFrame->matCombined);

    pFrame->pMeshContainer = NULL;
    pFrame->pFrameSibling = NULL;
    pFrame->pFrameFirstChild = NULL;

    *ppNewFrame = pFrame;
    pFrame = NULL;

    return S_OK;
}

HRESULT CAllocateHierarchy::CreateMeshContainer(LPCSTR Name,
                                            CONST D3DXMESHDATA *pMeshData,
                                            CONST D3DXMATERIAL *pMaterials,
                                            CONST D3DXEFFECTINSTANCE *pEffectInstances,
                                            DWORD NumMaterials,
                                            CONST DWORD *pAdjacency,
                                            LPD3DXSKININFO pSkinInfo,
                                            LPD3DXMESHCONTAINER *ppNewMeshContainer)
{
    *ppNewMeshContainer = NULL;

    MESHCONTAINER *pMeshContainer = new MESHCONTAINER;    
    ZeroMemory(pMeshContainer, sizeof(MESHCONTAINER));

    // get device
    LPDIRECT3DDEVICE9 pDevice;
    pMeshData->pMesh->GetDevice(&pDevice);


    if(Name != NULL)
    {
        pMeshContainer->Name = new TCHAR[strlen(Name) + 1];
        strcpy((char*)pMeshContainer->Name, (const char*)Name);
    }
    else
    {
        pMeshContainer->Name = new TCHAR[10];
        strcpy((char*)pMeshContainer->Name, "Unnamed");
    }

    // store mesh
    pMeshContainer->MeshData.Type = D3DXMESHTYPE_MESH;    
     // if no normals are in the mesh, add them
    if (!(pMeshData->pMesh->GetFVF() & D3DFVF_NORMAL))
    {
        // clone the mesh to make room for the normals
        pMeshData->pMesh->CloneMeshFVF( pMeshData->pMesh->GetOptions(),
                                    pMeshData->pMesh->GetFVF() | D3DFVF_NORMAL,
                                    pDevice, &pMeshContainer->MeshData.pMesh );


        // now generate the normals for the pmesh
        D3DXComputeNormals( pMeshContainer->MeshData.pMesh, NULL );
    }
    else  // if no normals, just add a reference to the mesh for the mesh container
    {
        pMeshContainer->MeshData.pMesh = pMeshData->pMesh;        
        pMeshContainer->MeshData.pMesh->AddRef();
    }    

    DWORD dwNumFaces = pMeshData->pMesh->GetNumFaces();

    // allocate memory to contain the material information.  This sample uses
    // the D3D9 materials and texture names instead of the EffectInstance style materials
    pMeshContainer->NumMaterials = max(1, NumMaterials);
    pMeshContainer->pMaterials = new D3DXMATERIAL[pMeshContainer->NumMaterials];
    pMeshContainer->ppTextures = new LPDIRECT3DTEXTURE9[pMeshContainer->NumMaterials];
    pMeshContainer->pAdjacency = new DWORD[dwNumFaces * 3];

    memcpy(pMeshContainer->pAdjacency, pAdjacency, sizeof(DWORD) * dwNumFaces * 3);
    memset(pMeshContainer->ppTextures, 0, sizeof(LPDIRECT3DTEXTURE9) * pMeshContainer->NumMaterials);

    // if materials provided, copy them
    if (NumMaterials > 0)            
    {
        memcpy(pMeshContainer->pMaterials, pMaterials, sizeof(D3DXMATERIAL) * NumMaterials);
        // copy ambient color
//        pMeshContainer->pMaterials->MatD3D.Ambient =
//            pMeshContainer->pMaterials->MatD3D.Diffuse;

        for (int iMaterial = 0; iMaterial < NumMaterials; iMaterial++)
        {
            if (pMeshContainer->pMaterials[iMaterial].pTextureFilename != NULL)
            {
                // store the texture filename instead,
                // we will load the texture later on the GenerateSkinnedMesh function
                // so we could append the texture path
                pMeshContainer->pMaterials[iMaterial].pTextureFilename = new char[strlen(pMaterials[iMaterial].pTextureFilename) + 1];
                strcpy(pMeshContainer->pMaterials[iMaterial].pTextureFilename, pMaterials[iMaterial].pTextureFilename);
            }
        }
    }
    else // if no materials provided, use a default one
    {
        pMeshContainer->pMaterials[0].pTextureFilename = NULL;
        memset(&pMeshContainer->pMaterials[0].MatD3D, 0, sizeof(D3DMATERIAL9));        
        pMeshContainer->pMaterials[0].MatD3D.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
    }

    // if there is skinning information, save off the required data and then setup for HW skinning
    if (pSkinInfo != NULL)
    {
        // first save off the SkinInfo and original mesh data
        pMeshContainer->pSkinInfo = pSkinInfo;
        pMeshContainer->pSkinInfo->AddRef();

        pMeshContainer->pOrigMesh = pMeshData->pMesh;
        pMeshContainer->pOrigMesh->AddRef();        

        // Will need an array of offset matrices to move the vertices from the figure space to the bone's space
        DWORD dwNumBones = pSkinInfo->GetNumBones();
        pMeshContainer->pBoneOffsetMatrices = new D3DXMATRIX[dwNumBones];

        // get each of the bone offset matrices so that we don't need to get them later
        for (DWORD iBone = 0; iBone < dwNumBones; iBone++)
        {
            pMeshContainer->pBoneOffsetMatrices[iBone] = *(pMeshContainer->pSkinInfo->GetBoneOffsetMatrix(iBone));
        }
    }

    *ppNewMeshContainer = pMeshContainer;
    pMeshContainer = NULL;

    pDevice->Release();
    pDevice = NULL;

    return S_OK;
}

HRESULT CAllocateHierarchy::DestroyFrame(LPD3DXFRAME pFrameToFree)
{    
    delete [] pFrameToFree->Name;
    pFrameToFree->Name = NULL;

    delete pFrameToFree;
    pFrameToFree = NULL;

    return S_OK;
}

#define SAFE_DELETE(p)       { if(p) { delete (p);     (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
#define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }

HRESULT CAllocateHierarchy::DestroyMeshContainer(LPD3DXMESHCONTAINER pMeshContainerBase)
{
    UINT iMaterial;
    MESHCONTAINER *pMeshContainer = (MESHCONTAINER*)pMeshContainerBase;

    SAFE_DELETE_ARRAY( pMeshContainer->Name );
    SAFE_DELETE_ARRAY( pMeshContainer->pAdjacency );
    SAFE_DELETE_ARRAY( pMeshContainer->pMaterials );
    SAFE_DELETE_ARRAY( pMeshContainer->pBoneOffsetMatrices );

    // release all the allocated textures
    if (pMeshContainer->ppTextures != NULL)
        for (iMaterial = 0; iMaterial < pMeshContainer->NumMaterials; iMaterial++)
            SAFE_RELEASE( pMeshContainer->ppTextures[iMaterial] );

    // not found in skinned mesh
    SAFE_DELETE_ARRAY(pMeshContainer->pAttributeTable);
    SAFE_DELETE_ARRAY( pMeshContainer->pBoneMatrices );

    SAFE_DELETE_ARRAY( pMeshContainer->ppTextures );
    SAFE_DELETE_ARRAY( pMeshContainer->ppBoneMatrixPtrs );    
    SAFE_RELEASE( pMeshContainer->pBoneCombinationBuf );        
    SAFE_RELEASE( pMeshContainer->MeshData.pMesh );
    SAFE_RELEASE( pMeshContainer->MeshData.pPatchMesh );
    SAFE_RELEASE( pMeshContainer->MeshData.pPMesh );
    SAFE_RELEASE( pMeshContainer->pSkinInfo );    
    SAFE_RELEASE( pMeshContainer->pOrigMesh );

    SAFE_DELETE( pMeshContainer );
    return S_OK;
}

bool CMesh::GenerateSkinnedMesh(LPD3DXFRAME pCurFrame)
{
    if(pCurFrame == NULL)
        return true;

    // init skinned mesh
    MESHCONTAINER *pCurMeshContainer = (MESHCONTAINER*)pCurFrame->pMeshContainer;
    while(pCurMeshContainer != NULL)
    {
        // check if skinned mesh
        if(pCurMeshContainer->pSkinInfo != NULL)
        {
            if (m_bEnableVertexBlending)
            {
                // release since we had a reference a while back
                pCurMeshContainer->MeshData.pMesh->Release();

                if(FAILED(pCurMeshContainer->pSkinInfo->ConvertToBlendedMesh(
                    pCurMeshContainer->pOrigMesh,
                    D3DXMESH_MANAGED|D3DXMESHOPT_VERTEXCACHE,
                    pCurMeshContainer->pAdjacency,
                    NULL, NULL, NULL,
                    &pCurMeshContainer->dwNumInfl,
                    &pCurMeshContainer->dwNumAttributeGroups,
                    &pCurMeshContainer->pBoneCombinationBuf,
                    &pCurMeshContainer->MeshData.pMesh
                    )))
                    return false;            
            }
            else
            {
                // release since we had a reference a while back
                pCurMeshContainer->MeshData.pMesh->Release();
                
                pCurMeshContainer->pOrigMesh->CloneMeshFVF(
                    D3DXMESH_MANAGED, pCurMeshContainer->pOrigMesh->GetFVF(),
                    m_pDevice, &pCurMeshContainer->MeshData.pMesh);

                pCurMeshContainer->MeshData.pMesh->GetAttributeTable(NULL, &pCurMeshContainer->dwNumAttributeGroups);

                delete [] pCurMeshContainer->pAttributeTable;
                pCurMeshContainer->pAttributeTable  = new D3DXATTRIBUTERANGE[pCurMeshContainer->dwNumAttributeGroups];

                pCurMeshContainer->MeshData.pMesh->GetAttributeTable(pCurMeshContainer->pAttributeTable, NULL);

                pCurMeshContainer->dwNumBones = pCurMeshContainer->pSkinInfo->GetNumBones();

                // Allocate space for blend matrices            
                pCurMeshContainer->pBoneMatrices  = new D3DXMATRIX[pCurMeshContainer->dwNumBones];        
            }
        }

        // load textures, it's here so i could construct the full path for the texture
        for (DWORD iMaterial = 0; iMaterial < pCurMeshContainer->NumMaterials; iMaterial++)
        {            
            if (pCurMeshContainer->pMaterials[iMaterial].pTextureFilename != NULL)
            {
                // create path for texture
                char *pTexturePath = NULL;
                // search last occurance of '\'
                char *pCh = strrchr(m_pFilename, '\\');
                if(pCh != NULL) // attach                {
                {
                    // get offset from last occurance of '\'
                    int iOffset = pCh - m_pFilename + 1;
                    pTexturePath = new char[iOffset + strlen(pCurMeshContainer->pMaterials[iMaterial].pTextureFilename) + 1];
                    memcpy(pTexturePath, m_pFilename, iOffset);
                    pTexturePath[iOffset] = 0; // add nul terminator
                    strcat(pTexturePath, pCurMeshContainer->pMaterials[iMaterial].pTextureFilename);
                }
                // then the texture filename must be on the same directory of the current app
                else
                {
                    pTexturePath = new char[strlen(pCurMeshContainer->pMaterials[iMaterial].pTextureFilename) + 1];
                    strcpy(pTexturePath, pCurMeshContainer->pMaterials[iMaterial].pTextureFilename);
                }

                if( FAILED( D3DXCreateTextureFromFile( m_pDevice, pTexturePath,
                    &pCurMeshContainer->ppTextures[iMaterial] ) ) )
                    pCurMeshContainer->ppTextures[iMaterial] = NULL;

                // now we delete
                delete [] pTexturePath;
                pTexturePath = NULL;
                // we allocated awhile ago while loading the mesh
                delete [] pCurMeshContainer->pMaterials[iMaterial].pTextureFilename;
                    pCurMeshContainer->pMaterials[iMaterial].pTextureFilename = NULL;
            }
        }
        m_dwNumMeshes++;
        pCurMeshContainer = (MESHCONTAINER*)pCurMeshContainer->pNextMeshContainer;
    }
    
    if(pCurFrame->pFrameFirstChild != NULL)
        if(!GenerateSkinnedMesh(pCurFrame->pFrameFirstChild))
            return false;

    if(pCurFrame->pFrameSibling != NULL)
        if(!GenerateSkinnedMesh(pCurFrame->pFrameSibling))
            return false;

    return true;
}

bool CMesh::MapFramesToBones(LPD3DXFRAME pCurFrame)
{
    UINT iBone, cBones;
    FRAME *pFrame = (FRAME*)pCurFrame;

    if(pCurFrame->pMeshContainer != NULL)
    {
        MESHCONTAINER *pMeshContainer = (MESHCONTAINER*)pCurFrame->pMeshContainer;        

        // if there is a skinmesh , then setup the bone matrices
        if (pMeshContainer->pSkinInfo != NULL)
        {
            cBones = pMeshContainer->pSkinInfo->GetNumBones();
            pMeshContainer->ppBoneMatrixPtrs = new D3DXMATRIX*[cBones];

            for(iBone = 0; iBone < cBones; iBone++)
            {
                // get a pointer to the frame
                pFrame = (FRAME*)D3DXFrameFind(m_pFrameRoot, pMeshContainer->pSkinInfo->GetBoneName(iBone));
                // get a pointer to the frame's combined matrix
                pMeshContainer->ppBoneMatrixPtrs[iBone] = &pFrame->matCombined;
            }
        }        
    }    

    if (pCurFrame->pFrameFirstChild != NULL)
        MapFramesToBones(pCurFrame->pFrameFirstChild);

    if (pCurFrame->pFrameSibling != NULL)   
        MapFramesToBones(pCurFrame->pFrameSibling);

    return true;
}

CMesh::CMesh(void) :
m_vBoxMin(0.0f, 0.0f, 0.0f),
m_vBoxMax(0.0f, 0.0f, 0.0f),
m_vSphereCenter(0.0f, 0.0f, 0.0f)
{
    m_pFrameRoot        = NULL;
    m_pAnimController    = NULL;
    m_pStaticMesh        = NULL;

    m_dwNumMeshes        = 0;    
    m_fSphereRadius        = 0;

    m_pFilename            = NULL;
    m_pBoundingSphere    = NULL;
    m_pBoundingBox        = NULL;
}

CMesh::~CMesh(void)
{
    CAllocateHierarchy Alloc;
    D3DXFrameDestroy(m_pFrameRoot, &Alloc);    

    if(m_pAnimController != NULL)
    {
        m_pAnimController->Release();
        m_pAnimController = NULL;
    }
    if(m_pBoundingSphere != NULL)
    {
        m_pBoundingSphere->Release();
        m_pBoundingSphere = NULL;
    }
    if(m_pBoundingBox != NULL)
    {
        m_pBoundingBox->Release();
        m_pBoundingBox = NULL;
    }
    if(m_pStaticMesh != NULL)
    {
        m_pStaticMesh->Release();
        m_pStaticMesh = NULL;
    }
    if(m_pFilename != NULL)
    {
        delete [] m_pFilename;
        m_pFilename = NULL;
    }
}

#include <stack>
using namespace std;

bool CMesh::Load(LPDIRECT3DDEVICE9 pDevice, char *pFilename, bool bVertexBlending)
{
    m_pDevice = pDevice;
    m_bEnableVertexBlending = bVertexBlending;
    char cError[128];

    // store filename
    m_pFilename = new char[strlen(pFilename) + 1];
    strcpy(m_pFilename, pFilename);

    CAllocateHierarchy Alloc;

    if(SUCCEEDED(D3DXLoadMeshHierarchyFromX(pFilename,
        D3DXMESH_MANAGED, m_pDevice, &Alloc, NULL, &m_pFrameRoot, &m_pAnimController)))
    {
        // load it also as a static mesh
        D3DXLoadMeshFromX(pFilename, D3DXMESH_MANAGED, m_pDevice,
        NULL, NULL, NULL, NULL, &m_pStaticMesh);
    }
    else
    {
        sprintf(cError, "Failed to load mesh: %s", pFilename);
        throw(cError);
    }    

    // create skinned mesh
    if(!GenerateSkinnedMesh(m_pFrameRoot))
        return false;

    MapFramesToBones(m_pFrameRoot);    

//    if(FAILED(D3DXFrameCalculateBoundingSphere(m_pFrameRoot,
//        &m_vSphereCenter, &m_fSphereRadius)))
//        return false;    

    if(!InitBounds())
        return false;

    return true;
}

bool CMesh::InitBounds()
{    
    D3DXVECTOR3 vBoxMin, vBoxMax;
    float fRadius;
    LPD3DXFRAME pCurFrame = m_pFrameRoot;
    LPD3DXMESHCONTAINER pCurMesh;
    stack<LPD3DXFRAME> st;
    st.push(pCurFrame);
    while(!st.empty())
    {
        pCurFrame = st.top();
        st.pop();
        if(pCurFrame != NULL)
        {
            pCurMesh = pCurFrame->pMeshContainer;
            while(pCurMesh != NULL)
            {
                if(pCurMesh->MeshData.pMesh != NULL)
                {
                    BYTE *pVertices;
                    pCurMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);                
                    D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, pCurMesh->MeshData.pMesh->GetNumVertices(),
                        D3DXGetFVFVertexSize(pCurMesh->MeshData.pMesh->GetFVF()), &vBoxMin, &vBoxMax );
                    // m_vSphere center is dummy, dx function won't work if not filled
                    D3DXComputeBoundingSphere((D3DXVECTOR3*)pVertices, pCurMesh->MeshData.pMesh->GetNumVertices(),
                        D3DXGetFVFVertexSize(pCurMesh->MeshData.pMesh->GetFVF()), &m_vSphereCenter, &fRadius);
                    pCurMesh->MeshData.pMesh->UnlockVertexBuffer();

                    m_vBoxMin.x = min(m_vBoxMin.x, vBoxMin.x);
                    m_vBoxMin.y = min(m_vBoxMin.y, vBoxMin.y);
                    m_vBoxMin.z = min(m_vBoxMin.z, vBoxMin.z);
                    m_vBoxMax.x = max(m_vBoxMax.x, vBoxMax.x);
                    m_vBoxMax.y = max(m_vBoxMax.y, vBoxMax.y);
                    m_vBoxMax.z = max(m_vBoxMax.z, vBoxMax.z);
                    m_fSphereRadius = max(m_fSphereRadius, fRadius);
                }
                pCurMesh = pCurMesh->pNextMeshContainer;
            }
            st.push(pCurFrame->pFrameFirstChild);
            st.push(pCurFrame->pFrameSibling);            
        }
    }
    m_vSphereCenter = (m_vBoxMax + m_vBoxMin) / 2.0f;        

    BYTE* pVertices;
    m_pStaticMesh->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices);
    D3DXComputeBoundingBox( (D3DXVECTOR3*)pVertices, m_pStaticMesh->GetNumVertices(),
        D3DXGetFVFVertexSize(m_pStaticMesh->GetFVF()), &m_vMind, &m_vMaxd );
    D3DXComputeBoundingSphere((D3DXVECTOR3*)pVertices, m_pStaticMesh->GetNumVertices(),
        D3DXGetFVFVertexSize(m_pStaticMesh->GetFVF()), &m_vCenterd, &m_fRadiusd);
    m_pStaticMesh->UnlockVertexBuffer();            

    D3DXCreateBox(m_pDevice, fabs(m_vBoxMax.x - m_vBoxMin.x), fabs(m_vBoxMax.y - m_vBoxMin.y),
        fabs(m_vBoxMax.z - m_vBoxMin.z), &m_pBoundingBox, NULL);
    D3DXCreateSphere(m_pDevice, m_fSphereRadius, 12, 12, &m_pBoundingSphere, NULL);

    D3DXComputeNormals(m_pBoundingBox, NULL);
    D3DXComputeNormals(m_pBoundingSphere, NULL);

    return true;
}

Thanks


 

Edited by lucky6969b

Share this post


Link to post
Share on other sites

After you load the mesh, call LockVertexBuffer and cast the pointer to an appropriately shaped type. It may be easiest to use a char*, incrementing by GetNumBytesPerVertex each time, and cast to a float* to get the position data. Then you can just multiply the values.

 

You should be aware that this is going to force a copy to (and potentially from) GPU memory. 

Share this post


Link to post
Share on other sites

Hi.

And after edit of the mesh call

CommitToDevice(); on the mesh, Because it holds a software buffer you change first if I remember correctly.

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