纹理映射
为在场景中添加纹理映射,需要执行以下步骤。
(1).纹理控制,定义纹理如何包裹物体的表面;
(2).定义纹理,利用矩形图像进行贴图是二维纹理贴图中常用的方法;
(3).纹理映射方式,用函数可以设置纹理映射的方式。
(4).纹理坐标,定义纹理与几何坐标对应关系的纹理坐标;
颜色贴图
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
using namespace std;
// 评测代码所用头文件-结束
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLuint texName;
void makeCheckImage(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++)
{
for (j = 0; j < checkImageWidth; j++)
{
c = (((i & 0x8) == 0 ^ ((j & 0x8)) == 0)) * 255;
checkImage[i][j][0] = (GLubyte)c;
checkImage[i][j][1] = (GLubyte)c;
checkImage[i][j][2] = (GLubyte)c;
checkImage[i][j][3] = (GLubyte)255;
}
}
}
void init(void)
{
glClearColor(0.5, 2.0, 0.5, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeCheckImage();
glBindTexture(GL_TEXTURE_2D, texName);
//********1.请输入代码设置控制纹理映射函数***************//
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//******************************************************//
//********2.请输入代码设置纹理的定义函数*****************//
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
//******************************************************//
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
//********3.请输入代码进行纹理映射方式设置*********//
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//**********************************************//
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
//********4.请输入代码进行纹理坐标设置**********//
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
//***************************************** **//
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, ((GLsizei)w / (GLsizei)h), 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("几何变换示例");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
贴图映射
// 提示:在合适的地方修改或添加代码
#include <GL/glut.h>
#include<stdlib.h>
#include <cstdio>
#include<iostream>
using namespace std;
// 评测代码所用头文件-结束
#define WindowWidth 400
#define WindowHeight 400
#define WindowTitle "OpenGL纹理测试"
#define _CRT_SECURE_NO_WARNINGS
//定义两个纹理对象编号
GLuint texGround;
GLuint texWall;
#define BMP_Header_Length 54 //图像数据在内存块中的偏移量
static GLfloat angle = 220.0f; //旋转角度
// 函数power_of_two用于判断一个整数是不是2的整数次幂
int power_of_two(int n)
{
if (n <= 0)
return 0;
return (n & (n - 1)) == 0;
}
/* 函数load_texture
* 读取一个BMP文件作为纹理
* 如果失败,返回0,如果成功,返回纹理编号
*/
GLuint load_texture(const char* file_name)
{
GLint width, height, total_bytes;
GLubyte* pixels = 0;
GLuint last_texture_ID = 0, texture_ID = 0;
// 打开文件,如果失败,返回
FILE* pFile = fopen(file_name, "rb");
if (pFile == 0)
return 0;
// 读取文件中图象的宽度和高度
fseek(pFile, 0x0012, SEEK_SET);
fread(&width, 4, 1, pFile);
fread(&height, 4, 1, pFile);
fseek(pFile, BMP_Header_Length, SEEK_SET);
printf("w=%d h=%d\n", width, height);
// 计算每行像素所占字节数,并根据此数据计算总像素字节数
{
GLint line_bytes = width * 3;
while (line_bytes % 4 != 0)
++line_bytes;
total_bytes = line_bytes * height;
}
// 根据总像素字节数分配内存
pixels = (GLubyte*)malloc(total_bytes);
if (pixels == 0)
{
cout << "pixels" << total_bytes << endl;
fclose(pFile);
return 0;
}
// 读取像素数据
if (fread(pixels, total_bytes, 1, pFile) <= 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 对就旧版本的兼容,如果图象的宽度和高度不是的整数次方,则需要进行缩放
// 若图像宽高超过了OpenGL规定的最大值,也缩放
{
GLint max;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
if (!power_of_two(width)
|| !power_of_two(height)
|| width > max
|| height > max)
{
const GLint new_width = 256;
const GLint new_height = 256; // 规定缩放后新的大小为边长的正方形
GLint new_line_bytes, new_total_bytes;
GLubyte* new_pixels = 0;
// 计算每行需要的字节数和总字节数
new_line_bytes = new_width * 3;
while (new_line_bytes % 4 != 0)
++new_line_bytes;
new_total_bytes = new_line_bytes * new_height;
// 分配内存
new_pixels = (GLubyte*)malloc(new_total_bytes);
if (new_pixels == 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 进行像素缩放
gluScaleImage(GL_RGB,
width, height, GL_UNSIGNED_BYTE, pixels,
new_width, new_height, GL_UNSIGNED_BYTE, new_pixels);
// 释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height
free(pixels);
pixels = new_pixels;
width = new_width;
height = new_height;
}
}
// 分配一个新的纹理编号
glGenTextures(1, &texture_ID);
if (texture_ID == 0)
{
free(pixels);
fclose(pFile);
return 0;
}
// 绑定新的纹理,载入纹理并设置纹理参数
// 在绑定前,先获得原来绑定的纹理编号,以便在最后进行恢复
GLint lastTextureID = last_texture_ID;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &lastTextureID);
glBindTexture(GL_TEXTURE_2D, texture_ID);
//********1.请输入代码设置纹理映射控制函数********//
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//**********************************************//
//*********2.请输入代码设置纹理的定义函数*********//
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels);
//**********************************************//
//********3.请输入代码进行纹理映射方式设置*********//
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//**********************************************//
glBindTexture(GL_TEXTURE_2D, lastTextureID); //恢复之前的纹理绑定
free(pixels);
return texture_ID;
}
void display(void)
{
// 清除屏幕
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 设置视角
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, 1, 1, 21);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(13, 5, 5, 0, 0, 0, 0, 0, 1);
glRotatef(angle, 0.0f, 0.0f, 1.0f); //旋转
// 绘制底面以及纹理
glBindTexture(GL_TEXTURE_2D, texGround);
glBegin(GL_QUADS);
//**********4.1请输入代码进行底面纹理坐标设置******//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-6.0f, -3.0f, 0.0f);//右边近点坐标
glTexCoord2f(0.0f, 3.0f); glVertex3f(-6.0f, 7.0f, 0.0f); //左边近点坐标
glTexCoord2f(3.0f, 3.0f); glVertex3f(6.0f, 7.0f, 0.0f); //左边远点坐标
glTexCoord2f(3.0f, 0.0f); glVertex3f(6.0f, -3.0f, 0.0f); //右边远点坐标
//**********************************************//
glEnd();
// 绘制立面
glBindTexture(GL_TEXTURE_2D, texWall);
glBegin(GL_QUADS);
//*******4.2请输入代码进行右侧矩形纹理坐标设置*****//
glTexCoord2f(0.0f, 0.0f); glVertex3f(-6.0f, -3.0f, 0.0f); //下边近点坐标
glTexCoord2f(0.0f, 1.0f); glVertex3f(-6.0f, -3.0f, 5.0f); //上边近点坐标
glTexCoord2f(2.0f, 1.0f); glVertex3f(6.0f, -3.0f, 5.0f); //上边远点坐标
glTexCoord2f(2.0f, 0.0f); glVertex3f(6.0f, -3.0f, 0.0f); //下边远点坐标
//**********************************************//
glEnd();
//绘制另外一个立面
glBindTexture(GL_TEXTURE_2D, texWall);
glBegin(GL_QUADS);
//*****4.3请输入代码进行左侧矩形纹理坐标设置******//
glTexCoord2f(0.0f, 0.0f); glVertex3f(6.0f, 7.0f, 0.0f); //下左边坐标
glTexCoord2f(0.0f, 1.0f); glVertex3f(6.0f, 7.0f, 5.0f); //上左坐标
glTexCoord2f(2.0f, 1.0f); glVertex3f(6.0f, -3.0f, 5.0f); //上右坐标
glTexCoord2f(2.0f, 0.0f); glVertex3f(6.0f, -3.0f, 0.0f); //下右边坐标
//**********************************************//
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("几何变换示例");
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); // 启用纹理
texGround = load_texture("ground1.bmp"); //加载纹理
texWall = load_texture("wall1.bmp");
glutDisplayFunc(&display); //注册函数
glutMainLoop();
return 0;
}