• opengl制作天空盒


    首先创建顶点数组

    1. unsigned int m_uiVaoBufferID;
    2. glGenVertexArrays(1, &m_uiVaoBufferID);

    然后创建顶点缓冲区

    1. float skyboxVertices[] = {
    2. // positions
    3. -1.0f, 1.0f, -1.0f,
    4. -1.0f, -1.0f, -1.0f,
    5. 1.0f, -1.0f, -1.0f,
    6. 1.0f, -1.0f, -1.0f,
    7. 1.0f, 1.0f, -1.0f,
    8. -1.0f, 1.0f, -1.0f,
    9. -1.0f, -1.0f, 1.0f,
    10. -1.0f, -1.0f, -1.0f,
    11. -1.0f, 1.0f, -1.0f,
    12. -1.0f, 1.0f, -1.0f,
    13. -1.0f, 1.0f, 1.0f,
    14. -1.0f, -1.0f, 1.0f,
    15. 1.0f, -1.0f, -1.0f,
    16. 1.0f, -1.0f, 1.0f,
    17. 1.0f, 1.0f, 1.0f,
    18. 1.0f, 1.0f, 1.0f,
    19. 1.0f, 1.0f, -1.0f,
    20. 1.0f, -1.0f, -1.0f,
    21. -1.0f, -1.0f, 1.0f,
    22. -1.0f, 1.0f, 1.0f,
    23. 1.0f, 1.0f, 1.0f,
    24. 1.0f, 1.0f, 1.0f,
    25. 1.0f, -1.0f, 1.0f,
    26. -1.0f, -1.0f, 1.0f,
    27. -1.0f, 1.0f, -1.0f,
    28. 1.0f, 1.0f, -1.0f,
    29. 1.0f, 1.0f, 1.0f,
    30. 1.0f, 1.0f, 1.0f,
    31. -1.0f, 1.0f, 1.0f,
    32. -1.0f, 1.0f, -1.0f,
    33. -1.0f, -1.0f, -1.0f,
    34. -1.0f, -1.0f, 1.0f,
    35. 1.0f, -1.0f, -1.0f,
    36. 1.0f, -1.0f, -1.0f,
    37. -1.0f, -1.0f, 1.0f,
    38. 1.0f, -1.0f, 1.0f
    39. };
    40. unsigned int m_uiVboBufferID;
    41. glGenBuffers(1, &m_uiVboBufferID);
    42. glBindBuffer(GL_ARRAY_BUFFER, m_uiVboBufferID);
    43. glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);
    44. glEnableVertexAttribArray(0))
    45. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), 0);

    创建并编译shader(这里我将编译shader封装成了类,通用的用法)

    m_pShaderObject = new CShaderObject("/res/shaders/skybox.shader", m_pOpenglParent);

    创建纹理

    1. glGenTextures(1, &m_uiBufferID);
    2. glBindTexture(GL_TEXTURE_CUBE_MAP, m_uiBufferID);
    3. std::vector strFilePathVector = {
    4. "/res/textures/skybox2/cube_+x.png",
    5. "/res/textures/skybox2/cube_-x.png",
    6. "/res/textures/skybox2/cube_+y.png",
    7. "/res/textures/skybox2/cube_-y.png",
    8. "/res/textures/skybox2/cube_+z.png",
    9. "/res/textures/skybox2/cube_-z.png",
    10. };
    11. int iWidth, iHeight, iBpp;
    12. for(unsigned int i=0; isize(); i++){
    13. stbi_set_flip_vertically_on_load(0);
    14. unsigned char* data = stbi_load(strFilePathVector.at(i).c_str(), &iWidth, &iHeight, &iBpp, 0);
    15. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, \
    16. GL_RGB, iWidth, iHeight, 0, \
    17. GL_RGB, GL_UNSIGNED_BYTE, data);
    18. if(data){
    19. stbi_image_free(data);
    20. }
    21. }
    22. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    23. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    24. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    25. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    26. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    27. glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

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

    以下是切割后的图片

    以下是图片顺序

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

    或者微软应用商店搜索PanoSplit

  • 相关阅读:
    安全、灵活、低成本,华为云OBS如何提升用户体验
    声纹技术(二):音频信号处理基础
    Android .kl按键布局文件
    (定时器/计数器)&中断系统(详解与使用)
    Docker数据管理与镜像创建
    C#学习系列相关之多线程(二)----Thread类介绍
    【学习笔记】Spring Security 01 认识Spring Security的重要特征(Features)
    阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展
    PDF怎么转换成Word文档呢?不妨试试这两种方法!
    MediaPlayer_Analyze-6-MediaCodec和ACodec
  • 原文地址:https://blog.csdn.net/Groot_Lee/article/details/134564433