Java OpenGL绑定中的GLU.gluLook似乎什么都不做

Joe*_*oel 4 java opengl jogl glu

我已经检查了关于这个主题的其他问题,他们的解决方案对我没用.我有点失落.我的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();
}
Run Code Online (Sandbox Code Playgroud)

将我放入gluLookAt()矩阵的值无关紧要,视图不会改变.我仍然最终看着立方体的同一面.

有任何想法吗?

谢谢

Bob*_*oss 6

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

我认为你的问题出在你的立方体绘图代码中.请查看下面的评论: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(); 
Run Code Online (Sandbox Code Playgroud)

这是关于相关电话会做什么的非常快速的解释.有关更多信息,请参阅文档:

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.
Run Code Online (Sandbox Code Playgroud)

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

原文:您需要添加多维数据集绘图代码 - 如果您将多维数据集放在(0,30,0)附近,则代码很可能正在按照您的要求进行操作.

检查OpenGL FAQ,有一个特定的问题和答案可能与您正在做的事情有关:8.080为什么gluLookAt不工作? 我将引用整个答案,因为确实没有一个好的休息,但请访问OpenGL FAQ,答案很可能是:

这通常是由不正确的转换引起的.

假设您在Projection矩阵堆栈上使用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);
Run Code Online (Sandbox Code Playgroud)

重要的是要注意Projection和ModelView如何一起转换.

在此示例中,Projection变换设置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个单位并且远远落后于放置在7.0单位的zFar剪切平面.

如果这让您感到困惑,请阅读OpenGL红皮书或OpenGL规范中的转换.在了解了对象坐标空间,眼睛坐标空间和剪辑坐标空间之后,上面的内容应该变得清晰.此外,尝试小型测试程序.如果您在主应用程序项目中遇到正确的转换时遇到问题,那么编写一小段代码可能会很有教育意义,这些代码会尝试使用更简单的几何体来重现问题.