Solar system animation in 3D
Tool used:
1)Visual studio 2017
2) language used: c
3) library used for graphics : openGL
In this project we have main file called "source.cpp". It consist of code as shown below:
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 headerfile | |
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#include <time.h> | |
#include "GL/glut.h" | |
#include "b.h" | |
// necessary variable declaration and initialization | |
bool one = false; | |
static float rotx = 0.0; | |
static float worldX = 15.0; | |
static float worldY = 0.0; | |
static float scaleFactor = 0.6; | |
static bool animate = false; | |
static float first = 1; | |
static float second = 1; | |
point stars[500]; | |
int globalW, globalH; | |
float speed = 0.5; | |
// for text display | |
void keimeno(const char *str, float size){ | |
glPushMatrix(); | |
glScalef(size, size, size); | |
for(int i = 0; i < strlen(str); i++) | |
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]); | |
glPopMatrix(); | |
} | |
void Render(){ | |
//CLEARS FRAME BUFFER ie COLOR BUFFER& DEPTH BUFFER (1.0) | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glTranslatef(0, 0, -500); | |
if (!animate) { | |
glPushMatrix(); | |
glDisable(GL_LIGHTING); | |
glDisable(GL_LIGHT0); | |
glColor3f(1, 1, 1); | |
glTranslatef(-8, 250, 0.0); | |
keimeno("Paused", 0.1f); | |
glPopMatrix(); | |
} | |
glEnable(GL_LIGHTING); | |
glEnable(GL_LIGHT0); | |
GLUquadricObj *quadric; | |
quadric = gluNewQuadric(); | |
gluQuadricDrawStyle(quadric, GLU_FILL); | |
gluDeleteQuadric(quadric); | |
glScalef(scaleFactor, scaleFactor, scaleFactor); | |
glRotatef(worldX, 1, 0, 0); | |
glRotatef(worldY, 0, 1, 0); | |
glDisable(GL_LIGHTING); | |
glPushMatrix(); | |
glColor3f(0.8, 0.498039, 0.196078); | |
gluSphere(quadric, 15, 36, 18); | |
glColor3f(1, 1, 0); | |
glEnable(GL_BLEND); | |
glBlendFunc(first, second); | |
gluSphere(quadric, 35, 36, 18); | |
glDisable(GL_BLEND); | |
glPopMatrix(); | |
glEnable(GL_LIGHTING); | |
GLfloat light_position[] = { 0.0, 0.0, 0.0,1 }; | |
glLightfv(GL_LIGHT0, GL_POSITION, light_position); | |
GLfloat light_diff[] = { 1.0, 1.0, 1.0, 1.0 }; | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diff); | |
GLfloat light_amb[] = { 0.0, 0.0, 0.0, 1.0 }; | |
glLightfv(GL_LIGHT0, GL_AMBIENT, light_amb); | |
glPushMatrix(); //1 planet | |
glRotatef(rotx*speed *1.9, 0, 1, 0); | |
glTranslatef(40, 0, -100.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(1, 0.1, 0.6); | |
gluSphere(quadric, 17, 36, 18); | |
glPopMatrix(); | |
glPushMatrix();// 2 planet | |
glRotatef(rotx*speed * 1.8, 0, 1, 0); | |
glTranslatef(-200, 0.0, -150); | |
glRotatef(rotx, 0, 0, 1); | |
glColor3f(1, 0.1, 0.29); | |
gluSphere(quadric, 10, 16, 18); | |
glPopMatrix(); | |
glPushMatrix();// 3 planet | |
glRotatef(rotx* speed *1.7, 0, 1, 0); | |
glTranslatef(-60, 0, -200.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(1, 0, 0); | |
gluSphere(quadric, 17, 36, 18); | |
glPopMatrix(); | |
glPushMatrix();// 4 planet | |
glRotatef(rotx*speed * 1.6, 0, 1, 0); | |
glTranslatef(20, 0, -250.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(0.1, 0.6, 0.9); | |
gluSphere(quadric, 17, 36, 18); | |
glPopMatrix(); | |
glPushMatrix();// 5 planet | |
glRotatef(rotx *speed * 1.4, 0, 1, 0); | |
glTranslatef(-30, 0, -300.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(1, 0.7, 0.3); | |
gluSphere(quadric, 13, 36, 8); | |
glPopMatrix(); | |
glPushMatrix();// 6 planet | |
glRotatef(rotx *speed * 1.3, 0, 1, 0); | |
glTranslatef(20, 0, -350.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(0.5, 0, 0.5); | |
gluSphere(quadric, 17, 36, 18); | |
glPopMatrix(); | |
glPushMatrix();// 7 planet | |
glRotatef(rotx *speed * 1.5, 0, 1, 0); | |
glTranslatef(-30, 0, -400.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(0.3, 0.8, 0); | |
gluSphere(quadric, 13, 36, 8); | |
glPopMatrix(); | |
glPushMatrix();// 8 planet | |
glRotatef(rotx *speed *1.2, 0, 1, 0); | |
glTranslatef(-30, 0, -450.0); | |
glRotatef(rotx, 1, 0, 0); | |
glColor3f(0.5, 0.5, 0.5); | |
gluSphere(quadric, 13, 36, 8); | |
glPopMatrix(); | |
DrawStars(); | |
glutSwapBuffers(); // All drawing commands applied to the | |
// hidden buffer, so now, bring forward | |
// the hidden buffer and hide the visible one | |
} | |
//----------------------------------------------------------- | |
void Resize(int w, int h) | |
{ | |
// define the visible area of the window ( in pixels ) | |
if (h == 0) h = 1; | |
glViewport(0, 0, w, h); | |
globalH = h; | |
globalW = w; | |
// Setup viewing volume | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(60.0, (float)w / (float)h, 1.0, 1000.0); | |
} | |
void Idle() | |
{ | |
if (animate) | |
rotx += 1.1; | |
glutPostRedisplay(); | |
} | |
void Keyboard(unsigned char key, int x, int y) | |
{ | |
switch (key) | |
{ | |
case 'q': exit(0); | |
break; | |
case 'w': if (animate) worldX -= 1.0f; | |
break; | |
case 's': if (animate) worldX += 1.0f; | |
break; | |
case 'a': if (animate) worldY -= 1.0f; | |
break; | |
case 'd': if (animate) worldY += 1.0f; | |
break; | |
case ' ': animate = !animate; | |
break; | |
default: break; | |
} | |
glutPostRedisplay(); | |
} | |
void Setup() // TOUCH IT !! | |
{ | |
//get random cordinates for the stars | |
for (int i = 0; i < 500; i++) | |
RandomCoordinates(&stars[i]); | |
srand(time(0)); | |
//Parameter handling | |
glShadeModel(GL_SMOOTH); | |
glEnable(GL_DEPTH_TEST); | |
// polygon rendering mode | |
glEnable(GL_COLOR_MATERIAL); | |
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); | |
// Black background | |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
} | |
void RandomCoordinates(point *star) | |
{ | |
int lowest = -1000, highest = 1000; | |
int range = (highest - lowest) + 1; | |
star->x = lowest + int(range*rand() / (RAND_MAX + 1.0)); | |
star->y = lowest + int(range*rand() / (RAND_MAX + 1.0)); | |
star->z = lowest + int(range*rand() / (RAND_MAX + 1.0)); | |
} | |
void DrawStars() | |
{ | |
GLUquadricObj *quadric; | |
quadric = gluNewQuadric(); | |
gluQuadricDrawStyle(quadric, GLU_FILL); | |
gluDeleteQuadric(quadric); | |
for (int i = 0; i < 500; i++) { | |
glPushMatrix(); | |
glTranslatef(stars[i].x, stars[i].y, stars[i].z); | |
glColor3f(1, 1, 1); | |
gluSphere(quadric, 1, 36, 18); | |
glPopMatrix(); | |
} | |
} |
Now we made custom header file named "b.h", and "b.cpp" file for defining header file function.
the source code for these file is also given as:
b.cpp source 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
#include <math.h> | |
#include "GL/glut.h" | |
#include <stdio.h> | |
#include <string.h> | |
#include "b.h" | |
// Header file for our OpenGL functions | |
int main(int argc, char* argv[]) | |
{ | |
// initialize GLUT library state | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); | |
// Define the main window size and initial position | |
glutInitWindowSize(800, 800); | |
glutInitWindowPosition(50, 50); | |
// Create and label the main window | |
glutCreateWindow("Solar system"); | |
// Configure various properties of the OpenGL rendering context | |
Setup(); | |
// Callbacks for the GL and GLUT events: | |
// The rendering function | |
glutDisplayFunc(Render); | |
glutReshapeFunc(Resize); | |
glutIdleFunc(Idle); | |
glutKeyboardFunc(Keyboard); | |
//Enter main event handling loop | |
glutMainLoop(); | |
return 0; | |
} |
b.h source 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
struct point{ | |
float x; | |
float y; | |
float z; | |
}; | |
struct face { | |
int vtx[3]; | |
}; | |
extern int globalW, globalH; | |
//-------- Functions -------------------------------- | |
void Render(); | |
// The function responsible for drawing everything in the | |
// OpenGL context associated to a window. | |
void Resize(int w, int h); | |
// Handle the window size changes and define the world coordinate | |
// system and projection type | |
void Setup(); | |
// Set up the OpenGL state machine and create a light source | |
void Idle(); | |
void Keyboard(unsigned char key, int x, int y); | |
//functin for handling mouse whell event | |
void RandomCoordinates(point*); | |
void DrawStars(); | |
No comments:
Post a Comment