c - 以椭圆路径旋转地球 :OpenGL

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

最佳答案

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

关于c - 以椭圆路径旋转地球 :OpenGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27612838/

相关文章:

c - Makefile fatal error : can't create obj/calc. o

linux - 在 Linux 中执行 OpenGL 程序

c++ - 为什么 GLUT 如此糟糕?

c++ - OpenGL 在 2 个纹理之间切换

c - Codeblocks 中的第一个 C 程序

使用包装器将 shell 脚本转换为 C 代码

python - 为 Windows 7 构建 Mesa。Mesa 9.1

c - OpenGL 中的 MyReshape 函数不起作用

c - OpenGL glutWireCube 可以工作,但 glutWireCylinder 不能。我究竟做错了什么?

c - 通过 C 的 GUI 用户输入