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

来源:互联网 发布:sql server2012向导 编辑:程序博客网 时间:2024/06/05 01:06

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


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

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

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

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

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

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

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

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

#include <GLTools.h>#include <GLShaderManager.h>#include <GLFrustum.h>#include <GLBatch.h>#include <GLMatrixStack.h>#include <GLGeometryTransform.h>#include <StopWatch.h>#include <math.h>#include <stdio.h>#pragma comment(lib, "gltools.lib")#ifdef __APPLE__#include <glut/glut.h>#else#define FREEGLUT_STATIC#include <GL/glut.h>#endifGLShaderManagershaderManager;// Shader ManagerGLMatrixStackmodelViewMatrix;// Modelview MatrixGLMatrixStackprojectionMatrix;// Projection MatrixGLFrustumviewFrustum;// View FrustumGLGeometryTransformtransformPipeline;// Geometry Transform PipelineGLTriangleBatchtorusBatch;GLBatchfloorBatch;//////////////////////////////////////////////////////////////////// This function does any needed initialization on the rendering// context. void SetupRC(){// Initialze Shader ManagershaderManager.InitializeStockShaders();glEnable(GL_DEPTH_TEST);  //glEnable(GL_DEPTH_CLAMP)  开启深度截取功能,暂时先不开启glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// This makes a torusgltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30);floorBatch.Begin(GL_LINES, 324);for (GLfloat x = -20.0; x <= 20.0f; x += 0.5) {floorBatch.Vertex3f(x, -0.55f, 20.0f);floorBatch.Vertex3f(x, -0.55f, -20.0f);floorBatch.Vertex3f(20.0f, -0.55f, x);floorBatch.Vertex3f(-20.0f, -0.55f, x);}floorBatch.End();}///////////////////////////////////////////////////// Screen changes size or is initializedvoid ChangeSize(int nWidth, int nHeight){glViewport(0, 0, nWidth, nHeight);// Create the projection matrix, and load it on the projection matrix stackviewFrustum.SetPerspective(35.0f, float(nWidth) / float(nHeight), 1.0f, 100.0f);projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());// Set the transformation pipeline to use the two matrix stacks transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);}// Called to draw scenevoid RenderScene(void){// Color valuesstatic GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f };static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f };// Time Based animationstatic CStopWatchrotTimer;float yRot = rotTimer.GetElapsedSeconds() * 60.0f;// Clear the color and depth buffersglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Save the current modelview matrix (the identity matrix)modelViewMatrix.PushMatrix();// Draw the groundshaderManager.UseStockShader(GLT_SHADER_FLAT,transformPipeline.GetModelViewProjectionMatrix(),vFloorColor);floorBatch.Draw();// Draw the spinning TorusmodelViewMatrix.Translate(0.0f, 0.0f, -1.2f);modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f);shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(),vTorusColor);torusBatch.Draw();// Restore the previous modleview matrix (the idenity matrix)modelViewMatrix.PopMatrix();// Do the buffer SwapglutSwapBuffers();// Tell GLUT to do it againglutPostRedisplay();}int main(int argc, char* argv[]){gltSetWorkingDirectory(argv[0]);glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize(800, 600);glutCreateWindow("OpenGL SphereWorld");glutReshapeFunc(ChangeSize);glutDisplayFunc(RenderScene);GLenum err = glewInit();if (GLEW_OK != err) {fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));return 1;}SetupRC();glutMainLoop();return 0;}

最终运行的结果如下:


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

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

程序运行结果如下:


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