intustab / saitoobjloader

Automatically exported from code.google.com/p/saitoobjloader
0 stars 0 forks source link

No normals in a facegen OBJ file #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
I'm testing your tool with facegen generated models. Unfortunately facegen 
doesn't export normals in OBJ files.
Would it be possible to compute them by hand when loading the OBJ into P5?

Many thanks!
Josep M

Original issue reported on code.google.com by josep.co...@gmail.com on 2 Aug 2009 at 8:12

GoogleCodeExporter commented 9 years ago
Not in the short term. I'm swamped under a few projects at the moment, so a 
faster solution for you might be to 
go through a 3rd party like Max or Blender to calculate the normals and 
re-export the OBJ.

MattD

Original comment by mattdit...@gmail.com on 3 Aug 2009 at 3:26

GoogleCodeExporter commented 9 years ago

Original comment by mattdit...@gmail.com on 3 Aug 2009 at 3:29

GoogleCodeExporter commented 9 years ago
what about cross product? http://en.wikipedia.org/wiki/Cross_product

  // Get OBJ model //
  OBJModel  modelis = new OBJModel(this, "modelis.obj"); // load world

  // Init array list //
  println("Setting up model");
  ArrayList  faceArrayList = new ArrayList(); // create array list to hold all faces
  //Load everything into Vector3d arrays
  for (int faceNr=0;faceNr<modelis.getTotalFaceCount();faceNr++) { // for all faces in the model
    PVector[] vertexesThatShapeAFacePV = modelis.getFaceVertArray(faceNr); // get the PVector array for coords of vertexes that make a face
    Vector3d[] vertexesThatShapeAFace = new Vector3d[vertexesThatShapeAFacePV.length]; // load that into Vector3d array
    for (int i=0;i<vertexesThatShapeAFace.length;i++) {
            vertexesThatShapeAFace[i] = new Vector3d(vertexesThatShapeAFacePV[i].x,vertexesThatShapeAFacePV[i].y,vertexesThatShapeAFacePV[i].z);
    }
   faceArrayList.add(vertexesThatShapeAFace); // add to array list
  }
  println("There are "+faceArrayList.size()+" faces in the model");

  // create face normals for each face
println("Get plane normals for each face");
  ArrayList  faceNormalsArrayList = new ArrayList();
   for(int i=0; i<faceArrayList.size(); i++) { // for each face in the OBJ model
    Vector3d[] currVector = (Vector3d[])faceArrayList.get(i); // get the Vector3d array that holds all vertexes coords
    // if there are at least 3 vertexes ( assert(currVector.length >= 3) should always evaluate, if not then obj file is corrupt as min poly is trangle by definition and also 
OBJ spec.)
    if (currVector.length >= 3) {
      // search for vectors that are not parallel
      for (int j=2;j<currVector.length;j++) { // for each vertex
        Vector3d vect1 = new Vector3d(currVector[j].x-currVector[j-1].x,currVector[j].y-currVector[j-1].y,currVector[j].z-currVector[j-1].z);
        Vector3d vect2 = new Vector3d(currVector[j-2].x-currVector[j-1].x,currVector[j-2].y-currVector[j-1].y,currVector[j-2].z-currVector[j-1].z);
        if (vect1.angle(vect2) != 0 || vect1.angle(vect2) != PI) { // if these are not parallel vectors!
          println ("Vectors chosen for calculating plane normal for poly no. "+i+" are "+vect1+" and "+vect2);
          Vector3d planeNormal = new Vector3d(); // surface normal
          planeNormal.cross(vect1, vect2);
          println ("Plane normal vector for poly no. "+i+" is: "+planeNormal);
          planeNormal.normalize(); // !
          faceNormalsArrayList.add(planeNormal);
          println ("Normalized plane normal vector for poly no. "+i+" is: "+planeNormal);
          break;
        }
      }
    }
  }

Original comment by m...@krokoarch.lv on 13 Sep 2009 at 8:31