首先創建頂點數組
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