gpt4 book ai didi

java - Java OpenGL 绑定(bind)中的 GLU.gluLookAt 似乎什么都不做

转载 作者:搜寻专家 更新时间:2023-11-01 02:17:04 25 4
gpt4 key购买 nike

我已经检查过有关此主题的其他问题,但他们的解决方案对我没有用。我有点不知所措。我的 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() 矩阵中输入什么值, View 都不会改变。我仍然最终看着立方体的同一张面。

有什么想法吗?

谢谢

最佳答案

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

我认为您的问题出在立方体绘制代码中。查看下面的评论: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 FAQ,有一个特定的问题和答案可能与您正在做的事情相关:8.080 Why doesn't gluLookAt work?我将引用整个答案,因为确实没有很好的休息时间,但请访问 OpenGL FAQ ,答案很可能在那里:

This is usually caused by incorrect transformations.

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.

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

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

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.

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.

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.

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 - Java OpenGL 绑定(bind)中的 GLU.gluLookAt 似乎什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5107016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com