【问题标题】:Rotating a Quad in OpenGL not displaying correctly (rotatef)在 OpenGL 中旋转四边形无法正确显示 (rotatef)
【发布时间】:2014-04-01 13:16:40
【问题描述】:

我目前正在使用 LWJGL 创建一个简单的 2D 游戏。我想创建一些对象来让事情变得更容易一些。目前简单地绘制一个 Quad 对象可以正常工作,但后来我决定实现一种方法来绘制一个旋转的四边形。

这是目前绘制旋转四边形的代码:

@Override
public void draw(float angle) {
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // translate the quad to the origin before rotation
    glTranslatef(-(this.x + (this.w / 2)), -(this.y + (this.h / 2)), 0);

    glRotatef(angle, (this.x + (this.w / 2)), (this.y + (this.h / 2)), 0);

    // translate the quad back to its original position
    glTranslatef((this.x + (this.w / 2)), (this.y + (this.h / 2)), 0);

    glColor4f(this.color.getRed(), this.color.getAlpha(), this.color.getBlue(), this.color.getAlpha());
    glBegin(GL_QUADS);
        glVertex2f(this.x, this.y);
        glVertex2f(this.x + this.w, this.y);
        glVertex2f(this.x + this.w, this.y + this.h);
        glVertex2f(this.x, this.y + this.h);
    glEnd();
}

我这样做是为了画一个四边形 Quad q = 新的 Quad(10, 10, 100, 100); // 新四边形(x, y, w, h) q.draw(数学.PI); // 它是 180 度,但它不会像现在这样绘制四边形

这就是它的绘制方式

我不确定这里发生了什么。

附带问题

如果我简单地使用二维旋转矩阵计算新的x和y,然后简单地计算其他x和y,会不会有同样的效果?

【问题讨论】:

    标签: opengl rotation lwjgl


    【解决方案1】:

    glRotatef 的参数定义了轴。你想绕 z 轴旋转。因此:

    glRotatef(angle, 0, 0, 1);
    

    【讨论】:

    • 哦,我明白了,谢谢 Nico。我现在对此感到相当愚蠢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多