首先创建顶点数组
- 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
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