Modelling the solar system in openGL [closed]

心不动则不痛 提交于 2020-04-07 10:40:31

问题


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.


回答1:


Regarding the planetary positions, you have several alternatives.

  • 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;  
}



回答2:


If you want to develop a full scale solar or other system in OpenGL I would recommend to take a look into this book .It teaches you this very sort of application development.Otherwise your question has too many possible solutions.You should me more specific .



来源:https://stackoverflow.com/questions/16222505/modelling-the-solar-system-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!