【问题标题】:opengl rotating an object which is already in rotation something like earth moon rotationopengl旋转一个已经在旋转的物体,比如地球月球旋转
【发布时间】:2014-07-18 10:50:23
【问题描述】:
#include <glut.h>

float _angle = 0.0;

void Draw() {
    glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glPointSize(8);
glPushMatrix();


        glTranslatef(0.3,0.3,0.0);
        glRotatef(_angle, 0.4, 0.1, 0.5);
        glBegin(GL_POINTS);
        glVertex3f(0.3, 0.0, 0.0);


    glEnd();
glPopMatrix();  


glFlush();


}

void update(int value)
{
_angle += 3;
if (_angle > 360)
{
_angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(5,update,0);
}

void Initialize() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}

int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(200, 200);
glutCreateWindow("CSE");
Initialize();
glutDisplayFunc(Draw);
glutTimerFunc(25,update,0);
glutMainLoop();
return 0;
}

我正在尝试模拟太阳地球和月球自转。现在我已经围绕上面代码中的特定点旋转了一个对象,并假设它的月球绕地球运行,现在我如何围绕原点旋转整个物体,使其看起来围绕太阳旋转。

【问题讨论】:

    标签: c++ opengl rotation


    【解决方案1】:

    您可以通过使用 glPushMatrix()/glPopMatrix() 对来做到这一点。您只需要记住,平移和旋转会使点围绕其原点旋转。下面展示了如何实现这种效果。我只是稍微修改了你的程序。

    #include <GL/glut.h>
    
    float _angleMoon = 0.0;
    float angleEarth = 0.0;
    
    void Draw() {
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0, 1.0, 1.0);
        glPointSize(8);
        glPushMatrix();
    
        glBegin(GL_POINTS);
            glVertex3f(0.f, 0.f, 0.f);
        glEnd();
    
        glRotatef(angleEarth, 0.f, 0.f, 1.0);
        glPushMatrix();
            glTranslatef(0.3,0.3,0.0);
            glBegin(GL_POINTS);
                glVertex3f(0.f, 0.f, 0.f);
            glEnd();
    
            glPushMatrix();
            glRotatef(_angleMoon, 0., 0., 1.);
            glTranslatef(0.3, 0.f, 0.f);
                glBegin(GL_POINTS);
                    glVertex3f(0.0, 0.0, 0.0);
                glEnd();
            glPopMatrix();
        glPopMatrix();  
    
        glPopMatrix();
        glFlush();
    }
    
    void update(int value)
    {
        _angleMoon += 3;
        if (_angleMoon > 360)
        {
            _angleMoon -= 360
        }
    
        angleEarth += 1;
        if (angleEarth > 360)
        {
            angleEarth -= 360;
        }
    
        glutPostRedisplay();
        glutTimerFunc(5,update,0);
    }
    
    void Initialize() {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    }
    
    int main(int iArgc, char** cppArgv) {
        glutInit(&iArgc, cppArgv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(250, 250);
        glutInitWindowPosition(200, 200);
        glutCreateWindow("CSE");
        Initialize();
        glutDisplayFunc(Draw);
        glutTimerFunc(25,update,0);
        glutMainLoop();
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      你的作图顺序应该是这样的,你上面贴的好像不正确

      //正确的绘制顺序应该是

      glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0); //Rotate y axis
      
      glTranslatef(2.0, 0.0, 0.0); //Move to new position
      
      gluSphere("required arguments");
      

      比如说,在您的场景中,您想要围绕太阳旋转行星 你需要做这样的事情......

      //你需要把每一个全局转换都放在这里..

      **//Then Draw the sun**
      
      glPushMatrix();
      
      glColor3f(1.0, 1.0, 0.0);
      
      glRotatef(360.0 * DayOfYear / 365.0, 0.0, 1.0, 0.0); //This will self-rotate the sun
      
      gluSphere(quad[0], 1.f, 64, 64);
      
      glPopMatrix();
      
      **//Drawing the Planet X**
      
      glPushMatrix();
      
      glColor3f(0.5, 0.5, 0.5);
      
      glRotatef(360.0*DayOfYear/365.0, 0.0, 1.0, 0.0);
      
      glTranslatef(2.0, 0.0, 0.0); //eerste parameter afstand tot zon
      
      gluSphere(quad[1], 0.2f, 64, 64);
      
      glPopMatrix();
      
      
      **//Drawing the Earth**
      
      glPushMatrix();
      
      glColor3f(0.2, 0.2, 1.0);
      
      glRotatef(360.0*3*DayOfYear/365.0, 0.0, 1.0, 0.0);
      
      glTranslatef(3.0, 0.0, 0.0); //eerste paramater afstand tot zon
      
      gluSphere(quad[2], 0.6f, 64, 64);
      
      glPopMatrix();
      

      另外你可能想在你的主渲染函数中使用 glutSwapBuffers(),因为我没有看到..

      【讨论】:

        猜你喜欢
        • 2019-05-03
        • 2021-05-16
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2014-01-27
        • 2015-04-26
        • 2015-01-28
        • 1970-01-01
        相关资源
        最近更新 更多