Java OpenGL绑定中的GLU.gluLook似乎什么都不做 [英] GLU.gluLookAt in Java OpenGL bindings seems to do nothing

查看:146
本文介绍了Java OpenGL绑定中的GLU.gluLook似乎什么都不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了关于这个主题的其他问题,他们的解决方案对我没用。我有点失落。我的GLEventListener实现中有以下函数。

I've already checked the other questions on this topic and their solutions haven't worked for me. I'm at a bit of a loss. I have the following functions in my GLEventListener implementation.

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()矩阵的值无关紧要,视图不会改变。我仍然最终看着立方体的同一面。

It doesn't matter what values I put into the gluLookAt() matrix, the view doesn't change. I still end up looking at the same face of a cube.

任何想法?

谢谢

推荐答案

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

Responding to the edit in the original question. Leaving the original text below because people seem to find it to be useful.

我认为你的问题出在你的立方体绘图代码中。检查下面的评论: glLoadIdentity 调用正在做你期望的事情 - 迫使立方体出现在你面前:

I think your problem is in your cube drawing code. Check the commentary below: the glLoadIdentity call is doing exactly what you would expect - forcing the cube to be there in front of you:

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(); 

以下是关于相关电话会做什么的快速解释。有关详细信息,请参阅文档:

Here's a very quick explanation about what the related calls will do. See the documentation for more information:

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时,我花了一些时间来移植我的公用事业,然后我再也没有回头。

The great thing about the matrix code in OpenGL is that once you get a portfolio of example code that you understand, you'll always have it as a reference. When I switched from IrisGL to OpenGL back in the day, it took me a little while to port my utilities over and then I never looked back.

原文:你需要添加你的立方体绘图代码 - 如果你把立方体放在在(0,30,0)附近,代码很可能是你所要求的。

ORIGINAL: You need to add your cube drawing code - if you are putting the cube in the vicinity of (0, 30, 0), it's highly likely that the code is doing what you asked it to.

检查OpenGL FAQ,有一个特定的问题和答案这可能与您正在做的事情有关: 8.080为什么不能使用gluLook? 我将引用整个答案,因为确实没有好的休息,但请访问 OpenGL常见问题解答,答案可能就在那里:

Checking the OpenGL FAQ, there's a specific question and answer that is likely relevant to what you're doing: 8.080 Why doesn't gluLookAt work? I'm going to quote the whole answer as there really isn't a good break but please visit the OpenGL FAQ, the answer is likely there:


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

This is usually caused by incorrect transformations.

假设你在Projection
矩阵堆栈上使用
gluPerspective(),zNear和zFar为
第三和第四个参数,你需要
在ModelView $上设置gluLookAt b $ b矩阵堆栈,并传递参数,所以
几何介于zNear和
zFar之间。

Assuming you are using gluPerspective() on the Projection matrix stack with zNear and zFar as the third and fourth parameters, you need to set gluLookAt on the ModelView matrix stack, and pass parameters so your geometry falls between zNear and zFar.

通常最好试用
$ b尝试理解查看
转换的$ b简单代码。假设你是
试图查看以原点为中心的单位范围
。您需要
设置转换为
如下:

It's usually best to experiment with a simple piece of code when you're trying to understand viewing transformations. Let's say you are trying to look at a unit sphere centered on the origin. You'll want to set up your transformations as follows:



 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如何变换一起工作

It's important to note how the Projection and ModelView transforms work together.

在这个例子中,Projection
变换设置一个50.0度的字段
视图,带有一个方面比率为1.0。
zNear剪裁平面在眼前为3.0单位
,zFar
剪裁平面在前方
眼中为7.0单位。这留下了Z单位
距离为4.0单位,充足的空间为
a单位范围。

In this example, the Projection transform sets up a 50.0-degree field of view, with an aspect ratio of 1.0. The zNear clipping plane is 3.0 units in front of the eye, and the zFar clipping plane is 7.0 units in front of the eye. This leaves a Z volume distance of 4.0 units, ample room for a unit sphere.

ModelView变换设置眼睛
的位置at(0.0,0.0,5.0)和
的观察点是我们单位范围的
中心的原点。请注意,从观察点来看,
的眼睛位置是5.0单位
。这是
很重要,因为眼前的5.0
单位的距离是
投影变换定义的Z体积的
中间。如果
gluLookAt()调用已将注意力放在
(0.0,0.0,1.0)处,那么它将产生与原点相距1.0的
距离。这个
不够长,不能在视图卷中包含
球体,并且
将被zNear限幅
平面限制。

The ModelView transform sets the eye position at (0.0, 0.0, 5.0), and the look-at point is the origin in the center of our unit sphere. Note that the eye position is 5.0 units away from the look at point. This is important, because a distance of 5.0 units in front of the eye is in the middle of the Z volume that the Projection transform defines. If the gluLookAt() call had placed the eye at (0.0, 0.0, 1.0), it would produce a distance of 1.0 to the origin. This isn't long enough to include the sphere in the view volume, and it would be clipped by the zNear clipping plane.

同样,如果你把眼睛放在
(0.0,0.0,10.0)处,10.0
到看点的距离将导致
单位范围距离眼睛10.0个单位
,远远落后于7.0单位的zFar
剪裁平面。

Similarly, if you place the eye at (0.0, 0.0, 10.0), the distance of 10.0 to the look at point will result in the unit sphere being 10.0 units away from the eye and far behind the zFar clipping plane placed at 7.0 units.

如果这让你感到困惑,请阅读在OpenGL红皮书
或OpenGL规范中进行
转换。在
了解对象坐标空间,
眼睛坐标空间,以及剪辑
坐标空间后,上面应该
变得清晰。此外,尝试
小测试程序。如果你在主应用程序项目中获得正确的变换
时遇到
的麻烦,那么
可以教育你写一小段
的代码试图重现
更简单的几何问题。

If this has confused you, read up on transformations in the OpenGL red book or OpenGL Specification. After you understand object coordinate space, eye coordinate space, and clip coordinate space, the above should become clear. Also, experiment with small test programs. If you're having trouble getting the correct transforms in your main application project, it can be educational to write a small piece of code that tries to reproduce the problem with simpler geometry.

这篇关于Java OpenGL绑定中的GLU.gluLook似乎什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆