当前位置:   article > 正文

关于OpenGL中深度截取(GL_DEPTH_CLAMP)的讲解_depth clamp

depth clamp

首先了解下视椎体,关于透视投影视椎体,如下图所示


在视觉坐标系下,片段fragment的深度放缩到0-1之间的一个有限范围内。片段的深度值为0的话则表明它与近裁截面

重合相交(如果是真实世界的话就是刺入你的眼睛),1代表物体的最远距离(但不是无限远)。为了消除远裁截面

在任意距离上绘制图形,我们需要在深度缓存depth buffer中储存任意大的深度值–当然这是计算机不可能实现的。

那么假设,我们希望在Z轴方向上可以看到近平面和远平面之外的场景,该怎么办?特别是在进行阴影区域渲染

时,要尽可能多的沿着Z轴方向保留几何图形,下面深度截取就起到作用了。

OpenGL可以用深度数值截取到0-1范围选项来关闭近远截面所造成的截面。这意味着任何在近平面之前,和远平面

后的几何体都会被最终投射到平面之上从而被看见。

下面通过一个演示程序,在不开启深度截取的情况下,几何图形在视椎体范围的变化过程,代码如下:

  1. #include <GLTools.h>
  2. #include <GLShaderManager.h>
  3. #include <GLFrustum.h>
  4. #include <GLBatch.h>
  5. #include <GLMatrixStack.h>
  6. #include <GLGeometryTransform.h>
  7. #include <StopWatch.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #pragma comment(lib, "gltools.lib")
  11. #ifdef __APPLE__
  12. #include <glut/glut.h>
  13. #else
  14. #define FREEGLUT_STATIC
  15. #include <GL/glut.h>
  16. #endif
  17. GLShaderManager shaderManager; // Shader Manager
  18. GLMatrixStack modelViewMatrix; // Modelview Matrix
  19. GLMatrixStack projectionMatrix; // Projection Matrix
  20. GLFrustum viewFrustum; // View Frustum
  21. GLGeometryTransform transformPipeline; // Geometry Transform Pipeline
  22. GLTriangleBatch torusBatch;
  23. GLBatch floorBatch;
  24. //
  25. // This function does any needed initialization on the rendering
  26. // context.
  27. void SetupRC()
  28. {
  29. // Initialze Shader Manager
  30. shaderManager.InitializeStockShaders();
  31. glEnable(GL_DEPTH_TEST); //glEnable(GL_DEPTH_CLAMP) 开启深度截取功能,暂时先不开启
  32. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  33. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  34. // This makes a torus
  35. gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);
  36. floorBatch.Begin(GL_LINES, 324);
  37. for (GLfloat x = -20.0; x <= 20.0f; x += 0.5) {
  38. floorBatch.Vertex3f(x, -0.55f, 20.0f);
  39. floorBatch.Vertex3f(x, -0.55f, -20.0f);
  40. floorBatch.Vertex3f(20.0f, -0.55f, x);
  41. floorBatch.Vertex3f(-20.0f, -0.55f, x);
  42. }
  43. floorBatch.End();
  44. }
  45. ///
  46. // Screen changes size or is initialized
  47. void ChangeSize(int nWidth, int nHeight)
  48. {
  49. glViewport(0, 0, nWidth, nHeight);
  50. // Create the projection matrix, and load it on the projection matrix stack
  51. viewFrustum.SetPerspective(35.0f, float(nWidth) / float(nHeight), 1.0f, 100.0f);
  52. projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
  53. // Set the transformation pipeline to use the two matrix stacks
  54. transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
  55. }
  56. // Called to draw scene
  57. void RenderScene(void)
  58. {
  59. // Color values
  60. static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };
  61. static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
  62. // Time Based animation
  63. static CStopWatch rotTimer;
  64. float yRot = rotTimer.GetElapsedSeconds() * 60.0f;
  65. // Clear the color and depth buffers
  66. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67. // Save the current modelview matrix (the identity matrix)
  68. modelViewMatrix.PushMatrix();
  69. // Draw the ground
  70. shaderManager.UseStockShader(GLT_SHADER_FLAT,
  71. transformPipeline.GetModelViewProjectionMatrix(),
  72. vFloorColor);
  73. floorBatch.Draw();
  74. // Draw the spinning Torus
  75. modelViewMatrix.Translate(0.0f, 0.0f, -1.2f);
  76. modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);
  77. shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),
  78. vTorusColor);
  79. torusBatch.Draw();
  80. // Restore the previous modleview matrix (the idenity matrix)
  81. modelViewMatrix.PopMatrix();
  82. // Do the buffer Swap
  83. glutSwapBuffers();
  84. // Tell GLUT to do it again
  85. glutPostRedisplay();
  86. }
  87. int main(int argc, char* argv[])
  88. {
  89. gltSetWorkingDirectory(argv[0]);
  90. glutInit(&argc, argv);
  91. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  92. glutInitWindowSize(800, 600);
  93. glutCreateWindow("OpenGL SphereWorld");
  94. glutReshapeFunc(ChangeSize);
  95. glutDisplayFunc(RenderScene);
  96. GLenum err = glewInit();
  97. if (GLEW_OK != err) {
  98. fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
  99. return 1;
  100. }
  101. SetupRC();
  102. glutMainLoop();
  103. return 0;
  104. }

最终运行的结果如下:


从上图可以看出,当几何图形运行到近平面之外时,不在进行显示。下面在代码中的SetupRC()函数里面添加

glEnable(GL_DEPTH_CLAMP),开启深度截取功能。

程序运行结果如下:


从这两个动画可以明显的区别出是否开启深度截取,对几何图形的影响。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号