Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Normal question

This topic is 415 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

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!