开发者

Using glTranslate and glRotate to move and rotate?

I am writing an application that draws a 2D triangle and rotates it around its z-axis depending on its position. Its middle (t1.tx, t1.ty) is constantly being changed when the triangle is dragged with the mouse. The problem is that when I drag the triangle to another location, instead of staying where its at and rotating, it rotates in a circle path around its center point.

What I am doing wrong? I want it to rotate in its position.

void drawTriangle() {
    glBegin(GL_POLYGON);

    glColor3f((float)200/255, (float)200/255, (float)200/255);
    glVertex2f(t1.tx, t1.ty + .2); // top point of triangle
    glVertex2f(t1.tx - .2, t1.ty - .2); // left point
    glVertex2f(t1.tx + .2, t1.ty - .2); // right point

    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_B开发者_开发技巧IT);
    glLoadIdentity();

    glPushMatrix();
        glTranslatef(t1.tx, t1.ty, 0); // move matrix to triangle's current center point
        glRotatef(theta, 0, 0, 1.0); // rotate on z-axis
        drawTriangle();
    glPopMatrix();

    glutPostRedisplay();
    glutSwapBuffers();
}


It has been a WHILE since I did this stuff, but based on what you are saying, my first guess would be to swap the order of the glTranslatef and glRotatef. You want to rotate in model space then transform to world space.


I fixed it. The problem was that I was translating the matrix, and then drawing onto that matrix that was translated. What I did was draw my triangle at the center of the matrix, so it still moves and rotates at its current position as the matrix is translated. Thank you for the help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜