混合渲染

 

 

颜色向量有四个分量RGBA,之前只是用了RGB颜色分量,从来没有使用过A-alpha透明度,今天看了一下资料,简单总结一下。

1、在frag着色器中使用discard,对透明度小于0.1的片段进行丢弃

#version 330 core
out vec4 color;

uniform sampler2D texture1;

in vec3 FragPos;
in vec2 TexCoords;

void main()
{   
    color=texture(texture1, TexCoords);
    if(color.a<0.1f){
        discard;
    }
}

效果图:(图中的草是一张方形的纹理图,可以对alpha<0.1f的部分进行丢弃,只绘制草的部分)

 

2、开启混合功能,呈现透明感

void Display() {
    std::vector<glm::vec3> windows
    {
        glm::vec3(-1.5f, 0.0f, -0.48f),
        glm::vec3(1.5f, 0.0f, 0.51f),
        glm::vec3(0.0f, 0.0f, 0.7f),
        glm::vec3(-0.3f, 0.0f, -2.3f),
        glm::vec3(0.5f, 0.0f, -0.6f)
    };
    //setup blend
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // draw objects
    shader_cube->use();
    glm::mat4 projection = phc.getProjectionMatrix();
    glm::mat4 view = phc.getViewMatrix();
    glm::mat4 model = glm::mat4(1.0f);
    shader_cube->setMat4("projection", projection);
    shader_cube->setMat4("view", view);
    // cubes
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, cubeTexture);
    model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
    shader_cube->setMat4("model", model);
    cube->DrawCube();

    model = glm::mat4(1.0f);
    model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
    shader_cube->setMat4("model", model);
    cube->DrawCube();
    // floor
    glBindTexture(GL_TEXTURE_2D, floorTexture);
    model = glm::mat4(1.0f);
    shader_cube->setMat4("model", model);
    cube->DrawFloor();
    // transparent
    glBindTexture(GL_TEXTURE_2D, transparentTexture);
    std::map<float, glm::vec3> sorted;
    for (unsigned int i = 0; i < windows.size(); i++) {
        float distance = glm::length(phc.getPos() - windows[i]);
        sorted[distance] = windows[i];
    }
    for (std::map<float,glm::vec3>::reverse_iterator it=sorted.rbegin(); it!=sorted.rend(); it++)
    {
        model = glm::mat4(1.0f);
        model = glm::translate(model, it->second);
        shader_cube->setMat4("model", model);
        cube->DrawVegetation();
    }
    
    movement();
}

效果图:

 

转载于:https://www.cnblogs.com/chen9510/p/11377032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值