# problem with normals in direct3d9

This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there. I've completed the directx9 3d sdk tutorials up to lights, but I have a problem when calculating normals. I'm not making the cylinder as in the example, but just a simple triangle lying at the floor. Here's the code: vertices[0].pos=D3DXVECTOR3(-1.0f,0.0f,-1.0f); vertices[1].pos=D3DXVECTOR3(-1.0f,0.0f,1.0f); vertices[2].pos=D3DXVECTOR3(1.0f,0.0f,-1.0f); vertices[0].normal=D3DXVECTOR3(-1.0f,1.0f,-1.0f); vertices[1].normal=D3DXVECTOR3(-1.0f,1.0f,1.0f); vertices[2].normal=D3DXVECTOR3(1.0f,1.0f,-1.0f); but it shows up strange. If I change normal values the triangle changes(should that even be possible) and it might even disappear. The color of the triangle is right(i've made it purple) and the light seems OK too. Have i done wrong in this code above or must it be somewhere else?

##### Share on other sites
Show us the struct declaration of vertices and also the FVF you are using.
Also, if the triangle is on the floor you'd want your normals poining up, that is D3DXVECTOR3( 0.0f, 1.0f, 0.0f ).

##### Share on other sites
I just found out the problem is probably because FVF was written after vertice declaration. Why is this?

code(wrong one):

struct CUSTOMVERTEX
{
D3DXVECTOR3 pos;
D3DXVECTOR3 normal;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)

##### Share on other sites
Altough I can't find what your problem is, that is certainly not it.

##### Share on other sites
I'm pretty sure it is... I vaguely remember my friend had some problem with this long time ago. I think that's the case, because now that i changed it it works fine. The vertices won't jump out of place now no matter what value the normals are. I'm fine with it even without knowing why it happens, but i'm curious to knowing why.

##### Share on other sites
If you are completely sure that is the problem, then you should e-mail your compiler's creator because it has a bug.
Just out of curiosity, try this:
DWORD D3DFVF_CUSTOMVERTEX = D3DFVF_XYZ|D3DFVF_NORMAL;struct CUSTOMVERTEX{   D3DXVECTOR3 pos;   D3DXVECTOR3 normal;};

post here the results, also, if doesn't work correctly then try putting it after the structure declaration and see if it works.

This is for sure an odd problem and I've never experienced it before.

Could you also tell us what compiler do you use?

##### Share on other sites
As far as I know, declaring the FVF after your vertex structure should not cause any problems.

##### Share on other sites
If moving that fixes the problem, then your compiler definately has issues. My code works fine if I have it before, after, or in a different file. It shouldn't matter where you define it, as long as its before you use it.

##### Share on other sites
It should work the same, but I like to do it like so :

struct CUSTOMVERTEX
{
enum
{
FVF = D3DFVF_CUSTOMVERTEX = D3DFVF_XYZ|D3DFVF_NORMAL;
};

D3DXVECTOR3 pos;
D3DXVECTOR3 normal;
};

That way you can do CUSTOMVERTEX::FVF or OTHERVERTEX::FVF.

##### Share on other sites
Quote:
 Original post by MuruGuruHello there. I've completed the directx9 3d sdk tutorials up to lights, but I have a problem when calculating normals. I'm not making the cylinder as in the example, but just a simple triangle lying at the floor. Here's the code: vertices[0].pos=D3DXVECTOR3(-1.0f,0.0f,-1.0f); vertices[1].pos=D3DXVECTOR3(-1.0f,0.0f,1.0f); vertices[2].pos=D3DXVECTOR3(1.0f,0.0f,-1.0f); vertices[0].normal=D3DXVECTOR3(-1.0f,1.0f,-1.0f); vertices[1].normal=D3DXVECTOR3(-1.0f,1.0f,1.0f); vertices[2].normal=D3DXVECTOR3(1.0f,1.0f,-1.0f);but it shows up strange. If I change normal values the triangle changes(should that even be possible) and it might even disappear. The color of the triangle is right(i've made it purple) and the light seems OK too. Have i done wrong in this code above or must it be somewhere else?

Hi MuruGuru,

The normals you are using are not "normalized".
Try something like this:
vertices[0].normal=D3DXVECTOR3(-1.0f,1.0f,-1.0f);
D3DXVec3Normalize( &vertices[0].normal, &vertices[0].normal );

You will see that (-1.0f,1.0f,-1.0f) is
(-0.55735026f, 0.55735026f, -0.55735026f ) when normalized.

HTH,
Cambo_frog

##### Share on other sites
I really dunno why it happened. I know it didn't work before i put the FVF first. Now it works even if i put it back to where it was before. Might be something with the compiler. I use MSVC++ right now by the way. It's also possible that i changed something somewhere else and forgot. Anyways, thanks for all the help. Now i can continue.

##### Share on other sites

This topic is 4560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account