Rotating an object around a fixed point in opengl

后端 未结 4 357
我在风中等你
我在风中等你 2020-12-16 01:19

I have a problem with this openGL code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // put current matrix on stack

//glTranslatef(0.0f, 0.         


        
相关标签:
4条回答
  • 2020-12-16 01:58

    Simply do the rotation after the translation. The order matters.

    glTranslatef(xpos, ypos, zpos);
    glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);
    
    0 讨论(0)
  • 2020-12-16 01:59

    try rotating after translation:

        glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix(); // put current matrix on stack
    
    //glTranslatef(0.0f, 0.0f, 0.0f);   
    //glTranslatef(-4*1.5, 0.0, 4*1.5);
    
    glTranslatef(xpos, ypos, zpos);
    glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot on its y-axis
    DrawRobot(xpos, ypos, zpos); // draw the robot
    glPopMatrix();
    
    0 讨论(0)
  • 2020-12-16 02:03

    Example of rotating an object around its centre along the z-axis:

    glPushMatrix();
    
    glTranslatef(250,250,0.0); // 3. Translate to the object's position.
    
    glRotatef(angle,0.0,0.0,1.0); // 2. Rotate the object.
    
    glTranslatef(-250,-250,0.0); // 1. Translate to the origin.
    
    // Draw the object
    glPopMatrix();
    
    0 讨论(0)
  • 2020-12-16 02:06

    Use this

    house();
    
    glTranslatef(x, y, 0.0); // 3. Translate back to original
    glRotatef(theta, 0.0, 0.0, 1.0); // 2. Rotate the object around angle
    glTranslatef(-m, -n, 0.0); // 1. Move to origin
    
    house();
    

    where m and n are the point on the object around which you want to rotate and x and y are the points around which you want to rotate.

    0 讨论(0)
提交回复
热议问题