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

相关推荐
菜鸟射手6 分钟前
QT creater和vs2017文件路径问题
linux·c++·windows·qt
wuqingshun31415913 分钟前
蓝桥杯17. 机器人塔
c++·算法·职场和发展·蓝桥杯·深度优先
simple_whu26 分钟前
解决编译pcl时报错‘chrono_literals‘: is not a member of ‘std‘
c++·windows·visual studio
图灵科竞社资讯组43 分钟前
图论基础:图存+记忆化搜索
算法·图论
chuxinweihui1 小时前
数据结构——栈与队列
c语言·开发语言·数据结构·学习·算法·链表
爱编程的鱼1 小时前
C# 结构(Struct)
开发语言·人工智能·算法·c#
啊我不会诶2 小时前
CF每日4题
算法
uhakadotcom2 小时前
人工智能如何改变医疗行业:简单易懂的基础介绍与实用案例
算法·面试·github
吴_知遇3 小时前
【华为OD机试真题】428、连续字母长度 | 机试真题+思路参考+代码解析(E卷)(C++)
开发语言·c++·华为od