我正在用C ++编写引力n体模拟,使用OpenGL和GLUT(这是一个爱好项目)进行动画制作。在大多数情况下,动画效果很好,但是我有两个我无法解决的主要问题:
我的问题是,如何解决这些问题?
这两个问题都可以在下面的图片中看到(链接道歉,但我没有足够的代表发布图片)。这些是来自简单轨道模拟的快照,查看边缘。
Here黄色球体被绘制在紫色球体前面,应该如此。
After half an orbit黄色球体仍在前方,即使它离得更远。
下面给出了用于创建动画的代码。
#include <GL/glut.h>
#include "Cluster.h" // My own class.
// Scale for animation. Each unit in the animation = 1/SCALE m.
const double SCALE = 1e-10;
// Size of spheres for animation.
const double SPHERE_SIZE = 2e10*SCALE;
// Cluster object contains bodies and updates their positions.
Cluster cluster();
// Array of rgb colors for spheres.
GLfloat colorArr[4][4] =
{
{0.7, 0.7, 0.0, 1.0},
{0.73, 0.24, 0.95, 1.0},
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0.0*SCALE, 5e11*SCALE, 0.0*SCALE // eye is on y-axis outside orbit.
, 0.0, 0.0, 0.0
, 0.0, 0.0, 1.0 );
for (int i=0; i<N; i++) // N is the number of bodies in cluster.
{
glPushMatrix();
glTranslated( SCALE*cluster.getX(i) // Get coordinate of ith body.
, SCALE*cluster.getY(i)
, SCALE*cluster.getZ(i) );
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, colorArr[i]);
glutSolidSphere(SPHERE_SIZE, 50, 50);
glCullFace(GL_BACK);
glPopMatrix();
}
glutSwapBuffers();
}
void reshape(GLint w, GLint h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat)w/(GLfloat)h, SCALE, 5e11*SCALE);
glMatrixMode(GL_MODELVIEW);
}
void animate()
{
// Update positions and redraw.
cluster.update();
display();
}
void init()
{
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 0.5};
GLfloat direction[] = {1.0, 1.0, 1.0, 0.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialf(GL_FRONT, GL_SHININESS, 10);
glLightfv(GL_LIGHT0, GL_AMBIENT, black);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_SPECULAR, white);
glLightfv(GL_LIGHT0, GL_POSITION, direction);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Test Orbit");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
init();
glutMainLoop();
}
答案 0 :(得分:0)
OpenGL的深度缓冲区精度有限,而且您使用的数字足够大,可能会遇到精度限制。您可能需要阅读this article about depth-buffer optimization,或者只是使用比米更大的单位来表示天文距离。