【问题标题】:GLU.gluLookAt in Java OpenGL bindings seems to do nothingJava OpenGL 绑定中的 GLU.gluLookAt 似乎什么都不做
【发布时间】:2011-07-03 16:23:09
【问题描述】:

我已经检查了有关此主题的其他问题,但他们的解决方案对我不起作用。我有点不知所措。我的 GLEventListener 实现中有以下函数。

public void init(GLAutoDrawable gl) {
    GL2 gl2 = gl.getGL().getGL2();

    gl2.glMatrixMode(GL2.GL_PROJECTION);
    gl2.glLoadIdentity();
    GLU glu = GLU.createGLU(gl2);
    glu.gluPerspective(45.0f, 1, 0.1f,100.0f);
    gl2.glMatrixMode(GL2.GL_MODELVIEW);
    gl2.glLoadIdentity();
    gl2.glViewport(0, 0, width, height);
    gl2.glEnable(GL.GL_DEPTH_TEST);
}

private void render(GLAutoDrawable drawable) {

    GL2 gl = drawable.getGL().getGL2();
    GLU glu = GLU.createGLU(gl);

    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
    glu.gluLookAt(5,  0, 20, 
                  0, 30,  0, 
                  0,  1,  0);

    gl2.glPushMatrix();
    gl2.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );                
    gl2.glLoadIdentity();
    gl2.glTranslatef(x, y, z);        
    gl2.glBegin( GL2.GL_QUADS );
        gl2.glColor3f( 1, 0, 0 );

        //24 glVertex3f calls & some colour changes go here.
        gl2.glVertex3f(...)

    gl2.glEnd();
    gl2.glPopMatrix();

    gl.glFlush();
}

无论我将什么值放入 gluLookAt() 矩阵,视图都不会改变。我最终还是看到了立方体的同一张脸。

有什么想法吗?

谢谢

【问题讨论】:

  • 您是否使用着色器来绘制立方体?
  • 我是新手,不知道这意味着什么。立方体只有 24 个 glVertex3f() 调用和 glBegin(quads) 和 glEnd() 之间的一些颜色变化。我将编辑我的帖子以显示更多我的绘图代码。
  • 查看下面的答案 - 我认为您的绘图代码中有错误。

标签: java opengl jogl glu


【解决方案1】:

编辑:回应原始问题中的编辑。将原文留在下面,因为人们似乎觉得它很有用。

我认为您的问题出在您的立方体绘图代码中。检查下面的评论:glLoadIdentity 调用完全符合您的预期 - 强制立方体出现在您面前:

gl2.glPushMatrix();     
gl2.glClear( GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
/** Try removing the following glLoadIdentity call below.
  * That call was blowing out the MODELVIEW matrix - it's removing your 
  * gluLookAt call and returning to the identity.
  * As a result, the cube will always be right there in front of you.
  */
// gl2.glLoadIdentity();
gl2.glTranslatef(x, y, z);
gl2.glBegin( GL2.GL_QUADS );
gl2.glColor3f( 1, 0, 0 ); //24 glVertex3f calls & some colour changes go here.
gl2.glVertex3f(...)
gl2.glEnd();
gl2.glPopMatrix(); 

这里有一个关于相关调用将做什么的非常快速的解释。有关详细信息,请参阅文档:

gl2.glPushMatrix(); // This preserves current MODEL_VIEW matrix so you can get back here.
                    // Think of it as a checkpoint save in a game.
                    // Most of your objects will be wrapped in push and pop.
gl2.glLoadIdentity(); // This erases the MODEL_VIEW and replaces it with an identity.
                      // This un-does your previous gluLookAt call.  You will rarely use
                      // this inside an object (but it's not impossible).
                      // Does not apply here so don't use.
gl2.glTranslatef(x, y, z); // This is what puts your object out in space for you to find
                           // as opposed to putting it at the origin.  Most objects will
                           // have a translate (and likely a rotate as well).
                           // Note that the order of operations matters:
                           // translate and then rotate != rotate and then translate.
// QUAD strip code with vertices and colors - you're okay with these.
gl2.glPopMatrix(); // This brings back the MODEL_VIEW that you originally saved by pushing
                   // it.

OpenGL 中的矩阵代码的好处在于,一旦您获得了一组您理解的示例代码,您将始终将其作为参考。当我从 IrisGL 切换到 OpenGL 时,我花了一点时间来移植我的实用程序,然后我再也没有回头。

原文:您需要添加立方体绘制代码 - 如果您将立方体放在 (0, 30, 0) 附近,则代码很可能正在执行您所要求的操作它到。

查看 OpenGL 常见问题解答,有一个特定的问题和答案可能与您正在做的事情相关:8.080 Why doesn't gluLookAt work? 我将引用整个答案,因为确实没有很好的休息,但请访问OpenGL FAQ,答案很可能就在那里:

这通常是由于不正确造成的 转换。

假设您正在使用 投影上的 gluPerspective() zNear 和 zFar 的矩阵堆栈为 第三个和第四个参数,你 需要在 ModelView 上设置 gluLookAt 矩阵堆栈,并传递参数所以 您的几何图形介于 zNear 和 zFar。

通常最好尝试一下 简单的一段代码 试图理解观看 转变。假设你是 试图看一个单位球体 以原点为中心。你会想要 将您的转换设置为 如下:

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity(); 
 gluPerspective(50.0, 1.0, 3.0, 7.0); 
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity(); 
 gluLookAt(0.0, 0.0, 5.0,
           0.0, 0.0, 0.0,
           0.0, 1.0, 0.0);

重要的是要注意投影如何 和 ModelView 转换工作 在一起。

在本例中,投影 transform 设置一个 50.0 度的场 视图,纵横比为 1.0。 zNear 剪裁平面为 3.0 个单位 在眼前,zFar 剪裁平面在前面是 7.0 个单位 的眼睛。这留下了一个 Z 体积 4.0单位的距离,充足的空间 一个单位球体。

ModelView 变换引人注目 位置在 (0.0, 0.0, 5.0),并且 观察点是原点 我们单位球体的中心。注意 眼睛位置距离为 5.0 个单位 从外观上看。这是 很重要,因为距离为 5.0 眼前的单位在 Z 体积的中间 投影变换定义。如果 gluLookAt() 调用已将目光放在 (0.0, 0.0, 1.0),它会产生一个 到原点的距离为 1.0。这 不够长,无法包含 视体积中的球体,并且它 会被 zNear 剪裁 飞机。

同样,如果你把眼睛放在 (0.0, 0.0, 10.0),距离为 10.0 看点会​​导致 单位球体距离 10.0 个单位 从眼睛远在zFar后面 剪裁平面放置在 7.0 个单位。

如果这让您感到困惑,请继续阅读 OpenGL 红皮书中的转换 或 OpenGL 规范。在你之后 了解对象坐标空间, 眼睛坐标空间和剪辑 坐标空间,上面应该 变得清晰。另外,尝试 小测试程序。如果你有 无法获得正确的转换 在您的主应用程序项目中,它 可以有教育意义的写一个小 一段试图重现的代码 简单几何的问题。

【讨论】:

  • 我实际上已经阅读了常见问题解答的那一部分,但没有找到任何帮助。我在这里输入 gluLookAt() 矩阵的数字是任意的。立方体以 (0, 0, -5.0) 为中心,边长为 1。当我的程序启动时,我总是直视正面,而不管 gluLookAt() 中的坐标如何。
  • 成功了!这将教会我不要遗漏我认为是样板代码的内容......不过是一个问题。我认为 loadIdentity() 仅适用于堆栈的顶部。既然我是在调用之前push了一个矩阵,那么出栈后应该没有效果吧?
  • @Joel,我会补充答案。等一下。
猜你喜欢
  • 2022-01-14
  • 2015-10-29
  • 2012-09-12
  • 2011-03-17
  • 2017-09-11
  • 2013-05-31
  • 2016-09-22
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多