VulkanSceneGraph —— texture

本文介绍了如何在VulkanSceneGraph中实践纹理处理,包括加载PNG图像、设置DescriptorSet和PipelineLayout,以及将纹理绑定到StateGroup节点以实现渲染过程中的纹理映射。
摘要由CSDN通过智能技术生成

纹理

本篇介绍VulkanSceneGraph简单的纹理实践,内容实际与vsgExamples中的vsgDraw类似,但另有些许不同。
代码看这里

使用过程

纹理的使用往往有两个过程

  1. 加载纹理图像
  2. 将纹理图映射到所需的片元上

加载纹理图

加载纹理

// read texture image
    vsg::Path textureFile("textures/container.jpg");
    auto textureData = vsg::read_cast<vsg::Data>(vsg::findFile(textureFile, vsg::Paths{ std::string(RESOURCES_DIR)}),options);
    if (!textureData)
    {
        std::cout << "Could not read texture file : " << textureFile << std::endl;
        return 1;
    }

这里由于使用的是png格式的图片,需要引入vsgXchange配套库,vsg库提供有有限的几种格式的文件读取,但不包括png图片。

#include<vsgXchange/all.h>
	...
    options->add(vsgXchange::all::create());

纹理映射

这里,结合Vulkan,有几个关键的步骤

  1. 着色器
  2. descriptor和graphics pipeline管线设置
  3. bindDescriptorSet绑定所需的descriptor
    着色器部分与其他使用基本相同,此处略过
    descriptor和管线设置如下,配置DescriptorSetLayoutBindings和DescriptorSetLayout,最后设置好PipelineLayout即可
    // set up graphics pipeline
    vsg::DescriptorSetLayoutBindings descriptorBindings{
        {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr} // { binding, descriptorType, descriptorCount, stageFlags, pImmutableSamplers}
    };

    auto descriptorSetLayout = vsg::DescriptorSetLayout::create(descriptorBindings);

    auto pipelineLayout = vsg::PipelineLayout::create(vsg::DescriptorSetLayouts{ descriptorSetLayout }, vsg::PushConstantRanges{});

最后的使用绑定过程

    auto descriptorSet = vsg::DescriptorSet::create(descriptorSetLayout, vsg::Descriptors{ texture });
    auto bindDescriptorSet = vsg::BindDescriptorSet::create(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, descriptorSet);
    
    ...
    
    // create StateGroup as the root of the scene/command graph to hold the GraphicsPipeline
    auto scenegraph = vsg::StateGroup::create();
    scenegraph->add(bindGraphicsPipeline);
    scenegraph->add(bindDescriptorSet);

VSG可以将该过程绑定到StateGroup节点中,该节点绑定了管线并可以将绘制命令集成到该节点中,次吃就将绑定DescriptorSet的命令作了内部集成,即可在渲染过程中绑定对应的纹理。

最终效果

在这里插入图片描述

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亮微尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值