立方体贴图

 

  立方体贴图,也称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

  

 

posted @ 2019-08-29 19:23 茶飘香~ 阅读(...) 评论(...) 编辑 收藏