Face flipping when loading OBJ [Solved]

I know that this should be something basic that I'm missing but I'm getting crazy to get the obj models load correctly. They load like back is front and front is back, like the faces where fliipped. Or something that is wrong with that… I dont know if I'm exporting them bad or what.



Thanks for any help.



maxtrix.

Im assuming you're using Blender.



Enable the Normals view in Blender (Should be located underneath Mesh tools). As selection mode pick "faces". You should see blue lines coming from the faces (as well as the edges). Select the faces that appear to have the blue line turning inwards. Then in the same Mesh Tools dialog, click "Flip Normals" which should flip the normals to the opposite direction.



Had the same problem. It sometimes happends when you create your own vertices and connect them.

Nope, I'm ussing 3dmax. 2008 but I think that exporter is the same for all versions.

:S. I get an effect like if I'm rendering the inside of the object.>:S

read the beginning of this thread:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=7496.0



Could it be something similar to your problems? In that case the normals were flipped. Even if you are not using Blender, there should be a way to do what Darklord suggested. Sorry I cannot give you a step-by-step guide, but its worth trying out.

I had the same problem with Maya exporter. The OBJ loader takes the vertices order as they appear in the file, which may sometimes result in flipped triangles. The solution was to calculate the face normal and compare that to the normal loaded from file, and flip the triangle if they were pointing in opposite direction. In my case it is the exporters bug, but since i cannot fix the exporter, this was the workaround that helped.

Thank you all!



I will try to figure out what is happening with the normals. and try to rewrite the loading process.

I think that is something different, because I have flipped all normals and everything…


vear said:

I had the same problem with Maya exporter. The OBJ loader takes the vertices order as they appear in the file, which may sometimes result in flipped triangles. The solution was to calculate the face normal and compare that to the normal loaded from file, and flip the triangle if they were pointing in opposite direction. In my case it is the exporters bug, but since i cannot fix the exporter, this was the workaround that helped.

How did you calculate that in runtime? have you the code or something? maybe we cannot fix the exporter but we can fix the importer. ;)

During loading of the OBJ model. The key parts:



Calculating face normal:


// generate face normal
            Vector3f v = new Vector3f(obj.vertexList.get(second.vIndex));
            Vector3f w = new Vector3f(obj.vertexList.get(third.vIndex));
            v.subtractLocal(obj.vertexList.get(first.vIndex));
            w.subtractLocal(obj.vertexList.get(first.vIndex));
            v.crossLocal(w);
            v.normalizeLocal();



Guessing if the face is flipped:


int flipped = 0;
                Vector3f n1 = obj.normalList.get(first.nIndex);
                float angle1 = v.angleBetween(n1);
                if(angle1>(FastMath.DEG_TO_RAD * 160)) {
                    flipped++;
                }
                Vector3f n2 = obj.normalList.get(second.nIndex);
                float angle2 = v.angleBetween(n2);
                if(angle2>(FastMath.DEG_TO_RAD * 160)) {
                    flipped++;
                }
                Vector3f n3 = obj.normalList.get(third.nIndex);
                float angle3 = v.angleBetween(n3);
                if(angle3>(FastMath.DEG_TO_RAD * 160)) {
                    flipped++;
                }
                Vector3f fn = new Vector3f();
                fn.set(n1).addLocal(n2).addLocal(n3);
                fn.normalizeLocal();
                // if the angle between normal in the file and the generated are not in
                // the same direction, flip the face
               
                float angle = fn.angleBetween(v);
                if(flipped>2 && angle>(FastMath.DEG_TO_RAD * 160)) {
                    // change second and thrird index
                    this.numflipped++;
                }



The actual flipping of triangles:


// the whole group needs to be flipped
                int mpos = thisMat.indexes.size();
                for(int i=this.smoothpos; i<mpos; i+:3) {
                    // change the second and third index
                    int second = thisMat.indexes.get(i+1);
                    int third = thisMat.indexes.get(i+2);
                    thisMat.indexes.set(i+1, third);
                    thisMat.indexes.set(i+2, second);
                }



I my case, whole meshes needed to be flipped, in your case it may be different.

Hi All, I just downloaded a new Max2Obj.dle pluggin. And then all started to work fine. So I think that is the obj exporter in max that is wront.



Thanks to al for the help.



The address where I found the new workin pluggin is:

http://forums.relicnews.com/archive/index.php/t-20963.html



There you should select the first link.

That is:

http://demoninfinite.star-fleet.org/3ds/



Again thanks all for the quick help.