Skip to main content
gamedev.net
🔒 Locked

Finding Normals

Started by smittix 2 replies 1k views
Original Post
smittix
smittix
Basically I have verticies and texture coordinates, but I need to find the normals. This is what I have so far:

IDirect3DIndexBuffer9*  IB;
IDirect3DVertexBuffer9* VertexVB;
IDirect3DVertexBuffer9* NormalsVB;
IDirect3DVertexBuffer9* TextureVB;
IDirect3DVertexDeclaration9* Decl;

D3DVERTEXELEMENT9 ModelEl[] =
{
	{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
	{1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
	{2, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
	D3DDECL_END();)
};
D3DDevice->CreateVertexDeclaration( ModelEl, &Decl );

//Render
D3DDevice->SetVertexDeclaration(Decl);

D3DDevice->SetIndices(IB);
D3DDevice->SetStreamSource(0,VertexVB,0,sizeof(POSITION_VERTEX));
D3DDevice->SetStreamSource(1,NormalsVB,0,sizeof(POSITION_VERTEX));
D3DDevice->SetStreamSource(2,TextureVB,0,sizeof(TEXTURE_VERTEX));

D3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NumVert, 0, NumTri);


The texture buffer and vertex buffer get filled earlier, but I was wondering if there was a way to compute the normals and have them stored into the normals buffer. Thanks, -Chris
cannonicus
cannonicus
you could use the ID3DXMesh interface. Just load the vertex and indexbuffer into a mesh object, then use the function D3DXComputeNormals to have D3DX compute the normals for you.

//emil
Emil Jonssonvild
smittix
smittix
That is what I was originally thinking of doing, but I cannot seem to implement it. Could anyone post a way to get the normals and put them into a seperate vertex buffer?

Thanks,
-Chris
neneboricua19
neneboricua19
Computing the normals yourself is not too difficult. The easiest way to do it is to just compute the normals of each face. So you would lock your vertex and index buffers and for each triangle, compute the vector that is perpendicular to that face. To do that, you would perform the cross prodcut between two edges of the triangle.

A second, more accurate procedure to compute the normal vectors of a mesh would be to take into account the adjacency information of the mesh. So the normal at every vertex would be the average of all the normals of each face that connects to this vertex. This would give the normals of each vertex a smoother shape. To do this, you need to be able to figure out which faces are adjacent to eachother or which triangles share a particular vertex.

If you want to see how to create an object of the ID3DXMesh interface to do some of this for you, take a look at the MeshFromOBJ sample. That sample builds an object of ID3DXMesh by loading the data from another file type. Once you've got that object created, you can copy its vertex data wherever you need it.

neneboricua

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.