opengl - OpenGL 中 3D 坐标系的透视问题

标签 opengl graphics projection perspective

当我尝试将坐标系的轴渲染到场景中时,我目前面临一些透视问题。对于这些轴,我绘制了穿过 3D 立方体中心的三条正交线。 很难解释问题是什么,所以我想最能说明问题的方式就是发布一些图片。

1)全场景 View :click here

2) 在坐标系原点上放大 View :click here

3)当我进一步放大一点时,其中两个轴消失了,另一个轴似乎由于某种原因发生了移位:click here

为什么会发生这种情况以及如何预防?

我的模型 View 和投影矩阵如下所示:

 // Set ProjectionMatrix
projectionMatrix = glm::perspective(90.0f, (GLfloat)width / (GLfloat) height, 0.0001f, 1000.f);
glBindBuffer(GL_UNIFORM_BUFFER, globalMatricesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), glm::value_ptr(projectionMatrix));
glBindBuffer(GL_UNIFORM_BUFFER, 0);

// Set ModelViewMatrix
glm::mat4 identity        = glm::mat4(1.0); // Start with the identity as the transformation matrix
glm::mat4 pointTranslateZ = glm::translate(identity, glm::vec3(0.0f, 0.0f, -translate_z)); // Zoom in or out by translating in z-direction based on user input 
glm::mat4 viewRotateX     = glm::rotate(pointTranslateZ, rotate_x, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the whole szene in x-direction based on user input
glm::mat4 viewRotateY     = glm::rotate(viewRotateX,  rotate_y, glm::vec3(0.0f, 1.0f, 0.0f)); // Rotate the whole szene in y-direction based on user input
glm::mat4 pointRotateX    = glm::rotate(viewRotateY, -90.0f, glm::vec3(1.0f, 0.0f, 0.0f)); // Rotate the camera by 90 degrees in negative x-direction to get a frontal look on the szene
glm::mat4 viewTranslate   = glm::translate(pointRotateX, glm::vec3(-dimensionX/2.0f, -dimensionY/2.0f, -dimensionZ/2.0f)); // Translate the origin to be the center of the cube

最佳答案

这就是所谓的“剪辑”。轴撞击近剪裁平面,因此被剪裁。第三轴没有“位移”;它只是被部分剪辑。拍摄第二张图像并遮盖大部分图像,这样您就只能看到对角轴的一部分;这就是你得到的。

对此有一些通用的解决方案。首先,您不能允许用户放大那么远。或者,当相机靠近目标对象时,您可以向内调整近剪裁平面。这也会导致远处物体的精度问题,因此您可能也需要向内调整远裁剪平面。

或者,您可以只打开深度钳位(假设您有 GL 3.x+,或访问 ARB_depth_clampNV_depth_clamp )。这不是一个完美的解决方案,因为当事物到达相机后面时,它们仍然会被剪掉。如果两个这样的对象重叠,则与近裁剪平面相交的对象将不再具有适当的深度缓冲。但总体来说已经足够好了。

关于opengl - OpenGL 中 3D 坐标系的透视问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507989/

相关文章:

c++ - 如何保存帧缓冲区然后将其取回

python - matplotlib:半球/楔形的自定义投影

c++ - 如何根据 Wavefront (.obj) 文件中给定的纹理索引对纹理位置进行排序?

java - 在java绘图程序中添加清除和撤消Jbutton

opengl - 如何在现代 OpenGL 中进行光线追踪?

c++ - 使用opengl进行真正的等距投影

sql - SQL 中投影的语法

java - lwjgl里面有GLfloat吗?

opengl - 如何在 OpenGL 中使用着色器对图像进行后处理?

opengl - 在没有glNormal的片段着色器中计算法线