在 JOGL 中翻译后无法按预期使用 glLoadIdentity

发布于 2024-12-07 05:08:56 字数 2767 浏览 1 评论 0 原文

我刚刚开始使用 JOGL,有一些我无法做到的事情: NeHe 教程,一切都正常,除了在第一个三角形之后和下一个四边形之前使用的 glLoadIdentity(); 。接下来是 JOGL 代码:

float rtri  = 0.0f;
float rquad = 0.0f;

@Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glTranslatef(-1.5f,0.0f,-6.0f);
    gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);

    gl.glBegin(GL2.GL_TRIANGLES);
    {
        gl.glColor3f(1.0f,0.0f,0.0f);
        gl.glVertex3f( 0.0f, 1.0f, 0.0f);

        gl.glColor3f(0.0f,1.0f,0.0f);
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);

        gl.glColor3f(0.0f,0.0f,1.0f);
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);
    }
    gl.glEnd();

    // Here seems to be the problem
    gl.glLoadIdentity();

    gl.glTranslatef(3.0f, 0.0f, 0.0f);
    gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);

    gl.glBegin(GL2.GL_QUADS);
    {
        gl.glColor3f(0.5f, 0.5f, 1.0f);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glVertex3f( 1.0f, 1.0f, 0.0f);
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);
    }
    gl.glEnd();

    gl.glFlush();

    rtri    += 0.15f;
    rquad   -= 0.5f;
}

如您所见,在定义 Quad 之前有一个 gl.glLoadIdentity();。如果注释掉这一行,最终的程序将渲染第一个三角形旋转,并且四边形围绕三角形平移,但四边形也有自己的旋转。

如果该行没有注释,则根本不会渲染四边形,而只会渲染三角形及其定义的颜色和旋转。

到目前为止,我看到的所有示例似乎都使用 gl.glLoadIdentity(); 来恢复当前矩阵模式 (GL_MODELVIEW) 的默认状态,但在这种情况下,似乎发生了其他情况。

我在每次绘图操作之前使用 gl.glPushMatrix();gl.glPopMatrix(); 进行了测试,围绕所有绘图代码,无论有没有 gl.glLoadIdentity(); 在中间,但似乎没有任何帮助。

我检查了其他示例的 init 和 reshape 方法,但一切似乎都很好:

@Override
public void init(GLAutoDrawable drawable) {
    final GL2 gl = drawable.getGL().getGL2();
    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glClearDepth(1.0f);
    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    final GL2 gl = drawable.getGL().getGL2();
    if (height <= 0) {
        height = 1;
    }
    float h = (float) width / (float) height;
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(50.0f, h, 1.0, 1000.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}

我想继续学习 OpenGL,同时使用 Java(我喜欢 Java,很久以前我不使用 C++),但我应该了解 OpenGL 的基础知识,我不想继续错过这样一个“基本”的东西。谢谢大家的帮助,如果我的英语有错误,抱歉。

I'm just starting with JOGL, and there's something I haven't been able to do: There's an example of basic drawing and rotation in NeHe tutorials, and everything works just fine, except the glLoadIdentity(); used after the first triangle and before the next quad. The JOGL code is the next:

float rtri  = 0.0f;
float rquad = 0.0f;

@Override
public void display(GLAutoDrawable drawable) {

    final GL2 gl = drawable.getGL().getGL2();

    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();

    gl.glTranslatef(-1.5f,0.0f,-6.0f);
    gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);

    gl.glBegin(GL2.GL_TRIANGLES);
    {
        gl.glColor3f(1.0f,0.0f,0.0f);
        gl.glVertex3f( 0.0f, 1.0f, 0.0f);

        gl.glColor3f(0.0f,1.0f,0.0f);
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);

        gl.glColor3f(0.0f,0.0f,1.0f);
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);
    }
    gl.glEnd();

    // Here seems to be the problem
    gl.glLoadIdentity();

    gl.glTranslatef(3.0f, 0.0f, 0.0f);
    gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);

    gl.glBegin(GL2.GL_QUADS);
    {
        gl.glColor3f(0.5f, 0.5f, 1.0f);
        gl.glVertex3f(-1.0f, 1.0f, 0.0f);
        gl.glVertex3f( 1.0f, 1.0f, 0.0f);
        gl.glVertex3f( 1.0f,-1.0f, 0.0f);
        gl.glVertex3f(-1.0f,-1.0f, 0.0f);
    }
    gl.glEnd();

    gl.glFlush();

    rtri    += 0.15f;
    rquad   -= 0.5f;
}

As you can see, there is a gl.glLoadIdentity(); before the Quad is defined. If this line is commented, the final program renders the first triangle rotating, and the quad translating around the triangle, but the quad has it's own rotation too.

If that line is NOT commented, the quad is not rendered at all, and only the triangle is rendered, with it's defined colors and rotation.

All examples I've seen so far seem to use the gl.glLoadIdentity(); to restore the default state for current matrix mode (GL_MODELVIEW), but in this case, something else seems to be happening.

I've tested with gl.glPushMatrix(); and gl.glPopMatrix(); before each drawing operation, surrounding all the drawing code, with and without the gl.glLoadIdentity(); in the middle, but nothing seems to help.

I've checked other examples' init and reshape method, but everything seems just fine:

@Override
public void init(GLAutoDrawable drawable) {
    final GL2 gl = drawable.getGL().getGL2();
    gl.glShadeModel(GL2.GL_SMOOTH);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    gl.glClearDepth(1.0f);
    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glDepthFunc(GL2.GL_LEQUAL);
    gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    final GL2 gl = drawable.getGL().getGL2();
    if (height <= 0) {
        height = 1;
    }
    float h = (float) width / (float) height;
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(50.0f, h, 1.0, 1000.0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}

I'd like to keep learning OpenGL, while using Java (I'd love Java, and a long time ago I don't use C++), but I'm supposed to be in the very basics of OpenGL, and I wouldn't like to go on missing such a "basic" thing. Thank you all for your help, and sorry if my english has errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-14 05:08:56

您的问题不是 glLoadIdentity。您的问题是这样的:

gl.glTranslatef(3.0f, 0.0f, 0.0f);
gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);

您在 X 方向上平移四边形 3 个单位。好的,但它的 Z 值仍然为零。您的相机的 Z 值也为零,因为您是在相机空间中操作。由于您的近 Z 值为 1,因此您将剪掉相机的大部分内容。

另外,事实上,考虑到您的 FOV 为 50 度,并且您将 4 个 3 单元放置在 X 方向,它可能位于相机的右侧。所以它根本就看不见。

如果您想看到物体,则需要将其移离相机。

Your problem isn't glLoadIdentity. Your problem is this:

gl.glTranslatef(3.0f, 0.0f, 0.0f);
gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);

You translate the quad 3 units in the X direction. OK, but it still has a Z of zero. Your camera also has a Z of zero, since you're operating in camera space. And since your near-Z is 1, you're going to clip out a lot of the camera.

Plus, there's the fact that, given your FOV of 50 degrees, and the fact that you put the quad 3 units in the X direction, it is probably off to the right of the camera. So it's simply out of view.

You need to move your object away from the camera if you want to see it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文