How many vertices in a teapot?

Started by
16 comments, last by hupsilardee 11 years, 10 months ago
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 have 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.
Advertisement
So you have vertex struct and declaration, then:

//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.Stream == 0xFF || decl.Stream != 0)
{
break;
}

if((decl.Usage == D3DDECLUSAGE_POSITION) && (decl.Type == D3DDECLTYPE_FLOAT3))
{
offsetToPosition = decl.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.position.x = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition);
pNewVertices.position.y = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition + sizeof(float));
pNewVertices.position.z = *reinterpret_cast<float*>((i * bytesPerVertex) + pVertices + offsetToPosition + sizeof(float) * 2);

pNewVertices.color = yourColor;
}

newMesh->UnlockVertexBuffer();
teapotMesh->UnlockVertexBuffer();
You have not actually declared the static declarator member, you have only defined it. In the header fileyou have something like this


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

static IDirect3DVertexDeclaration9* Decl;
};


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


// VertexCol.cpp
IDirect3DVertexDeclaration9* VertexCol::Decl = NULL;


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:


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
I have defined VertexColElements like so...

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));


I still don't know why the environment won't recognize VertexColElements when I insert it into the CloneMesh function like this...

mTeapot->CloneMesh(0, VertexColElements, gd3dDevice, &mTeapotClone);


What's wrong?
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
You have defined a structure but you have not declared a var to hold the struc in memory.

You have defined a structure but you have not declared a var to hold the struc in memory.

Not quite correct, since VertexCol::Decl is a static variable. Refer to this:

Making sure that you have initialised VertexCol::Decl


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

[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.

Not quite correct, since VertexCol::Decl is a static variable. Refer to this:

Making sure that you have initialised VertexCol::Decl


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.

This topic is closed to new replies.

Advertisement