#include <stdio.h>
#include <GL/glut.h>
#include <GL/glaux.h>
#include <cstdlib>
GLuint texture;
static GLfloat spin = 0.0;
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File=NULL;
if (!Filename) return NULL;
File=fopen(Filename, "rb");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
void LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage;
TextureImage = NULL;
if (TextureImage = LoadBMP("earth.bmp"))
{
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureImage->sizeX, TextureImage->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}
if(TextureImage)
{
if(TextureImage->data)
free(TextureImage->data);
free(TextureImage);
}
}
void init()
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 5.0 };
GLfloat light_position[] = { 10.0, 0.0, 10.0, 0.0 };
GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
void draw_sphere()
{
GLUquadricObj *quadratic = gluNewQuadric();
gluQuadricTexture(quadratic, GL_TRUE);
glBindTexture(GL_TEXTURE_2D, texture);
gluSphere(quadratic, 1.0, 48, 48);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float)w/(float)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glDrawBuffer(GL_BACK);
glPushMatrix();
glRotatef((GLfloat)spin, 0.0, 0.0, 1.0);
draw_sphere();
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = spin + 0.25;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'y':
spin = spin + 5;
if(spin > 360) spin -= 360;
glutPostRedisplay();
break;
case 'Y':
spin = spin - 5;
if(spin < 0) spin += 360;
glutPostRedisplay();
break;
case 'U':
case 'u':
glMatrixMode(GL_MODELVIEW);
glRotatef(5.0, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 'D':
case 'd':
glMatrixMode(GL_MODELVIEW);
glRotatef(-5.0, 1.0, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600,500);
glutInitWindowPosition(200,100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Earth");
init();
LoadGLTextures();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(spinDisplay);
glutKeyboardFunc(keyboard);
glutMainLoop();
glutIdleFunc(NULL);
return 0;
}