我正在寻找在c ++和openGL中模拟太阳能系统,我想知道是否有一种便宜的方法可以返回我可用于更新位置的x,y,z向量每个星球每个星球。
答案 0 :(得分:6)
关于行星位置,你有几种选择。
我发现了一些我刚才写的代码,用于展示使用SPICE和OpenGL可视化DE421数据的“快速而肮脏”的方法。也许它可以帮助你。
#include<cstdlib>
#include<cmath>
#include<OpenGL/gl.h>
#include<OpenGL/glu.h>
#include<GLUT/glut.h>
#include<SpiceUsr.h>
// hard-code some parameters - in a real application all this would be dynamic
#define ALTITUDE 700E6 // in kilometers
#define CLIPPING 100E7
#define FOV 45.0 // 45-degree field-of-view
#define WIN_WIDTH 1024
#define WIN_HEIGHT 1024
// callback to render the trajectory of a planet using spice (notice
// that I use 366 points - one per day starting at epoch 0.0
// (01-Jan-2000 12:00:00 ET) - (r, g, b) is the color
void render_planet(const char* planet, int r, int g, int b) {
unsigned int N = 366;
double et = 0.0;
double state[3];
double lt;
// we just want a simple line
glBegin(GL_LINE_STRIP);
glColor4d(r, g, b, 1.0);
for(unsigned int k=0; k<N; k++) {
// call spice to calculate position
spkpos_c(planet, et, "ECLIPJ2000", "None", "Sun", state, <);
// add the point to the pipeline
glVertex3d(state[0], state[1], state[2]);
// increase time by one day
et = 86400 * k;
}
glEnd();
}
// callback to handle window resizing
void changeSize(int w, int h) {
if (h == 0) h = 1;
float ratio = w * 1.0 / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(FOV, ratio, 0.2f, CLIPPING);
glMatrixMode(GL_MODELVIEW);
}
// callback to render scene
void renderScene() {
// use a nice dark gray for the background (as opposed to pitch black)
glClearColor(50/255.0, 50/255.0, 50/255.0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, ALTITUDE,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
// here we tell the application which planets to draw, the colors
// are (r, g, b), so (1, 1, 0) is all red and all green (yellow),
// and so forth - of course this can be simplified to use arbitrary
// colors
render_planet("Mercury", 1, 1, 0);
render_planet("Venus", 0, 1, 0);
render_planet("Earth", 0, 0, 1);
render_planet("Mars", 1, 0, 0);
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
// initialize spice kernels
furnsh_c("/data/spice/allkernels.txt");
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
glutCreateWindow("Simple Trajectory Viewer");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutMainLoop();
return EXIT_SUCCESS;
}
答案 1 :(得分:2)
如果您想在OpenGL中开发全尺寸太阳能系统或其他系统,我建议您查看this本书。它会教您这种类型的应用程序开发。否则您的问题有太多可能的解决方案你应该更具体一点。