No Color in a Mesh that has Color !??

Started by
5 comments, last by lipsryme 14 years ago
Ok I'm trying for like 2 hours to find out why the shader doesn't get color information when the mesh definitily has color information. I've imported a 3ds model file (palm) into 3ds max 2010. Then I've exported that model with pandasofts X file export plugin. Now when I view this model in the DirectX Viewer it looks like this: As you can see it clearly HAS colors and I don't think that these are textures. But when I load it in my engine I'm always getting this Error: Direct3D9: (ERROR) :Vertex shader function usage (D3DDECLUSAGE_COLOR,0) does not have corresponding usage in the current vertex declaration Which means that the shader does not get any color information which totally confuses me because it does have color. Well look at my code at tell me what's wrong please :( VertexDeclaration:

const D3DVERTEXELEMENT9 declarationColoredMesh[] =
{
	{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
	{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
	{0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},

	D3DDECL_END()
};



HRESULT CMesh::InitializeVertexDeclaration(LPDIRECT3DDEVICE9EX device)
{
	// Create VertexDeclaration
	if(FAILED(device->CreateVertexDeclaration(declarationTexturedNormalMesh, &m_pD3DVertexDeclTextureNormal)))
	{
		MessageBox(NULL, L"Failed to create VertexDeclaration", L"CreateVertexDeclaration() - HRESULT", MB_OK);
		return E_FAIL;
	}

	if(FAILED(device->CreateVertexDeclaration(declarationColoredMesh, &m_pD3DVertexDeclColored)))
	{
		MessageBox(NULL, L"Failed to create VertexDeclaration", L"CreateVertexDeclaration() - HRESULT", MB_OK);
		return E_FAIL;
	}

	return S_OK;
}


Mesh Creation:

HRESULT CMesh::LoadCharacterMesh(LPDIRECT3DDEVICE9EX device, LPCWSTR MeshLocation)
{
	if(FAILED(D3DXLoadMeshFromX(MeshLocation,
								D3DXMESH_SYSTEMMEM,
								device,
								NULL,
								&m_pD3DMaterialBuffer,
								NULL,
								&m_dwNumberOfCharacterMaterials,
								&m_pD3DMeshCharacter)))
	{
		MessageBox(NULL, L"Failed to load mesh from file", L"D3DXLoadMeshFromX() - HRESULT", MB_OK);
		return E_FAIL;
	}

	// extract Material information from the X file Buffer to our own Material buffer
	D3DXMATERIAL* pMaterials = (D3DXMATERIAL*)m_pD3DMaterialBuffer->GetBufferPointer();


	m_pD3DMeshCharacterMaterials = new D3DMATERIAL9[m_dwNumberOfCharacterMaterials];
	m_pD3DMeshCharacterTextures = new LPDIRECT3DTEXTURE9[m_dwNumberOfCharacterMaterials];


	for(DWORD i = 0; i < m_dwNumberOfCharacterMaterials; i++)
	{
		// copy the material
		m_pD3DMeshCharacterMaterials = pMaterials.MatD3D;

		// Set ambient color for materials
		m_pD3DMeshCharacterMaterials.Ambient = m_pD3DMeshCharacterMaterials.Diffuse;


		// Create texture for mesh (if it exists)
		m_pD3DMeshCharacterTextures = NULL;
		if(pMaterials.pTextureFilename)
		{
			if(FAILED(D3DXCreateTextureFromFileA(device, pMaterials.pTextureFilename, &m_pD3DMeshCharacterTextures)))
			{
				MessageBox(NULL, L"Failed to create Texture from file", L"D3DXCreateTextureFromFile() - HRESULT", MB_OK);
				return E_FAIL;
			}
		}
	}

	m_pD3DMaterialBuffer = NULL;


	return S_OK;
}


Mesh Rendering:

void CMesh::RenderCharacterMesh(LPDIRECT3DDEVICE9EX device)
{
	device->SetVertexDeclaration(m_pD3DVertexDeclColored);

	for(DWORD i = 0; i < m_dwNumberOfCharacterMaterials; i++)
	{
		device->SetMaterial(&m_pD3DMeshCharacterMaterials);
		device->SetTexture(0, m_pD3DMeshCharacterTextures);

		m_pD3DMeshCharacter->DrawSubset(i);
	}
}




Advertisement
Shouldnt D3DDECLTYPE_D3DCOLOR be 4 x sizeof(float)

so instead:
const D3DVERTEXELEMENT9 declarationColoredMesh[] ={	{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},	{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},	{0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},	D3DDECL_END()};


this:
const D3DVERTEXELEMENT9 declarationColoredMesh[] ={	{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},	{0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},	{0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},	D3DDECL_END()};
EDIT: Just noticed:

device->SetVertexDeclaration(m_pD3DVertexDeclColored);	for(DWORD i = 0; i < m_dwNumberOfCharacterMaterials; i++)	{		device->SetMaterial(&m_pD3DMeshCharacterMaterials);		device->SetTexture(0, m_pD3DMeshCharacterTextures);		m_pD3DMeshCharacter->DrawSubset(i);	}


I think that func. "DrawSubset(x)" calls setVertx/index buffer and setVertDecl of its own data
so first one "device->SetVertexDeclaration(m_pD3DVertexDeclColored);" is ignored?
Try with this "flat" shader if you are sure that your mesh has pos, color & normal:

float4x4 WorldViewProj;struct app2vert{   float3 Pos    : POSITION;   float4 Color  : COLOR;   float3 Normal : NORMAL;};struct vert2pixel{    float4 Pos   : POSITION;    float4 Color : TEXCOORD0;};struct pixel2app{    float4 Color : COLOR;};void VS(in app2vert IN, out vert2pixel OUT){    OUT.Position = mul(float4(IN.Position, 1.0f), WorldViewProj);    OUT.Color    = IN.Color;}void PS(in vert2pixel IN, out pixel2app OUT){    OUT.Color = float4(IN.Color.rgb, 1.0f);}technique tTech{    pass p0    {        VertexShader = compile vs_2_0 VS();        PixelShader = compile ps_2_0 PS();    }}
Quote:Original post by belfegor
Shouldnt D3DDECLTYPE_D3DCOLOR be 4 x sizeof(float)

so instead:
*** Source Snippet Removed ***

this:
*** Source Snippet Removed ***
You need to hit the docs up again. The D3DCOLOR field is 4x bytes.
------------------------------Great Little War Game
Argh finally I wanted to post yesterday but the whole forum was down for me.
Anyway I've managed to fix the problem.
The Mesh really had no vertex colors defined in 3ds Max which was weird for me as everything else displayed colors for me. Well I had to reassign those colors onto the mesh and now it works.

This topic is closed to new replies.

Advertisement