glTranslate/rotate 在我的代码中不起作用

发布于 2024-11-05 03:48:12 字数 1426 浏览 2 评论 0原文

我想绘制一个立方体并翻译它,但模型视图矩阵上的转换似乎不起作用。它们的效果为0。

gl.glClearColor(0, 0, 0, 0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(gl.GL_DEPTH_BUFFER_BIT);
gl.glFrustum(-10, 10, -10, 10, 0.01, 20);



gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslated(0.8, 0, 0); //no effect
    gl.glRotated(30, 1, 0, 0); //no effect
    drawCube(gl);

public void drawCube(GL gl) {

    gl.glMatrixMode(GL.GL_MODELVIEW);

    //spate
    gl.glLoadIdentity();
    drawSquare(gl);
    //fata
    gl.glLoadIdentity();
    gl.glTranslated(0, 0, WIDTH);
    drawSquare(gl);

    //st
    gl.glLoadIdentity();
    gl.glRotated(-90, 0, 1, 0);
    drawSquare(gl);

    //dr
    gl.glLoadIdentity();
    gl.glRotated(-90, 0, 1, 0);
    gl.glTranslated(0, 0, -WIDTH);
    drawSquare(gl);

    //sus
    gl.glLoadIdentity();
    gl.glRotated(90, 1, 0, 0);
    drawSquare(gl);
    //jos
    gl.glLoadIdentity();
    gl.glRotated(90, 1, 0, 0);
    gl.glTranslated(0, 0, -WIDTH);
    drawSquare(gl);

}

public void drawSquare(GL gl) {
    gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
    gl.glBegin(GL.GL_POLYGON);
        gl.glTexCoord2d(0, 0);
        gl.glVertex3d(0, 0,0);

        gl.glTexCoord2d(0, 1);
        gl.glVertex3d(0, WIDTH,0);

        gl.glTexCoord2d(1, 1);
        gl.glVertex3d(WIDTH, WIDTH,0);

        gl.glTexCoord2d(1, 0);
        gl.glVertex3d(WIDTH, 0,0);
    gl.glEnd();
}

I want to draw a cube and translate it, but the transformations on the Model View matrix don't seem to work. They have 0 effect.

gl.glClearColor(0, 0, 0, 0);
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClear(gl.GL_DEPTH_BUFFER_BIT);
gl.glFrustum(-10, 10, -10, 10, 0.01, 20);



gl.glMatrixMode(GL.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glTranslated(0.8, 0, 0); //no effect
    gl.glRotated(30, 1, 0, 0); //no effect
    drawCube(gl);

public void drawCube(GL gl) {

    gl.glMatrixMode(GL.GL_MODELVIEW);

    //spate
    gl.glLoadIdentity();
    drawSquare(gl);
    //fata
    gl.glLoadIdentity();
    gl.glTranslated(0, 0, WIDTH);
    drawSquare(gl);

    //st
    gl.glLoadIdentity();
    gl.glRotated(-90, 0, 1, 0);
    drawSquare(gl);

    //dr
    gl.glLoadIdentity();
    gl.glRotated(-90, 0, 1, 0);
    gl.glTranslated(0, 0, -WIDTH);
    drawSquare(gl);

    //sus
    gl.glLoadIdentity();
    gl.glRotated(90, 1, 0, 0);
    drawSquare(gl);
    //jos
    gl.glLoadIdentity();
    gl.glRotated(90, 1, 0, 0);
    gl.glTranslated(0, 0, -WIDTH);
    drawSquare(gl);

}

public void drawSquare(GL gl) {
    gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);
    gl.glBegin(GL.GL_POLYGON);
        gl.glTexCoord2d(0, 0);
        gl.glVertex3d(0, 0,0);

        gl.glTexCoord2d(0, 1);
        gl.glVertex3d(0, WIDTH,0);

        gl.glTexCoord2d(1, 1);
        gl.glVertex3d(WIDTH, WIDTH,0);

        gl.glTexCoord2d(1, 0);
        gl.glVertex3d(WIDTH, 0,0);
    gl.glEnd();
}

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

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

发布评论

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

评论(1

紫罗兰の梦幻 2024-11-12 03:48:12

drawCube()内部,您正在重置模型视图矩阵:

gl.glMatrixMode(GL.GL_MODELVIEW);

//spate
gl.glLoadIdentity();

我怀疑您想在此处使用glPushMatrix()glPopMatrix()

附带说明:自 OpenGL 3.1 起,这些操作已被弃用。

Inside drawCube() you're resetting the modelview matrix:

gl.glMatrixMode(GL.GL_MODELVIEW);

//spate
gl.glLoadIdentity();

I suspect you want to use glPushMatrix() and glPopMatrix() here.

As a side note: those operations are deprecated since OpenGL 3.1.

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