Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glut.h>
- static int day = 0;
- static int year = 0;
- static int months = 0;
- static bool hidden = true;
- static float dz = 0;
- static float dx = 0;
- static float dy = 0;
- void init(void)
- {
- glClearColor(0.0,0.0,0.0,0.0);
- glColor3f(1.0,1.0,1.0);
- }
- void display(void)
- {
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- if(hidden)
- glEnable(GL_DEPTH_TEST);
- else
- glDisable(GL_DEPTH_TEST);
- glPushMatrix();
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- GLfloat lightPos[] = {dx,dy,dz,1.0};
- glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- GLfloat mDiffs[] = {1.0,1.0,0.0,1.0};
- glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiffs);
- //sun
- glRotatef(year,0.0,1.0,0.0);
- glColor3f(1.0, 1.0,0.0);
- glutSolidSphere(1.0, 20, 16);
- //earth
- //glColor3f(0.0, 0.0,1.0);
- GLfloat mDiffe[] = {0.0,0.0,1.0,1.0};
- glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiffe);
- glTranslatef(2.0,0.0,0.0);
- glRotatef(day,0.0,1.0,0.0);
- glutSolidSphere(0.2, 10, 8);
- //moon
- GLfloat mDiff[] = {1.0,1.0,1.0,1.0};
- glMaterialfv(GL_FRONT,GL_DIFFUSE,mDiff);
- //glColor3f(1.0, 1.0,1.0);
- glRotatef(months,1.0,0.0,0.0);
- glTranslatef(0.0,0.5,0.0);
- glutSolidSphere(0.1, 10, 8);
- glPopMatrix();
- glutSwapBuffers();
- }
- void reshape (int w, int h)
- {
- glViewport (0,0, (GLsizei) w, (GLsizei)h);
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
- }
- void specialKey(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_UP:
- dz-=0.25;
- std::cout<<"X:Y:Z = "<<dx<<":"<<dy<<":"<<dz<<std::endl;
- glutPostRedisplay();
- break;
- case GLUT_KEY_DOWN:
- dz+=0.25;
- std::cout<<"X:Y:Z = "<<dx<<":"<<dy<<":"<<dz<<std::endl;
- glutPostRedisplay();
- break;
- }
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case 'd':
- day = (day + 15) % 360;
- glutPostRedisplay();
- break;
- case 'D':
- day = (day - 15) % 360;
- glutPostRedisplay();
- break;
- case 'y':
- year = (year + 30) % 360;
- glutPostRedisplay();
- break;
- case 'Y':
- year = (year - 30) % 360;
- glutPostRedisplay();
- break;
- case 'm':
- months = (months + 12) % 360;
- glutPostRedisplay();
- break;
- case 'M':
- months = (months - 12) % 360;
- glutPostRedisplay();
- break;
- case 'a':
- day = (day + 15) % 360;
- year = (year + 5) % 360;
- months = (months + 5) % 360;
- glutPostRedisplay();
- break;
- case 'h':
- hidden = !hidden;
- glutPostRedisplay();
- break;
- case 27:
- exit(0);
- break;
- default:
- break;
- }
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB| GLUT_DEPTH);
- glutInitWindowSize(600, 600);
- glutInitWindowPosition(50, 200);
- glutCreateWindow("OpenGL-XI");
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutSpecialFunc(specialKey);
- init();
- glutMainLoop();
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement