Advertisement

Normal question

Started by August 01, 2017 11:36 AM
4 comments, last by Zao 7 years, 6 months ago

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();
	

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.

.:vinterberg:.

Advertisement

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.

Yep, 

vinterberq made my day...

Thanks

Jack

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.

To make it is hell. To fail is divine.

This topic is closed to new replies.

Advertisement