Skip to content

"PShape::attrib()" and "PShape::setAttrib()" are not working. #5560

@Stephcraft

Description

@Stephcraft

Description

PShape::attrib() and PShape::setAttrib() are not working.

As discussed on the forum:
https://discourse.processing.org/t/pshape-attrib-and-pshape-setattrib-dont-work-proprely/845

Expected Behavior

Make Shape::attrib() and PShape::setAttrib() assign some attributes in glsl shaders and make processing not crash.

Current Behavior

When using PShape::attrib() it works fine.
Meanwhile, when using Shape::attrib() on a shape then added as a child in a GROUP PShape, it throws error like : java.lang.NullPointerException and crashes. Also when using PShape::setAttrib() it does not work in any case.

Steps to Reproduce

3 following Exemples to demonstrate the problem.

  1. Working Exemple
//
//  Working Exemple
//

PShape shape;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  shape = createShape();
  shape.beginShape();
  
  // Visual
  shape.noStroke();
  shape.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  shape.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Supposed to be Orange
  
  shape.vertex(-100, -100);
  shape.vertex(100, -100);
  shape.vertex(100, 100);
  shape.vertex(-100, 100);
  
  shape.endShape();
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_Position = transform * position;",
    "  vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}
  1. PShape::attrib() in GROUP PShape crash
//
//  Does not work when shape is GROUP
//  ERROR : java.lang.NullPointerException
//

PShape shape;
PShape child;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  child = createShape();
  child.beginShape();
  
  // Visual
  child.noStroke();
  child.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  child.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Supposed to be Orange
  
  child.vertex(-100, -100);
  child.vertex(100, -100);
  child.vertex(100, 100);
  child.vertex(-100, 100);
  
  child.endShape();
  
  shape = createShape(GROUP);
  shape.addChild(child);
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_Position = transform * position;",
    "  vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}
  1. PShape::setAttrib() crash
//
//  Does not work when using PShape::setAttrib() later
//  ERROR : java.lang.NullPointerException
//

PShape shape;
PShader shader;

public void setup() {
  size(500, 500, P2D);
  
  shape = createShape();
  shape.beginShape();
  
  // Visual
  shape.noStroke();
  shape.fill(255, 0, 0); // Not supposed to be Red
  
  // Custom Attribute
  //shape.attrib("vert_color", 1.0, 0.5, 0.0, 1.0); // Not yet
  
  shape.vertex(-100, -100);
  shape.vertex(100, -100);
  shape.vertex(100, 100);
  shape.vertex(-100, 100);
  
  shape.endShape();
  
  // Set Custom Attributes
  for(int i=0; i<shape.getVertexCount(); i++) {
    shape.setAttrib("vert_color", i, 1.0, 0.5, 0.0, 1.0); // java.lang.NullPointerException, huh
  }
  
  // Exemple shader
  shader = createShader();
}

public void draw() {
  background(255);
  
  shader(shader);
  translate(width/2, height/2);
  shape(shape);
}

public PShader createShader() {
  String[] vert = new String[] {
    "#version 110",
    "uniform mat4 transform;",
    "",
    "attribute vec4 position;",
    "attribute vec4 vert_color;",
    "",
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_Position = transform * position;",
    "  vertColor = vert_color;",
    "}"
  };
  
  String[] frag = new String[] {
    "varying vec4 vertColor;",
    "",
    "void main() {",
    "  gl_FragColor = vertColor;",
    "}"
  };
  
  return new PShader(this, vert, frag);
}

Your Environment

  • Processing version: 3.3.7
  • Operating System and OS version: Mac OS 10.12.6

Possible Causes / Solutions

The functions PShape::attrib() and PShape::setAttrib() are located in : processing/opengl/PShapeOpenGL.java

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions