# FVF question

## Recommended Posts

akaitora    399
Once I load a 3d asset and get a pointer to its vertex buffer, I would like to draw lines which represent each vertices normal. In order to do that, I need information such as the order that the vertex attributes are defined so that I can decode the data. What is the best way to do this? I can call the getFVF() method on the mesh and I am assuming I can do some bit masking to get the orders but I would like to know how you guys would normally do it. Thanks!

##### Share on other sites
Buckeye    10747
You can use bitmasks to determine the vertex format. However, mesh->GetDeclaration(..) would probably be easier.

##### Share on other sites
akaitora    399
Thanks Buckeye! If it wouldn't be too much trouble, could you please show me an example on how to use the GetDeclaration method? I am not really sure how to use the d3dvertexelement structure to get the information I need. Thanks in advance!

##### Share on other sites
solenoidz    591
http://www.gamedev.net/community/forums/topic.asp?topic_id=403210

##### Share on other sites
Buckeye    10747
Modified from DXNut's post in the thread solenoidz mentions. Off the top of my head, so look for errors! By the way, if you want to display normals, you'll need the vertex position for each instance also. The line you want to draw will be from vertex_pos to vertex_pos + normal.
D3DVERTEXELEMENT9 elems[MAX_FVF_DECL_SIZE];BOOL hasVerts = FALSE, hasNormals = FALSE;WORD normalOffset, vertOffset;hr = ObjectMesh->GetDeclaration(elems);if (SUCCEEDED(hr)){	for(int i = 0; i < MAX_FVF_DECL_SIZE; ++i)	{		// Did we reach D3DDECL_END() {0xFF,0,D3DDECLTYPE_UNUSED, 0,0,0}?		if(elems[i].Stream == 0xff)			break;		if( elems[i].Type == D3DDECLTYPE_FLOAT3 &&			elems[i].Usage == D3DDECLUSAGE_NORMAL &&			elems[i].UsageIndex == 0 )		{			hasNormals = TRUE;                        normalOffset = elems[i].Offset;                        		}		if( elems[i].Type == D3DDECLTYPE_FLOAT3 &&			elems[i].Usage == D3DDECLUSAGE_POSITION &&			elems[i].UsageIndex == 0 )		{			hasVerts = TRUE;                        vertOffset = elems[i].Offset;		}	}		}if( !hasNormals || !hasVerts ) { .. process some error message .. }

Then vertOffset and normalOffset will be the offset in bytes from the beginning of each vertex in the buffer.

##### Share on other sites
akaitora    399
Thanks for your help Buckeye. I am using your method to extract the position information. The following is a initial draft approach.

pVertBuf->Lock(0, 0, (void **) &pVoid, 0);char *pTemp = (char *)&pVoid;pTemp += wPositionOffset;float *fTemp = (float *)pTemp;float x = *fTemp;float y = *++fTemp;float z = *++fTemp;m_pRenderEngine->drawLine(D3DXVECTOR3(x,y,z), D3DXVECTOR3(0,1000,0), D3DXCOLOR(1,0,0,1));

Unfortunately, I am getting faulty values for the x, y, z components. I am sure I am doing something wrong but not sure what. Could you give me a pointer? Thanks.

##### Share on other sites
Buckeye    10747
Quote:
 The line you want to draw will be from vertex_pos to vertex_pos + normal.

The buffer is comprised of a bunch of vertex structures in series in memory. You're using the first vertex in the buffer, and that's fine for your demonstration.

It appears you're getting the vertex position from that first vertex. You also need to get the normal, if that's what you want to draw.

Create a vector (vertex_pos) with the vertex position from the buffer. Create another vector (normal) with the normal from the buffer.

Draw a line from vertex_pos to vertex_pos + normal - from one point to another. I'm not sure what your drawLine function does.