java - OpenGL JOGL 相机透视

标签 java opengl graphics jogl

我画了三个盒子,每个盒子大小相同,但距相机的距离不同。当这些盒子远离相机时,它们应该被视为尺寸减小。我如何实现这种距离错觉。

//这些是盒子的三个平面

  // 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/

相关文章:

c - 我的 OGL 程序找不到 gl.h 和 glu.h 了

opengl - 如何持续更新 glut 窗口?

c++ - 在 Android 上进行实时图形编程的最佳语言是什么?

java - 每次我单击 JEditorPane 中的超链接时,hyperlinkUpdate() 都会给我 NullPointerException?

Java邮件在公司网络中无法使用

java - 构建一个监听数据库变化并调用其他方法的软件。我正在做正确的建筑吗?

java - Web 服务方法结果的类是什么?

c++ - 指向 glutDisplayFunc 函数的指针

r - 修改 GGplot2 对象

windows - 如何在 Windows 中获得 common-lisp GUI?