本文流程
1.Texturing基礎
2.裝載Texturing和mipmapping
3.紋理過濾和包裝
4.Texture level-of-detail, swizzles, and depth comparison
3D 圖像渲染最基礎的操作是使用紋理,它經典應用是坐標系應用在圖像的表面,
2D Textures
一個2D紋理是在OpenGL ES 應用中最基礎和普遍的。它的坐標系是作(s, t),有時也寫作(u,v),紋理的左下角在st坐標系中定義為(0.0,0.0).右上角定義為(1.0,1.0).超過[0.0, 1.0]的坐標也是允許的,
紋理的基本格式見下圖
3D Textures
可以被認為是多個2DTexture
紋理對象和加載紋理
紋理應用第一步是創建一個紋理對象。紋理對象是一個包含着圖片數據、過濾模型、包裝模型等用於渲染的數據的容器。紋理對象是一個無符號整型(GLuint)的句柄。使用void glGenTextures(GLsizei n, GLuint *textures)來創建
第二步將一個紋理對象綁定為當前紋理對象。
第三步裝載圖像數據
// Texture object handle GLuint textureId; // 2 x 2 Image, 3 bytes per pixel (R, G, B) GLubyte pixels[4 * 3] = { 255, 0, 0, // Red 0, 255, 0, // Green 0, 0, 255, // Blue 255, 255, 0 // Yellow }; // Use tightly packed data glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Generate a texture object glGenTextures(1, &textureId); // Bind the texture object glBindTexture(GL_TEXTURE_2D, textureId); // Load the texture glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); // Set the filtering mode glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);