Solar System animation in C++
code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//necessary header | |
#include<iostream> | |
#include<math.h> | |
//Opengl utilities | |
#include <GL\glew.h> | |
#include<GL/GLU.h> | |
#include<GL/GL.h> | |
#include <GL\freeglut.h> | |
//function prototype | |
void display(); | |
void init(); | |
void reshape(int, int); | |
void timer(int); | |
void DrawCircle(float cx, float cy, float r, int num_segments); | |
//custom function prototype | |
int *orbitXY(float); | |
int main(int argc, char** argv) { | |
//initializing glut library | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); | |
//creating window | |
glutInitWindowPosition(0, 0); | |
glutInitWindowSize(3000, 3000); | |
//window name | |
glutCreateWindow("Solar system Simulation"); | |
//function to hold the screen | |
glutDisplayFunc(display); | |
//function to reshape the window | |
glutReshapeFunc(reshape); | |
//for animation | |
glutTimerFunc(0, timer, 0); | |
//to change window background | |
init(); | |
glutMainLoop(); | |
return(0); | |
} | |
int xc_position = 600; | |
int yc_position = 0; | |
void display() { | |
//clear | |
glClear(GL_COLOR_BUFFER_BIT); | |
//reset | |
glLoadIdentity(); | |
//draw things | |
glPointSize(100.0); | |
//circle | |
//for sun | |
glColor3f(1.0, 0.0, 0.0); | |
DrawCircle(0.0, 0.0, 150.0, 10000); | |
//for earth | |
glColor3f(0.0, 0.0, 1.0); | |
DrawCircle(xc_position, yc_position, 100.0, 1000); | |
//display | |
glutSwapBuffers(); | |
} | |
void init() { | |
glClearColor(0.0, 0.0, 0.0, 1); | |
glColor3f(1.0, 1.0, 1.0); | |
} | |
void reshape(int w, int h) { | |
//view port | |
glViewport(0, 0, w, h); | |
//projection | |
glMatrixMode(GL_PROJECTION); | |
//reset | |
glLoadIdentity(); | |
gluOrtho2D(-1000, 1000, -1000, 1000); | |
glMatrixMode(GL_MODELVIEW); | |
} | |
void DrawCircle(float cx, float cy, float r, int num_segments) | |
{ | |
glBegin(GL_LINE_LOOP); | |
for (int ii = 0; ii < num_segments; ii++) | |
{ | |
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle | |
float x = r * cosf(theta);//calculate the x component | |
float y = r * sinf(theta);//calculate the y component | |
glVertex2f(x + cx, y + cy);//output vertex | |
} | |
glEnd(); | |
} | |
float angle = 0; | |
void timer(int) { | |
glutPostRedisplay(); | |
glutTimerFunc(1000 / 60, timer, 0); | |
if (angle > 360) { | |
angle = 0; | |
} | |
angle += 0.001; | |
int *p = orbitXY(angle); | |
xc_position = *(p + 0); | |
yc_position = *(p + 1); | |
} | |
int *orbitXY(float angleInDegree) { | |
static int tempxy[2]; | |
tempxy[0] = (int)(cos(angleInDegree) * 600 - sin(angleInDegree) * 0); | |
tempxy[1] = (int)(sin(angleInDegree) * 600 + cos(angleInDegree) * 0); | |
return tempxy; | |
} | |
No comments:
Post a Comment