opengl制作天空盒

首先创建顶点数组

cpp 复制代码
unsigned int m_uiVaoBufferID;
glGenVertexArrays(1, &m_uiVaoBufferID);

然后创建顶点缓冲区

cpp 复制代码
    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封装成了类,通用的用法)

cpp 复制代码
m_pShaderObject = new CShaderObject("/res/shaders/skybox.shader", m_pOpenglParent);

创建纹理

cpp 复制代码
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

相关推荐
z人间防沉迷k43 分钟前
堆(Heap)
开发语言·数据结构·笔记·python·算法
不二狗1 小时前
每日算法 -【Swift 算法】Two Sum 问题:从暴力解法到最优解法的演进
开发语言·算法·swift
LabVIEW开发1 小时前
LabVIEW中样条插值实现及应用
算法·labview知识
LabVIEW开发1 小时前
LabVIEW机械振动信号分析与故障诊断
算法·labview知识·labview开发案例
hjjdebug1 小时前
constexpr 关键字的意义(入门)
c++·constexpr
啊我不会诶1 小时前
CF每日5题(1300-1500)
算法·深度优先·图论
芒果量化2 小时前
量化交易 - 网格交易策略实现与原理解析
python·算法·机器学习·金融
MUTA️2 小时前
ultalytics代码中模型接收多层输入的处理
深度学习·算法·yolo·机器学习·计算机视觉
虾球xz2 小时前
游戏引擎学习第282天:Z轴移动与摄像机运动
c++·学习·游戏引擎
.小墨迹3 小时前
Apollo学习——planning模块(3)之planning_base
linux·开发语言·c++·学习·自动驾驶