DrawPrimitiveUP and data from a vector
Hi everyone,
I'm currently loading model files. I read out the vertex and face data and saved them each in a vector.
Now I wanted to draw the faces onto screen using dx9 and DrawPrimitiveUP. But I need a 'user memory pointer to the vertex data'. But how can I convert from 'std::vector' to 'const void*'???
thx for help!
cu
std::vector<vertex_type> vertices;
// fill vertices
...
device->DrawPrimitiveUP(..., &vertices[0], ...);
// fill vertices
...
device->DrawPrimitiveUP(..., &vertices[0], ...);
No, that doesn't work, but maybe it's my fault:
Maybe there is some sizeof mistake in the following code
// MODEL LOADING CODE
...
SModelMesh *pMesh = new SModelMesh;
pMesh->Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
pMesh->Position = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
pMesh->Position = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
delete pMesh;
if(FAILED(Engine.Window.m_pD3DDevice->CreateVertexBuffer(3*sizeof(SModelMesh),
0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&Model.VBuffer,
NULL)))
{
// Error --> Exit!
Engine.Exit();
}
VOID* pVertices;
if(FAILED(Model.VBuffer->Lock(0,
3*sizeof(SModelMesh),
(void**)&pVertices,
0)))
{
// Error --> Exit!
Engine.Exit();
}
memcpy(pVertices,
&Model.Mesh,
3*sizeof(SModelMesh));
Model.VBuffer->Unlock();
...
// END OF MODEL LOADING CODE
// RENDERING CODE
...
for(int index = 0; index < Models.m_NumModels; index++)
{
Engine.Window.m_pD3DDevice->SetStreamSource(0,
Models.GetModelVB(index),
0,
sizeof(SModelMesh));
Engine.Window.m_pD3DDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
Engine.Window.m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,
1,
Models.GetModelMesh(index),
sizeof(SModelMesh));
}
...
// END OF RENDERING CODE
btw the '3' is just for testing, so this will be changed into Model.Mesh.size()!
[Edited by - stefan5000 on July 19, 2005 3:45:30 PM]
Maybe there is some sizeof mistake in the following code
// MODEL LOADING CODE
...
SModelMesh *pMesh = new SModelMesh;
pMesh->Position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
pMesh->Position = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
pMesh->Position = D3DXVECTOR3(1.0f, 0.0f, 1.0f);
pMesh->Color = D3DCOLOR_XRGB(255, 0, 0);
Model.Mesh.push_back(*pMesh);
delete pMesh;
if(FAILED(Engine.Window.m_pD3DDevice->CreateVertexBuffer(3*sizeof(SModelMesh),
0,
D3DFVF_XYZ | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
&Model.VBuffer,
NULL)))
{
// Error --> Exit!
Engine.Exit();
}
VOID* pVertices;
if(FAILED(Model.VBuffer->Lock(0,
3*sizeof(SModelMesh),
(void**)&pVertices,
0)))
{
// Error --> Exit!
Engine.Exit();
}
memcpy(pVertices,
&Model.Mesh,
3*sizeof(SModelMesh));
Model.VBuffer->Unlock();
...
// END OF MODEL LOADING CODE
// RENDERING CODE
...
for(int index = 0; index < Models.m_NumModels; index++)
{
Engine.Window.m_pD3DDevice->SetStreamSource(0,
Models.GetModelVB(index),
0,
sizeof(SModelMesh));
Engine.Window.m_pD3DDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
Engine.Window.m_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,
1,
Models.GetModelMesh(index),
sizeof(SModelMesh));
}
...
// END OF RENDERING CODE
btw the '3' is just for testing, so this will be changed into Model.Mesh.size()!
[Edited by - stefan5000 on July 19, 2005 3:45:30 PM]
Change this:
To:
Also, what does "Models.GetModelMesh(index) do? You're passing whatever it returns to DrawPrimitiveUP, so it could be doing something wrong.
memcpy(pVertices, &Model.Mesh, 3*sizeof(SModelMesh));
To:
memcpy(pVertices, &Model.Mesh[0], 3 * sizeof(SModelMesh));
Also, what does "Models.GetModelMesh(index) do? You're passing whatever it returns to DrawPrimitiveUP, so it could be doing something wrong.
I'm not sure why it won't work for you, this works for me:
But then again, I am using DirectX 8...
//D3DCUSTOMVERTEX - Vertex shader for non-textured primitives//'pflag' and 'primitives' are determined elsewhereD3DCUSTOMVERTEX *vData;//For vectors ( vector<D3DCUSTOMVERTEX> example )vData=&example[0];lpD3DDevice->DrawPrimitiveUP(pflag,primitives,vData,sizeof(D3DCUSTOMVERTEX));
But then again, I am using DirectX 8...
Here is the helper function:
void *CModel::GetModelMesh(int index)
{
return &m_ModelList[index].Mesh;
}
When I try that '[0]'-thing I get memory errors when running my exe!
void *CModel::GetModelMesh(int index)
{
return &m_ModelList[index].Mesh;
}
When I try that '[0]'-thing I get memory errors when running my exe!
Well, that '[0]'-thing seems to work now at this line:
memcpy(pVertices, &Model.Mesh[0], 3 * sizeof(SModelMesh));
But the DrawPrimitiveUP-function still results in an error when writing that '[0]'-thing!
I changed that void *GetModelMesh(int index) thing to
SModelMesh *CModel::GetModelMesh(int index)
{
SModelMesh *pMesh;
pMesh = &m_ModelList[index].Mesh[0];
return pMesh;
}
Has anyone an idea?!
memcpy(pVertices, &Model.Mesh[0], 3 * sizeof(SModelMesh));
But the DrawPrimitiveUP-function still results in an error when writing that '[0]'-thing!
I changed that void *GetModelMesh(int index) thing to
SModelMesh *CModel::GetModelMesh(int index)
{
SModelMesh *pMesh;
pMesh = &m_ModelList[index].Mesh[0];
return pMesh;
}
Has anyone an idea?!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement