Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <glut.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <math.h>
- #define M_PI 3.14159265358979323846
- #define deg2rad(a) (a)* M_PI / 180
- GLfloat aspect = 1.0;
- GLuint width = 1, height = 1;
- GLuint delay = 25; // delay 25 msec = 40 fps (max)
- enum Planets { Mercury = 0, Venus, Terra, Mars };
- enum Moons { Moon = 4, Phobos, Deimos };
- GLfloat radius [] = { 5, 12, 13, 7, 4, 2, 2 };
- GLfloat dist [] = { 58, 108, 150, 207, 20, 15, 10 };
- GLfloat period [] = { 0.3, 0.6, 1, 2, 0.3, 0.35, 0.25 };
- GLfloat spin [] = {0, 0, 0, 0, 0, 0, 0, 0};
- GLfloat fdist = 1;
- GLfloat fperiod = 1;
- GLfloat fradius = 1;
- void normalize( GLfloat *xx, GLfloat *yy, GLfloat *zz,
- GLfloat x, GLfloat y, GLfloat z )
- {
- GLfloat mag = sqrt(x*x + y*y + z*z);
- *xx = x/mag; *yy = y/mag; *zz = z/mag;
- }
- void cross( GLfloat *rx, GLfloat *ry, GLfloat *rz,
- GLfloat ax, GLfloat ay, GLfloat az,
- GLfloat bx, GLfloat by, GLfloat bz)
- {
- *rx = ay*bz - az*by;
- *ry = az*bx - ax*bz;
- *rz = ax*by - ay*bx;
- }
- void myPerspective(GLfloat fov, GLfloat asp, GLfloat zNear, GLfloat zFar)
- {
- //gluPerspective(fov, asp, zNear, zFar); // todo:
- GLfloat f = 2/tan(deg2rad(fov));
- GLfloat m[16] = {f/asp, 0, 0, 0,
- 0, f, 0, 0,
- 0, 0, (zNear+zFar)/(zNear-zFar), -1,
- 0, 0, (2*zNear*zFar)/(zNear-zFar), 0};
- glMultMatrixf(m);
- }
- void myTranslate(GLfloat x, GLfloat y, GLfloat z)
- {
- //glTranslatef(x, y, z); // todo:
- GLfloat m[16] = {1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 1, 0,
- x, y, z, 1};
- glMultMatrixf(m);
- }
- void myLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
- GLdouble centerx, GLdouble centery, GLdouble centerz,
- GLdouble upx, GLdouble upy, GLdouble upz)
- {
- /*gluLookAt(eyex, eyey, eyez,
- centerx, centery, centerz,
- upx, upy, upz); */// todo:
- GLfloat Fx = centerx-eyex;
- GLfloat Fy = centery-eyey;
- GLfloat Fz = centerz-eyez;
- GLfloat fxx, fyy, fzz;
- normalize (&fxx, &fyy, &fzz, Fx, Fy, Fz);
- GLfloat sx, sy, sz;
- cross (&sx, &sy, &sz, fxx, fyy, fzz, upx, upy, upz);
- GLfloat sxx, syy, szz;
- normalize (&sxx, &syy, &szz, sx, sy, sz);
- GLfloat ux, uy, uz;
- cross (&ux, &uy, &uz, sxx, syy, szz, fxx, fyy, fzz);
- GLfloat uxx, uyy, uzz;
- normalize (&uxx, &uyy, &uzz, ux, uy, uz);
- const GLfloat m[16] = {sxx, uxx, -fxx, 0,
- syy, uyy, -fyy, 0,
- szz, uzz, -fzz, 0,
- 0, 0, 0, 1};
- glMultMatrixf (m);
- myTranslate(-eyex, -eyey, -eyez);
- }
- void myRotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
- {
- //glRotatef(angle, x, y, z); // todo:
- GLfloat s = sin(deg2rad(angle));
- GLfloat c = cos(deg2rad(angle));
- GLfloat m[16] = {x*x*(1-c)+c, y*x*(1-c)+z*s, z*x*(1-c)-y*s, 0,
- x*y*(1-c)-z*s, y*y*(1-c)+c, z*y*(1-c)+x*s, 0,
- x*z*(1-c)+y*s, y*z*(1-c)+x*s, z*z*(1-c)+c, 0,
- 0, 0, 0, 1};
- glMultMatrixf(m);
- }
- void myScale(GLfloat x, GLfloat y, GLfloat z)
- {
- //glScalef(x, y, z); // todo:
- GLfloat m[16] = {x, 0, 0, 0,
- 0, y, 0, 0,
- 0, 0, z, 0,
- 0, 0, 0, 1};
- glMultMatrixf(m);
- }
- void init(void)
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_NORMALIZE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- }
- void display(void)
- {
- //Render objects
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glPushMatrix();
- myScale(5, 5, 5);
- glutSolidSphere(10, 20, 20); // the Sun
- glPopMatrix();
- myRotate(spin[Terra], 0, 1, 0);
- myTranslate(dist[Terra] * fdist, 0, 0);
- glutSolidSphere(radius[Terra] * fradius, 20, 20); // the Earth
- myRotate(spin[Moon], 0, 1, 0);
- myTranslate(dist[Moon] * fdist, 0, 0);
- glutSolidSphere(radius[Moon] * fradius, 20, 20); // the Moon
- glPopMatrix();
- glPushMatrix();
- myRotate(spin[Mercury], 0, 1, 0);
- myTranslate(dist[Mercury] * fdist, 0, 0);
- glutSolidSphere(radius[Mercury] * fradius, 20, 20); // the Mercury
- glPopMatrix();
- glPushMatrix();
- myRotate(spin[Venus], 0, 1, 0);
- myTranslate(dist[Venus] * fdist, 0, 0);
- glutSolidSphere(radius[Venus] * fradius, 20, 20); // the Venus
- glPopMatrix();
- glPushMatrix();
- myRotate(spin[Mars], 0, 1, 0);
- myTranslate(dist[Mars] * fdist, 0, 0);
- glutSolidSphere(radius[Mars] * fradius, 20, 20); // the Mars
- glPushMatrix();
- myRotate(spin[Phobos], 0, 1, 0);
- myTranslate(dist[Phobos] * fdist, 0, 0);
- glutSolidSphere(radius[Phobos] * fradius, 20, 20); // the Phobos
- glPopMatrix();
- glPushMatrix();
- myRotate(spin[Deimos], 0, 1, 0);
- myTranslate(dist[Deimos] * fdist, 0, 0);
- glutSolidSphere(radius[Deimos] * fradius, 20, 20); // the Deimos
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
- void timer(int value)
- {
- glutTimerFunc(value, timer,value);
- for (int i=0; i<7; i++)
- {
- spin[i] += 1/period[i] * fperiod;
- if (spin[i] >= 360) spin[i] -= 360;
- }
- glutPostRedisplay();
- }
- GLfloat xx=300,yy=300,zz=300;
- void reshape(int w, int h)
- {
- glViewport(0,0, (GLsizei) w, (GLsizei) h);
- aspect = (GLfloat) w / (GLfloat) h;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- myPerspective(45.0f, aspect, 0.1f, 1000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- myLookAt(xx,yy,zz, 0,0,0, 0,1,0);
- width = w;
- height = h;
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27: exit(0);
- case 'w': {if(yy>0) yy=-300; reshape (width,height);break;}
- case 's': {if(yy<0) yy=300; reshape (width,height);break;}
- case 'a': {if(xx>0) xx=-300; reshape (width,height);break;}
- case 'd': {if(xx<0) xx=300; reshape (width,height);break;}
- }
- }
- int main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(800, 600);
- glutInitWindowPosition(100,100);
- glutCreateWindow("Transforms");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutTimerFunc(delay, timer, delay);
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment