OpenGL notes six: Texture common API (a)

Grain

Only a texture may be applied to the image data scene triangle. It is filtered texture unit (texel, texture corresponding to the pixel-based) solid filled areas.

Texture object

Texture object allows us more time to load a texture state (contains texture images). And quickly switch between them. Texture state is currently bound texture object maintenance. Identifying an unsigned integer and when the object texture.

Image storage space

The image memory of the image ADVANCED = * image width * number of bytes per pixel

Such as: the RGB (8-bit), image width 200 pixels, each row of stored image requires: 200 * 8 * 3 = 600 bytes (one byte note 98)
TGA (texture file), a byte is one byte storage, there will be no free memory space.

//改变像素存储⽅式 
void glPixelStorei(GLenum pname,GLint param);

//恢复像素存储⽅式 
void glPixelStoref(GLenum pname,GLfloat param);

Texture object allocation

//使用函数分配纹理对象
//指定纹理对象的数量 和 指针(指针指向一个无符号整形数组,由纹理对象标识符填充)。
void glGenTextures(GLsizei n,GLuint * textTures);

//绑定纹理状态
//参数target:GL_TEXTURE_1D、GL_TEXTURE_2D、GL_TEXTURE_3D
//参数texture:需要绑定的纹理对象
void glBindTexture(GLenum target,GLunit texture);

//删除绑定纹理对象
//纹理对象 以及 纹理对象指针(指针指向一个无符号整形数组,由纹理对象标识符填充)。
void glDeleteTextures(GLsizei n,GLuint *textures);

//测试纹理对象是否有效
//如果texture是一个已经分配空间的纹理对象,那么这个函数会返回GL_TRUE,否则会返回GL_FALSE。
GLboolean glIsTexture(GLuint texture);

Read TGA texture

GLbyte *gltReadTGABits(const char *szFileName, GLint *iWidth, GLint *iHeight, GLint *iComponents, GLenum *eFormat);

GLbyte *pBits;
int nWidth, nHeight, nComponents;
GLenum eFormat;
    
 /*
   参数1:纹理文件名称
   参数2:文件宽度地址
   参数3:文件高度地址
   参数4:文件组件地址
   参数5:文件格式地址
   返回值:pBits,指向图像数据的指针
   这里传入地址带回值
*/
pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat);

Texture parameters

glTexParameterf(GLenum target,GLenum pname,GLFloat param);
glTexParameteri(GLenum target,GLenum pname,GLint param);
glTexParameterfv(GLenum target,GLenum pname,GLFloat *param);
glTexParameteriv(GLenum target,GLenum pname,GLint *param);

/*
   参数1:target,指定这些参数将要应用在那个纹理模式上
   如:GL_TEXTURE_1D、GL_TEXTURE_2D、GL_TEXTURE_3D。
   参数2:pname,指定需要设置那个纹理参数
   参数3:param,设定特定的纹理参数的值
*/
  • Enlargement / reduction filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  • S / T-axis surround mode
glTextParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAR_S,GL_CLAMP_TO_EDGE);
glTextParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAR_T,GL_CLAMP_TO_EDGE);

Load Texture

void glTexImage1D(GLenum target,GLint level,GLint internalformat,GLsizei width,GLint border,GLenum format,GLenum type,void *data);

void glTexImage2D(GLenum target,GLint level,GLint internalformat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,void * data);

void glTexImage3D(GLenum target,GLint level,GLint internalformat,GLSizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,void *data);

/*
   target:GL_TEXTURE_1D、GL_TEXTURE_2D、GL_TEXTURE_3D 。 
   Level:指定所加载的mip贴图层次。一般我们都把这个参数设置为0。
   internalformat:每个纹理单元中存储多少颜色成分。
   width、height、depth 参数:指加载纹理的宽度、高度、深度。
   border参数:允许为纹理贴图指定一个边界宽度。
   format参数:gltReadTGABits函数中,通过 eFormat 参数返回图片的颜色格式
   type参数:OpenGL 数据存储方式,一般使用 GL_UNSIGNED_BYTE
   data参数:图片数据指针
*/
  • The sample code
glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits);
/* 
   参数1:纹理维度
   参数2:mip贴图层次
   参数3:纹理单元存储的颜色成分(从读取像素图是获得)
   参数4:加载纹理宽
   参数5:加载纹理高
   参数6:加载纹理的深度
   参数7:像素数据的数据类型(GL_UNSIGNED_BYTE,每个颜色分量都是一个8位无符号整数)
   参数8:指向纹理图像数据的指针
*/

 //使用完毕释放pBits
 free(pBits);

//4.加载Mip,纹理生成所有的Mip层
//参数:GL_TEXTURE_1D、GL_TEXTURE_2D、GL_TEXTURE_3D
glGenerateMipmap(GL_TEXTURE_2D);

Guess you like

Origin blog.csdn.net/weixin_33694620/article/details/90829759