我正在C中使用OpenGL编写太阳系模拟。 有一个存储所有行星信息的主体数组。 我希望行星轨道倾斜一定的角度,然而从结果来看,行星和轨道一直围绕(0,0,0)旋转。我怎么能解决这个问题? 这是我的一些代码。
这里是数组'
的定义typedef struct {
char name[20]; /* name */
GLfloat r, g, b; /* colour */
GLfloat orbital_radius; /* distance to parent body (km) */
GLfloat orbital_tilt; /* angle of orbit wrt ecliptic (deg) */
GLfloat orbital_period; /* time taken to orbit (days) */
GLfloat radius; /* radius of body (km) */
GLfloat axis_tilt; /* tilt of axis wrt body's orbital plane (deg) */
GLfloat rot_period; /* body's period of rotation (days) */
GLint orbits_body; /* identifier of parent body */
GLfloat spin; /* current spin value (deg) */
GLfloat orbit; /* current orbit value (deg) */
} body;
以下是精确绘制行星的功能。它需要绕轴旋转才能旋转,轨道也需要倾斜。
void drawBody(int n)
{
glRotatef(bodies[n].orbital_tilt,0,0,1); //toilet
drawOrbit(n);
glRotatef(bodies[n].orbit,0,1,0);//orb
glTranslatef(bodies[n].orbital_radius,0,0);
// self axis tilt
glRotatef(bodies[n].axis_tilt,0,0,1);//atilt
/* rotate on its own */
glPushMatrix();
glRotatef(bodies[n].spin,0,1,0); //self-spin
glRotatef(90,1,0,0); //vert
glColor3f(bodies[n].r,bodies[n].g,bodies[n].b);
glLineWidth(1.0);
glutWireSphere(bodies[n].radius,15,15);
glPopMatrix();
}
和我的绘制轨道功能:</ p>
void drawOrbit (int n)
{ /* Draws a polygon to approximate the circular
orbit of body "n" */
float angle = 2*PI/ORBIT_POLY_SIDES;
float r = bodies[n].orbital_radius;
float i ;
glPushMatrix();
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glBegin(GL_POLYGON);
glColor3f(bodies[n].r,bodies[n].g,bodies[n].b);
for (i = 0; i < 2*PI; i+=angle){
glVertex3f(r*sin(i),0,r*cos(i));
}
glEnd();
glPopMatrix();
/* This is for you to complete. */
}