C 以椭圆路径旋转地球:OpenGL

C 以椭圆路径旋转地球:OpenGL,c,opengl,glut,C,Opengl,Glut,我需要在OpenGL中使地球围绕太阳旋转。我现在已经能够绕太阳旋转了,但是我需要在椭圆轨道上旋转它。现在椭圆已经产生了,但我不知道如何沿着椭圆旋转 绘制功能类似于: //orbit glColor3f(1,1,1); drawEllipse(3.2f,1.0f); //draws ellipse //sun glTranslatef(0,0,0);//suns position glColor3d(1,1,0); glutSolidSp

我需要在OpenGL中使地球围绕太阳旋转。我现在已经能够绕太阳旋转了,但是我需要在椭圆轨道上旋转它。现在椭圆已经产生了,但我不知道如何沿着椭圆旋转

绘制功能类似于:

    //orbit
    glColor3f(1,1,1); 
    drawEllipse(3.2f,1.0f); //draws ellipse 
    //sun
    glTranslatef(0,0,0);//suns position
    glColor3d(1,1,0);
    glutSolidSphere(2,50,50);

    //EARTH
    glRotatef(angle,0.0f,1.0f,0.0f);
    glTranslatef(6.0f,0.0f,0.0f);//postion earths


    glPushMatrix();
    glRotatef(angle2, 0.0f,1.0f,0.0f);
    glColor3d(0.5,0.8,1);

    glutSolidSphere(1,50,50);
    glPopMatrix();
这样您可以在椭圆上的每个点绘制球体。

定义两个跟踪(x,y)坐标的变量,用于沿椭圆平移地球

float xForEarth, yForEarth;
以及计数度数的计数器(从0到360,然后再到0)

并使用以下代码(在绘图方法中)制作椭圆并在其上绘制地球:

glBegin(GL_POINTS);
    for (int i=0; i < 360; i++) 
    { 
        float degInRad = i*DEG2RAD; //const float DEG2RAD = 3.14159/180.0;
        glVertex3f(cos(degInRad)*6.0, 
            sin(degInRad)*2.3,0.0f);
    }
    glEnd();
    if(counterForEarth>359)
        counterForEarth = 0;//reset to 0 when becomes 360
    else
        counterForEarth++;//this will control the speed. Do counterForEarth += 2 to increase it's speed and vice-versa

    xForEarth = cos(counterForEarth*DEG2RAD)*6.0f;//to change the x co-ordinate
    yForEarth = sin(counterForEarth*DEG2RAD)*2.3f;//to change the y co-ordinate
    glPushMatrix();
    glTranslatef(xForEarth,yForEarth,0.0f);
    glRotatef(angle,0.0f,1.0f,0.0f);
    glutSolidSphere(.4,30,30);//draw Earth
    glPopMatrix();
glBegin(GL_点);
对于(int i=0;i<360;i++)
{ 
float degInRad=i*DEG2RAD;//常数float DEG2RAD=3.14159/180.0;
glVertex3f(cos(degInRad)*6.0,
sin(degInRad)*2.3,0.0f;
}
格伦德();
如果(反地球>359)
反地球=0//变为360时重置为0
其他的
反地球++//这将控制速度。做反地球+=2来增加它的速度,反之亦然
xForEarth=cos(对地*DEG2RAD)*6.0f//更改x坐标的步骤
yForEarth=sin(对地*DEG2RAD)*2.3f//更改y坐标的步骤
glPushMatrix();
glTranslatef(xForEarth,yForEarth,0.0f);
glRotatef(角度,0.0f,1.0f,0.0f);
固体球(.4,30,30)//拔土
glPopMatrix();

您将需要参数化以获得地球在每个时间步的目标位置。秒,您不能简单地使用旋转+平移来获得椭圆上的正确位置。试着看看这个:
int counterForEarth = 0;
glBegin(GL_POINTS);
    for (int i=0; i < 360; i++) 
    { 
        float degInRad = i*DEG2RAD; //const float DEG2RAD = 3.14159/180.0;
        glVertex3f(cos(degInRad)*6.0, 
            sin(degInRad)*2.3,0.0f);
    }
    glEnd();
    if(counterForEarth>359)
        counterForEarth = 0;//reset to 0 when becomes 360
    else
        counterForEarth++;//this will control the speed. Do counterForEarth += 2 to increase it's speed and vice-versa

    xForEarth = cos(counterForEarth*DEG2RAD)*6.0f;//to change the x co-ordinate
    yForEarth = sin(counterForEarth*DEG2RAD)*2.3f;//to change the y co-ordinate
    glPushMatrix();
    glTranslatef(xForEarth,yForEarth,0.0f);
    glRotatef(angle,0.0f,1.0f,0.0f);
    glutSolidSphere(.4,30,30);//draw Earth
    glPopMatrix();