added geometry shaders to ofxShader

Not sure if this is already in one of the current versions (or even which version is the canonical current version) but I added the ability to apply geometry shaders to ofxShader. If that’s going in core I can just branch and submit for git.

Hey sounds nicee ! Could you show any result of this functionality?

Here’s a really simple demo with a modified ofxShader in it. The attached image shows it running. Geometry shaders are a little gnarly but let you do some crazy stuff because you can modify multiple vertexes after the vertex shader, meaning you can use the GPU to recreate geometry REALLY fast. I’m trying to make some demos that do them justice but I’m still just learning.

shader.zip

![](http://forum.openframeworks.cc/uploads/default/876/Screen shot 2010-08-26 at 2.29.45 PM.png)

Just ran this, it’s interesting… I still don’t completely understand when you would want to have a geometry shader. Basically, the advantage is that you can create vertices on the GPU rather than on the CPU followed by uploading them.

Do you know any situations where it’s really helpful to have the geometry shaders?

I’m working on ofxShader at the moment, and I think my version is the most recent:

http://code.google.com/p/kyle/source/browse/#svn/trunk/openframeworks/addons/ofxShader/src

Apparently they’re really awesome for dynamic particle systems (i.e. send a point, generate a triangle/quad from that), hair, automatic level of detail, and also fluid/soft-body simulations though a lot of that stuff is still a little over my head.

Some CV algorithms can be run in geometry shaders (warning: directX code) http://http.developer.nvidia.com/GPUGem-…–ch41.html

or marching cubes: http://www.icare3d.org/blog-techno/gpu/-…-cubes.html

I wanted to really dive into these but I’ve been pulled into some other things that are interfering with my GS study-time but I’ll post another example soon.

Do you know any situations where it’s really helpful to have the geometry shaders?

anything generative! why create on the CPU and then dl to GPU when you can create dynamically on the GPU whenever you need something? Josh has listed a few examples, particles, automatic tessellation of geometry etc. but you can create entire fields of grass, trees, forests, worlds etc! :stuck_out_tongue:

Yeah I was also going to suggest on-the-fly generation of terrain for 3D worlds…

Here’s another quick example of how this can be used: Make some GL_POINTS, pass them to the geometry shader that generates two triangles in a GL_TRIANGLE_STRIP for the geometry, voila, instead geometry:

frag // just pass through

  
  
#version 120  
void main(void)  
{  
    gl_FragColor = vec4(1.0, 0.0, 0.0, 0.5);  
}  
  

vert // just pass through

  
  
void main()  
{  
	gl_FrontColor = gl_Color;  
	gl_TexCoord[0] = gl_MultiTexCoord0;  
	gl_Position = ftransform();  
}  
  

geom

  
  
#version 120  
#extension GL_EXT_geometry_shader4 : enable  
  
//layout(points) in;  
//layout(triangle_strip, max_vertices = 4) out;  
  
uniform float height;  
uniform float width;  
   
void main() {  
  
  
	gl_PositionIn[0];  
  
	gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_PositionIn[0].x, gl_PositionIn[0].y, gl_PositionIn[0].z, 1.0);  
	EmitVertex();  
  
	gl_Position =  gl_ModelViewProjectionMatrix * vec4(gl_PositionIn[0].x, gl_PositionIn[0].y+height, gl_PositionIn[0].z, 1.0);  
	EmitVertex();  
  
	gl_Position =  gl_ModelViewProjectionMatrix *  vec4(gl_PositionIn[0].x+width, gl_PositionIn[0].y, gl_PositionIn[0].z, 1.0);  
	EmitVertex();  
  
	gl_Position =  gl_ModelViewProjectionMatrix * vec4(gl_PositionIn[0].x+width, gl_PositionIn[0].y+height, gl_PositionIn[0].z, 1.0);  
	EmitVertex();  
  
	/*gl_Position = vec4(gl_PositionIn[0].x+width, gl_PositionIn[0].y+height, 0.0, 1.0);  
	EmitVertex();  
  
	gl_Position = vec4(gl_PositionIn[0].x, gl_PositionIn[0].y+height, 0.0, 0.1);  
	EmitVertex();*/  
	// output  
	EndPrimitive();  
}