Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Questions about glCullFace and gl_Fragcoord.z

  1. #1
    Newbie Newbie
    Join Date
    Aug 2017
    Posts
    2

    Questions about glCullFace and gl_Fragcoord.z

    Hello.

    I have questions about glCullFace and gl_Fragcoord.z.

    First, when I turn on and off glCullFace, the depth value from shader stage does not make sense to me.

    This is the scene

    Click image for larger version. 

Name:	pro2_camView.jpg 
Views:	20 
Size:	4.6 KB 
ID:	2460

    This is when the cullface is off

    Click image for larger version. 

Name:	cullOff.jpg 
Views:	21 
Size:	8.6 KB 
ID:	2458

    This is when culling is on.

    Click image for larger version. 

Name:	cullOn.jpg 
Views:	27 
Size:	7.4 KB 
ID:	2459

    Code :
    void DeferredRenderer::ShadowPass(Entity * gLight, const Scene * scene)
    {
      if (auto shadowBuffer = mRenderTarget[ShadowBuffer])
      {
        assert(shadowBuffer->Bind());
        glViewport(0, 0, shadowBuffer->GetBuffer().mWidth, shadowBuffer->GetBuffer().mHeight);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        auto shadowShader = this->mOwnerSystem->GetSubManager<ShaderManager>("Static_ESM");
        assert(shadowShader != nullptr);
        shadowShader->Use();
        for (auto drawable : scene->mDrawableList)
        {
          drawable->Draw(shadowShader);
        }
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_CULL_FACE);
      }
      else DEBUG_PRINT("No shadow buffer");
    }

    Second, what is the difference between gl_Fragcoord.z and depth value?

    Can gl_Fragcoord.z represent depth value in depth buffer?

    This is from gl_Fragcoord.z

    Click image for larger version. 

Name:	cullOn.jpg 
Views:	27 
Size:	7.4 KB 
ID:	2459

    This is from depth buffer

    Click image for larger version. 

Name:	cullOnDepth.jpg 
Views:	22 
Size:	7.1 KB 
ID:	2461

    This is how I created color buffer and depth buffer
    Code :
      mRenderTarget.insert(std::make_pair(ShadowBuffer, new RenderTarget()));
      mRenderTarget[ShadowBuffer]->AddColorBuffer(mShadowSize.x, mShadowSize.y,
        GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT, GL_RED, GL_R32F, GL_FLOAT);
      mRenderTarget[ShadowBuffer]->AddDepthBuffer(mShadowSize.x, mShadowSize.y);

    This is the fragment stage.

    Code :
     
    in float oClipZ;
     
    uniform float uExpConstant;
     
    out float oGShadow;
     
    void main()
    {
      float depthValue = oClipZ * gl_FragCoord.w * 0.5f + 0.5f;
      oGShadow = exp(depthValue * uExpConstant);
      oGShadow = depthValue; 
      oGShadow = gl_FragCoord.z; // gl_FragCoord.z is same with depthValue that I calculated because gl_FragCoord.z = clip.Z / clip.W
    }
    Last edited by jerome503; 08-16-2017 at 11:16 PM.

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,167
    Quote Originally Posted by jerome503 View Post
    Second, what is the difference between gl_Fragcoord.z and depth value?
    They're the same. So if and only if the fragment depth is written to the depth buffer (render target has depth buffer, fragment passes depth/stencil/etc. tests, depth write is on, etc.) and your fragment shader does not write to gl_FragDepth, then the gl_FragCoord.z value would be written.

    First, when I turn on and off glCullFace, the depth value from shader stage does not make sense to me.
    This apparently isn't your depth buffer but some color buffer (**)? You're going to need to look deeper. Compare actual depth buffer values between the cullface ON and cullface OFF cases specifically. Assuming they look the same, then look at how you're computing those color images.

    (**) The reason I say this is because these two confusing images are named cullOn and cullOff. Yet below, you have one named cullOnDepth, which is presumably a depth image (and looks completely respectable by the way), implying the former two images are not depth images.

    Also, before you dig deeper, ensure that the model you're trying to draw with cullface ON actually has the faces oriented so that it renders properly with cullface ON. Otherwise, you're going to have to 1) fix the model, or 2) render it with cullface OFF.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •