Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:49:32+00:00 2026-05-25T21:49:32+00:00

In reference to the problem diskussed in article OpenGL clipping a new question arises.

  • 0

In reference to the problem diskussed in article OpenGL clipping a new question arises.

I am implementing the clipping for a node based 2D scene graph. Whenever the clipping boxes are axis aligned I am using the glScissor like proposed in OpenGL clipping. I have sucessfully implemented node clipping of boxes that are axis aligned.

It turns out that each node has to intersect it’s clipping rectangle with the ones of it’s ancestors that use clipping. (That is necessary in case of siblings that are overlapping with the clipping box of an ancestor).

The mechanism of intersecting non axis aligned rectangles has to be implemented using the stencil buffer. I started implementing the proposed solution in OpenGL clipping but am having problems with chidrens having clip rects overlaping with theyr ancestors ones.

Right now stencil clipping works perfect with only one clipping box.
But in case of a child or grand child that intersects with an ancestor the algorith fails, because the intersection of the 2 involved rectangles would be needed here (like in the axis aligned version) as mask and not the full rectangle of the child.

I have thought out the following algorithm:

The topmost node writes starts with a counter value of 1 and draws it’s clipping rectangle filled with 1s into the stencil buffer and renders it’s children stencil-testing against 1. Each sibling that also has clipping turned on draws it’s bounding rectangle by adding 1 to the stencil buffer and then testing against 2 and so on. Now when a siblings clipping rect overlaps with an ancestors one,the region where they overlap will be filled with 2s, giving perfect clipping when testing against 2. This algorithm can be extended to a maximum of 255 nested clipping nodes.

Here comes my questions:

How do I perform rendering to the stencil buffer in a way that instead of 1s being writing,1s are added to the current stencil buffer value, whenever rendering is performed.

This is my code I use to prepare for rendering to the stencil buffer:

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, ref, mask);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

I am looking for a setup that will increase the stencil buffers current value by the value of ref instead of writing ref into the buffer.

Can someone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:49:33+00:00Added an answer on May 25, 2026 at 9:49 pm

    glStencilFunc(GL_ALWAYS, ref, mask); says that:

    • the first argument says that the stencil comparison test always succeeds
    • the second and third are hence ignored, but in principle would set a constant to compare the value coming from the stencil buffer to and which of the bits are used for comparison

    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); has the effect that:

    • when the stencil test fails, the new value replaces the old (per the first argument)
    • when the depth test fails but the stencil test passes, the new value replaces the old (per the second argument)
    • when the stencil and depth test both pass, the new value replaces the old (per the third argument)

    So you’re setting things up so that everything you try to draw will pass the stencil test, and in any case its value will be copied into the stencil buffer whether it passes the test or not.

    Probably what you want to do is to change your arguments to glStencilOp so that you perform a GL_INCR on pixels that pass. So in your stencil buffer you’ll end up with a ‘1’ anywhere touched by a single polygon, a ‘2’ anywhere that you drew two successive polygons, a ‘3’ anywhere you drew 3, etc.

    When drawing the user-visible stuff you can then use something like glStencilFunc set to GL_GEQUAL and to compare incoming values to ‘1’, ‘2’, ‘3’ or whatever.

    If you prefer not to keep track of how many levels deep you are, supposing you have one clip area drawn into the stencil, you can modify it to the next clip area by:

    1. drawing all new geometry with GL_INCR; this’ll result in a stencil buffer where all pixels that were in both areas have a value of ‘2’ and all pixels that were in only one area have a value of ‘1’
    2. draw a full screen polygon, to pass only with stencil fund GL_GREATER and reference value 0, with GL_DECR. Result is ‘0’ everywhere that was never painted or was painted only once, ‘1’ everywhere that was painted twice.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is in reference to the question previously asked The problem here is, each
Completely restated my question: Problem: Losing reference to an iFrame with Mozilla firefox 3.6
There is very strange assembly reference problem and loading problem I experience with my
I noticed people wrote about this circular reference problem quite a bit before but
With reference to Problem with date day/month reversing on save I have further noted
I'm having a problem adding a reference to a .NET assembly I built. Here's
I am having problem in getting reference of Ajax control extender using $find() or
I have a problem with the reference of a variable when loading a saved
I'm having a problem with my compiler telling me there is an 'undefined reference
Occasionally I have a problem when I attempt to update a web reference in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.