Results 1 to 4 of 4

Thread: Stencil BUffer Functioning

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4

    Stencil BUffer Functioning

    Dear All,

    I'd like to know how whether my code walkthrough interpretation is correct or not in the given code snippet below. What I expect to see based on the runing code is: First triangle(skewed one) to be used as mask and as a resulting of this stencil buffer to be filled with masquerading value of 1s.

    Draw the 2nd triangle on screen but using the stencil buffer to block only the portion of screen where filled with 1s. I'm pretty much familiar with related stencil buffer calls and read many tutorials related to that, but seems that I never get any tangible solution up to now. I don't want to succeed simply to get what I want on screen for this example, instead I really want to grasp the full idea behind of stenciling.

    I also commented out the code and what I interpret from it. I really will appreciate your help if you point out where actually I'm making mistake, or my interpretation is wrong or correct.

    Code :
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     
    glEnable(GL_STENCIL_TEST);
    glClear(GL_STENCIL_BUFFER_BIT);
     
    glStencilFunc(GL_NEVER,1,1); // Stencil func will always fail, so due to that any drawn image on color buffer will affect stencil buffer also 
    glStencilOp(GL_REPLACE, GL_KEEP,GL_KEEP); // Here we specify how to modify the stencil buffer, 0s will be replaced by 1s in stencil buffer where shape is drawn 
     
    glStencilMask(1); // Enable writing to stencil buffer 
    glPushMatrix();
    glRotatef(30, 0.0f, 0.0f, 1.0f);
    DrawTri();        // Draw the triangle so the stencil buffer will be overwritten with 1s where there is skewed triangle
    glPopMatrix();
     
    glStencilMask(0); // Turn off writing to stencil buffer 
    glStencilFunc(GL_EQUAL,1,1); // Instruct where to draw 2nd triangle clipped by 1st triangle (skewed).
     
    DrawTri();  // Draw the 2nd triangle where there is 1s in stencil buffer  
    SwapBuffers(hDC);

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655
    Quote Originally Posted by Ronan2013 View Post
    glStencilFunc(GL_NEVER,1,1); // Stencil func will always fail, so due to that any drawn image on color buffer will affect stencil buffer also
    This says that the stencil "test" will always fail. What happens when it fails is dictated by the first argument to glStencilOp(). You state in the next line that it should replace the stencil buffer value with the ref value, 1.

    glStencilMask(1); // Enable writing to stencil buffer
    Not quite. This says to enable writing to the first bit of the stencil buffer only. For instance, if you're ref value was 2 (or any value where the least significant bit is zero), then you'd be writing 0s to that stencil buffer bit instead of 1.

    glStencilMask(0); // Turn off writing to stencil buffer
    glStencilFunc(GL_EQUAL,1,1); // Instruct where to draw 2nd triangle clipped by 1st triangle (skewed).
    Instead of StencilMask, I think you could instead use StencilOp() and set them all to KEEP. But this should work too.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    4
    Thank You,

    glStencilMask(1); // Enable writing to stencil buffer
    Not quite. This says to enable writing to the first bit of the stencil buffer only. For instance, if you're ref value was 2 (or any value where the least significant bit is zero), then you'd be writing 0s to that stencil buffer bit instead of 1.
    Your prognosis was completely correct the code is working, I created a project from scratch and its operating as I expected probably I mis-configured something undeliberately.

    In order to fully grasp the idea behind of stenciling, I'd like to know the below code is exactly where writing to stencil buffer is performed right?
    Code :
    glStencilMask(1); // 
    glPushMatrix();
    glRotatef(30, 0.0f, 0.0f, 1.0f);
    DrawTri();        // Draw the triangle so the stencil buffer will be overwritten with 1s where there is skewed triangle
    glPopMatrix();

    Thanks,

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,655
    Yes, once the triangles dispatched in DrawTri() are rasterized on the GPU.

Similar Threads

  1. Replies: 1
    Last Post: 09-11-2013, 11:35 AM
  2. copy app generated stencil data to stencil buffer
    By guilder in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 12-23-2010, 06:46 PM
  3. Coping from FBO's stencil to main stencil buffer ?
    By gregory_x in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 08-30-2010, 01:09 AM
  4. Depth buffer and stencil buffer / Alpha test
    By greg2 in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 08-05-2004, 01:50 AM
  5. How to clear stencil buffer after stencil test?
    By in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-14-2003, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean