4
votes

I'm using a simple Geometry Shader in Processing:

shader(shader);
  
beginShape();
   vertex(0.1, 0.1);
   vertex(0.0, 0.0);
   vertex(0.001, 0.02);
endShape();

So I'm applying a shader on a triangle.

We first have a simple Vertex shader that does nothing.

in vec4 position;
 
void main() {
  gl_Position = position;
}

Then we have the Geometry Shader that should return a vertex for each input vertex, so 3 vertex in total.

#version 150

layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;
 
out FragData {
  vec4 color;
} FragOut;
 
void main(){

  for(int i=0; i<gl_in.length(); i++){
    if(i%3==0){
      FragOut.color = vec4(1., 0., 0., 1.);
    }else if(i%3==1){
      FragOut.color = vec4(0., 1., 0., 1.);
    }else if(i%3==2){
      FragOut.color = vec4(0., 0., 1., 1.);
    }
    gl_Position = gl_in[i].gl_Position;
    EmitVertex();
  }
  
  EndPrimitive();
}

Finally we have a simple Fragment Shader that does nothing:

#version 150

in FragData {
  vec4 color;
} FragIn;

out vec4 fragColor;

void main() {
  fragColor = FragIn.color;
}

The result should be a triangle, with a different color for each vertex. But the result are four triangles and I don't know why.

Here's the result

1
Different result, but also wrong. Similar to the picture I already uploaded but with two more triangles. Here's the repo: github.com/PauRosello97/Formorgel-ShadersPau Rosello
No, I just changed to OpenFrameworks and it workedPau Rosello
Anyway, I've answered the question. All you have to do is to invoke noStroke(); before drawing the geoemetry.Rabbid76

1 Answers

0
votes

When drawing the shape, not only the filled polygon is drawn, but also the outline (stroke). Disable drawing the outlines by calling noStroke().

Complete example:

PShader shader;

void settings() {
    size(300, 300, P2D);
}

void setup() {
    shader = new GeometryShader(this, "Vertex.glsl", "Geometry.glsl", "Fragment.glsl");
}

void draw() {
    background(128);  
    
    noStroke();     // <-- this is important
    shader(shader);
    
    beginShape();
        vertex(-0.5, -0.5);
        vertex( 0.5, -0.5);
        vertex( 0.5,  0.5);
    endShape();
}

void keyPressed(){
    saveFrame();
}

For the sake of completeness the class GeometryShader:

import com.jogamp.opengl.GL3;

class GeometryShader extends PShader {
  int glGeometry;
  String geometrySource;

  GeometryShader(PApplet parent, String vertFilename, String geoFilename, String fragFilename) {
    super(parent, vertFilename, fragFilename);
    geometrySource = PApplet.join(parent.loadStrings(geoFilename), "\n");
  }

  // Setup the geometry shader (fragment and vertex shaders are automatically handled in 
  // by the PShader superclass).
  void setup() {
    glGeometry = pgl.createShader(GL3.GL_GEOMETRY_SHADER);
    pgl.shaderSource(glGeometry, geometrySource);
    pgl.compileShader(glGeometry);

    pgl.getShaderiv(glGeometry, PGL.COMPILE_STATUS, intBuffer);
    boolean compiled = intBuffer.get(0) == 0 ? false : true;
    if (!compiled) {
      println("Cannot compile geometry shader:\n" + pgl.getShaderInfoLog(glGeometry));
      return;
    }
    
    pgl.attachShader(glProgram, glGeometry);
  }
}