#include <iostream>
#include <fstream>
#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<stdlib.h>
#include<stdio.h>
#define FALSE 0
#define TRUE 1
#define KEY_ESCAPE 27
#define g_rotation_speed 0.2
float g_rotation;
#define FALSE 0
#define TRUE 1
using namespace std;
/********************************************************************************
C:\TDM-GCC-32>g++ -o menu.exe menu.cpp -lopengl32 -lglut32 -lglu32
C:\TDM-GCC-32>menu
*******************************************************************************/
/************************************************************************************
* TGA CLASS
to generate to fille moon.tga and earth.tga USE gimp program and export -> to tga format don't use paint
it isn't to program for grafic
************************************************************************************/
class TGA
{
public:
bool Load(const char *filename);
void Release();
void Draw();
char *imageData;
struct tga_header
{
unsigned char idLength;
unsigned char colorMapType;
unsigned char imageTypeCode;
unsigned char colorMapSpec[5];
unsigned short xOrigin;
unsigned short yOrigin;
unsigned short width;
unsigned short height;
unsigned char bpp;
unsigned char imageDesc;
};
tga_header tgaheader;
};
void TGA::Release()
{
free(imageData); // Free image data from memory
}
bool TGA::Load(const char* filename)
{
fstream filestr;
filestr.open (filename, ios::in | ios::binary);
if (filestr.is_open())
{
// read TGA header
filestr.read((char*) &tgaheader , sizeof(struct tga_header));
// read pixel data
int imageSize = tgaheader.width * tgaheader.height * tgaheader.bpp;
this->imageData = (char*) malloc(imageSize);
filestr.read((char*) this->imageData, imageSize);
/*
* TGA is stored in BGR (Blue-Green-Red) format,
* we need to convert this to Red-Green-Blue (RGB).
* The following section does BGR to RGB conversion
*/
if (tgaheader.bpp == 24)
{
for (int i = 0; i < imageSize; i+=3)
{
char c = this->imageData[i];
this->imageData[i] = this->imageData[i+2];
this->imageData[i+2] = c;
}
}
else
if (tgaheader.bpp == 32)
{
for (int i = 0; i < imageSize; i+=4)
{
// 32 bits per pixel = 4 byte per pixel
char c = this->imageData[i];
this->imageData[i] = this->imageData[i+2];
this->imageData[i+2] = c;
}
}
filestr.close();
}
else
{
cout << "Error opening file" << endl;
return -1;
}
return 0;
}
/************************************************************************************
* Rest of code
************************************************************************************/
class magic
{
public:
GLuint year;
GLuint day;
GLuint texture[1];
GLuint earthX;
GLuint earthY;
GLuint earthZ;
void earth();
void moon();
}control;
int LoadGLTextures( )
{
/* Status indicator */
int Status = FALSE;
TGA *TextureImage= new TGA;
/* Create storage space for the texture */
/* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */
TextureImage->Load( "2010.tga" );
/* Set the status to true */
Status = TRUE;
/* Create The Texture */
glGenTextures( 1, &control.texture[0] );
/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D,control.texture[0]);
/* Generate The Texture */
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,TextureImage->tgaheader.width ,TextureImage->tgaheader.height, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->imageData);
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 */
TextureImage->Load( "moon.tga" );
/* Set the status to true */
Status = TRUE;
/* Create The Texture */
glGenTextures( 2, &control.texture[1] );
/* Typical Texture Generation Using Data From The Bitmap */
glBindTexture( GL_TEXTURE_2D, control.texture[1] );
/* Generate The Texture */
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,TextureImage->tgaheader.width ,TextureImage->tgaheader.height, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->imageData);
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);
}
void magic::earth()
{
glPushMatrix();
GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
glRotatef (33, 27.0, 0, 0.0);
glBindTexture( GL_TEXTURE_2D, control.texture[0] );
gluSphere(sphere, 1.0, 20, 20);
glPopMatrix();
glFlush();
}
void magic::moon()
{
glPushMatrix();
GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
glBindTexture( GL_TEXTURE_2D, control.texture[1] );
glRotatef (control.year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef (control.day, 0.0, 1.0, 0.0);
gluSphere(sphere, 0.4, 20, 20);
glPopMatrix();
glFlush();
}
void display(void)
{
magic *wizird= new magic;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
if( (control.year<180) ){
wizird->moon();
wizird->earth();
}
else{
wizird->earth();
wizird->moon();
}
glutSwapBuffers();
}
void initialize ()
{
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 keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'd':
control.day = (control.day + 10) % 360;
glutPostRedisplay();
break;
case 'D':
control.day = (control.day - 10) % 360;
glutPostRedisplay();
break;
case 'y':
control.year = (control.year + 5) % 360;
glutPostRedisplay();
break;
case 'k':
control.earthX = control.earthX - 65;
glutPostRedisplay();
break;
case 's':
control.earthY = control.earthY - 65;
glutPostRedisplay();
break;
case 'a':
control.earthZ = control.earthZ - 65;
glutPostRedisplay();
break;
default:
break;
}
}
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);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
initialize();
LoadGLTextures( );
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
No comments:
Post a Comment