Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
SSSimulator - main.c

main.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Lorenzo La Porta   *
  3.  *   lorelapo@gmail.com   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21. #ifdef __APPLE__
  22. #include <GLUT/glut.h>
  23. #else
  24. #include <GL/glut.h>
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <math.h>
  30.  
  31. #define D2(p1, p2) ((p1.position.x-p2.position.x)*(p1.position.x-p2.position.x)+\
  32.                     (p1.position.y-p2.position.y)*(p1.position.y-p2.position.y)+\
  33.                     (p1.position.z-p2.position.z)*(p1.position.z-p2.position.z))
  34. #define G 6.67428e-11
  35. #define ZOOM 1e-11
  36. #define N 9
  37.  
  38. typedef long double float_t;
  39.  
  40. typedef struct __vector
  41. {
  42.     float_t x, y, z;
  43. } vector;
  44.  
  45. typedef struct __planetary_body
  46. {
  47.     vector position;
  48.     vector speed;
  49.     float_t r, g, b;
  50.     float_t mass;
  51.     float_t radius;
  52. } planet;
  53.  
  54. float k=.2, DELTA = 10000, pr=700.0;
  55.  
  56. planet ssp[N], sun;
  57.  
  58. /* GLUT callback Handlers */
  59.  
  60. static void resize(int width, int height)
  61. {
  62.     const float ar = (float) width / (float) height;
  63.  
  64.     glViewport(0, 0, width, height);
  65.     glMatrixMode(GL_PROJECTION);
  66.     glLoadIdentity();
  67.     glFrustum(-ar, ar, -1.0, 1.0, 2.0, 10000.0);
  68.  
  69.     glMatrixMode(GL_MODELVIEW);
  70.     glLoadIdentity() ;
  71. }
  72.  
  73. static void display(void)
  74. {
  75.     int i;
  76.     glColor3d(1, 1, 0);
  77.  
  78.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  79.  
  80.     glLoadIdentity();
  81.  
  82.     gluLookAt(0.0*k, 0.0*k, 20.0*k, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0);
  83.  
  84.     glPushMatrix();
  85.  
  86.     glPushMatrix();
  87.             glTranslated(ZOOM*sun.position.x,ZOOM*sun.position.y,ZOOM*sun.position.z);
  88.             glutSolidSphere(ZOOM*sun.radius,20,20);
  89.     glPopMatrix();
  90.  
  91.     glColor3d(1, 0, 0);
  92.  
  93.     for(i=0;i<N;i++)
  94.     {
  95.         glColor3f(ssp[i].r, ssp[i].g, ssp[i].b);
  96.         glPushMatrix();
  97.             glTranslated(ZOOM*ssp[i].position.x,ZOOM*ssp[i].position.y,ZOOM*ssp[i].position.z*(i==3?1.1:1.0));
  98.             glutSolidSphere(pr*ZOOM*ssp[i].radius,20,20);
  99.         glPopMatrix();
  100.     }
  101.  
  102.     glPopMatrix();
  103.  
  104.     glutSwapBuffers();
  105. }
  106.  
  107.  
  108. static void key(unsigned char key, int x, int y)
  109. {
  110.     switch (key)
  111.     {
  112.         case 27 :
  113.         case 'q':
  114.             exit(0);
  115.             break;
  116.         case 'a':
  117.             DELTA+=100;
  118.             break;
  119.         case 's':
  120.             DELTA-=100;
  121.             break;
  122.         case 'z':
  123.             pr+=100;
  124.             break;
  125.         case 'x':
  126.             pr-=100;
  127.             break;
  128.         case '+':
  129.             k-=0.01;
  130.             printf("k=%g\n",k);
  131.             break;
  132.         case '-':
  133.             k+=0.01;
  134.             printf("k=%g\n",k);
  135.             break;
  136.         default:
  137.             printf("key=%#x\n\a",key);
  138.     }
  139.     glutPostRedisplay();
  140. }
  141.  
  142. void initData(void)
  143. {
  144.     int i;
  145.     /*inizializza pianeti e sole*/
  146.     sun.position.x=0.0;
  147.     sun.position.y=0.0;
  148.     sun.position.z=0.0;
  149.     sun.speed.x=0.0;
  150.     sun.speed.y=0.0;
  151.     sun.speed.z=0.0;
  152.     sun.mass=1.9891e30;
  153.     sun.radius=6e9;
  154.  
  155.     FILE *fp=fopen("/root/GLUTex/bin/Debug/Planet.txt", "rt");
  156.  
  157.     if(fp==NULL)
  158.     {
  159.         printf("NO DATA FILE FOUND in %s\n",__FILE__);
  160.         exit(1);
  161.     }
  162.  
  163.     for(i=0;i<N;i++)
  164.     {
  165.         fscanf(fp,"(%Lg;%Lg;%Lg)\n",&ssp[i].position.x,&ssp[i].position.y,&ssp[i].position.z);
  166.         fscanf(fp,"(%Lg;%Lg;%Lg)\n",&ssp[i].speed.x,&ssp[i].speed.y,&ssp[i].speed.z);
  167.         fscanf(fp,"(%Lg;%Lg;%Lg)\n",&ssp[i].r,&ssp[i].g,&ssp[i].b);
  168.         fscanf(fp,"%Lg\n",&ssp[i].mass);
  169.         fscanf(fp,"%Lg\n",&ssp[i].radius);
  170.     }
  171.  
  172.     fclose(fp);
  173. }
  174.  
  175. float gravPwr(planet p1, planet p2)
  176. {
  177.     float_t d2=D2(p1,p2),
  178.             mp=p1.mass*p2.mass*G;
  179.     return mp/d2;
  180. }
  181.  
  182. float ang1(planet p1, planet p2)
  183. {
  184.     float_t ang=atan2(p1.position.y-p2.position.y, p1.position.x-p2.position.x);
  185.     return ang;
  186. }
  187.  
  188. float ang2(planet p1, planet p2)
  189. {
  190.     float_t ang=atan2(p1.position.y-p2.position.y, p1.position.z-p2.position.z);
  191.     return ang;
  192. }
  193.  
  194. float ang3(planet p1, planet p2)
  195. {
  196.     float_t ang=atan2(p1.position.z-p2.position.z, p1.position.x-p2.position.x);
  197.     return ang;
  198. }
  199.  
  200. static void idle(void)
  201. {
  202.     /*
  203.      *differenzia la posizione dei pianeti
  204.      */
  205.     int i,j;
  206.     float_t pwr, ang;
  207.     for(i=0;i<N;i++)
  208.     {
  209.         pwr=gravPwr(sun, ssp[i]);
  210.         //printf("%Lg\n",pwr);
  211.         ang=ang1(sun,ssp[i]);
  212.         sun.speed.y-=DELTA*sin(ang)*pwr/sun.mass;
  213.         sun.speed.x-=DELTA*cos(ang)*pwr/sun.mass;
  214.         ssp[i].speed.y+=DELTA*sin(ang)*pwr/ssp[i].mass;
  215.         ssp[i].speed.x+=DELTA*cos(ang)*pwr/ssp[i].mass;
  216.         ang=ang2(sun,ssp[i]);
  217.         //sun.speed.y+=DELTA*sin(ang)*pwr/sun.mass;
  218.         sun.speed.z-=DELTA*cos(ang)*pwr/sun.mass;
  219.        // ssp[i].speed.y+=DELTA*sin(ang)*pwr/ssp[i].mass;
  220.         ssp[i].speed.z+=DELTA*cos(ang)*pwr/ssp[i].mass;
  221.      /*   ang=ang3(sun,ssp[i]);
  222.         sun.speed.z+=DELTA*sin(ang)*pwr/sun.mass;
  223.         sun.speed.x+=DELTA*cos(ang)*pwr/sun.mass;
  224.         ssp[i].speed.z+=DELTA*sin(ang)*pwr/ssp[i].mass;
  225.         ssp[i].speed.x+=DELTA*cos(ang)*pwr/ssp[i].mass;*/
  226.     }
  227.     for(i=0;i<N;i++)
  228.     {
  229.         for(j=0;j<N;j++)
  230.         {
  231.             if(i==j)continue;
  232.             pwr=gravPwr(ssp[j], ssp[i]);
  233.             //printf("%Lg\n",pwr);
  234.             ang=ang1(ssp[j],ssp[i]);
  235.             ssp[j].speed.y-=DELTA*sin(ang)*pwr/ssp[j].mass;
  236.             ssp[j].speed.x-=DELTA*cos(ang)*pwr/ssp[j].mass;
  237.             /*ssp[i].speed.y+=DELTA*sin(ang)*pwr/ssp[i].mass;
  238.             ssp[i].speed.x+=DELTA*cos(ang)*pwr/ssp[i].mass;*/
  239.             ang=ang2(ssp[j],ssp[i]);
  240.             //sun.speed.y+=DELTA*sin(ang)*pwr/sun.mass;
  241.             ssp[j].speed.z-=DELTA*cos(ang)*pwr/ssp[j].mass;
  242.             //ssp[i].speed.y+=DELTA*sin(ang)*pwr/ssp[i].mass;
  243.            // ssp[i].speed.z+=DELTA*cos(ang)*pwr/ssp[i].mass;
  244.             /*ang=ang3(sun,ssp[i]);
  245.             sun.speed.z+=DELTA*sin(ang)*pwr/sun.mass;
  246.             sun.speed.x+=DELTA*cos(ang)*pwr/sun.mass;
  247.             ssp[i].speed.z+=DELTA*sin(ang)*pwr/ssp[i].mass;
  248.             ssp[i].speed.x+=DELTA*cos(ang)*pwr/ssp[i].mass;*/
  249.         }
  250.     }
  251.     for(i=0;i<N;i++)
  252.     {
  253.         ssp[i].position.x+=DELTA*ssp[i].speed.x;
  254.         ssp[i].position.y+=DELTA*ssp[i].speed.y;
  255.         ssp[i].position.z+=DELTA*ssp[i].speed.z;
  256.     }
  257.     sun.position.x+=DELTA*sun.speed.x;
  258.     sun.position.y+=DELTA*sun.speed.y;
  259.     sun.position.z+=DELTA*sun.speed.z;
  260.     glutPostRedisplay();
  261. }
  262.  
  263. const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
  264. const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
  265. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  266. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  267.  
  268. const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
  269. const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
  270. const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
  271. const GLfloat high_shininess[] = { 100.0f };
  272.  
  273. /* Program entry point */
  274.  
  275. int main(int argc, char *argv[])
  276. {
  277.     glutInit(&argc, argv);
  278.     glutInitWindowSize(640,480);
  279.     glutInitWindowPosition(10,10);
  280.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  281.  
  282.     glutCreateWindow("Solar System");
  283.  
  284.     glutReshapeFunc(resize);
  285.     glutDisplayFunc(display);
  286.     glutKeyboardFunc(key);
  287.     glutIdleFunc(idle);
  288.  
  289.     glClearColor(0,0,0,0);
  290.     glEnable(GL_CULL_FACE);
  291.     glCullFace(GL_BACK);
  292.  
  293.     glEnable(GL_DEPTH_TEST);
  294.     glDepthFunc(GL_LESS);
  295.  
  296.     glEnable(GL_LIGHT0);
  297.     glEnable(GL_NORMALIZE);
  298.     glEnable(GL_COLOR_MATERIAL);
  299.     glEnable(GL_LIGHTING);
  300.  
  301.     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
  302.     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
  303.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  304.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  305.  
  306.     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
  307.     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
  308.     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
  309.     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  310.  
  311.     initData();
  312.  
  313.     glutMainLoop();
  314.  
  315.     return EXIT_SUCCESS;
  316. }