StencilBuffer and RenderMode(GL_SELECT)

Is possible use the StencilBuffer in glRenderMode(GL_SELECT) ??? – I think no.
If I use this code (for check):

glRenderMode(GL_RENDER);
gluPickMatrix(for full view);
glOrtho …

glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS,1,1);
glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);

DrawPolygonSelect();//Drawing some select
polygon
glStencilFunc(GL_EQUAL,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);

DrawScene();//drawing scene
glFinish();
SwapBuffer();

I can see corect,- only the elements included in polygon.
But if I use this code:

glRenderMode(GL_RENDER);
gluPickMatrix(for full view);
glOrtho …

glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
glStencilFunc(GL_ALWAYS,1,1);
glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
DrawPolygonSelect();//Drawing some select
polygon
glStencilFunc(GL_EQUAL,1,1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);

glSelectBuffer(xxx,SelectBuffer);
glRenderMode(GL_SELECT);

DrawSceneSelect();//included glLoadName etc.

found = glRenderMode(GL_RENDER);

in SelectBuffer is all elements from view. Stencil buffer is not used.
Where is my mistake ??? Help me !
Thanks

When changing render mode to GL_SELECT, no fragments are generated. So using the stencil buffer won’t work, since it’s a fragment operation.

How make polygon selection (or circle or elipse). I wanted use Stencil - bad way.
Is posible the select make by OpenGL or I have to use a mathematic algorithm… ?

You can do manuall selection using the frame buffer.

Disable EVERYTHING that affects the final color of the fragment, like texturing, lighting, blending, dithering, fogging, and so on. Then, enable flat shading and render your objects to the back buffer where each object has it’s own unique color. Object in this case is whatever you want to select. If you want to select individual triangles, then a triangle is an object.

Then, when performing the selction, read the color of the pixel where you click, and identity the color with the object.

Example; You have three object you want to choose between. Render the first in red, the second in green and the third in blue. If the color of the pixel where you click is green, you have selected the second object.

If you have several object, you will have to use shades of the same color to identify the objects.

This also assumes you know the exact pixel format of the frame buffer. You must be 100% sure that the color you intend to draw is the color that is actually drawn. So for a frame buffer with 16 bit color depth it will be unsafe to use glColor3ub to select color, since you may have some form of rounding when the color you pass does not exactly match a valid color that is representable in the frame buffer.

Is no it… my English is limited :frowning: … sorry.
I mean this (like CAD application):
1.I have many geometrical objects. (Lines, Circles etc. ) – DrawIt
2.I chose for example 3 points in view by mouse. The 3 points makes polygon selection (barrier, trap select) (Now triangle selection).
3.I would like to know all objects which crossing this polygon (now triangle)

I do not need to know information about one pixel. I need information about all object crossing the selection polygon.

By gluPickMatrix is possible to know a objects crossing square(box).

Ok, I see.

Maybe it’s possible to use three clip planes. First let the user draw the triangle. Then setup three clip planes so that they touch the three sides of the triangle. Then enter selection mode, and select everything on the screen or the bounding square of the triangle, and let the clip planes clip geometry so that everything outside the triangle is clipped.

For more complex shapes you may have to do some kind of multipass selection, and select parts of the selection region in each pass. You’re not guaranteed to have more than 6 or 8 clip planes if I remember correct. So a triangle and quad should be possible in one selection pass.