WoSGamers

WoSGamers welcome you again, the view faces, pleasing your body - thanking!

Hi, I'm currently working on an MD5 loader and having problems with the normals. I cant get them to display properly unless I enable front face culling. Basically what I want to do is flip them so I can work with back face culling. I've tried flipping vectors I use to calculate the crossproduct and tried different orders to calculate the vector as well. But no matter what I try the it always require front face culling. Below is the code I use to calculate the normals:

//Iterate through all the meshes
for(unsigned int m = 0; m < model.numMeshes; ++m)
{
    //We now need to calculate the vertex normal for each triangle in the mesh.
    for(unsigned int i = 0; i < model.meshes[m].numTriangles; ++i)
    {
        //to calculate the normal we need to find 3 points. create two vectors,
        //and then tale the cross product of these two. Each mesh has a list of
        //triangles which contains the index value for the three vertices building the triangle.
        //That means, we can create three points by using the vertices that are being pointed to by
        //the index list for each triangle.
        glm::vec3 p1 = model.meshes[m].Position[model.meshes[m].triangles[i].vertexIndex[0]];
        glm::vec3 p2 = model.meshes[m].Position[model.meshes[m].triangles[i].vertexIndex[1]];
        glm::vec3 p3 = model.meshes[m].Position[model.meshes[m].triangles[i].vertexIndex[2]];

        //Now that we have three points, we can create two vectors by subtracting two of the points.
        glm::vec3 v1 = p2 - p1;
        glm::vec3 v2 = p3 - p1;

        //To find the normal now we need to take the cross product of these two vectors.
        glm::vec3 normal = glm::cross(v1,v2);

        //The next step is to normalise this normal
        normal = glm::normalize(normal);

        //We now have the normal for this set of vertices. Add this normal to the vertices normal.
        model.meshes[m].vertices[model.meshes[m].triangles[i].vertexIndex[0]].normal += normal;
        model.meshes[m].vertices[model.meshes[m].triangles[i].vertexIndex[1]].normal += normal;
        model.meshes[m].vertices[model.meshes[m].triangles[i].vertexIndex[2]].normal += normal;
     }

     //Each vertex of this mesh now has a normal. We take this normal and add it
     //to our buffer of normals which will be used for rendering.
     model.meshes[m].Normal.resize(model.meshes[m].numVertices);
     for(unsigned int i = 0; i < model.meshes[m].numVertices; ++i)
         model.meshes[m].Normal[i] = model.meshes[m].vertices[i].normal;
}

Can anyone see any errors or anything that could be the reason why front face culling is required. I've attached a picture where u can see what it looks like with/without front face culling

*UPDATE*

Right so I guess I've been able to flip the normals, but it doesnt fix the problem with front/back face culling. In the picture you can see below both models are rendered with back face culling, now one of them has the normals pointed towards us, and the other one doesnt, but still, it removes the front when I use back face culling. basically what we can see of the model there is the backside of it. Any ideas what might be the reason for this? 

*UPDATE 2*

I decided to render the actual normals and the model on the right had its normals pointing inwards, so I took the negative cross product to flip the normals, and on this picture you can actually see that the normals are pointing out from the model, yet it's still culling them...

*UPDATE 3*

Ok so I figured out what the problem was.... The model was created in the positive z axis, so I flipped the z coordinates and it worked....

Views: 47

Attachments:

Reply to This

Forum

Closing WosGamers.ning.com 2 Replies

Started by John Sutherland in News. Last reply by Daniel Livingstone May 11.

C++ graphics programming job at Vertual 2 Replies

Started by Peter Bloomfield in Portfolios & Employment. Last reply by Peter Bloomfield May 2.

Get to Brighton...

Started by Daniel Livingstone in Portfolios & Employment Apr 19.

Groups

Events

© 2012   Created by John Sutherland.   Powered by .

Badges  |  Report an Issue  |  Terms of Service