From 34cdab6a4977a060e55e754c62a5d70bdba47f7a Mon Sep 17 00:00:00 2001
From: Petr Babic <pbabic@redhat.com>
Date: Thu, 18 Apr 2024 09:55:41 +0200
Subject: [PATCH] add transparent box examples

Signed-off-by: Petr Babic <pbabic@redhat.com>
---
 src/presenter.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/src/presenter.cpp b/src/presenter.cpp
index 9fd5f73..da5899a 100644
--- a/src/presenter.cpp
+++ b/src/presenter.cpp
@@ -171,6 +171,87 @@ static com::Folder *generate_point_light(
     return point_light;
 }
 
+static com::Folder *alpha_box() {
+    auto shader = gfx::shader_system()->insert_shader<gfx::ShaderGraph>(
+        { "shaders" }, "alpha-box-shader", gfx::ShaderGraph::LIT, gfx::ShaderGraph::PARTIAL
+    );
+
+    auto container = shader->insert<gfx::ShaderGraph::TextureNode>(
+        com::ContextPath{ "age", "texture", "container2.png" });
+    auto specular = shader->insert<gfx::ShaderGraph::TextureNode>(
+        com::ContextPath{ "age", "texture", "container2_specular.png" });
+
+    auto tex_coords = shader->insert<gfx::ShaderGraph::NodeVaryingTexcoord>();
+    shader->connect(container, gfx::ShaderGraph::TextureNode::inputs::tex_coord, tex_coords, 0);
+    shader->connect(specular, gfx::ShaderGraph::TextureNode::inputs::tex_coord, tex_coords, 0);
+
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::ambient, container, 0);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::diffuse, container, 0);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::specular, specular, 0);
+
+    auto alpha = shader->insert<gfx::ShaderGraph::NodeConstant>(0.5f);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::alpha, alpha, 0);
+
+    auto material = gfx::material_system()->insert_material(
+        "alpha-box-material", shader, { gfx::material_system()->materials()->name() }
+    );
+
+    auto box = gfx::object_system()->insert_object(
+        { "transparent", "alpha-box" }, material,
+        gfx::buffer_generators()->insert_procedural_box_solid(
+            vec3{0.5}, "alpha-box", { "maker" }
+        )
+    );
+
+    auto frame = root()->push_back<com::Folder>("alpha-box")->push_back<com::Frame>();
+    gfx::object_system()->push_frame_back(box, frame);
+
+    gfx::Renderer::configure_blending(true);
+
+    return box;
+}
+
+static com::Folder *alpha_box_threshold() {
+    auto shader = gfx::shader_system()->insert_shader<gfx::ShaderGraph>(
+        { "shaders" }, "alpha-box-thresh-shader", gfx::ShaderGraph::LIT, gfx::ShaderGraph::THRESHOLD
+    );
+
+    auto tex_coords = shader->insert<gfx::ShaderGraph::NodeVaryingTexcoord>();
+    auto specular = shader->insert<gfx::ShaderGraph::TextureNode>(
+        com::ContextPath{ "age", "texture", "container2_specular.png" });
+    shader->connect(specular, gfx::ShaderGraph::TextureNode::inputs::tex_coord, tex_coords, 0);
+
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::ambient, specular, 0);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::diffuse, specular, 0);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::specular, specular, 0);
+
+    auto decomp = shader->insert<gfx::ShaderGraph::NodeDecomposeVector>(gfx::ShaderGraph::Node::DataType::VEC3);
+    shader->connect(decomp, 0, specular, 0);
+
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::alpha, decomp, 0);
+
+    auto thresh = shader->insert<gfx::ShaderGraph::NodeConstant>(0.01f);
+    shader->connect(shader->root(), gfx::ShaderGraph::MasterNodeLit::inputs::alpha_threshold, thresh, 0);
+
+    auto material = gfx::material_system()->insert_material(
+        "alpha-box-thresh-material", shader, { gfx::material_system()->materials()->name() }
+    );
+
+    auto box = gfx::object_system()->insert_object(
+        { "transparent", "alpha-box-thresh" }, material,
+        gfx::buffer_generators()->insert_procedural_box_solid(
+            vec3{0.5}, "alpha-box-thresh", { "maker" }
+        )
+    );
+
+    auto frame = root()->push_back<com::Folder>("alpha-box-thresh")->push_back<com::Frame>();
+    gfx::object_system()->push_frame_back(box, frame);
+
+    gfx::Renderer::configure_blending(true);
+
+    return box;
+}
+
 void Presenter::initialize() {
     osi::presenters()->push_back<com::Folder>("maker")->push_back<com::Link>(self_name() + ".link", this);
 
@@ -223,6 +304,10 @@ void Presenter::next_round() {
     auto forward = gfx::object_system()->objects()->find<com::Folder>("forward");
     if (forward != nullptr)
         gfx::renderer()->present_collection(forward, gfx::Renderer::FORWARD);
+
+    auto transparent = gfx::object_system()->objects()->find<com::Folder>("transparent");
+    if (transparent != nullptr)
+        gfx::renderer()->present_collection(transparent, gfx::Renderer::FORWARD);
 }
 
 } // namespace mak
-- 
GitLab