OBJ file and Shape problem

hi to all!! :) i've a little problem with this code...It basicly read the vertex points from an obj file and save it in a PVetor matrix...but it doesn't work and i don't know why!! thanks to all!! here the code:

PShape s;
ObjClass obj;

void setup() {
  size(1000, 1000, P3D);
  // The file "bot.obj" must be in the data folder
  // of the current sketch to load successfully
  s = loadShape("FinalBaseMesh.obj");
  obj=new ObjClass(s);
  obj.createShapes();
}

void draw() {
  background(0);
  obj.render();
}

class ObjClass {
  PShape shape, finals;
  PVector[][] pos;
  ObjClass(PShape shape) {
    this.shape=shape;
    readChild();
  }

  void readChild() {
    for (int i=0; i<shape.getChildCount()/8; i++) {
      PShape mom=shape.getChild(i);
      pos=new PVector[shape.getChildCount()/8][mom.getVertexCount()];
      for (int j=0; j<mom.getVertexCount(); j++) {
        pos[i][j]=mom.getVertex(j);
      }
    }
    println("file read");
  }

  void createShapes() {
    finals=createShape();
    finals.beginShape(POINTS);
    finals.stroke(255);
    finals.fill(255);
    for (int i=0; i<pos.length; i++) {
      for (int j=0; j<pos[0].length; j++) {
       <del>finals.vertex(pos[i][j].x, pos[i][j].y, pos[i][j].z);</del>
      }
    }
    finals.endShape(CLOSE);
  }

  void render(){
    shape(finals);
  }
}

problem is a nullPointerExpection on the deleted line!!

Answers

  • Answer ✓

    solved!!!

    if anyone is interested the problem was that i initialize pos in readChild method!

  • edited March 2017

    Given I was already refactoring your class in order to spot the NPE from the 2D PVector[][], I'm gonna post it below even though you've fixed it by yourself already: =D>

    // forum.Processing.org/two/discussion/21319/obj-file-and-shape-problem#Item_2
    // pietroLama (2017-Mar-10)
    
    class ShapeMaker {
      static final color FILL = -1;
    
      PShape original, created;
      PVector[][] coords;
    
      ShapeMaker(final PShape shp) {
        original = shp;
        storeChildrenVecs();
      }
    
      void render() {
        shape(created);
      }
    
      void storeChildrenVecs() {
        final int children = original.getChildCount() >> 3;
        coords = new PVector[children][];
    
        for (int r = 0; r < children; ++r) {
          final PShape child = original.getChild(r);
          final int vertices = child.getVertexCount();
          final PVector[] vecs = coords[r] = new PVector[vertices];
    
          for (int c = 0; c < vertices; vecs[c] = child.getVertex(c++));
        }
      }
    
      PShape createPShape() {
        created = createShape();
    
        created.beginShape(POINTS);
        created.noStroke();
        created.fill(FILL);
    
        for (final PVector[] row : coords)  for (final PVector v : row)
          created.vertex(v.x, v.y, v.z);
    
        created.endShape(CLOSE);
        return created;
      }
    }
    
Sign In or Register to comment.