Shader not displaying ofMesh altered by geometry shader

Hey folks,

first off let me thank everyone for this great project and its community. It’s been amazing how easy and fun it was to get started with openFrameworks with nearly no background in OpenGL, heck even C++. Thanks for all the tutorials,forum replys and the documentation !

So I recently discovered shaders and I’m trying to figure them out .Unfortunately i ran into some very odd stuff.
I want to display 3 squares (build by the geometry shader around the vectors in the mesh) and color them. What i’m left with is just a blank screen.
I’m using a vector, geometry and fragment shader which i tried to keep simple to get used too. Simple seems
to be to hard though :smiley:

So here’s the app’s code:

ofApp.cpp

void ofApp::setup(){

ofSetFrameRate(60);
renderShader.load("shaders/render.vert","shaders/render.frag","shaders/render.geom");
renderShader.setGeometryInputType(GL_POINTS);
renderShader.setGeometryOutputType(GL_TRIANGLES);

mesh.setMode(OF_PRIMITIVE_POINTS);
mesh.addVertex(ofVec3f(150,50,0));
mesh.addColor(ofColor(255,0,0));
mesh.addVertex(ofVec3f(100,150,0));
mesh.addColor(ofColor(255,0,255));
mesh.addVertex(ofVec3f(50,120,0));
mesh.addColor(ofColor(255,255,0));

}


//--------------------------------------------------------------
void ofApp::draw(){

    renderShader.begin();
	renderShader.setUniform1f("size",4);
		mesh.draw();
	renderShader.end();

}

Vertex Shader

#version 150
#extension GL_EXT_gpu_shader4 : enable
#extension GL_ARB_texture_rectangle : enable

uniform mat4 modelViewProjectionMatrix;
in vec4 position;

void main(){
   gl_Position = position;
}

Geometry Shader

#version 120
#extension GL_EXT_geometry_shader4 : enable
#extension GL_EXT_gpu_shader4 : enable

uniform float size;

void main(void){
for(int i = 0; i < gl_VerticesIn; i++){
    gl_Position = gl_ModelViewProjectionMatrix * ( gl_PositionIn[i] + vec4(-size,-size,0.0,0.0));
    EmitVertex();
    
    gl_Position = gl_ModelViewProjectionMatrix * (gl_PositionIn[i] + vec4(size,-size,0.0,0.0));
    EmitVertex();
    
    gl_Position = gl_ModelViewProjectionMatrix * (gl_PositionIn[i] + vec4(size,size,0.0,0.0));
    EmitVertex();
    EndPrimitive();

    gl_Position = gl_ModelViewProjectionMatrix * (gl_PositionIn[i] + vec4(-size,-size,0.0,0.0));
    EmitVertex();
    
    gl_Position = gl_ModelViewProjectionMatrix * (gl_PositionIn[i] + vec4(-size,size,0.0,0.0));
    EmitVertex();
    
    gl_Position = gl_ModelViewProjectionMatrix * (gl_PositionIn[i] + vec4(size,size,0.0,0.0));
    EmitVertex();
    EndPrimitive();
}
}

and last but not least:

Fragment Shader

#version 120
#extension GL_ARB_texture_rectangle : enable

void main()
{ 

gl_FragColor = vec4(255, 255, 0, 1);
}

I appreciate every hint :smile: thanks in advance !

Hi,

As far as I understand your output primitive is one of:
points
line_strip
triangle_strip

if you check the Primitive in/out specification section here:
https://www.opengl.org/wiki/Geometry_Shader

And this talks about it a bit as well:
http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/geometry-shader/

Hope that helps!

/A

Hey,

thanks for the reply. I changed “GL_TRIANGLES” to “GL_TRIANGLE_STRIP” but still nothing gets displayed. I’m still quite lost, no idea what i’m missing :smiley:

thanks !

They can be a bit of a nightmare…

Did you set a maximum geometry output count?

shader.setGeometryOutputCount(4);

Hi there!

With a quick look, I noticed that you are using two different GLSL versions. The vertex shader is 150, instead of 120. (Considering that you are using the geometry shader extension, I assume you want to use 120).

If so, you need to change the vertex shader to 120 and pass the vertices with ftransform() or gl_ModelViewProjectionMatrix * gl_Vertex. Also, in the fragment shader, you are using ints and a 0-255 range for the outputs. In shaders, the outputs must be floats and normalised, so from 0.0 to 1.0.

It’s a very common mistake. Here you can find some more info and recommendations. Also, books like this one have simple but acceptable chapters on syntax, functions and other stuff. But you can easily find it on the web.

Good work!

1 Like