-
September 24th, 2012, 01:53 AM
#1
Basic Data Persistance question
Code:
struct Mesh
{
public:
// a pointer to a series of frames
LPD3DXFRAME m_Root;
};
struct Shape
{
// Get the best mesh suited for the camera's viewpoint, allows LOD.
virtual const Mesh* getMesh( const CCamera& camera ) const = 0;
virtual void setMesh(Mesh *mesh) = 0;
};
struct WarehouseShape : public Shape
{
const Mesh* getMesh( const CCamera& camera ) const { return m_pMesh; }
void setMesh(Mesh *mesh) { m_pMesh = mesh; }
Mesh *m_pMesh;
};
struct Object
{
Shape* shape;
bool Load(TCHAR* pFilename);
D3DXMATRIX transform;
LPD3DXANIMATIONCONTROLLER m_pAnimCtrl;
};
bool Object::Load(TCHAR* pFilename)
{
LPD3DXFRAME frames;
CAllocMeshHierarchy Alloc;
HRESULT hr = E_FAIL;
D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
&Alloc, NULL, &frames, &m_pAnimCtrl );
Mesh mesh;
// Set the whole bunch of frames
// have tried to copy it using memcpy (&mesh.m_Root, &frames, sizeof(frames));
mesh.m_Root = frames;
shape = new WarehouseShape();
shape->setMesh(&mesh);
//shape->setMesh(frames);
return true;
}
extern CD3DRenderer g_D3DRenderer;
extern LPDIRECT3DDEVICE9 g_pDevice;
class Scene
{
public:
Scene() { SetCurCam(0); }
~Scene() { }
void Load()
{
m_Cam[0].Load(g_pDevice, _T("Data\\Others\\Demo1.cam"));
m_Cam[1].Load(g_pDevice, _T("Data\\Others\\Demo2.cam"));
m_Cam[2].Load(g_pDevice, _T("Data\\Others\\Demo3.cam"));
m_WarehouseObject.Load(_T("Data\\DemoWarehouse.x"));
m_pRenderObjects.push_back(&m_WarehouseObject);
}
void SetCurCam(int i)
{
m_iCurCam = i;
}
void render()
{
for (int i = 0; i < m_pRenderObjects.size(); i++)
{
const Mesh& mesh = *m_pRenderObjects[i]->shape->getMesh(m_Cam[m_iCurCam]);
Matrix4 mat = m_pRenderObjects[i]->transform;
g_D3DRenderer.render(mesh, mat, m_Cam[m_iCurCam]);
}
}
Object m_WarehouseObject;
Object m_CBObject;
Object m_OperatorObject;
CCamera m_Cam[3];
int m_iCurCam;
std::vector<Object *> m_pRenderObjects;
};
Problem identified:
for (int i = 0; i < m_pRenderObjects.size(); i++)
{
const Mesh& mesh = *m_pRenderObjects[i]->shape->getMesh(m_Cam[m_iCurCam]);
Matrix4 mat = m_pRenderObjects[i]->transform;
g_D3DRenderer.render(mesh, mat, m_Cam[m_iCurCam]);
}
shape has a value of 0xcccccccc
Does anyone know where the problem is?
Thanks
Jack
-
September 24th, 2012, 02:56 AM
#2
Re: Basic Data Persistance question
-
September 24th, 2012, 03:31 AM
#3
Re: Basic Data Persistance question
I discover that when I setMesh(&mesh);
I cannot transfer the whole object (deep copy) of the Mesh object to "Shape*"
Where should I put a copy constructor to?
Thanks
Jack
-
September 24th, 2012, 03:59 AM
#4
Re: Basic Data Persistance question
If I don't remember wrong you have been told in other threads to use stl containers and smart pointers instead of trying to handle dynamic memory yourself?
-
September 24th, 2012, 04:11 AM
#5
Re: Basic Data Persistance question
Yes, I was. I tried my best and still cannot completely grasp the concept. Sorry for the waste of effort. :*(
Smart pointers would manage the copying of resources?
Also, should I replace raw pointers with shared_ptr's as much as possible?
Thirdly, shared_ptr's would share the ownerships between multiple objects?
I don't quite get the benefits of smart pointers anyways in general if it still collapses the program when I use it
Last edited by lucky6969b; September 24th, 2012 at 04:26 AM.
-
September 24th, 2012, 11:34 AM
#6
Re: Basic Data Persistance question
Originally Posted by lucky6969b
Does anyone know where the problem is?
The problem is that shape has an invalid pointer.
Code:
bool Object::Load(TCHAR* pFilename)
{
LPD3DXFRAME frames;
CAllocMeshHierarchy Alloc;
HRESULT hr = E_FAIL;
D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
&Alloc, NULL, &frames, &m_pAnimCtrl );
Mesh mesh;
mesh.m_Root = frames;
shape = new WarehouseShape();
shape->setMesh(&mesh);
return true;
}
What you see in red are all local variables and your usage of them. When that Load function returns, those variables no longer exist. Are you still attempting to use "frames" or "mesh" in another function? If so, then that is no good. Local variables are just that -- local. Trying to point to them and use them after they've gone up in smoke is not going to work.
Also, you should have posted a main() program showing exactly how you're using these classes. Just showing classes and code where we have no idea what happened previously doesn't help.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 24th, 2012 at 12:04 PM.
-
September 24th, 2012, 11:59 AM
#7
Re: Basic Data Persistance question
I discover that when I setMesh(&mesh);
I cannot transfer the whole object (deep copy) of the Mesh object to "Shape*"
All I see is a function called "setMesh" that takes a pointer to a Mesh object and all it does is assign the passed-in pointer to a member variable. What does this have to do with a copy constructor?
Regards,
Paul McKenzie
-
September 24th, 2012, 11:41 PM
#8
Re: Basic Data Persistance question
Originally Posted by Paul McKenzie
The problem is that shape has an invalid pointer.
What you see in red are all local variables and your usage of them. When that Load function returns, those variables no longer exist. Are you still attempting to use "frames" or "mesh" in another function? If so, then that is no good. Local variables are just that -- local. Trying to point to them and use them after they've gone up in smoke is not going to work.
Also, you should have posted a main() program showing exactly how you're using these classes. Just showing classes and code where we have no idea what happened previously doesn't help.
Code:
//
#include "stdafx.h"
#include "PerfectSim V5.0.h"
#include "CMyApp.h"
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
CMyApp GameApp;
if(!GameApp.Create(hInstance))
return false;
if(!GameApp.Run())
return false;
return false;
}
if I put Mesh mesh inside Object, it seems to be redundant, cos Shape* is already storing a reference to the Mesh.
Where should I store the actual solid object of Mesh, in your opinion?
Thanks
Jack
-
September 25th, 2012, 04:47 AM
#9
Re: Basic Data Persistance question
Originally Posted by lucky6969b
Where should I store the actual solid object of Mesh, in your opinion?
The issue is that your "solid object" is not solid. It is a local variable that goes out of scope on return. The code you wrote is honestly just plain wrong. It isn't even a matter of design -- you can't return or refer to addresses or references to local variables after the function block that declares them is exited.
Either create it dynamically (preferably using a smart pointer), or one of your objects must have as a member a Mesh, so that it does not go out of scope.
Code:
#include <memory>
typedef std::shared_ptr<Mesh> MeshPtr;
typedef std::shared_ptr<Shape> ShapePtr;
struct WarehouseShape : public Shape
{
const Mesh* getMesh( const CCamera& camera ) const { return m_pMesh.get(); }
void setMesh(MeshPtr mesh) { m_pMesh = mesh; }
MeshPtr m_pMesh;
};
struct Object
{
ShapePtr shape;
LPD3DXFRAME m_frames;
MeshPtr m_Mesh;
bool Load(TCHAR* pFilename);
D3DXMATRIX transform;
LPD3DXANIMATIONCONTROLLER m_pAnimCtrl;
};
//...
bool Object::Load(TCHAR* pFilename)
{
CAllocMeshHierarchy Alloc;
HRESULT hr = E_FAIL;
D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
&Alloc, NULL, &m_frames, &m_pAnimCtrl );
m_mesh = MeshPtr(new Mesh);
m_mesh->m_Root = frames;
shape = ShapePtr(new WarehouseShape());
shape->setMesh(mesh);
return true;
}
And this is just off the top of my head.
Regards,
Paul McKenzie
-
September 27th, 2012, 04:00 AM
#10
Re: Basic Data Persistance question
Thanks Paul, I'll think thru your code snippet.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|