#include "windows.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdio.h>
bool myinit(void);
int LoadGLTextures();
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK stepDisplay(void);
void CALLBACK startIdleFunc(AUX_EVENTREC* event);
void CALLBACK stopIdleFunc(AUX_EVENTREC* event);
void CALLBACK display(void);
GLfloat xrot = 0; // X Rotation
GLfloat yrot = 0; // Y Rotation
GLfloat zrot = 0; // Z Rotation
GLuint texture[6]; // Storage For Six Texture
bool myinit(void)
{
if (!LoadGLTextures()) // Jump To Texture Loading Routine
{
return FALSE; // If Texture Didn't Load Return FALSE
}
GLfloat fogColor[4] = { 0.2, 0.3, 0.6, 1.0 };
glEnable(GL_FOG);
{
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogfv(GL_FOG_COLOR, fogColor);
glFogf(GL_FOG_START, 3.0);
glFogf(GL_FOG_END, 15.0);
glHint(GL_FOG_HINT, GL_DONT_CARE);
}
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.1f, 0.8f, 0.5f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LESS); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE;
}
AUX_RGBImageRec* LoadBMP(char* Filename) // Loads A Bitmap Image
{
FILE* File = NULL; // File Handle
if (!Filename) // Make Sure A Filename Was Given
{
return NULL; // If Not Return NULL
}
File = fopen(Filename, "r"); // Check To See If The File Exists
if (File) // Does The File Exist?
{
fclose(File); // Close The Handle
return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer
}
return NULL; // If Load Failed Return NULL
}
int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status = FALSE; // Status Indicator
AUX_RGBImageRec* TextureImage[6]; // Create Storage Space For The Texture
memset(TextureImage, 0, sizeof(void*) * 6); // Set The Pointer To NULL
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
if ((TextureImage[0] = LoadBMP("Data/NeHe.bmp"))
&& (TextureImage[1] = LoadBMP("Data/1.bmp"))
&& (TextureImage[2] = LoadBMP("Data/2.bmp"))
&& (TextureImage[3] = LoadBMP("Data/3.bmp"))
&& (TextureImage[4] = LoadBMP("Data/4.bmp"))
&& (TextureImage[5] = LoadBMP("Data/5.bmp")))
{
Status = TRUE; // Set The Status To TRUE
for (int i = 0; i < 6; i++)
{
glGenTextures(i + 1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);
//glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[i]->sizeX, TextureImage[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[i]->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
for (int i = 0; i < 6; i++)
{
if (TextureImage[i]) // If Texture Exists
{
if (TextureImage[i]->data) // If Texture Image Exists
{
free(TextureImage[i]->data); // Free The Texture Image Memory
}
free(TextureImage[i]); // Free The Image Structure
}
}
}
return Status; // Return The Status
}
//set rotate values
void CALLBACK stepDisplay(void)
{
xrot += 1.0f;
if (xrot > 360.0)
xrot = xrot - 360.0;
yrot += 2.0f;
if (xrot > 360)
yrot = yrot - 360;
zrot += 3.0f;
if (zrot > 360.0)
zrot = zrot - 360.0;
display();
}
//when press leftmouse down ,start the rotation
void CALLBACK startIdleFunc(AUX_EVENTREC* event)
{
auxIdleFunc(stepDisplay);
}
//when press rightmouse down ,stop the rotation
void CALLBACK stopIdleFunc(AUX_EVENTREC* event)
{
auxIdleFunc(0);
}
void CALLBACK display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(0.0f, 0.0f, -10.0f);
glRotatef(yrot, 0.0f, 1.0f, 0.0f);
glRotatef(xrot, 1.0f, 0.0f, 0.0f);
glRotatef(zrot, 0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_QUADS);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[3]);
glBegin(GL_QUADS);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[4]);
glBegin(GL_QUADS);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[5]);
glBegin(GL_QUADS);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glFlush();
auxSwapBuffers();/* 交换缓存 */
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.5, 1.5, -1.5 * (GLfloat)h / (GLfloat)w, 1.5 * (GLfloat)h / (GLfloat)w, -15.0, 15.0);
else
glOrtho(-1.5 * (GLfloat)w / (GLfloat)h, 1.5 * (GLfloat)w / (GLfloat)h, -1.5, 1.5, -15.0, 15.0);
//gluPerspective(25.0f,(GLfloat)w/(GLfloat)h,1.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main(void)
{
auxInitDisplayMode(AUX_DOUBLE | AUX_RGBA);
auxInitPosition(0, 0, 640, 480);
auxInitWindow("立方体纹理贴图");
myinit();
auxReshapeFunc(myReshape);
auxIdleFunc(stepDisplay);
auxMouseFunc(AUX_LEFTBUTTON, AUX_MOUSEDOWN, startIdleFunc);
auxMouseFunc(AUX_RIGHTBUTTON, AUX_MOUSEDOWN, stopIdleFunc);
auxMainLoop(display);
}
- 1
- 2
- 3
- 4
- 5
前往页