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 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

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

opengl制作天空盒_第1张图片

以下是切割后的图片

以下是图片顺序

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

或者微软应用商店搜索PanoSplit

你可能感兴趣的:(算法,opengl,C++,天空盒,立方体贴图)