problem with normals in direct3d9

Started by
9 comments, last by MuruGuru 18 years, 9 months ago
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?
Your memory is like cheese. It's full of holes, and most of it stinks
Advertisement
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 ).
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)
Your memory is like cheese. It's full of holes, and most of it stinks
Altough I can't find what your problem is, that is certainly not it.
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.
Your memory is like cheese. It's full of holes, and most of it stinks
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?
As far as I know, declaring the FVF after your vertex structure should not cause any problems.
There are some things so stupid that only an intellect could believe them.
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.
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.
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
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.

This topic is closed to new replies.

Advertisement