get vertex from an obj file.

hello guys...i've a question.

you can access the vertex of an obj file ?

i try with this...but it doesn't work.

import peasy.*;
PShape shp;
PeasyCam cam;

void setup() {
  size(1000, 1000, P3D);
  shp=loadShape("face.obj");
  println(shp.getVertexCount());
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);
}

void draw() {
  lights();
  directionalLight(255, 150, 200, -1, 0, 0);  
  background(0);
  shape(shp);
}

the result of println(shp.getVertexCount()) is 0.

thank you!! :)

Answers

  • As far as I know, the getVertexCount() and getVertex() functions only work with vertexes you've added in code using the vertex() function, not with vertexes loaded from a shape file.

    From the reference:

    The getVertex() method returns a PVector with the coordinates of the vertex point located at the position defined by the index parameter. This method works when shapes are created as shown in the example above, but won't work properly when a shape is defined explicitly

  • edited September 2016

    thanks for the answer...there isn't a way to get that vertexes?

  • I was working on some related aspects on PShape on another thread.

    As far as I know KevinWorkman's answer from the documentation is correct, however I'm not able to check your code because I don't have a face.obj file or anything similar to make it work. If you want to make an obj file available or link to one that is similar to the data you are working with then I could take a quick look.

  • there's a rocket.obj file included in the examples > Basic > Shapes

  • edited September 2016 Answer ✓

    Thank you koogs -- for some reason I had never looked at the LoadDisplayOBJ example sketch before, mainly being focused on 2D. Interesting simple data format.

    pietroLama -- your problem is that the main OBJ-imported PShape does not contain any vertices. Instead it contains a collection of child PShapes, each of which contain vertices.

    The reference that KevinWorkman cites is correct -- but misleading. You can use these methods, but you must first loop through child shapes in order to use them.

    Rather than vertices [0], [1], [2], [3], [4], [5]... you have vertices like [0][0], [0][1], [0][2], [1][0], [1][1], [1][2] ... where [s][v] is each child shape (each 3D polygon) containing some number of vertices (e.g. three in a triangular polygon).

    Below I wrote a couple quick demo functions to work with them -- printChildVertices and getChildrenVertexCount. I tested them by adding them to the standard LoadDisplayOBJ example.

    /**
     * Child vertex functions: added to the standard LoadDisplayOBJ example
     * 2016-09-07 Jeremy Douglass
     * forum.processing.org/two/discussion/18087/get-vertex-from-an-obj-file#latest
     */
    PShape rocket;
    float ry;
    int vcount;
    
    public void setup() {
      size(640, 360, P3D);
      rocket = loadShape("rocket.obj");
    
      //// print all vertices
      printChildVertices(rocket);
      //// print a specific vertex i,j : (x,y,z)
      println("rocket.getChild(0).getVertex(0):\n  ", rocket.getChild(0).getVertex(0), "\n");
      //// get summary counts
      vcount = getChildrenVertexCount(rocket);
    }
    
    public void draw() {
      background(0);
      lights();
      text(vcount, width/2, height/2, 200);
      translate(width/2, height/2 + 100, -200);
      rotateZ(PI);
      rotateY(ry);
      shape(rocket);
      ry += 0.02;
    }
    
    void printChildVertices(PShape shape){
      for(int i=0; i<shape.getChildCount(); i++){
        PShape child = shape.getChild(i);
        for(int j=0; j<child.getVertexCount();j++){
          PVector vert = child.getVertex(j);
          println(vert);
        }
      }
      println("");
    }
    
    int getChildrenVertexCount(PShape shape){
      int vertexCount = 0;
      for(PShape child : shape.getChildren()){
        vertexCount += child.getVertexCount();
      }
      int childCount = shape.getChildCount();
      println("Vertex count:", vertexCount);
      println("Child shapes:", childCount);
      println("Vert/children", vertexCount/float(childCount), "\n");
      return vertexCount;
    }
    

    Here is some of the output:

    Vertex count: 1101
    Child shapes: 367
    Vert/children 3.0
    
    rocket.getChild(0).getVertex(0):
       [ 0.088187, 27.748848, 0.016643 ] 
    

    You can see that rocket.obj is made of 367 polygons, which average 3 vertices per triangular tile, for a total of 1101 vertices defining the whole rocket (although many of these vertices are shared between multiple polygons where they meet).

  • sorry...for the late reply...really thanks about your support :D

  • Glad that it was helpful, @pietroLama.

Sign In or Register to comment.