Opengl学习笔记之Texture 2D Array

来源:互联网 发布:深圳经济数据 编辑:程序博客网 时间:2024/06/08 00:04
Array Textures 范例来自 OpenGl SuperBible(page 177-182),并做了些改动。

1.vertex shader 中改了 vs_out.alien = alien_index % 2; 因为我使用了两张图片。sampler2DArray对象的texture()函数采样的纹理坐标参数的z即指定使用纹理数组内图片的索引。
(color = texture(tex_onePiece, vec3(fs_in.tc, float(fs_in.alien)));)

2.使用DevIL加载图片,il只需执行一次,放置在start()内

    ilInit();    ilGenImages(2, images);    ilEnable(IL_ORIGIN_SET);    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);    LoadImageIndex("Lunamon.gif", 0);    LoadImageIndex("images.jpg", 1);    GenerateTextureObject();
    void LoadImageIndex(string imageName, int index)    {        ilBindImage(images[index]);        ILboolean success = ilLoadImage(imageName.c_str());        ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);        if (!success)        {            ilDeleteImages(1, images);        }    }    void GenerateTextureObject()    {        // general sampler        glGenSamplers(1, &samplers);        glSamplerParameteri(samplers, GL_TEXTURE_WRAP_S, GL_REPEAT);        glSamplerParameteri(samplers, GL_TEXTURE_WRAP_T, GL_REPEAT);        glSamplerParameteri(samplers, GL_TEXTURE_MAG_FILTER, GL_NEAREST);        glSamplerParameteri(samplers, GL_TEXTURE_MIN_FILTER, GL_NEAREST);        glBindSampler(textureUnit, samplers);        // texture object and texture 2d array         glGenTextures(2, textures);        glBindTexture(GL_TEXTURE_2D_ARRAY, textures[0]);        //allocate memory: depth is the length of texture 2d array        glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 100, 100, 100);        ilBindImage(images[0]);        int w = ilGetInteger(IL_IMAGE_WIDTH);        int h = ilGetInteger(IL_IMAGE_HEIGHT);        unsigned char * data = ilGetData();        //init pixel datas: zoffset is 0        glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 0, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);        ilBindImage(images[1]);        int w1 = ilGetInteger(IL_IMAGE_WIDTH);        int h1 = ilGetInteger(IL_IMAGE_HEIGHT);        unsigned char * data1 = ilGetData();        //init pixel datas: zoffset is 1        glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, 1, w1 , h1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data1);        // 2d texture         glBindTexture(GL_TEXTURE_2D, textures[1]);        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w1, h1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data1);    }

在编译时遇到一些问题:
1.glTexStorage3D()与glTexSubImage3D()的参数设置有问题。google了一些资料。
OpenGL ES: Array Texture初体验
Array Textures
2.GL_TRIANGLE_STRIP

编译成功运行结果: