#include <glut.h> /* glut.h includes gl.h and glu.h*/
#include<GLAUX.H>
#include <stdio.h>
#include <windows.h>
#include<SOIL.h>
GLfloat angle = 0;
GLuint texture[6]; //分别存储三种方式的纹理(mipmap、线性、临近过滤)
AUX_RGBImageRec *LoadBMP(CHAR *Filename) //载入位图图象
{
FILE *File=NULL; //文件句柄
if (!Filename) //确保文件名已提供
{
return NULL; //如果没提供,返回 NULL
}
File=fopen(Filename,"r"); //尝试打开文件
if (File) //文件存在
{
fclose(File); //关闭句柄
return auxDIBImageLoadA(Filename); //载入位图并返回指针
}
return NULL; //如果载入失败,返回 NULL
}
void LoadGLTextures() //载入位图(调用上面的代码)并转换成纹理
{
//方式一:mipmap
texture[0] = SOIL_load_OGL_texture( "pic1.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_MIPMAPS);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
texture[1] = SOIL_load_OGL_texture( "pic2.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_MIPMAPS);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
//方式二:线形滤波
texture[2] = SOIL_load_OGL_texture( "pic3.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
glBindTexture(GL_TEXTURE_2D, texture[2]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //线形滤波
texture[3] = SOIL_load_OGL_texture( "pic4.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
glBindTexture(GL_TEXTURE_2D, texture[3]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); //线形滤波
//方式三:临近过滤
texture[4] = SOIL_load_OGL_texture( "pic5.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
glBindTexture(GL_TEXTURE_2D, texture[4]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); //临近过滤
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //临近过滤
texture[5] = SOIL_load_OGL_texture( "pic6.bmp",SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
glBindTexture(GL_TEXTURE_2D, texture[5]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); //临近过滤
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //临近过滤
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存
glLoadIdentity(); //重置当前的模型观察矩阵
glOrtho(-3.0, 3.0, -3.0, 3.0, -3.0, 3.0);
glRotatef(angle,2,3,1);
// 前面
glBindTexture(GL_TEXTURE_2D, texture[0]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, 1.0); //纹理和四边形的左下
glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, -1.0, 1.0); //纹理和四边形的右下
glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, 1.0, 1.0); //纹理和四边形的右上
glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, 1.0, 1.0); //纹理和四边形的左上
glEnd();
// 后面
glBindTexture(GL_TEXTURE_2D, texture[1]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, -1.0, -1.0); //纹理和四边形的右下
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); //纹理和四边形的右上
glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, 1.0, -1.0); //纹理和四边形的左上
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); //纹理和四边形的左下
glEnd();
// 顶面
glBindTexture(GL_TEXTURE_2D, texture[2]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); //纹理和四边形的左上
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, 1.0, 1.0); //纹理和四边形的左下
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, 1.0, 1.0); //纹理和四边形的右下
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0); //纹理和四边形的右上
glEnd();
// 底面
glBindTexture(GL_TEXTURE_2D, texture[3]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); //纹理和四边形的右上
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); //纹理和四边形的左上
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, -1.0, 1.0); //纹理和四边形的左下
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, -1.0, 1.0); //纹理和四边形的右下
glEnd();
// 右面
glBindTexture(GL_TEXTURE_2D, texture[4]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0, -1.0); //纹理和四边形的右下
glTexCoord2f(1.0, 1.0); glVertex3f( 1.0, 1.0, -1.0); //纹理和四边形的右上
glTexCoord2f(0.0, 1.0); glVertex3f( 1.0, 1.0, 1.0); //纹理和四边形的左上
glTexCoord2f(0.0, 0.0); glVertex3f( 1.0, -1.0, 1.0); //纹理和四边形的左下
glEnd();
// 左面
glBindTexture(GL_TEXTURE_2D, texture[5]); //选择纹理
glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0, -1.0); //纹理和四边形的左下
glTexCoord2f(1.0, 0.0); glVertex3f(-1.0, -1.0, 1.0); //纹理和四边形的右下
glTexCoord2f(1.0, 1.0); glVertex3f(-1.0, 1.0, 1.0); //纹理和四边形的右上
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0, 1.0, -1.0); //纹理和四边形的左上
glEnd();
angle += 0.05;
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glCullFace(GL_BACK); //背面裁剪(背面不可见)
glEnable(GL_CULL_FACE); //启用裁剪
glEnable(GL_TEXTURE_2D);
LoadGLTextures(); //载入纹理贴图
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100,100);
glutCreateWindow("C3");
init ();
LoadGLTextures();
glutDisplayFunc(display);
//glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
评论1