在openGL中对太阳系进行建模 [英] Modelling the solar system in openGL

查看:333
本文介绍了在openGL中对太阳系进行建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在c ++和openGL中对太阳系进行建模的方法,我想知道是否有一种我可以产生的便宜方法,该方法可以返回x,y,z向量,可以用来更新其位置.每个星球每帧.

I'm looking to mode the solar system within c++ and openGL, and I was wondering if there was a cheap method that I could produce that would return an x,y,z vector that I could use to update the position of each planet every frame.

推荐答案

关于行星位置,您有几种选择.

Regarding the planetary positions, you have several alternatives.

  • 不准确(但在球场上"):假设行星在平面上以圆周运动
  • 有些不准确(但更加接近现实):下载"轨道元素开普勒方程传播轨道(比听起来简单得多).这在大多数情况下都是正确的(尤其是对于大行星而言).
  • 准确:下载 JPL星历以获取行星位置(DE405或DE421),并使用可用的阅读器之一(例如 SPICE )以尽可能高的准确性检索状态因为目前有可能(请注意,这不一定在计算上很昂贵")
  • 准确:下载 VSOP 数据和相关程序(不准确)作为JPL的星历表,也是任务初步设计等级".
  • Inaccurate (but "in the ball-park"): assume that planets move in circles, on a plane
  • Somewhat inaccurate (but much closer to reality): download the "orbital elements" for the planets from a the Solar System Dynamics group at JPL; propagate the orbits using Kepler's equation (much simpler than it sounds). This will be mostly correct (especially for the large planets).
  • Accurate: download the JPL Ephemerides for planetary positions (DE405 or DE421), and use one of the available readers (e.g., SPICE) to retrieve the state with as much accuracy as it is currently possible (notice this is not necessarily "computationally expensive")
  • Accurate: download the VSOP data and related programs (not as accurate as JPL's ephemerides, but also "mission preliminary design grade").

我找到了我之前写的一些代码,以演示可视化

I found some code I wrote a while ago to demonstrate a "quick and dirty" way to visualize DE421 data using SPICE and OpenGL. Maybe it can help you.

#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, &lt);
    // 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;  
}

这篇关于在openGL中对太阳系进行建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆