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

  • SEARCH
  • Home
  • 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 7631441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:21:18+00:00 2026-05-31T06:21:18+00:00

I want to set up multiple stencils in OpenGL and then draw into multiple

  • 0

I want to set up multiple stencils in OpenGL and then draw into multiple combinations of them.

For example:
I have two rectangular stencils like this:

multiple regions

Then I want to draw into:

  1. anywhere
  2. left rectangle (blue+purple)
  3. right rectangle (purple+red)
  4. middle rectangle (purple)
  5. whole colored area (blue+purple+red)

I have found that it is possible to declare multiple stencils in different bits of stencil buffer but I don’t know how.
How do I setup glStencilOp and glStencilFunc for this to work?

Or can I (should I) use glScissor for that?

  • 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-31T06:21:19+00:00Added an answer on May 31, 2026 at 6:21 am

    I don’t currently know, if it’s possible to configure the stencil buffer for being able to do all the above 5 steps without making any changes to the stencil buffer between them. It would be easy if glStencilOp provided bitwise OR, but it doesn’t and with just using increment or decrement you would have to draw the rectangles multiple times.

    But if the regions are always rectangles, why not just use the scissor test? So the first 3 steps (or actually 2 and 3) can be done by just setting the rectangle’s region with glScissor and enabling the scissor test (glEnable(GL_SCISSOR_TEST)).

    For the middle one (step 4) you either compute the purple intersection rectangle yourself and use the scissor test again, or you use the stencil test:

    glEnable(GL_STENCIL_TEST);
    glStencilFunc(/*whatever*/);
    glStencilOp(GL_INCR, GL_INCR, GL_INCR);    //increase the stencil value
    //draw both rectangles
    
    glStencilFunc(GL_EQUAL, 2, 0xFFFFFFFF);    //only draw where both rectangles are
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);    //don't change the stencil buffer
    //draw things
    

    So we first draw both rectangles and increase the stencil value everywhere they are drawn. Then we draw our things everywhere the stencil value is 2, meaning both rectangles were drawn.

    For the 5th step you use the same stencil buffer, but with

    glStencilFunc(GL_LEQUAL, 1, 0xFFFFFFFF);
    

    for the second pass. This way you draw something everywhere the stencil buffer is at least 1, meaning at least one rectangle was drawn.

    For more than two rectangles it can get more complicated and you need to play around a bit to find the most optimal way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple websites set up in the same folder, and I want to
Suppose you have a page with multiple datepickers and you want to set some
I have two listboxes set with multiple property and on adding the items from
I want to save a set of queries (multiple SQL updates) as a single
My question is : I have an EditText and I want set a margin
i have a problem, i want set text of a UILabel or UItextView or
I have used the same pendingIntent to set multiple alarms using different intentId for
I'm using multiple sheets within my Excel application and want to activate (set focus
I have a table containing multiple addresses, including their Lat/Long coordinates, and I want
I have a personal page (not a cms) where I want to insert multiple

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.