我画了三个盒子,每个盒子大小相同,但距相机的距离不同。当这些盒子远离相机时,它们应该被视为尺寸减小。我如何实现这种距离错觉。
//这些是盒子的三个平面
// first plane
gl.glVertex3i(0, 30, 30);
gl.glVertex3i(10, 30, 30);
gl.glVertex3i(10, 20, 30);
gl.glVertex3i(0, 20, 30);
//2nd Plane
gl.glVertex3i(20, 20, 37);
gl.glVertex3i(30, 20, 37);
gl.glVertex3i(30, 10, 37);
gl.glVertex3i(20, 10, 37);
//3rd Plane
gl.glVertex3i(40, 10, 45);
gl.glVertex3i(50, 10, 45);
gl.glVertex3i(50, 0, 45);
gl.glVertex3i(40, 0, 45);
//这是眼睛向上的代码。
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(
35, 15, 10,
25, 15, 30,
0, 1, 0
);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(-50.0, 50.0, -30.0, 30.0, 0.0, 60.0);
最佳答案
您需要使用透视投影,而不是正交投影。
而不是调用
gl.glOrtho(-50.0, 50.0, -30.0, 30.0, 0.0, 60.0);
您应该能够将该行替换为
GLU glu = new GLU();
glu.gluPerspective(60.0, 4.0/3.0, 1.0, 100.0);
我提供的参数可能不适合您的程序,因此您可能需要调整它们。
参数按顺序为:fovy、aspect、zNear 和 zFar。
来自manpage :
fovy: Specifies the field of view angle, in degrees, in the y direction.
aspect: Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
zNear: Specifies the distance from the viewer to the near clipping plane (always positive).
zFar: Specifies the distance from the viewer to the far clipping plane (always positive).
GLU 类位于此处
import javax.media.opengl.glu.GLU
关于java - OpenGL JOGL 相机透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23223818/