Effect problem: vertex color doesn't appear to be coming through correctly.

Started by
3 comments, last by CDProp 15 years, 10 months ago
It would be impractical for me to post all my code, so I'll try to post all of the relevant bit. This isn't my first time working with effect files, but this is the first time I've had this problem. I am implementing some framework for my rendering stuff. Here is the vertex structure that I'm using:

struct E3Vertex
{
	D3DXVECTOR3	pos;
	D3DXVECTOR3     norm;
	D3DXCOLOR	color;
	D3DXVECTOR2	uv;
};



Here is my vertex declaration:

D3DVERTEXELEMENT9 VertexPosElements[] =
	{
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
		{0, 40, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END()
	};

	HRESULT result;
	result = m_pDevice->CreateVertexDeclaration(VertexPosElements, &m_Materials[MATERIALTYPE_TEXTURED_COLORED_LIT].pVertexDecl);



Now, I have a function that creates a cube and adds it to the render list, just for testing purposes. I won't show the whole function, but it sets the appropriate vertex positions, and it zeros out the normal and uv components (since my shaders don't use them anyway), and it sets the color to white. I'll show just the color code since it's the only bit that I think is relevant to my problem. The actual shape of the cube that is drawn looks correct, indicating that the positions and indices are all correct.
verts.color	= D3DCOLOR_XRGB(255, 255, 255);


I set a break point in my cube creation function just to see what the color was being set at. It looks like it is correctly being set to white: I also set a break point to make sure that the vertex declaration is properly being set before the draw call, and that the correct effect is created from the above file, and that the correct technique is being set. However, what I actually see looks like this: I can't seem to figure out what's going wrong. The cube looks dark red like this no matter what color I set the vertex to, indicating that it's ignoring that and looking someplace else, I think. I captured a frame using pix and debugged the vertex shader. Check this out: I don't know much about asm, but it looks like v1 represents the input color, and the value of v1 is being moved to oD0, which I presume represents the color component of my output struct. And if you look in the watch, the value of v1 isn't white. It's 0.627, 0.0, 0.0, 0.753. I don't get it! Edit: I'm adding this to see if it helps. This is what PIX sees in the vertex buffer: If you look at ID's 6-9, which are the four color components of the first vertex, they all are set to 1.0, which is correct. You can't see from the picture, but if I scroll down and look at the whole buffer, the numbers are all correct as I've set them. Somehow, it's like the color component isn't properly being associated with the COLOR0 semantic or something. But I check and double check my vertex declaration, and I am using the D3DDECLUSAGE_COLOR usage, and I am using index 0. Edit2: I completely forgot to post the effect file itself!

uniform extern float4x4 gWVP;

struct OutputVS
{
	float4 posH : POSITION0;
	float4 color : COLOR0;
};

// Vertex Shader
OutputVS DefaultVS(float3 posL : POSITION0, float4 c : COLOR0)
{
	OutputVS outVS = (OutputVS)0;
	
	outVS.posH = mul(float4(posL, 1.0f), gWVP);
	outVS.color = c;
	
	return outVS;
}

// Pixel Shader
float4 DefaultPS(float4 c : COLOR0) : COLOR
{
	return c;
}

technique DefaultTech
{
	pass P0
	{
		vertexShader = compile vs_2_0 DefaultVS();
		pixelShader = compile ps_2_0 DefaultPS();
		
		FillMode = Solid;
	}
}

[Edited by - CDProp on June 22, 2008 8:47:34 PM]
Advertisement
I know very little about shaders, but try storing the vertex color as a DWORD. I don't think this requires changing any other parts of your code.
Gage64 is right. You are specifying D3DDECLTYPE_D3DCOLOR in your vertex declaration. So you have to use D3DCOLOR for that component (which is a DWORD, 4 bytes) instead of D3DXCOLOR (which is 4 floats, 16 bytes). Use D3DCOLOR and don't forget to modify the byte offset of the next vertex component in your vertex declaration (texture coordinates).
[EDIT]: That, or just replace D3DDECLTYPE_D3DCOLOR with D3DDECLTYPE_FLOAT4.
typedef DWORD D3DCOLOR;


This explains everything.
You guys are awesome. Thanks!

This topic is closed to new replies.

Advertisement