Created
January 20, 2015 18:59
-
-
Save jameskerr/1770e4b7fe501a659878 to your computer and use it in GitHub Desktop.
Assignment 6
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
#ifndef MAC //if not MAC | |
#include <GL\glut.h> | |
#else // MAC includes below | |
#include <GLUT/glut.h> | |
#endif | |
/********************** | |
::: UTILITY CLASSES ::: | |
***********************/ | |
struct Planet { // Our cute little planet class | |
GLfloat size, speed, angle, distance, color[3]; | |
Planet() {} | |
}; | |
// Our Menu Options | |
enum MENU_TYPE { ABOVE, BELOW, SIDE, SUN, DEFAULT }; | |
/************************* | |
::: GLOBAL VARIABLES ::: * | |
*************************/ | |
Planet planets[10]; // An array for all the planets to loop through | |
GLfloat width = 1290.0; // Initial screen width | |
GLfloat height = 800.0; // Initial screen height | |
/************************* | |
::: CALLBACK FUNCTIONS ::: | |
*************************/ | |
void display() { | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
for (int i = 0; i < 10; ++i) { | |
glPushMatrix(); | |
glRotatef(planets[i].angle, 0, 1, 0); | |
glTranslatef(planets[i].distance, 0, 0); | |
glColor3fv(planets[i].color); | |
glutSolidSphere(planets[i].size, 50, 50); | |
glPopMatrix(); | |
} | |
glutSwapBuffers(); | |
} | |
void idle() { | |
for (int i = 0; i < 10; ++i) { | |
planets[i].angle = planets[i].angle + planets[i].speed; | |
} | |
glutPostRedisplay(); | |
} | |
void reshape (int w, int h) { | |
glViewport(0, 0, (GLsizei) w, (GLsizei) h); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45, width / height, 1, 5000); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
gluLookAt(100.0, 200.0, 800.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
} | |
void menu(int item) { | |
glLoadIdentity(); | |
switch (item) { | |
case ABOVE: | |
gluLookAt(0.1, 2000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
break; | |
case BELOW: | |
gluLookAt(0.1, -2000.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
break; | |
case SIDE: | |
gluLookAt(0.0, 0.0, 800.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
break; | |
case SUN: | |
gluLookAt(40.0, 0.0, 0.0, 41.0, 0.0, 0.0, 0.0,1.0, 0.0); | |
break; | |
case DEFAULT: | |
gluLookAt(100.0, 200.0, 800.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); | |
break; | |
} | |
glutPostRedisplay(); | |
} | |
/******************************* | |
::: INITIALIZATION FUNCTIONS ::: | |
*******************************/ | |
void init() { | |
GLfloat ambient_light[] = { 1.0, 1.0, 1.0, 1.0 }; | |
GLfloat diffuse_light[] = { 1.0, 1.0, 1.0, 1.0 }; | |
GLfloat specular_light[] = { 1.0, 1.0, 1.0, 1.0 }; | |
GLfloat light_position_0[] = { 0.0, 0.0, -725.0, 1.0 }; | |
glShadeModel (GL_SMOOTH); | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light); | |
glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light); | |
glLightfv(GL_LIGHT0, GL_POSITION, light_position_0); | |
glEnable(GL_LIGHTING); | |
glEnable(GL_LIGHT0); | |
glEnable(GL_DEPTH_TEST); | |
glEnable(GL_COLOR_MATERIAL); | |
} | |
void initMenu() { | |
glutCreateMenu(menu); | |
glutAttachMenu(GLUT_RIGHT_BUTTON); | |
glutAddMenuEntry("Above", ABOVE); | |
glutAddMenuEntry("Below", BELOW); | |
glutAddMenuEntry("Side", SIDE); | |
glutAddMenuEntry("Sun", SUN); | |
glutAddMenuEntry("Default", DEFAULT); | |
} | |
void initPlanets() { | |
// THE SUN | |
Planet sun = Planet(); | |
sun.size = 40; | |
sun.speed = 0; | |
sun.angle = 0; | |
sun.distance = 0; | |
sun.color[0] = 255 / 255.0; | |
sun.color[1] = 100 / 255.0; | |
sun.color[2] = 30 / 255.0; | |
planets[0] = sun; | |
// Earth | |
Planet earth = Planet(); | |
earth.size = 2.5; | |
earth.speed = 1; | |
earth.angle = 0; | |
earth.distance = 100; | |
earth.color[0] = 63 / 255.0; | |
earth.color[1] = 95 / 255.0; | |
earth.color[2] = 111 / 255.0; | |
planets[3] = earth; | |
// MERCURY | |
Planet mercury = Planet(); | |
mercury.size = earth.size * 0.38; | |
mercury.speed = earth.speed * 1.607; | |
mercury.angle = 0; | |
mercury.distance = earth.distance * (0.387 + 0.15); | |
mercury.color[0] = 1 / 255.0; | |
mercury.color[1] = 1 / 255.0; | |
mercury.color[2] = 1 / 255.0; | |
planets[1] = mercury; | |
// VENUS | |
Planet venus = Planet(); | |
venus.size = earth.size * 0.95; | |
venus.speed = earth.speed * 1.174; | |
venus.angle = 0; | |
venus.distance = earth.distance * (0.723 - 0); | |
venus.color[0] = 148 / 255.0; | |
venus.color[1] = 85 / 255.0; | |
venus.color[2] = 22 / 255.0; | |
planets[2] = venus; | |
// Mars | |
Planet mars = Planet(); | |
mars.size = earth.size * 0.53; | |
mars.speed = earth.speed * 0.802; | |
mars.angle = 0; | |
mars.distance = earth.distance * (1.524 - 0); | |
mars.color[0] = 200 / 255.0; | |
mars.color[1] = 40 / 255.0; | |
mars.color[2] = 40 / 255.0; | |
planets[4] = mars; | |
// Jupiter | |
Planet jupiter = Planet(); | |
jupiter.size = earth.size * 11.19; | |
jupiter.speed = earth.speed * 0.434; | |
jupiter.angle = 0; | |
jupiter.distance = earth.distance * (5.203 - 3); | |
jupiter.color[0] = 250 / 255.0; | |
jupiter.color[1] = 50 / 255.0; | |
jupiter.color[2] = 50 / 255.0; | |
planets[5] = jupiter; | |
// Saturn | |
Planet saturn = Planet(); | |
saturn.size = earth.size * 9.4; | |
saturn.speed = earth.speed * 0.323; | |
saturn.angle = 0; | |
saturn.distance = earth.distance * (9.539 - 5); | |
saturn.color[0] = 200 / 255.0; | |
saturn.color[1] = 100 / 255.0; | |
saturn.color[2] = 40 / 255.0; | |
planets[6] = saturn; | |
// Uranus | |
Planet uranus = Planet(); | |
uranus.size = earth.size * 4.04; | |
uranus.speed = earth.speed * 0.228; | |
uranus.angle = 0; | |
uranus.distance = earth.distance * (19.18 - 12); | |
uranus.color[0] = 20 / 255.0; | |
uranus.color[1] = 20 / 255.0; | |
uranus.color[2] = 130 / 255.0; | |
planets[7] = uranus; | |
// Neptune | |
Planet neptune = Planet(); | |
neptune.size = earth.size * 3.88; | |
neptune.speed = earth.speed * 0.182; | |
neptune.angle = 0; | |
neptune.distance = earth.distance * (30.06 - 21); | |
neptune.color[0] = 90 / 255.0; | |
neptune.color[1] = 70 / 255.0; | |
neptune.color[2] = 120 / 255.0; | |
planets[8] = neptune; | |
// Pluto | |
Planet pluto = Planet(); | |
pluto.size = earth.size * 0.48; | |
pluto.speed = earth.speed * 0.159; | |
pluto.angle = 0; | |
pluto.distance = earth.distance * (39.52 - 29); | |
pluto.color[0] = 0 / 255.0; | |
pluto.color[1] = 0 / 255.0; | |
pluto.color[2] = 200 / 255.0; | |
planets[9] = pluto; | |
} | |
/******************** | |
::: MAIN FUNCTION ::: | |
********************/ | |
int main(int argc, char **argv) { | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); | |
glutInitWindowSize(width, height); | |
glutInitWindowPosition(0,0); | |
glutCreateWindow("The Galaxy"); | |
initMenu(); | |
initPlanets(); | |
init(); | |
glutReshapeFunc(reshape); | |
glutDisplayFunc(display); | |
glutIdleFunc(idle); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment