Basic Geometry shader, drawing a blank

I am trying to create a basic geometry shader, which will simply pass coordinates through from the vertex shader and onto the fragment shader. Geometry shaders in OF seem to be mostly undocumented. I have yet to find a working example.

The following is my code, along with my shader files.

.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    vid.loadMovie("video.mov");
    vid.setVolume(0);
    vid.play();
    
    ofDisableArbTex();
    
    ofBackground(0);
    ofSetColor(255);
    shader.load("shader.vert","shader.frag","shader.geom");
    shader.setGeometryInputType(GL_POINTS);
    shader.setGeometryOutputType(GL_TRIANGLE_STRIP);
    shader.setGeometryOutputCount(4);

    //Define the mesh
    mesh.setMode(OF_PRIMITIVE_LINES);

    //Set up vertices
    int W = 128;
    int H = 53;
    float xx = ofGetWidth()/W;
    float yy = ofGetHeight()/H;
    int meshSize = 10;
    for (int y=0; y<H; y++) {
        for (int x=0; x<W; x++) {
            mesh.addVertex(ofPoint(
                                   (x - W/2) * meshSize + (meshSize/2) * (y % 2),
                                   (y - H/2) * meshSize,
                                   0));
            mesh.addTexCoord(ofPoint(x * xx,y * yy));
            mesh.addColor(ofColor(255, 255, 255));
        }
    }
    
    float connectionDistance = 14;
    int numVerts = mesh.getNumVertices();
    for (int a=0; a<numVerts; ++a) {
        ofVec3f verta = mesh.getVertex(a);
        for (int b=a+1; b<numVerts; ++b) {
            ofVec3f vertb = mesh.getVertex(b);
            float distance = verta.distance(vertb);
            if (distance <= connectionDistance) {
                mesh.addIndex(a);
                mesh.addIndex(b);
            }
        }
    }
    
}

//--------------------------------------------------------------
void ofApp::update(){
    vid.update();
    vid.getPixelsRef();
}

//--------------------------------------------------------------
void ofApp::draw(){
    //bind the video texture
    vid.getTextureReference().bind();
    
    ofTranslate(ofGetWidth()*.5,ofGetHeight()*.5);
    
    shader.begin();
    mesh.draw();
    shader.end();
    
    //unbind video texture
    vid.getTextureReference().unbind();
    
}

shader.vert

#version 150

// these are for the programmable pipeline system and are passed in
// by default from OpenFrameworks
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 textureMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform sampler2DRect tex0;

in vec4 position;
in vec4 color;
in vec4 normal;
in vec2 texcoord;

out vec4 vFragColorVs;

void main()
{
    
    vFragColorVs = color;
    // send the vertices to the fragment shader
    //varyingTexCoord = texcoord.xy;
    gl_Position = modelViewProjectionMatrix * position;
}

shader.geom

#version 150
layout (points) in;
layout (triangle_strip,  max_vertices=3) out;

in vec4 vFragColorVs[];
out vec4 vFragColor;

void main() {
    int i;
    for ( i=0; i < gl_in.length(); i++) {
        vFragColor = vFragColorVs[i];
        gl_Position = gl_in[i].gl_Position;
        EmitVertex();
    }
    EndPrimitive();
}

shader.frag

// fragment shader
#version 150

// this is how we receive the texture
uniform sampler2DRect tex0;
uniform vec2 mouse;

in vec4 vFragColor;

out vec4 outputColor;

float map(float x,float in_min,float in_max,float out_min,float out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void main()
{
    outputColor = vFragColor;
}

Currently the shader compiles and does nothing. Nothing appears to be drawn to the screen and I have no info in debug. I am thinking that perhaps I am drawing a bunch of triangles with no area (3 overlapping points) with the geometry shader? Do the initial vertices from the vertex shader pass through, or are they replaced by the vertices from the geometry shader?

A hint in any direction would be greatly appreciated.

Have you seen the Geometry shader example that is bundled with OpenFrameworks?

You can find it in: of_release\examples\gl\geometryShaderExample

3 Likes

Literally right under my nose. Thank you!