glTexImage2D与gluBuild2DMipmaps [英] glTexImage2D vs. gluBuild2DMipmaps

查看:169
本文介绍了glTexImage2D与gluBuild2DMipmaps的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常基本的OpenGL纹理创建代码:

 int width, height;
BYTE * data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
// allocate buffer
width = 256;
height = 256;
data =(BYTE*) malloc( width * height * 3 );
// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
//gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,   height, GL_RGB, GL_UNSIGNED_BYTE, data );
glTexImage2D( GL_TEXTURE_2D,0, GL_RGB, width,   height,0, GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;
 

和渲染:

 glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );

glPushMatrix();
glRotatef( theta, 0.0f, 0.0f, 1.0f );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();

SwapBuffers( hDC );
 

使用glTexImage2D时,什么也没画,但是使用gluBuild2DMipmaps时,我看到gDebugger的结果是正确创建的纹理.问题是什么?

解决方案

在代码中使用glTexImage2D (...)时,不会构建完整的mipmap纹理.它仅创建存储并提供纹理 LOD 0 的数据.如果您将GL_..._MIPMAP_...用作缩小过滤器,则每个四分之一分辨率步骤都需要具有LOD(细节水平).

例如,尺寸为 32x32 的纹理需要 log 2 32 = 5 extra mipmap LOD,如下所述:

 LOD 0: 32x32  ( 1 /    1 )  [1024 Texels]
LOD 1: 16x16  ( 1 /    4 )  [ 256 Texels]
LOD 2: 8x8    ( 1 /   16 )  [  64 Texels]
LOD 3: 4x4    ( 1 /   64 )  [  16 Texels]
LOD 4: 2x2    ( 1 /  256 )  [   4 Texels]
LOD 5: 1x1    ( 1 / 1024 )  [   1 Texel ]
 

gluBuild2DMipmaps (...)做一些事情:

  1. 它构建了四分之一分辨率的mipmap LOD(因此得名).
  2. 它可以正确设置纹理中的LOD数量

    • OpenGL纹理中的默认LOD级别范围: 1001     (最小= 0,最大= 1000)
    • gluBuild2DMipmaps (...)之后: log 2 (最大值(Res x ,Res y ))+ 1   (最小值= 0,最大值= ...).

这将生成完整的mipmap纹理,可与mipmap缩小过滤器一起使用.


其他注意事项:

OpenGL中的 默认 缩小过滤器为:GL_NEAREST_MIPMAP_LINEAR,因此,除非将其更改为GL_LINEARGL_NEAREST,否则您需要一个mipmap完整纹理. /p> OpenGL中的

LOD索引在某种程度上是违反直觉的.较低的数字表示高分辨率的图像,这就是为什么使用负LOD偏差有时被称为纹理锐化"的原因.

几何级数: 1 + 1 / 4 + 1 / 16 + 1 / 64 + ... + 1 / N 收敛到 4 / 3 ; mipmap需要〜33%的额外存储空间.

Very basic OpenGL texture creation code:

int width, height;
BYTE * data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
// allocate buffer
width = 256;
height = 256;
data =(BYTE*) malloc( width * height * 3 );
// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
//gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,   height, GL_RGB, GL_UNSIGNED_BYTE, data );
glTexImage2D( GL_TEXTURE_2D,0, GL_RGB, width,   height,0, GL_RGB, GL_UNSIGNED_BYTE, data );
free( data );
return texture;

and rendering:

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, texture );

glPushMatrix();
glRotatef( theta, 0.0f, 0.0f, 1.0f );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
glPopMatrix();

SwapBuffers( hDC );

Wen using glTexImage2D, nothing draws but with gluBuild2DMipmaps works I saw result with gDebugger the texture created correctly. What is the problem?

解决方案

When you use glTexImage2D (...) in your code you do not build a mipmap complete texture. It only creates storage and supplies data for texture LOD 0. If you are using GL_..._MIPMAP_... as your minification filter you need to have LODs (levels-of-detail) for every quarter-resolution step.

For example, a texture with dimensions 32x32 requires log2 32 = 5 extra mipmap LODs, as described below:

LOD 0: 32x32  ( 1 /    1 )  [1024 Texels]
LOD 1: 16x16  ( 1 /    4 )  [ 256 Texels]
LOD 2: 8x8    ( 1 /   16 )  [  64 Texels]
LOD 3: 4x4    ( 1 /   64 )  [  16 Texels]
LOD 4: 2x2    ( 1 /  256 )  [   4 Texels]
LOD 5: 1x1    ( 1 / 1024 )  [   1 Texel ]

gluBuild2DMipmaps (...) does a couple of things:

  1. It builds the set of quarter-resolution mipmap LODs (hence the name).
  2. It properly sets the number of LODs in your texture

    • Default LOD level range in an OpenGL Texture: 1001                    (min = 0, max = 1000)
    • After gluBuild2DMipmaps (...): log2 (max (Resx, Resy)) + 1  (min = 0, max = ...).

This produces a mipmap complete texture, that can be used with a mipmap minification filter.


Miscellaneous things to note:

The default minification filter in OpenGL is: GL_NEAREST_MIPMAP_LINEAR, so you need a mipmap complete texture unless you change it to GL_LINEAR or GL_NEAREST.

LOD indices in OpenGL work somewhat counter-intuitively. Lower numbers represent higher resolution images, and this is why using a negative LOD bias is sometimes referred to as "texture sharpening."

The geometric series: 1 + 1/4 + 1/16 + 1/64 + ... + 1/N converges to 4/3; mipmaps require ~33% extra storage.

这篇关于glTexImage2D与gluBuild2DMipmaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆