how to calculate normals for my cube?

Started by
4 comments, last by ElectronicSandClock 20 years, 3 months ago
this is my cube(x,y,z,nx,ny,nz,tu,tv):

//front

		{-0.5f, 0.5f,-0.5f,0,0,0,0.0f,0.0f},
		{ 0.5f, 0.5f,-0.5f,0,0,0,1.0f,0.0f},
		{-0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},//triangle 1


		{-0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},
		{ 0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},
		{ 0.5f, 0.5f,-0.5f,0,0,0,1.0f,0.0f},//triangle 2


		//back

		{-0.5f, 0.5f, 0.5f,0,0,0,1.0f,0.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,0.0f,0.0f},
		{-0.5f,-0.5f, 0.5f,0,0,0,1.0f,1.0f},//triangle 1


		{-0.5f,-0.5f, 0.5f,0,0,0,1.0f,1.0f},
		{ 0.5f,-0.5f, 0.5f,0,0,0,0.0f,1.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,0.0f,0.0f},//triangle 2


		//top

		{-0.5f, 0.5f, 0.5f,0,0,0,0.0f,0.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,1.0f,0.0f},
		{-0.5f, 0.5f,-0.5f,0,0,0,0.0f,1.0f},//triangle 1


		{-0.5f, 0.5f,-0.5f,0,0,0,0.0f,1.0f},
		{ 0.5f, 0.5f,-0.5f,0,0,0,1.0f,1.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,1.0f,0.0f},//triangle 2


		//bottom

		{-0.5f,-0.5f, 0.5f,0,0,0,1.0f,0.0f},
		{ 0.5f,-0.5f, 0.5f,0,0,0,0.0f,0.0f},
		{-0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},//triangle 1


		{-0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},
		{ 0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},
		{ 0.5f,-0.5f, 0.5f,0,0,0,0.0f,0.0f},//triangle 2

		
		//right

		{ 0.5f, 0.5f,-0.5f,0,0,0,0.0f,0.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,1.0f,0.0f},
		{ 0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},//triangle 1


		{ 0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},
		{ 0.5f,-0.5f, 0.5f,0,0,0,1.0f,1.0f},
		{ 0.5f, 0.5f, 0.5f,0,0,0,1.0f,0.0f},//triangle 2


		//left

		{-0.5f, 0.5f,-0.5f,0,0,0,1.0f,0.0f},
		{-0.5f, 0.5f, 0.5f,0,0,0,0.0f,0.0f},
		{-0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},//triangle 1


		{-0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},
		{-0.5f,-0.5f, 0.5f,0,0,0,0.0f,1.0f},
		{-0.5f, 0.5f, 0.5f,0,0,0,0.0f,0.0f},//triangle 2

I tried to calculate it by this function (from andypike.com)

D3DXVECTOR3 cCube::GetTriangeNormal(D3DXVECTOR3* vVertex1, D3DXVECTOR3* vVertex2, D3DXVECTOR3* vVertex3)
{
    D3DXVECTOR3 vNormal;
    D3DXVECTOR3 v1;
    D3DXVECTOR3 v2;

    D3DXVec3Subtract(&v1, vVertex2, vVertex1);
    D3DXVec3Subtract(&v2, vVertex3, vVertex1);

    D3DXVec3Cross(&vNormal, &v1, &v2);

    D3DXVec3Normalize(&vNormal, &vNormal);

    return vNormal;
}
and then running for each triangle and calculate his normals, but it''s not good, one half of each face is visible, the other one is not... why is that?
Advertisement
Make sure all of your triangles are defined in the same "order". It is usually either clockwise or anticlockwise, they just all have to be the same.
hmm
I tried this:
                //front		{-0.5f, 0.5f,-0.5f,0,0,0,0.0f,0.0f},		{ 0.5f, 0.5f,-0.5f,0,0,0,1.0f,0.0f},		{-0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},//triangle 1		{-0.5f,-0.5f,-0.5f,0,0,0,0.0f,1.0f},		{ 0.5f, 0.5f,-0.5f,0,0,0,1.0f,0.0f},		{ 0.5f,-0.5f,-0.5f,0,0,0,1.0f,1.0f},//triangle 2

It''s CW, but now it dosent display anything...
Turn off backface culling!
why post as AP?

anyway, I had! I can see everything without lightning, but when I turn lightning on, I cant see nothing (and yes, I got a light source)...

EDIT:
hmm that's weird, I can see only half of the cube with lightning...
only back, right and bottom faces, when rotating the cube...

here is the vertex buffer:
http://www.rafb.net/paste/results/p2654620.html

[edited by - ElectronicSandClock on January 7, 2004 1:55:43 PM]
Make sure you don''t average your normals for each triangle that shares a point. Boxes are a special case where you want each face to have its own normal. Or else you''ll get this..

\
\----------
|
|

instead of this
|
|
|
-------
|
|
--------|
|

Cheers
Chris


CheersChris

This topic is closed to new replies.

Advertisement