jal*_*ghi 3 opengl texturing glteximage2d mipmaps glu
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;
Run Code Online (Sandbox Code Playgroud)
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 );
Run Code Online (Sandbox Code Playgroud)
Wen使用glTexImage2D
,没有绘制但是有gluBuild2DMipmaps
作品我看到gDebugger的结果正确创建了纹理.问题是什么?
glTexImage2D (...)
在代码中使用时,不要构建mipmap完整纹理.它仅为纹理LOD 0创建存储和提供数据.如果您使用GL_..._MIPMAP_...
缩小过滤器,则需要为每个四分之一分辨率步骤提供LOD(详细程度).
例如,尺寸为32x32的纹理需要log 2 32 = 5个 额外的 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 ]
Run Code Online (Sandbox Code Playgroud)
gluBuild2DMipmaps (...)
做了几件事:它可以正确设置纹理中的LOD数量
gluBuild2DMipmaps (...)
:log 2(max(Res x,Res y))+ 1 (min = 0,max = ...).这会生成一个mipmap完整纹理,可以与mipmap缩小过滤器一起使用.
OpenGL中的默认缩小过滤器是:GL_NEAREST_MIPMAP_LINEAR
,因此除非将其更改为GL_LINEAR
或,否则需要mipmap完整纹理GL_NEAREST
.
OpenGL中的LOD索引在某种程度上与直觉相反.较低的数字表示较高分辨率的图像,这就是为什么使用负LOD偏差有时被称为"纹理锐化".
几何级数:1 + 1/4 + 1/16 + 1/64 + ... + 1/Ñ收敛到4/3 ; mipmap需要大约33%的额外存储空间.