纹理滤波:
简单说就是纹理放大、缩小时采用的纹理算法计算纹理颜色值方式 。
使用函数glTexParameteri() 设置纹理参数。
代码如下:
设置纹理滤波参数
static unsigned createTexture(int w, int h, const void* data) {
unsigned texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
// --- 放大滤波 2种
//#define GL_NEAREST 0x2600 最近点采样
//#define GL_LINEAR 0x2601 线性采样
// --- 缩小滤波 6种
//#define GL_NEAREST 0x2600 最近点采样
//#define GL_LINEAR 0x2601 线性采样
//#define GL_NEAREST_MIPMAP_NEAREST 0x2700 mipmap最近点采样
//#define GL_LINEAR_MIPMAP_NEAREST 0x2701 mipmap线性采样
//#define GL_NEAREST_MIPMAP_LINEAR 0x2702
//#define GL_LINEAR_MIPMAP_LINEAR 0x2703
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
return texId;
}
绘制
static void render(GLFWwindow * window) {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//指定数据 顶点坐标 对应 纹理坐标
Vertex cubeVertices[] =
{
{ -1.0f, -1.0f, 1.0f, 0, 0 },
{ 1.0f, -1.0f, 1.0f, 1, 0 },
{ 1.0f, 1.0f, 1.0f, 1, 1 },
{ -1.0f, 1.0f, 1.0f, 0, 1 },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ -1.0f, 1.0f, -1.0f, 1, 0 },
{ 1.0f, 1.0f, -1.0f, 1, 1 },
{ 1.0f, -1.0f, -1.0f, 0, 1 },
{ -1.0f, 1.0f, -1.0f, 0, 0 },
{ -1.0f, 1.0f, 1.0f, 1, 0 },
{ 1.0f, 1.0f, 1.0f, 1, 1 },
{ 1.0f, 1.0f, -1.0f, 0, 1 },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ 1.0f, -1.0f, -1.0f, 1, 0 },
{ 1.0f, -1.0f, 1.0f, 1, 1 },
{ -1.0f, -1.0f, 1.0f, 0, 1 },
{ 1.0f, -1.0f, -1.0f, 0, 0 },
{ 1.0f, 1.0f, -1.0f, 1, 0 },
{ 1.0f, 1.0f, 1.0f, 1, 1 },
{ 1.0f, -1.0f, 1.0f, 0, 1 },
{ -1.0f, -1.0f, -1.0f, 0, 0 },
{ -1.0f, -1.0f, 1.0f, 1, 0 },
{ -1.0f, 1.0f, 1.0f, 1, 1 },
{ -1.0f, 1.0f, -1.0f, 0, 1 },
};
glMatrixMode(GL_MODELVIEW);
#if 0
glEnable(GL_DEPTH_TEST);
glInterleavedArrays( GL_T2F_V3F, sizeof(Vertex), cubeVertices );
#else
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &cubeVertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &cubeVertices[0].u);
#endif
//绘制第一个立方体
// 清成单位矩阵
glLoadIdentity();
// 产生一个矩阵
glTranslatef(-2, 0, -3);
for(int i = 0; i < 6; ++i) {
glBindTexture(GL_TEXTURE_2D, _texture[i%2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//纹理采用线性采样
glDrawArrays(GL_QUADS, i * 4, 4);
}
//绘制第二个立方体
// 清成单位矩阵
glLoadIdentity();
// 产生一个矩阵
glTranslatef(2, 0, -3);
for(int i = 0; i < 6; ++i) {
glBindTexture(GL_TEXTURE_2D, _texture[i % 2]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //纹理采用最近点采样
glDrawArrays(GL_QUADS, i * 4, 4);
}
glfwSwapBuffers(window);
glfwPollEvents();
}