Stellar system
The fllowing C program uses the FreeGLUT package to produce orbits for stellar objects. I have not yet applied Kepler's equations to produce elliptical orbits instead of the circular ones shown. As well as Debian 9 'Stretch' 64 bit Linux, I have got the program to run on the Raspberry Pi with Raspbian 10 'Buster'.
Here are screenshot of the program output:
The program can be compiled by using the syntax
gcc programname.c -o programname -lglut -lGL
Here is the program code
// ----------------------------------------------------------
// Includes
// ----------------------------------------------------------
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
static GLfloat rot; //rotation angle
// ----------------------------------------------------------
// Function Prototypes
// ----------------------------------------------------------
void display();
// ----------------------------------------------------------
// display() Callback function
// ----------------------------------------------------------
void display(){
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Other Transformations
// glTranslatef( 0.1, 0.0, 0.0 ); // Not included
// glRotatef( 180, 0.0, 1.0, 0.0 ); // Not included
// glScalef( 2.0, 2.0, 0.0 ); // Not included
glPushMatrix();//push on stack
glTranslatef(0.0, 0.0, -10.0); //move
glRotatef(rot, 0.0, 1.0, 0.0); //rotation
// glRotatef(rot*2, 1.0, 0.0, 0.0); //rotation
glColor3f (1.0, 1.0, 0.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(0.0, 0.0, -20.0); //move
glRotatef(rot*0.4, 0.0, 1.0, 0.0); //rotation
glTranslatef(7.0, 0.0, -1.5); //move
// glRotatef(rot*2, 1.0, 0.0, -5.0); //rotation
glColor3f(0.0, 1.0, 1.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(5.0, 0.0, -20.0); //move
glRotatef(rot*0.2, 0.0, 1.0, 0.0); //rotation
glTranslatef(7.0, 2.0, 2.0); //move
glColor3f (1.0, 1.0, 0.0);
glutSolidSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(5.0, 2.0, -25.0); //move
glRotatef(rot*0.5, 0.0, 1.0, 0.0); //rotation
glTranslatef(5.0, 0.0, 2.0); //move
glTranslatef(7.0, 0.0, 4.0); //move
// glRotatef(rot, 0.5, 1.0, 0.0); //rotation
glColor3f(0.0, 1.0, 1.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glFlush();
glutSwapBuffers(); // Request display update
glutPostRedisplay();
}
void reshape_func(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
void idle_func(void)
{
rot=0.1*(GLfloat)glutGet(GLUT_ELAPSED_TIME); //Returns the amount of time (in milliseconds) from the time you call the () glutInit.
glutPostRedisplay(); //re-draw the screen glut
}
// ----------------------------------------------------------
// main() function
// ----------------------------------------------------------
int main(int argc, char* argv[]){
// Initialize GLUT and process user parameters
glutInit(&argc,argv);
// glNewList(GLOBE, GL_COMPILE);
// Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,600);
// Create window
glutCreateWindow("Rotating spheres in C");
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
// Callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape_func);
glutIdleFunc(idle_func);
glEnable(GL_DEPTH_TEST);
// Pass control to GLUT for events
glutMainLoop();
// Return to OS
return 0;
}
Here are screenshot of the program output:
The program can be compiled by using the syntax
gcc programname.c -o programname -lglut -lGL
Here is the program code
// ----------------------------------------------------------
// Includes
// ----------------------------------------------------------
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
static GLfloat rot; //rotation angle
// ----------------------------------------------------------
// Function Prototypes
// ----------------------------------------------------------
void display();
// ----------------------------------------------------------
// display() Callback function
// ----------------------------------------------------------
void display(){
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Other Transformations
// glTranslatef( 0.1, 0.0, 0.0 ); // Not included
// glRotatef( 180, 0.0, 1.0, 0.0 ); // Not included
// glScalef( 2.0, 2.0, 0.0 ); // Not included
glPushMatrix();//push on stack
glTranslatef(0.0, 0.0, -10.0); //move
glRotatef(rot, 0.0, 1.0, 0.0); //rotation
// glRotatef(rot*2, 1.0, 0.0, 0.0); //rotation
glColor3f (1.0, 1.0, 0.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(0.0, 0.0, -20.0); //move
glRotatef(rot*0.4, 0.0, 1.0, 0.0); //rotation
glTranslatef(7.0, 0.0, -1.5); //move
// glRotatef(rot*2, 1.0, 0.0, -5.0); //rotation
glColor3f(0.0, 1.0, 1.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(5.0, 0.0, -20.0); //move
glRotatef(rot*0.2, 0.0, 1.0, 0.0); //rotation
glTranslatef(7.0, 2.0, 2.0); //move
glColor3f (1.0, 1.0, 0.0);
glutSolidSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glPushMatrix();//push on stack
glTranslatef(5.0, 2.0, -25.0); //move
glRotatef(rot*0.5, 0.0, 1.0, 0.0); //rotation
glTranslatef(5.0, 0.0, 2.0); //move
glTranslatef(7.0, 0.0, 4.0); //move
// glRotatef(rot, 0.5, 1.0, 0.0); //rotation
glColor3f(0.0, 1.0, 1.0);
glutWireSphere(1.0, 20, 50);
glPopMatrix(); //pop out stack
glFlush();
glutSwapBuffers(); // Request display update
glutPostRedisplay();
}
void reshape_func(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
void idle_func(void)
{
rot=0.1*(GLfloat)glutGet(GLUT_ELAPSED_TIME); //Returns the amount of time (in milliseconds) from the time you call the () glutInit.
glutPostRedisplay(); //re-draw the screen glut
}
// ----------------------------------------------------------
// main() function
// ----------------------------------------------------------
int main(int argc, char* argv[]){
// Initialize GLUT and process user parameters
glutInit(&argc,argv);
// glNewList(GLOBE, GL_COMPILE);
// Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600,600);
// Create window
glutCreateWindow("Rotating spheres in C");
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
// Callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape_func);
glutIdleFunc(idle_func);
glEnable(GL_DEPTH_TEST);
// Pass control to GLUT for events
glutMainLoop();
// Return to OS
return 0;
}


Comments
Post a Comment