Click here to Skip to main content
15,896,606 members

why we disable writing in color and depth buffer before stencil test and enable it after stencil test in reflection?

Sweety Khan asked:

Open original thread
this is an eg code
C#
void display (void) {

     glClearStencil(0); //clear the stencil buffer
     glClearDepth(1.0f);
     glClearColor (1.0,1.0,1.0,1);
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |GL_STENCIL_BUFFER_BIT);
     glLoadIdentity();
     glTranslatef(0, 0, -10);
     //start
     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //disable the color mask
     glDepthMask(GL_FALSE); //disable the depth mask

     glEnable(GL_STENCIL_TEST); //enable the stencil testing


     glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); //setthe stencil buffer to replace our next lot of data

     floor(); //set the data plane to be replaced

     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //enablethe color mask
     glDepthMask(GL_TRUE); //enable the depth mask

     glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //set the stencilbuffer to keep our next lot of data

     glDisable(GL_DEPTH_TEST); //disable depth testing of thereflection
     glPushMatrix();
     glScalef(1.0f, -1.0f, 1.0f); //flip the reflection vertically

     glTranslatef(0,2,0); //translate the reflection onto the drawing plane
     glRotatef(angle,0,1,0); //rotate the reflection
     square(); //draw the square as our reflection
     glPopMatrix();
     glEnable(GL_DEPTH_TEST); //enable the depth testing
     glDisable(GL_STENCIL_TEST); //disable the stencil testing

    //end

    glEnable(GL_BLEND); //enable alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blending function

     floor(); //draw our bench
     glDisable(GL_BLEND); //disable alpha blending

     glRotatef(angle,0,1,0); //rotate our square
     square(); //draw our square

    glutSwapBuffers();
    angle++;
 }

one thing more why we disable depth test before rendering reflection of square?
Tags: C++, OpenGL

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900