在椭圆路径上旋转地球:OpenGL

4
我需要在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();

3
每个时间步需要参数化获取地球的目标位置。 - ratchet freak
同意,你不能简单地使用旋转+平移来获得椭圆上的正确位置。尝试查看这个链接:http://en.wikipedia.org/wiki/Ellipse#General_parametric_form - vesan
1个回答

2
OpenGL does not store what you draw. If you draw a line in OpenGL, then OpenGL 
will take that line, perform various math operations on it, and write pixels
 into a framebuffer that makes the shape of a line. OpenGL does not remember
 that you drew a line; all OpenGL can do is write pixels to the framebuffer.

因此,您可以在椭圆上的每个点绘制球体。

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

float xForEarth, yForEarth;

还有一个计数器,用于计算角度(从0到360再回到0)

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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接