Vertex color not working

Started by
13 comments, last by bfarah 14 years ago
Hi, i know the basics of direct 3D and i have looked every where online and in books to change the vertex color and for some reason its not changing. I tried using FVF, and vertex element decl. here is the struct for my vertex struct TVERTEX { D3DXVECTOR3 vPos; float fU, fV; D3DXVECTOR3 vNormal; DWORD dwColor; TVERTEX( void ) { vPos.x = vPos.y = vPos.z = 0.0f; fU = fV = 0.0f; vNormal.x = vNormal.y = vNormal.z = 0.0f; dwColor = D3DCOLOR_ARGB(255, 255, 255, 255); } }; i set the default vertex color to white D3DVERTEXELEMENT9 decl[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, {0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, {0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0}, D3DDECL_END() }; i create and set the vertex element pDevice->CreateVertexDeclaration(decl, &m_ptVertexDecl); pDevice->SetVertexDeclaration( m_ptVertexDecl ); and i draw the object in wire frame and its not all white, half is a blue/teal color and the other half is black.
Advertisement
Quote:{0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},


This should be:

{0, 32, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
What kind of material set-up are you using?
Quote:Original post by Hodgman
What kind of material set-up are you using?


I am using a diffuse material

D3DMATERIAL9 m_tMaterial;

m_tMaterial.Diffuse.r = 1.0f;
m_tMaterial.Diffuse.g = 1.0f;
m_tMaterial.Diffuse.b = 1.0f;

Quote:{0, 32, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},


i changed it to 32 and now its all black instead of white.

If you're using lighting, it won't work because the vertex normal is set to zero.

If you don't want to use lighting (i.e., you just want to use vertex colors), you need to disable it:

device->SetRenderState(D3DRS_LIGHTING, false);
Quote:Original post by Gage64
If you're using lighting, it won't work because the vertex normal is set to zero.

If you don't want to use lighting (i.e., you just want to use vertex colors), you need to disable it:

device->SetRenderState(D3DRS_LIGHTING, false);


The verts do have normals because i have exported the model from Maya. I have disabled lighting and there is not change.
Since you aren't talking about shaders, this is fixed pipeline, right?

If so, well, your verterx format is a bit odd.

You have normals AND a vertex color.
In a fixed pipeline they can't both be used to calculate the final color.
What you would like to see?

If you want lighting you don't need a vertex color on the vertices but you have to:
enable lighting, setup correctly a light, setup material, draw

if you want to see vertex color you don't need normals so just:
disable lighting, draw

Then you should also check what texture are you binding on the texture stage.
A NULL texture, with default parameters should result in a black mesh.
Try to bind a white texture and check the result.
Txkun,

I put the vertex color in there so i can render the bounding box of the object which has texture and will be lit.

I might be doing the rendering of the bounding box differently. Which way is suitable for debug purposes in game so i can change the color of the bounding box to a different color when it detects a collision.

I have been using OpenGL on my last 3D project and switching to Direct3D. it was easy for me using OpenGL.

Thanks
Post the drawing code, and any other code you think might be relevant.

But please use source tags (see the FAQ for instructions).
Loading the mesh

CMesh *CMesh::LoadMesh( const char *szFile, IDirect3DDevice9 *pDevice, bool bBinaryFile ){	if( bBinaryFile )	{		// Open File		ifstream fFile( szFile, std::ios_base::binary );		if( fFile.is_open() )		{			size_t uNameLength = 0;			fFile.read( (char *)&uNameLength, sizeof(size_t) );			char *szBuffer = new char[uNameLength];			fFile.read( (char *)szBuffer, uNameLength );			m_szName = szBuffer;			delete [] szBuffer;			// Read how many verts and indices			fFile.read( (char *)&m_uNumVertices, sizeof(size_t) );			fFile.read( (char *)&m_uNumPolygons, sizeof(size_t) );			size_t uNumTextures = 0;			fFile.read( (char *)&uNumTextures, sizeof(size_t) );			// Read in the number of texture names HERE			for( int i = 0; i < uNumTextures; ++i )			{				size_t uTexNameLength = 0;				fFile.read( (char *)&uTexNameLength, sizeof(size_t) );				char *szTexBuffer = new char[uTexNameLength];				fFile.read( (char *)szTexBuffer, uTexNameLength );				//printf(szTexBuffer, "%s/Resources", szTexBuffer);				char *szFilePath = "../Resources/";				char *szFile = new char[strlen(szFilePath) + strlen(szTexBuffer) + 2];				strcpy(szFile, szFilePath);				strcpy(&szFile[strlen(szFilePath)], szTexBuffer);				D3DXCreateTextureFromFile(pDevice, szFile, &m_pTexture);				delete [] szTexBuffer;				delete [] szFile;			}			m_pVertices = new TVERTEX[ m_uNumVertices ];			// Create a Vert array for the size of the verts			for( size_t i = 0; i < m_uNumVertices; ++i )			{				fFile.read( (char *)&m_pVertices.vPos.x, sizeof(float) );				fFile.read( (char *)&m_pVertices.vPos.y, sizeof(float) );				fFile.read( (char *)&m_pVertices.vPos.z, sizeof(float) );				fFile.read( (char *)&m_pVertices.vNormal.x, sizeof(float) );				fFile.read( (char *)&m_pVertices.vNormal.y, sizeof(float) );				fFile.read( (char *)&m_pVertices.vNormal.z, sizeof(float) );				fFile.read( (char *)&m_pVertices.fU, sizeof(float) );				fFile.read( (char *)&m_pVertices.fV, sizeof(float) );			}			// Create an array for the size of the indices			m_puPolygons = new short[ m_uNumPolygons * 3 ];			for( size_t i = 0; i < (m_uNumPolygons * 3); i += 3 )			{				size_t temp = 0;				fFile.read( (char *)&temp, sizeof( size_t ) );				m_puPolygons = (short)temp;				fFile.read( (char *)&temp, sizeof( size_t ) );				m_puPolygons = (<span class="cpp-keyword">short</span>)temp;<br><br>				fFile.read( (<span class="cpp-keyword">char</span> *)&amp;temp, <span class="cpp-keyword">sizeof</span>( size_t ) );<br>				m_puPolygons = (<span class="cpp-keyword">short</span>)temp;<br>			}<br>			<br>			fFile.close();<br>		}<br>		<span class="cpp-keyword">else</span><br>			<span class="cpp-keyword">return</span> NULL;<br><br>		D3DVERTEXELEMENT9 decl[] = <br>		{<br>			{<span class="cpp-number">0</span>, <span class="cpp-number">0</span>,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, <span class="cpp-number">0</span>},<br>			{<span class="cpp-number">0</span>, <span class="cpp-number">12</span>, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, <span class="cpp-number">0</span>},<br>			{<span class="cpp-number">0</span>, <span class="cpp-number">20</span>, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL,   <span class="cpp-number">0</span>},<br>			{<span class="cpp-number">0</span>, <span class="cpp-number">32</span>, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,  <span class="cpp-number">0</span>},<br>			D3DDECL_END()<br>		};<br><br>		pDevice-&gt;CreateVertexDeclaration(decl, &amp;m_ptVertexDecl);<br>		pDevice-&gt;SetVertexDeclaration( m_ptVertexDecl );<br><br>		<span class="cpp-keyword">void</span> *pVBuffer = NULL;<br><br>		pDevice-&gt;CreateVertexBuffer( m_uNumVertices * <span class="cpp-keyword">sizeof</span>(TVERTEX), <span class="cpp-number">0</span>, <br>			<span class="cpp-number">0</span> , D3DPOOL_MANAGED, &amp;m_pVertexBuff, <span class="cpp-number">0</span>);<br><br>		m_pVertexBuff-&gt;Lock(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>, (<span class="cpp-keyword">void</span>**)&amp;pVBuffer, <span class="cpp-number">0</span>);<br>		memcpy(pVBuffer, m_pVertices, m_uNumVertices * <span class="cpp-keyword">sizeof</span>(TVERTEX));<br>		m_pVertexBuff-&gt;Unlock();<br><br>		<span class="cpp-comment">//	Setup the index buffer here</span><br>		pDevice-&gt;CreateIndexBuffer( (m_uNumPolygons * <span class="cpp-number">3</span>) * <span class="cpp-keyword">sizeof</span>(<span class="cpp-keyword">short</span>), <span class="cpp-number">0</span>, <br>			D3DFMT_INDEX16, D3DPOOL_MANAGED, &amp;m_pIndexBuff, <span class="cpp-number">0</span> );<br><br>		<span class="cpp-keyword">void</span> *pIBuffer = NULL;<br>		m_pIndexBuff-&gt;Lock( <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, (<span class="cpp-keyword">void</span>**)&amp;pIBuffer, <span class="cpp-number">0</span> );<br>		memcpy( pIBuffer, m_puPolygons, (m_uNumPolygons * <span class="cpp-number">3</span>) * <span class="cpp-keyword">sizeof</span>( <span class="cpp-keyword">short</span> ) );<br>		m_pIndexBuff-&gt;Unlock();<br><br><br>		m_tMaterial.Diffuse.r = <span class="cpp-number">1</span>.0f;<br>		m_tMaterial.Diffuse.g = <span class="cpp-number">1</span>.0f;<br>		m_tMaterial.Diffuse.b = <span class="cpp-number">1</span>.0f;<br><br><br>	}<br><br>	<span class="cpp-keyword">return</span> <span class="cpp-keyword">this</span>;<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>Rendering the objects<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-keyword">void</span> CRenderEngine::Render( vector&lt;CBaseObject *&gt; &amp;tObjects )<br>{<br>	Begin();<br><br>	DrawHUD();<br><br>	<span class="cpp-comment">//	Render all game objects here.</span><br>	<span class="cpp-keyword">for</span>( size_t i = <span class="cpp-number">0</span>; i &lt; tObjects.size(); ++i )<br>	{<br>		CMesh *pMesh = tObjects<span style="font-weight:bold;">-&gt;GetMesh();<br>		CBoundingVolume *pBV = tObjects<span style="font-weight:bold;">-&gt;GetBV();<br>		Vec3f tPos = tObjects<span style="font-weight:bold;">-&gt;GetPos();<br><br>		D3DXMATRIX transform;									<span class="cpp-comment">// basic transform</span><br>		D3DXMatrixIdentity(&amp;transform);							<span class="cpp-comment">// set to identity</span><br>		transform._41 = tPos.x;<br>		transform._42 = tPos.y;<br>		transform._43 = tPos.z;<br>		m_lpDirect3DDevice-&gt;SetTransform(D3DTS_WORLD, &amp;transform);				<span class="cpp-comment">// set world transform</span><br><br>		<span class="cpp-comment">//	Draw Bounding Box here</span><br>		<span class="cpp-keyword">if</span>( pBV )<br>		{<br>			CMesh *pBVMesh = pBV-&gt;GetBVMesh();<br><br>			m_lpDirect3DDevice-&gt;SetVertexDeclaration( pBVMesh-&gt;GetVertexDecl() );<br>			<span class="cpp-comment">//m_lpDirect3DDevice-&gt;SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );</span><br>			m_lpDirect3DDevice-&gt;SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );<br>			m_lpDirect3DDevice-&gt;SetRenderState(D3DRS_LIGHTING, <span class="cpp-keyword">false</span>);<br>			<span class="cpp-comment">//m_lpDirect3DDevice-&gt;SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );</span><br>			m_lpDirect3DDevice-&gt;SetStreamSource( <span class="cpp-number">0</span>, pBVMesh-&gt;GetVertexBuffer(), <span class="cpp-number">0</span>, <span class="cpp-keyword">sizeof</span>( TVERTEX ) );<br>			m_lpDirect3DDevice-&gt;SetIndices( pBVMesh-&gt;GetIndexBuffer() );<br><br>			m_lpDirect3DDevice-&gt;DrawIndexedPrimitive( D3DPT_TRIANGLELIST, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, pBVMesh-&gt;GetNumVerts(), <span class="cpp-number">0</span>, pBVMesh-&gt;GetNumPolygons() );<br><br>		}<br><br>		m_lpDirect3DDevice-&gt;SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );<br>		m_lpDirect3DDevice-&gt;SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );<br><br>		<span class="cpp-keyword">if</span>( pMesh )<br>		{<br>			m_lpDirect3DDevice-&gt;SetVertexDeclaration( pMesh-&gt;GetVertexDecl() );<br>			m_lpDirect3DDevice-&gt;SetStreamSource( <span class="cpp-number">0</span>, pMesh-&gt;GetVertexBuffer(), <span class="cpp-number">0</span>, <span class="cpp-keyword">sizeof</span>( TVERTEX ) );<br>			m_lpDirect3DDevice-&gt;SetIndices( pMesh-&gt;GetIndexBuffer() );<br>			m_lpDirect3DDevice-&gt;SetMaterial( &amp;pMesh-&gt;GetMaterial() );<br>			m_lpDirect3DDevice-&gt;SetTexture( <span class="cpp-number">0</span>, pMesh-&gt;GetTexture() );<br><br>			m_lpDirect3DDevice-&gt;DrawIndexedPrimitive( D3DPT_TRIANGLELIST, <span class="cpp-number">0</span>, <span class="cpp-number">0</span>, pMesh-&gt;GetNumVerts(), <span class="cpp-number">0</span>, pMesh-&gt;GetNumPolygons() );<br>		}<br>	}<br><br>	m_lpDirect3DDevice-&gt;SetTransform( D3DTS_VIEW, &amp;m_mViewMatrix );<br>	m_lpDirect3DDevice-&gt;SetTransform( D3DTS_PROJECTION, &amp;m_mProjectionMatrix );<br><br>	End();<br>}<br><br></pre></div><!–ENDSCRIPT–> 

This topic is closed to new replies.

Advertisement