PDA

View Full Version : Discarding vertices possible using element array buffer?



samvdj
12-18-2013, 05:01 AM
Hi all,

I'm trying to use the discard; command in my fragment shader to eliminate vertices with a specific color. When displaying using 2D textures, borderline effects can be eliminated by setting the third parameter in glTexParameterf to GL_NEAREST:



glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);


However, when I display my object in 3D using an element array buffer, I want to achieve the same functionality... Any idea how to do this equivalently?



__glewBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

//glTexParameterf(GL_ELEMENT_ARRAY_BUFFER, GL_TEXTURE_MAG_FILTER,GL_NEAREST); // <- Looking for appropriate function
glPolygonMode(GL_FRONT_AND_BACK, wireFrame ? GL_LINE : GL_FILL);
glDrawElements(GL_TRIANGLE_STRIP, 2*(width-1)*(height-1), GL_UNSIGNED_INT, 0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

__glewBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);


1202

Dark Photon
12-18-2013, 02:29 PM
I think you're a little confused about something here.


I'm trying to use the discard; command in my fragment shader to eliminate vertices with a specific color.

Eliminate "vertices" with a fragment shader? Frag shader has discard, but you're not eliminating vertices at that point, but fragments. We're not dealing with vertices at this point in the pipeline.


glTexParameterf to GL_NEAREST ... However, when I display my object in 3D using an element array buffer, I want to achieve the same functionality... Any idea how to do this equivalently?


__glewBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);[

//glTexParameterf(GL_ELEMENT_ARRAY_BUFFER, GL_TEXTURE_MAG_FILTER,GL_NEAREST); // <- Looking for appropriate function
...


The TEXTURE_MAG/MIN_FILTER settings are for texture units (the gizmos that sample textures), and the element array buffer is not a texture unit but just a bind point for a data buffer, so this doesn't work.

But reading between-the-lines, I think I know what you're trying to ask for. Whereas with texture sampling, MAG/MIN_FILTERS of NEAREST disable color interpolation when sampling, I "think" you're saying you would like to disable color vertex attribute interpolation across polygons (this is interpolation, but in a different context from texture sampling).

For fixed-function rendering (the old days), you could just use glShadeModel (https://www.opengl.org/sdk/docs/man2/xhtml/glShadeModel.xml)( GL_FLAT ) to disable color interpolation across triangles. Now when using shaders, you instead declare your color interpolator in the shader with the keyword "flat" to tell the driver to use no interpolation for this value -- see the GLSL manual. You also have control over which triangle vertex's attributes are used through PROVOKING_VERTEX control.

Backing up a step though, if you're trying to throw away whole triangles whose vertices have some color that matches a shader-determined criteria, then it would be much more efficient to kill the triangle in the vertex shader rather than in every single fragment (pixel or sample) generated by the triangle. There's no discard in the vertex shader, but you can mimic this by just moving each vertex behind the near clip plane. If you're using a perspective projection, here's one way to do that:

gl_Position = vec4( 0,0,1,0 ); // Clipped! (with perspective projection)