这里使用第九课中的Texture.h和Texture.cpp来实现对一个正方体的六面的纹理效果:
需要注意的代码就是相关纹理映射的部分;
(1)代码部分省去了Texture.h和Texture.cpp,因此运行时候需要先把这两个加入到工程目录下。
(2)需要在工程目录下建立一个名为data的文件夹,存放需要作为纹理的图片。
下面看主程序部分:
+_________________________________________________________________________________________________
#include <windows.h> // Windows的头文件
#include <gl/glew.h> // 包含最新的gl.h,glu.h库
#include <gl/glut.h> // 包含OpenGL实用库
#include"Texture.h" // 包含上一课中提供的头文件
//下列三行是自定义的变量 :
#define MAXTEXTURE 1 // 定义纹理贴图数量
GLuint texture[MAXTEXTURE]; // 纹理数组,保存纹理名字(name)
static float angle=0.0; //旋转角度
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // 重置OpenGL窗口大小
{
glViewport(0, 0, (GLsizei)(width), (GLsizei)(height)); // 重置当前视口大小
glMatrixMode(GL_PROJECTION); // 切换到投影矩阵模式
glLoadIdentity(); // 重置投影矩阵
gluPerspective(45, (float)width/(float)height, 0.1, 100); // 设置透视投影
glMatrixMode(GL_MODELVIEW); // 切换到模型视图矩阵
glLoadIdentity(); // 重置模型视图矩阵
}
void InitGL(GLvoid) // 此处开始对OpenGL进行所有设置
{
//*******此处根据具体绘制环境初始化:
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
//启用2D纹理映射:
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 最精细的透视计算
//下面这一行就体现了使用texture.h的好处:直接调用就能生成纹理,并保存在texture[0]中。
//当然,可以多次调用,生成多个纹理,如texture[1],texture[2]。。。
BuildTexture("data/waterfall.jpg", texture[0]);
}
void DrawTextureCube(float xPos,float yPos,float zPos)
{
glPushMatrix();
glTranslatef(xPos,yPos,zPos);
//顶面:
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(-0.5,0.5,0.5);//纹理坐标
glTexCoord2f(1.0,0.0);glVertex3f(0.5,0.5,0.5);
glTexCoord2f(1.0,1.0);glVertex3f(0.5,0.5,-0.5);
glTexCoord2f(0.0,1.0);glVertex3f(-0.5,0.5,-0.5);
glEnd();
//正面:
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(0.5,-0.5,0.5);
glTexCoord2f(1.0,0.0);glVertex3f(0.5,0.5,0.5);
glTexCoord2f(1.0,1.0);glVertex3f(-0.5,0.5,0.5);
glTexCoord2f(0.0,1.0);glVertex3f(-0.5,-0.5,0.5);
glEnd();
//右面:
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(0.5,0.5,-0.5);
glTexCoord2f(1.0,0.0);glVertex3f(0.5,0.5,0.5);
glTexCoord2f(1.0,1.0);glVertex3f(0.5,-0.5,0.5);
glTexCoord2f(0.0,1.0);glVertex3f(0.5,-0.5,-0.5);
glEnd();
//左面
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(-0.5,-0.5,0.5);
glTexCoord2f(1.0,0.0);glVertex3f(-0.5,0.5,0.5);
glTexCoord2f(1.0,1.0);glVertex3f(-0.5,0.5,-0.5);
glTexCoord2f(0.0,1.0);glVertex3f(-0.5,-0.5,-0.5);
glEnd();
//底面:
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(0.5,-0.5,0.5);
glTexCoord2f(1.0,0.0);glVertex3f(-0.5,-0.5,0.5);
glTexCoord2f(1.0,1.0);glVertex3f(-0.5,-0.5,-0.5);
glTexCoord2f(0.0,1.0);glVertex3f(0.5,-0.5,-0.5);
glEnd();
//后面:
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);glVertex3f(0.5,0.5,-0.5);
glTexCoord2f(1.0,0.0);glVertex3f(0.5,-0.5,-0.5);
glTexCoord2f(1.0,1.0);glVertex3f(-0.5,-0.5,-0.5);
glTexCoord2f(0.0,1.0);glVertex3f(-0.5,0.5,-0.5);
glEnd();
glPopMatrix();
}
void DrawGLScene(GLvoid) // 从这里开始进行所有的绘制
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-3.0);
glRotatef(angle,1.0,0.0,0.0);
glRotatef(angle,0.0,1.0,0.0);
glRotatef(angle,0.0,0.0,1.0);
DrawTextureCube(0.0,0.0,0.0);
glutSwapBuffers();
}
void MyIdle()
{
if(angle>=360)
angle=0.0;
angle+=0.2;
DrawGLScene();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow(" OpenGL程序:纹理映射");
InitGL();
glutDisplayFunc(DrawGLScene);
glutReshapeFunc(ReSizeGLScene);
glutIdleFunc(MyIdle);
glutMainLoop();
return 0;
}
----------------------------------------------------------------------------------