Mipmapping in OpenGL

MipMap

MIP来源于拉丁文中的multum in parvo,意为在一个小空间里的多数。MIP map(有时候拼写成mipmap)是一种电脑图形图像技术,用于在三维图像的二维代替物中达到立体感效应。MIP map技术与材质帖图技术结合,根据距观看者远近距离的不同,以不同的分辨率将单一的材质帖图以多重图像的形式表现出来并代表平面纹理:尺寸最大的图像放在前面显著的位置,而相对较小的图像则后退到背景区域。每一个不同的尺寸等级定义成一个MIP map水平。MIP map技术帮助避免了不想要的锯齿边缘(称为锯齿状图形)在图像中出现,这种锯齿状图形可能是由于在不同分辨率下使用bit map图像产生的2

通过Mip-Mapping,可以为较小的多边形映射上面积较小的纹理,这对减少纹理的扰动大有好处。举个例子,你有一块256x256大小的纹理,当它开始向远离观察者的方向开始移动时,你会看到它开始闪烁和颤动。这种现象的出现是因为我们把一大块纹理映射到一个很小的区域而引起的。你可能在上一帧时,画的是纹理中(50,20)处的像素,到了下一帧,却画的是纹理中(60,30)处的像素。如果这两个像素相差很大,你就会观察到前面所说的现象了。总的来说,这种剧烈的纹理座标的变化,会损害图像的品质,并且影响CACHE的效率,而Mip-Mapping无疑是解决这个问题的好办法。

OpenGL 中使用了 MipMap 之后,当移动 camera 时,图形硬件会根据视点的距离远近选择合适的图像进行显示。

MipMap 经常与三线性过滤一起使用。

下图是使用 mipmap 和未使用 mipmap 的比较3

No mipmapping
No mipmapping
Mipmapping
Mipmapping

可以对 Mipmap 的细节进行控制,如 GL_TEXTURE_BASE_LEVEL 和 GL_TEXTURE_MAX_LEVEL可控制 Mipmap 的层数,对某个 Mipmap 层的选择。

Steps to Add Mipmapping to Any Render System:
  1. Create and load the smaller averaged images.
  2. Tell the renderer (such as OpenGL) how to use them.
Creating/Loading Mipmap Levels in OpenGL

For Step 1 above, there's two choices: Create & load mipmaps manually or have OpenGL do it automatically.

To create and load manually, you must:

  1. Manually average 4 x 4 texel regions of the texture, store the reduced-size images as new image (e.g., PPM) files.
  2. Load each level into OpenGL. If these are stored in an array of images MIPLevels[], with MIPLevels[0] being the original image of 64 x 64 unsigned-byte RGBA pixels, then you would load them as follows:
glTexImage2D(GL TEXTURE 2D, 0, GL RGBA, 64, 64, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[0]);
glTexImage2D(GL TEXTURE 2D, 1, GL RGBA, 32, 32, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[1]);
glTexImage2D(GL TEXTURE 2D, 2, GL RGBA, 16, 16, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[2]);
glTexImage2D(GL TEXTURE 2D, 3, GL RGBA, 8, 8, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[3]);
glTexImage2D(GL TEXTURE 2D, 4, GL RGBA, 4, 4, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[4]);
glTexImage2D(GL TEXTURE 2D, 5, GL RGBA, 2, 2, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[5]);
glTexImage2D(GL TEXTURE 2D, 6, GL RGBA, 1, 1, 0, GL RGBA, GL UNSIGNED BYTE, MIPLevels[6]);


To create and load automatically, you would:

  1. Load the original texture into an array (let's call it origImage), just as if you were texturing without mipmapping.
  2. Tell OpenGL to automatically averate the 4 x 4 regions and store them in the texture using:
gluBuild2DMipmaps(GL TEXTURE 2D, GL RGBA, 64, 64, GL RGBA, GL UNSIGNED BYTE, origImage);

Using Mipmaps in OpenGL

To use MIP-maps in OpenGL, all you need to do is set the minification filter usign glTexParameteri() function.

glTexParameteri( GL TEXTURE 2D, GL TEXTURE MIN FILTER, <value> );

  • Without mipmapping, <value> was either GL NEAREST or GL LINEAR.
  • Using mipmaps, there are 4 new choices:
    1. GL NEAREST MIPMAP NEAREST (use the nearest neighbor in the nearest mipmap level)
    2. GL NEAREST MIPMAP LINEAR (linearly interpolate in the nearest mipmap level)
    3. GL LINEAR MIPMAP NEAREST (use the nearest neighbor after linearly interpolating between mipmap levels)
    4. GL LINEAR MIPMAP LINEAR (linearly interpolate both the mipmap levels and at between texels)
Sample Code
//Load mipmap

glGenTextures (1, &texture_id);
glBindTexture (GL_TEXTURE_2D, texture_id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);


glTexImage2D(GL TEXTURE 2D, 0, GL RGBA, 64, 64, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage64);
glTexImage2D(GL TEXTURE 2D, 1, GL RGBA, 32, 32, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage32);
glTexImage2D(GL TEXTURE 2D, 2, GL RGBA, 16, 16, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage16);
glTexImage2D(GL TEXTURE 2D, 3, GL RGBA, 8, 8, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage8);
glTexImage2D(GL TEXTURE 2D, 4, GL RGBA, 4, 4, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage4);
glTexImage2D(GL TEXTURE 2D, 5, GL RGBA, 2, 2, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage2);
glTexImage2D(GL TEXTURE 2D, 6, GL RGBA, 1, 1, 0, GL RGBA, GL UNSIGNED BYTE, mipmapimage1);



//render

glBindTexture (GL_TEXTURE_2D, texture_id);
glBegin (GL_QUADS);
   glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
   glTexCoord2f(0.0, 8.0); glVertex3f(-2.0, 1.0, 0.0);
   glTexCoord2f(8.0, 8.0); glVertex3f(2000.0, 1.0, -6000.0);
   glTexCoord2f(8.0, 0.0); glVertex3f(2000.0, -1.0, -6000.0);

glEnd();


Reference
  1. Mipmapping in OpenGL
  2. Mipmap@百度百科
  3. Advanced OpenGL Texture Mapping

转自:http://www.linuxgraphics.cn/opengl/mipmapping_in_opengl.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值