立方体贴图

Posted chen9510

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了立方体贴图相关的知识,希望对你有一定的参考价值。

 

  立方体贴图,也称CubeMap。其实就是一张包含六个面的纹理贴图,一般情况下是加载六张贴图构成cubemap。

  加载代码如下:

void WKS::CubeMap::LoadCubeMap(std::vector<std::string> faces) 
    glGenTextures(1, &this->textureID);
    glBindTexture(GL_TEXTURE_CUBE_MAP, this->textureID);

    int width, height, nrChannels;
    for (unsigned int i = 0; i < faces.size(); i++) 
        unsigned char* image = SOIL_load_image(faces[i].c_str(), &width, &height, &nrChannels, SOIL_LOAD_RGB);
        if (image) 
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
        
        else 
            std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
        
        SOIL_free_image_data(image);
    
    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);

  使用cubemap实现天空盒

#pragma once
#include "Model/Texture.h"
#include "Shader.h"
#include "Camera.h"

class SkyBox

public:
    SkyBox();
    ~SkyBox();
    void setTexture(std::vector<std::string>);
    void Draw();

private:
    GLuint VAO, VBO;
    GLuint textureID;
    Shader* shader;
    Camera* phc = Camera::getInstance();

private:
    void setup();
;

SkyBox::SkyBox()

    setup();


SkyBox::~SkyBox()



void SkyBox::setup() 
    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
    ;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glBindVertexArray(0);

    shader = new Shader("./Shader/skybox.vert", "./Shader/skybox.frag");


void SkyBox::setTexture(std::vector<std::string> faces) 
    WKS::CubeMap cubeMap;
    cubeMap.LoadCubeMap(faces);
    this->textureID = cubeMap.GetTextureID();


void SkyBox::Draw() 
    glDepthFunc(GL_LEQUAL);

    this->shader->use();
    glm::mat4 model = glm::scale(glm::mat4(1), glm::vec3(10, 10, 10));
    glm::mat4 view = glm::mat4(glm::mat3(phc->getViewMatrix()));
    this->shader->setMat4("model", model);
    this->shader->setMat4("view", view);
    this->shader->setMat4("projection", phc->getProjectionMatrix());

    glBindTexture(GL_TEXTURE_CUBE_MAP, this->textureID);
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);

  片段着色器中采样纹理

uniform samplerCube skybox;

  效果展示:

  1、反射 - 盒子

  技术图片

  2、反射 - nanosuit

  技术图片

  3、折射 - nanosuit

  技术图片

 

以上是关于立方体贴图的主要内容,如果未能解决你的问题,请参考以下文章

Opengl:渲染到立方体贴图?

C++学习(三三六)球面贴图Sphere mapping 立方体贴图Cube mapping

OpenGL+OpenCV实现立方体贴图

CUDA 立方体贴图纹理

立方体贴图

OpenGL:渲染天空盒立方体贴图问题