Code:
#include "stdafx.h"
#include <gl/glut.h>
float angle=180;
int direction;
//Functions-----------------------------------------------------------
void init (void);
void reshape (int width, int height);
void drawxx(void);
void keyboard (unsigned char key, int x, int y);
//--------------------------------------------------------------------
void main (int argc, char** argv)
{
glutInit (&argc, argv);
init ();
glutDisplayFunc (drawxx);
glutFullScreen ();
glutKeyboardFunc (keyboard);
glutReshapeFunc (reshape);
glutIdleFunc (drawxx);
glutMainLoop ( );
}
//--------------------------------------------------------------------
void init (void)
{
glutInitDisplayMode (GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow ("cxxxx");
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glEnable (GL_COLOR_MATERIAL);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
}
//--------------------------------------------------------------------
void reshape (int width, int height)
{
if (height ==0)
{height = 1;}
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45.0f, (GLfloat) width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
//--------------------------------------------------------------------
void drawxx (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//Make the moon-------------------------------------------------------
glLoadIdentity ();
glTranslatef (-2.0f, 1.5f, -7.0f); //left side
glColor3f (0.95, 0.95, 0.95);
glRotatef (-30, 0.0f, 0.0f, 1.0f);
GLUquadricObj *disk1;
disk1 = gluNewQuadric ();
gluQuadricDrawStyle (disk1, GLU_FILL);
gluPartialDisk (disk1, 0, 0.4, 90, 9, 180, 180);
glTranslatef(0.0f, 0.0f, 0.0f); //right side
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
if (angle > 90 && angle < 180)
{glColor3f (0.95, 0.95, 0.95);}
else
{glColor3f (0.0, 0.0, 0.0);}
GLUquadricObj *disk2;
disk2 = gluNewQuadric ();
gluQuadricDrawStyle (disk2, GLU_FILL);
gluPartialDisk (disk2, 0, 0.4, 90, 9, 180, 180);
//Make the sky--------------------------------------------------------
//Sunrise&Sunset------------------------------------------------------
float alphasunset = (angle -60)/30;
glLoadIdentity ();
glTranslatef (0.0f, 0.0f, -6.5f);
glBegin (GL_QUAD_STRIP);
glColor4f (.97, .33, .46, alphasunset);
glVertex3f (-4.0f, -1.7f, 0.0f);
glVertex3f (4.0f, -1.7f, 0.0f);
glColor4f (.92, .92, .56, alphasunset);
glVertex3f (-4.0f, -0.7f, 0.0f);
glVertex3f (4.0f, -0.7f, 0.0f);
glColor4f (.79, .86, .95, alphasunset);
glVertex3f (-4.0f, 0.5f, 0.0f);
glVertex3f (4.0f, 0.5f, 0.0f);
glColor4f (.27, .42, .62, alphasunset);
glVertex3f (-4.0f, 3.5f, 0.0f);
glVertex3f (4.0f, 3.5f, 0.0f);
glEnd ();
//Blue Sky------------------------------------------------------------
float alphablue = (angle - 100)/40;
glLoadIdentity ();
glTranslatef (0.0f, 0.0f, -6.4f);
glBegin (GL_QUADS);
glColor4f (.46f, .65f, .92f, alphablue);
glVertex3f (-4.0f, -2.5, 0.0f);
glVertex3f (4.0f, -2.5f, 0.0f);
glVertex3f (4.0f, 3.5f, 0.0f);
glVertex3f (-4.0f, 3.5f, 0.0f);
glEnd ();
//Ground--------------------------------------------------------------
glLoadIdentity ();
glTranslatef (0.0f, -2.0f, 0.0f);
glColor3f (.1, 0.6, 0.1);
glBegin (GL_QUADS);
glVertex3f (-4.0f, 0.0f, 0.0f);
glVertex3f (4.0f, 0.0f, 0.0f);
glVertex3f (4.0f, 0.4f, -6.3f);
glVertex3f (-4.0f, 0.4f, -6.3f);
glEnd ();
//Sun-----------------------------------------------------------------
float sunb;
if (angle > 110)
{sunb = 0.3;}
else
{sunb = 0.3 + (110-angle)/40*0.3;}
float sunr = 1.0;
float sung = (angle - 70)/50;
float sunx = 2.0;
float suny = -6 + (angle / 180 * 8);
float sunz = -6.3;
glColor3f (sunr, sung, sunb);
glLoadIdentity ();
glColor3f (sunr, sung, sunb);
glTranslatef (sunx, suny, sunz);
GLUquadricObj *disk3;
disk3 = gluNewQuadric ();
gluQuadricDrawStyle (disk3, GLU_FILL);
gluDisk (disk3, 0, .45, 90, 90);
//if(night)
//{
glTranslatef (0.0, 0.6, 0.0);
glColor3f (.90, .90, .90);
glutSolidSphere (0.5, 50.0, 50.0);
glLineWidth (5);
glColor3f (0.7, 0.7, 0.7);
glRotatef (90, 1.0, 0.0, 0.0);
glutWireSphere (0.51, 16, 12);
glLineWidth (1);
glTranslatef (0.5, -0.9, .75); //base
glColor3f (.93, 0.91, 0.85);
GLUquadricObj *cylinder1;
cylinder1 = gluNewQuadric ();
gluCylinder(cylinder1, .10f, .20f, .2f, 60, 60);
glTranslatef (0.0, 0.0, -1.2);
GLUquadricObj *cylinder2;
cylinder2 = gluNewQuadric ();
gluCylinder(cylinder2, .10f, .10f, 1.2f, 60, 60);
glTranslatef (0.0, 0.0, -0.2);
GLUquadricObj *cylinder3; cylinder3 = gluNewQuadric ();
gluCylinder(cylinder3, .20f, .10f, 0.2f, 60, 60);
glTranslatef (0.0, 0.0, -0.2);
GLUquadricObj *cylinder5;
cylinder5 = gluNewQuadric ();
gluCylinder(cylinder5, .20f, .20f, 0.2f, 60, 60);
glTranslatef (0.0, 0.0, -0.4);
GLUquadricObj *cylinder4;
cylinder4 = gluNewQuadric ();
gluCylinder(cylinder4, .10f, .10f, 0.6f, 60, 60);
glRotatef (180, 1.0, 0.0, 0.0);
glTranslatef (0.0, 0.0, -0.0);
glLineWidth (3);
glColor3f (0.7, 0.7, 0.7);
glutWireCone (.09, 0.6, 6, 1);
glLineWidth (1);
float malpha;
if (direction % 2 == 0 && (angle < 90 && angle > 40))
{malpha = 0.6;}
else
{malpha = 0.0;}
glColor4f (0.0, 1.0, 0.0, malpha);
glutSolidCone (.08, 0.6, 6, 1);
//}
//Animation Counter---------------------------------------------------
glPopMatrix();
if (direction % 5)
{angle +=.9;}
else
{angle -=1.5;}
if (angle < 0.1)
{direction += 1;}
if (angle >179.9)
{direction += 1;}
glutSwapBuffers( );
}
void keyboard (unsigned char key, int x, int y) // Create Keyboard Function
{
switch (key) {
case 27: // Escape key terminates the program
exit (0);
break;
default:
break;
}
}