【OpenGL】OpenGL过程式纹理及Mipmap效果

1.绘制目标

  1. 生成如下图所示的各级Mipmap图像
  2. 通过glTexImage2D设置各个Mipmap等级对应的纹理

【OpenGL】OpenGL过程式纹理及Mipmap效果_OpenGL

2.核心代码

TO DO 
// 创建Mipmap Level 1 对应的纹理图像
for (i = 0; i < 32; i++) {
for (j = 0; j < 32; j++) {
mipmapRes32[i][j][0] = 0x00;
mipmapRes32[i][j][1] = 0xFF;
mipmapRes32[i][j][2] = 0xFF;
}
}
// 创建Mipmap Level 2 对应的纹理图像
for (i = 0; i < 16; i++) {
for (j = 0; j < 16; j++) {
mipmapRes16[i][j][0] = 0xFF;
mipmapRes16[i][j][1] = 0x00;
mipmapRes16[i][j][2] = 0xFF;
}
}
// 创建Mipmap Level 3 对应的纹理图像
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
mipmapRes8[i][j][0] = 0xFF;
mipmapRes8[i][j][1] = 0xFF;
mipmapRes8[i][j][2] = 0x00;
}
}
// 创建Mipmap Level 4 对应的纹理图像
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
mipmapRes4[i][j][0] = 0xFF;
mipmapRes4[i][j][1] = 0x00;
mipmapRes4[i][j][2] = 0x00;
}
}
// 创建Mipmap Level 5 对应的纹理图像
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
mipmapRes2[i][j][0] = 0x00;
mipmapRes2[i][j][1] = 0xFF;
mipmapRes2[i][j][2] = 0x00;
}
}
// 创建Mipmap Level 6 对应的纹理图像
for (i = 0; i < 1; i++) {
for (j = 0; j < 1; j++) {
mipmapRes1[0][0][0] = 0x00;
mipmapRes1[0][0][1] = 0x00;
mipmapRes1[0][0][2] = 0x00;
}
}


/ TO DO
//通过glTexImage2D设置各个Mipmap等级的纹理图像/
//设置Mipmap等级0的纹理图像
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes64);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes64[0]);
//设置Mipmap等级1的纹理图像
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes32);
//glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 32, 32, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes32[0]);
//设置Mipmap等级2的纹理图像
glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes16);
//glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes16[0]);
//设置Mipmap等级3的纹理图像
glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes8);
//glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes8[0]);
//设置Mipmap等级4的纹理图像
glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes4);
//glTexImage2D(GL_TEXTURE_2D, 4, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes4[0]);
//设置Mipmap等级5的纹理图像
glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes2);
//glTexImage2D(GL_TEXTURE_2D, 5, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes2[0]);
//设置Mipmap等级6的纹理图像
glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes1);
//glTexImage2D(GL_TEXTURE_2D, 6, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, mipmapRes1[0]);
/

3.运行结果

【OpenGL】OpenGL过程式纹理及Mipmap效果_纹理图像_02


【OpenGL】OpenGL过程式纹理及Mipmap效果_纹理图像_03

【OpenGL】OpenGL过程式纹理及Mipmap效果_2d_04


【OpenGL】OpenGL过程式纹理及Mipmap效果_2d_05


【OpenGL】OpenGL过程式纹理及Mipmap效果_i++_06


【OpenGL】OpenGL过程式纹理及Mipmap效果_OpenGL_07