OPENGL 纹理贴图 旋转立方体(Nehe glut版)

#define _CRT_SECURE_NO_WARNINGS
#pragma comment (lib,"glaux.lib")
#include <windows.h>
#include <gl\gl.h>
#include <stdio.h>
#include <time.h>
#include <gl\glu.h>
#include <gl\glut.h>
#include <gl\glaux.h>
#include <stdlib.h>
GLuint texture[1];    //存储纹理数据
GLfloat x = 0.0f,y = 0.0f,z = 0.0f;


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 NULL;
   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[1];      // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*1);           // Set The Pointer To NULL

// 装入bmp格式图像,并检查错误,工程下创建的文件夹data
if (TextureImage[0]=LoadBMP("data/yazi.bmp"))
{
   Status=TRUE;          // Set The Status To TRUE
   glGenTextures(1, &texture[0]);      // 创建纹理
   glBindTexture(GL_TEXTURE_2D, texture[0]);
   glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}

if (TextureImage[0])          // If Texture Exists
{
   if (TextureImage[0]->data)        // If Texture Image Exists
   {
    free(TextureImage[0]->data);      // Free The Texture Image Memory
   }

   free(TextureImage[0]);         // Free The Image Structure
}

return Status;           // Return The Status
}

void reshape(int width,int height){
glViewport(0,0,(GLsizei)width,(GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,1,2,-5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,6.0,0.0,0.0,0.0,0.0,1.0,0.0);//设置眼睛
}

void display(void){
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glRotatef(x,1.0,0.0,0.0);
glRotatef(y,0.0,1.0,0.0);
glRotatef(z,0.0,0.0,1.0);
glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
    glColor3f(1.0,1.0,1.0);
   // 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);
   // 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);
   // 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);
   // 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);
   // 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);
   // 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();
glPopMatrix();
    glutSwapBuffers();
   x += 0.3f;
    y += 0.2f;
    z += 0.4f;

}

int Init(void){
if (!LoadGLTextures())         // Jump To Texture Loading Routine ( NEW )
{
   return 0;          // 没有加载成功
}
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
return 1;
}
int main(int argc,char ** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(300,400);
glutInitWindowSize(600,600);
glutCreateWindow("...");
Init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}




你可能感兴趣的:(OPENGL 纹理贴图 旋转立方体(Nehe glut版))