Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by MuruGuru
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?

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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this