VulkanSceneGraph —— hello triangle

本文介绍了如何使用VSG库在Vulkan环境中绘制一个三角形,包括设置渲染管线、着色器、顶点数据和索引缓冲,以及在VSGStateGroup中添加图形指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hello triangle

本篇主要讲如何用VSG绘制一个三角形。本篇主要参考了vsgExamples的vsgdraw,内容精简为绘制一个三角形,将涉及到uniform相关的部分放到未来的博客中吧。主要内容与learnOpenGL的hello Triangle章节相近。

由于下面写的很是杂乱一方面是Vulkan内容繁多,另一方面是本人的能力和时间有限,各位可以查看这里直接阅读代码可能会更加清晰,个人为了培养自己的能力,使用了英文注释,敬请谅解。
共有两个简单的示例,使用了类似于OpenGL中drawArraydrawElement两种绘制方式

VSG在很大程度上与Vulkan的接口类似,如果想使用VSG随心所欲的做各种各样的功能,还是需要多去查看Vulkan的相关文档和教程的。vulkan官网上的教程还是需要查看的。

渲染管线

渲染管线的各个阶段各种渲染API应该是基本相同的,部分阶段可能会有所删节,这个部分想要深刻了解还是应该参考 Real Time Rendering这个部分各种博客文章大多都是将RTR中的管线图粘贴出来而已。

Vulkan需要先将整个绘制管线基本确定,随后进行绘制操作。整个管线设置是非常冗长的,VSG做了封装,让很多过程变的简单了很多。使用vsg的默认管线会比较容易配置,我觉得还是需要先看一看相对复杂但更容易与Vulkan做对照的配置过程

着色器

这里使用了learnOpenGL的中对应章节的着色器。

std::string vertexShaderSource{R"(
#version 450
layout(location = 0) in vec3 vsg_Vertex;

void main()
{
    gl_Position = vec4(vsg_Vertex, 1.0f);
})"
};
std::string fragmentShaderSource{R"(
#version 450
layout(location = 0) out vec4 outColor;

void main()
{
    outColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
})"
};

创建shader

	vsg::ref_ptr<vsg::ShaderStage> vertexShader = vsg::ShaderStage::create(VK_SHADER_STAGE_VERTEX_BIT, "main", vertexShaderSource);
    vsg::ref_ptr<vsg::ShaderStage> fragmentShader = vsg::ShaderStage::create(VK_SHADER_STAGE_FRAGMENT_BIT, "main", fragmentShaderSource);
    if (!vertexShader || !fragmentShader)
    {
        std::cout << "Could not create shaders." << std::endl;
        return 1;
    }

顶点数据的信息描述

vulkan中我暂时没看到VAO对应的概念,这里但是与VBO设置相近的概念如下

    vsg::VertexInputState::Bindings vertexBindingsDescriptions{
        VkVertexInputBindingDescription{0, sizeof(vsg::vec3), VK_VERTEX_INPUT_RATE_VERTEX}, // vertex data
    };

    vsg::VertexInputState::Attributes vertexAttributeDescriptions{
        VkVertexInputAttributeDescription{0, 0, VK_FORMAT_R32G32B32_SFLOAT, 0}, // vertex data
    };

管线构造

代码如下

     // I use data from learnOpengl - hello Triangle, the triangl direction is not point out of the screen because of different NDC in vulkan
   auto rasterization = vsg::RasterizationState::create();
   rasterization->cullMode = VK_CULL_MODE_NONE;

   vsg::GraphicsPipelineStates pipelineStates{
       vsg::VertexInputState::create(vertexBindingsDescriptions, vertexAttributeDescriptions),
       vsg::InputAssemblyState::create(),
       rasterization,
       vsg::MultisampleState::create(),
       vsg::ColorBlendState::create(), 
   };

这里有两个要强调一下的点,发生在从视点坐标系映射到规范化坐标系的过程中。与OpenGL不同,Vulkan中的规范化坐标系是右手系,z坐标指向屏幕内。其次就是vulkan中的z坐标范围为0.0-1.0。

StateGroup节点

VSG毕竟是一个场景图,vsg::StateGroup就是描述渲染管线的对应节点,该节点下添加的图元自然就是使用该渲染管线进行绘制了,下面是在本节配置该节点的方法。

    auto pipelineLayout = vsg::PipelineLayout::create();
    auto graphicsPipeline = vsg::GraphicsPipeline::create(pipelineLayout, vsg::ShaderStages{ vertexShader, fragmentShader }, pipelineStates);
    auto bindGraphicsPipeline = vsg::BindGraphicsPipeline::create(graphicsPipeline);

    // create StateGroup as the root of the scene/command graph to hold the GraphicsPipeline
    auto scenegraph = vsg::StateGroup::create();
    scenegraph->add(bindGraphicsPipeline);

添加图元

使用类似drawArray方式添加

    // set up vertex arrays
    auto vertices = vsg::vec3Array::create(
        { {-0.5f, -0.5f, 0.5f}, 
         {0.5f, -0.5f, 0.5f},
         {0.5f, 0.5f, 0.5f} }); 
    // setup geometry
    auto drawCommands = vsg::Commands::create();
    drawCommands->addChild(vsg::BindVertexBuffers::create(0, vsg::DataList{ vertices}));   
    drawCommands->addChild(vsg::Draw::create(3, 1, 0, 0));

    scenegraph->addChild(drawCommands);

类似drawElement方式添加

    // set up vertex arrays
    auto vertices = vsg::vec3Array::create(
        { {-0.5f, -0.5f, 0.5f}, 
         {0.5f, -0.5f, 0.5f},
         {0.5f, 0.5f, 0.5f} }); 

    // in vulkan NDC, x axis point to right, y axis point to bottom, z point in the screen, it is right handed system
    // in this section, use indices to let the traingle point out. 
    auto indices = vsg::ushortArray::create(
        { 0, 2, 1});
    // setup geometry
    auto drawCommands = vsg::Commands::create();
    drawCommands->addChild(vsg::BindVertexBuffers::create(0, vsg::DataList{ vertices }));
    drawCommands->addChild(vsg::BindIndexBuffer::create(indices));
    drawCommands->addChild(vsg::DrawIndexed::create(3, 1, 0, 0, 0));

    scenegraph->addChild(drawCommands);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亮微尘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值