加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2D-X源码分析之从cocos2D-X学习OpenGL(12)----立方体贴图

发布时间:2020-12-14 17:05:15 所属栏目:百科 来源:网络整理
导读:本篇介绍3d游戏中的天空盒概念,天空盒就是游戏中的背景,它是一个包裹整个场景的立方体,它由六个图像构成一个环绕的环境,给玩家一种所在场景比实际上大得多的感觉,如下图所示。 创建天空盒的方法和创建其他节点一样调用create函数,那我们看看create函数

本篇介绍3d游戏中的天空盒概念,天空盒就是游戏中的背景,它是一个包裹整个场景的立方体,它由六个图像构成一个环绕的环境,给玩家一种所在场景比实际上大得多的感觉,如下图所示。


创建天空盒的方法和创建其他节点一样调用create函数,那我们看看create函数里到底做了什么?

bool Skybox::init(const std::string& positive_x,const std::string& negative_x,const std::string& positive_y,const std::string& negative_y,const std::string& positive_z,const std::string& negative_z)
{
    auto texture = TextureCube::create(positive_x,negative_x,positive_y,negative_y,positive_z,negative_z);
    if (texture == nullptr)
        return false;
    
    init();
    setTexture(texture);
    return true;
}
这里调用了TextureCube这个类,这个类就是立方体贴图,立方体贴图就是6个2D纹理,每个2D纹理是立方体的一个面,绑定一个立方体纹理需要调用下面的函数:
GL::bindTextureN(0,_name,GL_TEXTURE_CUBE_MAP);
由于6个纹理,我们需要调用glTexImage2D六次,openGL提供了六个纹理目标,如下:


和其他openGL枚举一样,它的int值也是每次加一的,所以我们可以用如下的方式设置纹理:

for (int i = 0; i < 6; i++)
    {
        Image* img = images[i];

        Texture2D::PixelFormat  ePixelFmt;
        unsigned char*          pData = getImageData(img,ePixelFmt);
        if (ePixelFmt == Texture2D::PixelFormat::RGBA8888 || ePixelFmt == Texture2D::PixelFormat::DEFAULT)
        {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,// level
                         GL_RGBA,// internal format
                         img->getWidth(),// width
                         img->getHeight(),// height
                         0,// border
                         GL_RGBA,// format
                         GL_UNSIGNED_BYTE,// type
                         pData);             // pixel data
        }
        else if (ePixelFmt == Texture2D::PixelFormat::RGB888)
        {
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,// level
                         GL_RGB,// border
                         GL_RGB,// type
                         pData);             // pixel data
        }

        if (pData != img->getData())
            delete[] pData;
    }
其他的纹理设置和之前的2D纹理设置一样,这样就创建了立方体贴图

在cocos2d-x中创建天空盒的方式也是定义六个面的贴图就可以:

    //立方体贴图shader
    auto shader = GLProgram::createWithFilenames("Sprite3DTest/cube_map.vert","Sprite3DTest/cube_map.frag");
    auto state = GLProgramState::create(shader);
    
    //创建立方体纹理
    _textureCube = TextureCube::create("Sprite3DTest/skybox/left.jpg","Sprite3DTest/skybox/right.jpg","Sprite3DTest/skybox/top.jpg","Sprite3DTest/skybox/bottom.jpg","Sprite3DTest/skybox/front.jpg","Sprite3DTest/skybox/back.jpg");
    //立方体纹理参数
    Texture2D::TexParams tRepeatParams;
    tRepeatParams.magFilter = GL_LINEAR;
    tRepeatParams.minFilter = GL_LINEAR;
    tRepeatParams.wrapS = GL_MIRRORED_REPEAT;
    tRepeatParams.wrapT = GL_MIRRORED_REPEAT;
    _textureCube->setTexParameters(tRepeatParams);
    
    //设置uniform
    state->setUniformTexture("u_cubeTex",_textureCube);
    
    //创建天空盒
    _skyBox = Skybox::create();
    _skyBox->setCameraMask(s_CM[LAYER_BACKGROUND]);
    _skyBox->setTexture(_textureCube);
    _skyBox->setScale(700.f);
能力不足,水平有限,如有错误,欢迎指出。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读