#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>
#include <windows.h>
#define MAXIMUM 25000
GLdouble yEarth[MAXIMUM];
GLdouble yMars[MAXIMUM];
GLdouble yMoon[MAXIMUM];
GLint InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(1.0f, 1.0f, 1.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}
GLvoid resize(GLint width, GLint height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,MAXIMUM);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
double HeightOfY(double distance, double angle, double gravity, double speed)
{
double height=0;
const double
Pi = 3.141592653589793238462643383279502884197169399375105820974944592,
Conversion = Pi/180;
double Temp0 = distance * tan(angle*Conversion);
double Temp1 = gravity * (distance*distance);
double Temp2 = 2*(((speed*cos(angle*Conversion))*(speed*cos(angle*Conversion))));
height = Temp0-(Temp1/Temp2);
return height;
}
GLvoid FPS(GLvoid)
{
static GLint FramesPerSecond = 0; // Create a var, to hold the frame per second
static GLfloat lastTime = 0.0f; // Create a var, to hold the last time (ticks from last second)
static char strFrameRate[256] = {0}; // Create a string, needed to post the new title
GLfloat currentTime = GetTickCount() * 0.001f; // Get the current time, in ticks
++FramesPerSecond; // Raise framerate in the static var
if ((currentTime - lastTime) > 1.0f) // When a second has elasped
{
lastTime = currentTime; // Base on new time
sprintf(strFrameRate, "Space (running at: FPS: %d)", FramesPerSecond); // Update the string
glutSetWindowTitle(strFrameRate); // Update window title
FramesPerSecond = 0; // Start counting form 0, FramesPerSecond
}
}
GLvoid display(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-(MAXIMUM/2),-(MAXIMUM/5),-MAXIMUM);
glBegin (GL_LINES);
int xvalue=0;
glColor3f(1,0,0);
glVertex2f(0, 0);
glVertex2f(MAXIMUM, 0);
glVertex2f(0, MAXIMUM);
glVertex2f(0, 0);
glColor3f(0,0,1);
for (xvalue=1; xvalue<=MAXIMUM && yEarth[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yEarth[(xvalue-1)]);
glVertex2f(xvalue, yEarth[xvalue]);
}
glColor3f(0,1,1);
for (xvalue=1; xvalue<=MAXIMUM && yMars[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMars[(xvalue-1)]);
glVertex2f(xvalue, yMars[xvalue]);
}
glColor3f(0,1,0);
for (xvalue=1; xvalue<=MAXIMUM && yMoon[xvalue]>0 ; xvalue++)
{
glVertex2f((xvalue-1), yMoon[(xvalue-1)]);
glVertex2f(xvalue, yMoon[xvalue]);
}
glEnd();
glutSwapBuffers();
}
GLvoid key(unsigned char key, GLint x, GLint y)
{
switch (key)
{
case 27 :
exit(0);
break;
}
glutPostRedisplay();
}
GLvoid idle(GLvoid)
{
FPS();
glutPostRedisplay();
}
GLvoid Initialization(int argc, char *argv[])
{
glutInit(&argc, argv); // Initialization of glut
glutInitWindowSize(800,600); // Set WindowSize (based upon the var loaded)
glutInitWindowPosition(0,0); // Set default window position
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); // Initialate DisplayMode
}
GLint main(GLint argc, char *argv[])
{
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200); // Earth
}
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200); // Mars
}
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200); // The Moon
}
Initialization(argc, argv);
glutCreateWindow("Space");
glutReshapeFunc(resize); // Check if windows is being resized
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_DEPTH_TEST); // Enable Depth testing
glDepthFunc(GL_LESS); // Choose Depth function
glutMainLoop();
return EXIT_SUCCESS;
}
This is based upon GLUT, just a little thing I made up in a couple of minuets, basically it calculates 25000points for "y" based upon x'es from 0 to 25000, and then plots those using lines in OpenGL.
The program is VERY inefficient, and you should easily be able to make it more efficient
As the program is right now, it simply lots the parable for the Earth (blue), Mars (teal), Moon (green).
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yEarth[xuse]=HeightOfY(xuse, 35, 9.81, 200); // Earth
}
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMars[xuse]=HeightOfY(xuse, 35, 3.77, 200); // Mars
}
for (int xuse=0; xuse<MAXIMUM; xuse++)
{
yMoon[xuse]=HeightOfY(xuse, 35, 1.62, 200); // The Moon
}
and then export these values to a text file and let somethin' like GNUPlot handle the plotting
.