Skip to content

attrib() not working for POINTS shapes #5895

@cansik

Description

@cansik

Description

At the moment I have to work with pointclouds and render them as a PShape of type POINTS. But for animations and other stuff I need for example a target position for each individual point. As shown in #5560 creating new vertex attributes and setting them later works with PShape in processing. So I decided to use the same technique in an example with a few points.

Expected Behavior

I expected to use the defined attribute vert_color in the shader to set the color of my vertices.

Current Behavior

Unfortunately, the vert_color attribute is not set. It is always set to 0.0 and I have no clue why this happens. It does not matter if I change it during creation or after creation of my PShape.

Steps to Reproduce

Just run the following sketch:

PShape shape;
PShader shader;

public void setup() {
  size(500, 500, P3D);

  shape = createShape();
  shape.beginShape(POINTS);

  // Visual
  shape.noFill();
  shape.stroke(255, 0, 0);

  // 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 150", 
    "", 
    "uniform mat4 projection;", 
    "uniform mat4 modelview;", 
    "", 
    "in vec4 position;", 
    "in vec4 color;", 
    "in vec2 offset;", 
    "in vec4 vert_color;", 
    "", 
    "out vec4 vertColor;", 
    "", 
    "void main() {", 
    " vec4 pt = position;", 
    " pt /= 50.0;", 
    " vec4 pos = modelview * pt;", 
    " vec4 clip = projection * pos;", 
    " gl_Position = clip + projection * vec4(offset, 0, 0);", 
    "", 
    // setting the vert color
    " vertColor = vert_color;", 
    "}"
  };

  String[] frag = new String[] {
    "#version 150", 
    "in vec4 vertColor;", 
    "out vec4 fragColor;", 
    "", 
    "void main() {", 
    "  fragColor = vertColor;", 
    "}"
  };

  return new PShader(this, vert, frag);
}

void keyPressed() {
  for (int i = 0; i < shape.getVertexCount(); i++) {
    shape.setAttrib("vert_color", i, random(0f, 1f), random(0f, 1f), random(0f, 1f), 1.0);
  }
}

Your Environment

  • Processing version: 3.5.3
  • Operating System and OS version: MacOS 10.14.5

Possible Causes / Solutions

I did not have the time to debug the processing code and see if there is something different to the first example in #5560. But I believe that processing is overwriting something or not pushing to opengl because of the POINTS flag.

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