步骤如下:
1. 首先启用2D纹理, 不启用会发现纹理区域是黑色的
glEnable(GL_TEXTURE_2D)
2. 生成纹理
glGenTextures( 1, &m_nTextureID )
glBindTexture(1, &m_nTextureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_nWidth, m_nHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pData);
m_pData 像素数据
还可以开启一些过滤方式,如下
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
3. 绘制,先绑定下纹理,然后用四边形映射纹理坐标和空间坐标
glBindTexture(GL_TEXTURE_2D, m_nTextureID);
glBegin( GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(0, 0, 0.0f);
glTexCoord2f(1, 0);
glVertex3f(m_nWidth, 0, 0.0f);
glTexCoord2f(1, 1);
glVertex3f(m_nWidth, m_nHeight, 0.0f);
glTexCoord2f(0, 1);
glVertex3f(0, m_nHeight, 0.0f);
glEnd();
4. 完毕,加载png可以用lib.png,bmp可以用glaux