#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <SDL/SDL.h>
SDL_Surface* surface;
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_BPP 255
#define FALSE 0
#define TRUE 1
struct magic
{
GLuint year;
GLuint day;
GLuint texture[1];
}magic;
namespace SDL
{
int LoadGLTextures( )
{
/* Status indicator */
int Status = FALSE;
/* Create storage space for the texture */
SDL_Surface *TextureImage;
/* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */
if ( ( TextureImage= SDL_LoadBMP( "abb.bmp" ) ) )
{
/* Set the status to true */
Status = TRUE;
/* Create The Texture */
glGenTextures( 1, &magic.texture[0] );
/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D, magic.texture[0] );
/* Generate The Texture */
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->w , TextureImage->h, GL_BGR, GL_UNSIGNED_BYTE, TextureImage->pixels);
glGetError();
/* Linear Filtering */
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glEnable(GL_TEXTURE_2D);
}
/* Status indicator */
/* Create storage space for the texture */
/* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */
if ( ( TextureImage= SDL_LoadBMP( "moon.bmp" ) ) )
{
/* Set the status to true */
Status = TRUE;
/* Create The Texture */
glGenTextures( 1, &magic.texture[1] );
/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D, magic.texture[1] );
/* Generate The Texture */
glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage->w,
TextureImage->h, 0, GL_BGR,
GL_UNSIGNED_BYTE, TextureImage->pixels );
glGetError();
/* Linear Filtering */
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glEnable(GL_TEXTURE_2D);
}
}
};
namespace glut
{
int earth()
{
GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture( GL_TEXTURE_2D, magic.texture[0] );
gluSphere(sphere, 1.0, 20, 20);
}
int moon()
{
GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture( GL_TEXTURE_2D, magic.texture[1] );
gluSphere(sphere, 0.5, 20, 20);
}
void init(void)
{
GLfloat light_position[] = { 1.0, 1.0, -2.0, 0.0 };
GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
init();
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glRotatef (magic.year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef (magic.day, 0.0, 1.0, 0.0);
moon();
glPopMatrix();
glPushMatrix();
glRotatef (33.5, 3.5, 1.6, 1.0);
earth();
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 keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd':
magic.day = (magic.day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
magic.day = (magic.day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
magic.year = (magic.year + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
magic.year = (magic.year - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
};
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
SDL::LoadGLTextures();
glutDisplayFunc(glut::display);
glutReshapeFunc(glut::reshape);
glutKeyboardFunc(glut::keyboard);
glutMainLoop();
return 0;
}
No comments:
Post a Comment