How to fix contoured PShape lighting

edited April 2018 in Questions about Code

Hello again. I have a simple rectangle with holes cut into it. It renders like so:

PShape.badrender

How to fix? Thank you! (Here's the code.)

void setup(){
  size(800,600,P3D);  
}

void draw(){
  noLoop();
  background(200);

  Wall wall = new Wall(400, 300, 100, 50, 0);
  wall.makeWall();

  // default light
  lights();

  pushMatrix();
  translate(width*.5, height*.5);
  wall.draw();
  popMatrix();

}

class Wall {
  float wall_w, wall_h;
  PVector window_dim;
  PShape surface;
  ArrayList<PVector> window_placement = new ArrayList<PVector>();
  color wallColor = color(180, 170, 150);

  Wall(float w, float h){
    wall_w = w;
    wall_h = h;
  }

  Wall(float w, float h, float ww, float wh, float wd){
    this(w, h);
    window_dim = new PVector(ww, wh, wd);
    setWindowPlacements();
  }

  void setWindowPlacements(){
    for (int c=-2; c<=2; c+=2){
      window_placement.add(new PVector(0.0, window_dim.y*c, 0.0));
    }
  }

  void makeWall(){
    surface = createShape();
    surface.beginShape();
    surface.noStroke();

    // make the wall
    surface.fill(wallColor);
    surface.vertex(-wall_w*.5, -wall_h*.5);
    surface.vertex( wall_w*.5, -wall_h*.5);
    surface.vertex( wall_w*.5,  wall_h*.5);
    surface.vertex(-wall_w*.5,  wall_h*.5);

    // punch windows into it
    for (PVector v : window_placement){
      surface.beginContour();
      surface.vertex(-window_dim.x*.5+v.x, -window_dim.y*.5+v.y);
      surface.vertex(-window_dim.x*.5+v.x,  window_dim.y*.5+v.y);
      surface.vertex( window_dim.x*.5+v.x,  window_dim.y*.5+v.y);
      surface.vertex( window_dim.x*.5+v.x, -window_dim.y*.5+v.y);
      surface.endContour();
    }

    surface.endShape(CLOSE);
  }

  void draw(){
    shape(surface);
  }

}

Answers

  • edited April 2018 Answer ✓

    Figured it out... Needed to add surface.normal(0, 0, 1).

    I suppose if I'm going to build an object via vertices I need to specify its normals rather than make assumptions. Lesson learned.

Sign In or Register to comment.