opengl制作天空盒

首先创建顶点数组

unsigned int m_uiVaoBufferID;
glGenVertexArrays(1, &m_uiVaoBufferID);

然后创建顶点缓冲区

    float skyboxVertices[] = {
        // positions
        -1.0f,  1.0f, -1.0f,
        -1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

        -1.0f,  1.0f, -1.0f,
         1.0f,  1.0f, -1.0f,
         1.0f,  1.0f,  1.0f,
         1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
         1.0f, -1.0f, -1.0f,
         1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
         1.0f, -1.0f,  1.0f
    };

unsigned int m_uiVboBufferID;
glGenBuffers(1, &m_uiVboBufferID);
glBindBuffer(GL_ARRAY_BUFFER, m_uiVboBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0))
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), 0);

创建并编译shader(这里我将编译shader封装成了类,通用的用法)

m_pShaderObject = new CShaderObject("/res/shaders/skybox.shader", m_pOpenglParent);

创建纹理

glGenTextures(1, &m_uiBufferID);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_uiBufferID);

std::vector<std::string> strFilePathVector = {
    "/res/textures/skybox2/cube_+x.png",
    "/res/textures/skybox2/cube_-x.png",
    "/res/textures/skybox2/cube_+y.png",
    "/res/textures/skybox2/cube_-y.png",
    "/res/textures/skybox2/cube_+z.png",
    "/res/textures/skybox2/cube_-z.png",
};

int iWidth, iHeight, iBpp;
for(unsigned int i=0; i<strFilePathVector.size(); i++){
    stbi_set_flip_vertically_on_load(0);
    unsigned char* data = stbi_load(strFilePathVector.at(i).c_str(), &iWidth, &iHeight, &iBpp, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, \
                                                GL_RGB, iWidth, iHeight, 0, \
                                                GL_RGB, GL_UNSIGNED_BYTE, data);
    if(data){
        stbi_image_free(data);
    }
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

我们把下述全景图片进行切割后载入到程序中

以下是切割后的图片

推荐一款由全景图片切成符合opengl六面图的工具PanoSplit - Microsoft Store 中的官方应用

或者微软应用商店搜索PanoSplit

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值