• Advertisement
Sign in to follow this  

How many vertices in a teapot?

This topic is 2058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way to access the vertices using the ID3DXMesh interface of a teapot (after using D3DXCreateTeapot)? Is there any way to add a color of type D3DCOLOR to each vertex?

Share this post


Link to post
Share on other sites
Advertisement
Is this a trick question? LOL. You can access the vertex buffer through the mesh interface. To add a color type you may need to create a new vertexformat.

[url="http://www.mvps.org/directx/articles/d3dxmesh.htm"]http://www.mvps.org/directx/articles/d3dxmesh.htm[/url]

Share this post


Link to post
Share on other sites
How do I know if I've reached the end of the buffer? i.e. how many verts are in the teapot?

Share this post


Link to post
Share on other sites
Did you try the [url=http://msdn.microsoft.com/en-us/library/windows/desktop/bb205745(v=vs.85).aspx]ID3DXBaseMesh::GetNumVertices()[/url] member function?

Share this post


Link to post
Share on other sites
I guess it depends on the teapot. Some have more vertices than others (of course, more is not always better, especially when it comes to teapots).

[size=1]Ok, I'll leave now![/size]

Share this post


Link to post
Share on other sites
[quote name='DJTN' timestamp='1337994647' post='4943387']
Is this a trick question? LOL. You can access the vertex buffer through the mesh interface. To add a color type you may need to create a new vertexformat.

[url="http://www.mvps.org/directx/articles/d3dxmesh.htm"]http://www.mvps.org/...es/d3dxmesh.htm[/url]
[/quote]

I can create a new vertex, like so...
[CODE]
struct VertexCol
{
VertexCol():pos(0.0f, 0.0f, 0.0f),col(0x00000000){}
VertexCol(float x, float y, float z, D3DCOLOR c):pos(x,y,z), col(c){}
VertexCol(const D3DXVECTOR3& v, D3DCOLOR c):pos(v),col(c){}
D3DXVECTOR3 pos;
D3DCOLOR col;
static IDirect3DVertexDeclaration9* Decl;
};
[/CODE]

[CODE]
D3DVERTEXELEMENT9 VertexColElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
D3DDECL_END()
};
HR(gd3dDevice->CreateVertexDeclaration(VertexColElements, &VertexCol::Decl));
[/CODE]

The question is how do I implement this new format into the teapot? Edited by Hseptic

Share this post


Link to post
Share on other sites
use the ID3DXMesh::CloneMesh function, then lock the vertex buffer and write in all the colors. Make sure not to lock with the overwrite flag otherwise all the positions will be lost

Share this post


Link to post
Share on other sites
[quote name='hupsilardee' timestamp='1338057365' post='4943527']
use the ID3DXMesh::CloneMesh function, then lock the vertex buffer and write in all the colors. Make sure not to lock with the overwrite flag otherwise all the positions will be lost
[/quote]

I'm having trouble with the CloneMesh function. What exactly do I put for the second parameter? Right now I have:
[CODE]
mTeapot->CloneMesh(0, VertexColElements, gd3dDevice, &mTeapotClone);
[/CODE]
However it says that "VertexColElements" is undefined.

Share this post


Link to post
Share on other sites
You pass in a zero for options, vertex Elements, pointer to your device, and then (out) the new mesh. If it's telling you the VertexColElement is undefined then you need to define (initiate) it. If you've already defined it, debug (step through code) to see what happened to it.

Cloning allows you to create a structure (format) for the vertex buffer to hold the data YOU define.

It might be a good idea to convey what you're trying to accomplish. I think the standard format of a mesh already contains a vertex color section.

Share this post


Link to post
Share on other sites
I'm trying to add a color to each vertex in the teapot, though I'm still confused about the steps necessary to do so. I [i]have[/i] defined VertexColElements as shown in one of my above posts. It is not a compile or run time error. The coding environment merely shows a squiggly like under "VertexColElements" stating that it is an undefined idenitfier. This leads me to believe that I've missed something. I just don't know what.

Share this post


Link to post
Share on other sites
So you have vertex struct and declaration, then:
[code]
//afer CloneMesh
D3DVERTEXELEMENT9 decl[MAX_FVF_DECL_SIZE];
hr = teapotMesh->GetDeclaration(decl);
if(FAILED(hr))
{
...// on failed
}
WORD offsetToPosition = 0;
bool havePos = false;
for(UINT i = 0; i < MAX_FVF_DECL_SIZE; ++i)
{
if(decl[i].Stream == 0xFF || decl[i].Stream != 0)
{
break;
}

if((decl[i].Usage == D3DDECLUSAGE_POSITION) && (decl[i].Type == D3DDECLTYPE_FLOAT3))
{
offsetToPosition = decl[i].Offset;
havePos = true;
continue;
}
}

if(!havePos)
{
...// on failed
}
DWORD numVertices = teapotMesh->GetNumVertices();
DWORD numTri = teapotMesh->GetNumFaces();
DWORD numIndices = teapotMesh->GetNumFaces() * 3;
DWORD bytesPerVertex = teapotMesh->GetNumBytesPerVertex();

USHORT* pNewIndices = nullptr;
USHORT* pTeapotIndices = nullptr;
newMesh->LockIndexBuffer(0, 0, (void**)&pNewIndices, 0);
terrMesh->LockIndexBuffer(0, (void**)&pTeapotIndices);
memcpy(pNewIndices, pTeapotIndices, numIndices);
newMesh->UnlockIndexBuffer();
teapotMesh->UnlockIndexBuffer();

VERTEX* pNewVertices = nullptr;
BYTE* pTeapotVertices = nullptr;
newMesh->LockVertexBuffer(0, 0, (void**)&pNewVertices, 0);
teapotMesh->LockVertexBuffer(0, (void**)&pTeapotVertices);

for(DWORD i = 0; i < numVertices; ++i)
{
pNewVertices[i].position.x = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition);
pNewVertices[i].position.y = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition + sizeof(float));
pNewVertices[i].position.z = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition + sizeof(float) * 2);

pNewVertices[i].color = yourColor;
}

newMesh->UnlockVertexBuffer();
teapotMesh->UnlockVertexBuffer();
[/code] Edited by belfegor

Share this post


Link to post
Share on other sites
You have not actually [i]declared[/i] the static declarator member, you have only [i]defined[/i] it. In the header fileyou have something like this

[code]
// VertexCol.h
struct VertexCol
{
float x, y, z;
DWORD color;

static IDirect3DVertexDeclaration9* Decl;
};
[/code]

Then in one of your CPP files you need to type this

[code]
// VertexCol.cpp
IDirect3DVertexDeclaration9* VertexCol::Decl = NULL;
[/code]

Unfortunately, static members can be fiddly like this.
Then when this is in place you need to pass in the vertex elements to the CloneMesh function. Making sure that you have initialised VertexCol::Decl:

[code]
ID3DXMesh* oldMesh;
ID3DXMesh* newMesh;
D3DVERTEXELEMENT9 elements[MAX_FVF_DECL_SIZE]; // MAX_FVF_DECL_SIZE is the largest number of elements that any vertex declaration can have
VertexCol::Decl->GetDeclaration(elements, NULL);
oldMesh->CloneMesh(0, elements, device, &newMesh); // Instead of 0 consider using D3DXMESH_MANAGED as teh first arg
// now you have the new mesh with the vertex colors you can release the old one if you no longer need it
[/code]

Share this post


Link to post
Share on other sites
I have defined VertexColElements like so...
[CODE]
D3DVERTEXELEMENT9 VertexColElements[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
D3DDECL_END()
};
HR(gd3dDevice->CreateVertexDeclaration(VertexColElements, &VertexCol::Decl));
[/CODE]

I still don't know why the environment won't recognize VertexColElements when I insert it into the CloneMesh function like this...
[CODE]
mTeapot->CloneMesh(0, VertexColElements, gd3dDevice, &mTeapotClone);
[/CODE]

What's wrong?

Share this post


Link to post
Share on other sites
Is VertexColElements defined before the function call? It looks like mTeapot is a member, so can the class with the mTeapot member access it? As a quick workaround you could try copy-pasting that array directly above the call to mTeapot->CloneMesh(), that would work but is not great as a long term solution

Share this post


Link to post
Share on other sites
You have defined a structure but you have not declared a var to hold the struc in memory.

Share this post


Link to post
Share on other sites
[quote name='DJTN' timestamp='1338391021' post='4944697']
You have defined a structure but you have not declared a var to hold the struc in memory.
[/quote]
Not quite correct, since VertexCol::Decl is a static variable. Refer to this: [quote name='hupsilardee' timestamp='1338119523' post='4943677']
Making sure that you have initialised VertexCol::Decl
[/quote]

I would go with hupsilardee's advice of copy-pasting the VertexColElements definition directly above the call to CloneMesh().

Share this post


Link to post
Share on other sites
[quote name='KymikoLoco' timestamp='1338394625' post='4944716']
[quote name='DJTN' timestamp='1338391021' post='4944697']
You have defined a structure but you have not declared a var to hold the struc in memory.
[/quote]
Not quite correct, since VertexCol::Decl is a static variable. Refer to this: [quote name='hupsilardee' timestamp='1338119523' post='4943677']
Making sure that you have initialised VertexCol::Decl
[/quote]

I would go with hupsilardee's advice of copy-pasting the VertexColElements definition directly above the call to CloneMesh().
[/quote]

But like I said, not as a long-term solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement