OpenGL: 深度裁剪glClearDepth
2014-02-25 13:14
253 查看
glClearDepth(1.0); // 1.0是最大深度([0.0,1.0])
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); // 最好在display()中调用
以下面的代码为例:
GLfloat vertexArr1[] = { -5.0, 0.0, 0.0,
5.0, 0.0, 0.0,
0.0, 10.0, 0.0,
-5.0, 5.0, -5.0,
5.0, 5.0, -5.0,
0.0, -5.0, -5.0};
int vertArrSize = 6;
......
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.1, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
for(int i=0; i<3; i++) {
int offset = i*3;
glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]);
}
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
for(int i=3; i<6; i++) {
int offset = i*3;
glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]);
}
glEnd();
glFlush();
如果不使用glEnable(GL_DEPTH_TEST),则绘制的结果是绿色三角形在红色三角形之上;
这同时说明了openGL的绘制顺序与代码中的出现顺序相反。
代码如下:
运行结果如下:
Enable Depth
Not Enable Depth
http://blog.sina.com.cn/s/blog_8e6bfecf0100v0nr.html
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); // 最好在display()中调用
以下面的代码为例:
GLfloat vertexArr1[] = { -5.0, 0.0, 0.0,
5.0, 0.0, 0.0,
0.0, 10.0, 0.0,
-5.0, 5.0, -5.0,
5.0, 5.0, -5.0,
0.0, -5.0, -5.0};
int vertArrSize = 6;
......
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.1, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
for(int i=0; i<3; i++) {
int offset = i*3;
glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]);
}
glEnd();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
for(int i=3; i<6; i++) {
int offset = i*3;
glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]);
}
glEnd();
glFlush();
如果不使用glEnable(GL_DEPTH_TEST),则绘制的结果是绿色三角形在红色三角形之上;
这同时说明了openGL的绘制顺序与代码中的出现顺序相反。
代码如下:
#include <windows.h> #include <GL/glut.h> #include <GL/glaux.h> #pragma comment(lib,"glut32.lib") #pragma comment(lib,"glu32.lib") #pragma comment(lib,"opengl32.lib") #include <math.h> #include <iostream> using namespace std; GLfloat vertexArr1[] = { -5.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 10.0, 0.0, -5.0, 5.0, -5.0, 5.0, 5.0, -5.0, 0.0, -5.0, -5.0 }; int vertArrSize = 6; double calcDist(GLdouble _x,GLdouble _y,GLdouble _z,GLdouble _wx0,GLdouble _wy0,GLdouble _wz0,GLdouble _wx1,GLdouble _wy1,GLdouble _wz1) { double tDx = _wx1 - _wx0; double tDy = _wy1 - _wy0; double tDz = _wz1 - _wz0; double tDisUnit = sqrt(tDx*tDx + tDy*tDy + tDz * tDz); tDx = tDx / tDisUnit; tDy = tDy / tDisUnit; tDz = tDz / tDisUnit; double tVecX = _wx0 - _x; double tVecY = _wy0 - _y; double tVecZ = _wz0 - _z; double tDisX = tDy*tVecZ - tDz*tVecY; double tDisY = -tDx*tVecZ + tDz*tVecX; double tDisZ = tDx*tVecY - tDy*tVecX; return sqrt(tDisX*tDisX + tDisY*tDisY + tDisZ*tDisZ); } int findClosestPt(GLdouble _wx0,GLdouble _wy0,GLdouble _wz0,GLdouble _wx1,GLdouble _wy1,GLdouble _wz1) { double minDis; int minDisPos; minDisPos = 0; minDis = calcDist((double)vertexArr1[0], (double)vertexArr1[1], (double)vertexArr1[2], _wx0, _wy0, _wz0, _wx1, _wy1, _wz1); for(int i=1; i<vertArrSize; i++) { int offset = i * 3; double tmpDis = calcDist(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2], _wx0, _wy0, _wz0, _wx1, _wy1, _wz1); if(tmpDis < minDis) { minDis = tmpDis; minDisPos = i; } } return minDisPos; } void init() { glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); glClearDepth(1.0); // 1.0是最大深度([0.0,1.0]) glClear(GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); // 最好在display()中调用 } void display() { glClear(GL_COLOR_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 0.1, 500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 50.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glColor3f(1.0, 0.0, 0.0); glBegin(GL_TRIANGLES); for(int i=0; i<3; i++) { int offset = i*3; glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]); } glEnd(); glColor3f(0.0, 1.0, 0.0); glBegin(GL_TRIANGLES); for(int i=3; i<6; i++) { int offset = i*3; glVertex3f(vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]); } glEnd(); glFlush(); } void mouse(int button, int state, int x, int y) { GLint viewport[4]; GLdouble mvmatrix[16], projmatrix[16]; GLint realy; GLdouble wx0, wy0, wz0; GLdouble wx1, wy1, wz1; switch(button) { case GLUT_LEFT_BUTTON: if(state == GLUT_DOWN) { //printf(" %d %d \n", x, y); glGetIntegerv(GL_VIEWPORT, viewport); glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix); glGetDoublev(GL_PROJECTION_MATRIX, projmatrix); realy = viewport[3] - (GLint)y - 1; //printf("Coordinates at cursor are (M, M)\n", x, realy); gluUnProject((GLdouble)x, (GLdouble)realy, 0.0, mvmatrix, projmatrix, viewport, &wx0, &wy0, &wz0); printf("World coords at z=0.0 are (%.3f, %.3f, %.3f)\n", wx0, wy0, wz0); gluUnProject((GLdouble)x, (GLdouble)realy, 1.0, mvmatrix, projmatrix, viewport, &wx1, &wy1, &wz1); printf("World coords at z=1.0 are (%.3f, %.3f, %.3f)\n", wx1, wy1, wz1); int i = findClosestPt(wx0, wy0, wz0, wx1, wy1, wz1); int offset = i*3; printf("Choose Point (%.3f, %.3f, %.3f)\n\n", vertexArr1[offset], vertexArr1[offset+1], vertexArr1[offset+2]); } break; default: break; } } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(300, 300); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutMouseFunc(mouse); glutMainLoop(); return 0; }
运行结果如下:
Enable Depth
Not Enable Depth
http://blog.sina.com.cn/s/blog_8e6bfecf0100v0nr.html
相关文章推荐
- OpenGL 透视投影 齐次裁剪空间 深度缓存
- OpenGL 透视投影 齐次裁剪空间 深度缓存
- OpenGL,z值转深度值计算
- OPENGL—编码裁剪(Cohen-Sutherland)法裁剪图
- 【学习OpenGL】(二)——绘制矩形(视口与裁剪区)
- 什么是OpenGL中的深度、深度缓存、深度测试?
- OpenGL: 深度冲突(Z-Finghting or Depth Fighting)的解决
- openGL之深度测试、表面剔除、剪裁---openGL学习笔记(五)
- OpenGL 深度测试使用
- OpenGL深度测试带来的问题----Z冲突
- opengl与OSG的深度测试
- OpenGL.裁剪算法
- OpenGL学习09_裁剪平面ClipPlane
- GPU深度发掘(四)::Render to Vertexbuffer in OpenGL
- OpenGL中的Alpha测试,深度测试,模板测试,裁减测试
- OpenGL: 深度冲突(Z-Finghting or Depth Fighting)的解决
- 什么是OpenGL中的深度、深度缓存、深度测试?
- OpenGL-----深度测试,剪裁测试、Alpha测试和模板测试
- openGL 深度测试(转)
- OpenGL(二十一) glPolygonOffset设置深度偏移解决z-fighting闪烁问题