PDA

View Full Version : Line stipples, Selection and Picking: How to do these in GL 4.3?



sam_thedancer
09-02-2013, 10:11 AM
Greetings:
I am trying to rework some pre-shader programs in GL 4.3 core profile. Questions I have run into:

1. How to draw stippled lines? As there's no glEnable(GL_LINE STIPPLE) or glLineStipple() in 4.3.

2. How to do selection and picking? E.g., to pick the object clicked by the mouse. My pre-shader program uses gluPickMatrix() to generate the frustum centered at the clicked pixel (I believe gluPickMatrix() is still available though I haven't checked) and then glRenderMode(GL_SELECT) etc.

However, there is no glRenderMode() in 4.3.

Thanks in advance for your help.

thokra
09-02-2013, 10:30 AM
1: Implement the same logic as glLineStipple in a fragment shader and use discard.
2: Ray-casting - for my taste at least.

GClements
09-02-2013, 05:32 PM
2. How to do selection and picking? E.g., to pick the object clicked by the mouse. My pre-shader program uses gluPickMatrix() to generate the frustum centered at the clicked pixel (I believe gluPickMatrix() is still available though I haven't checked) and then glRenderMode(GL_SELECT) etc.

GLU in general is deprecated, and the OpenGL functionality on which gluPickMatrix() depends (OpenGL matrix functions) isn't present in the 3+ core profile. You would need to generate the picking matrix yourself then multiply it with the projection matrix which you pass to the shader.

The closest thing available to glRenderMode() in 3+ core profile is transform-feedback mode, but that's closer to GL_FEEDBACK than GL_SELECTION (and it returns the data prior to clipping/culling).

For picking, one option (among many) is to render the scene into a single-channel integer FBO, with object IDs in place of colours. An advantage of a raster-based approach is that it works with objects which use alpha and/or stencil tests.

sam_thedancer
09-03-2013, 01:46 AM
Thanks a lot for the suggestions.

I indeed got line stipple working reading gl_FragCoord. x and y, and discarding according to the stipple pattern.

This got me thinking about picking and I would ask your opinion if the following might work. Read
the pixel coords from the mouse click, then render the objects one by one and read gl_FragCoord.z if gl_FragCoord.xy match the pixel coords. Least z is picked.

Do you think this will work? Appreciate your opinion before I try to code. Thanks again.

Added: Just thought of a potential problem. In the first phase, when the z competition takes place and color mask is off, the fragment shader has to record the id of the winning object in a variable that can be accessed by the application program in the second drawing phase. How to do this?

GClements
09-03-2013, 06:22 AM
Added: Just thought of a potential problem. In the first phase, when the z competition takes place and color mask is off, the fragment shader has to record the id of the winning object in a variable that can be accessed by the application program in the second drawing phase. How to do this?
Store the object ID in the framebuffer (and use the built-in depth comparison). There are other options (e.g. buffer variables, images), but writing to the framebuffer is the fastest.