Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Normal question

Recommended Posts

If I had a quad which consists of 2 tris, when I work out their face normals, why one would face up, the other face down?

598067c8abc20_2017-08-0119_32_16-TestSB.png.2af473d0023431dc7cedcc08eb092847.png

	DWORD numVerts = pMesh->GetNumVertices();
    DWORD numTris = pMesh->GetNumFaces();
	VertexPN* v = NULL;
    DWORD* id = NULL;
	std::vector<VertexPN> verts;
	pMesh->LockVertexBuffer(0, (void**)&v);
    for (int i = 0; i < numVerts; i++)
    {
        const VertexPN& _v = v;
        verts.push_back(_v);
    }
	pMesh->UnlockVertexBuffer();
	pMesh->LockIndexBuffer(0, (void**)&id);
    for (int i = 0; i < numTris; i++)
    {
        const VertexPN& v0 = verts[id[i*3+0]];
        const VertexPN& v1 = verts[id[i*3+1]];
        const VertexPN& v2 = verts[id[i*3+2]];
	    D3DXVECTOR3 faceNormal;
        D3DXVec3Cross(&faceNormal, &v0.pos, &v1.pos);
	    if (faceNormal.y > 0)
        {
            TRACE("Face #" << i << " is up" << "\n");
	        ChangeAttrib(i, 246);
            
        }
	        TRACE("Face #: " << i << " normal is y:" << faceNormal.y << "\n");
    }
    pMesh->UnlockIndexBuffer();
	
Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement

If you've searched, you would find that normals aren't calculated like that.

You take a=v1-v0 and b=v2-v0 and use a and b for calculating the normal.

Share this post


Link to post
Share on other sites

Without looking at the code, it likely means you assume the wrong winding order for one of the tris. Just change the order in which you do the cross-product for the incorrect triangle (i assume you use backface-cullling, so the tri should be correct per se; if not just fix that.

Share this post


Link to post
Share on other sites

When showing source code in public, it may be beneficial to ensure that your indentation is consistent.

You seem to have a wild mixture of four spaces and four-wide tabs that expand very poorly in a eight-wide tab world. You may want to invest in a source code formatter or an IDE that unscrews your indentation.

Share this post


Link to post
Share on other sites

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  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!