Skip to content
Snippets Groups Projects
presenter.cpp 4.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • Marek Trtik's avatar
    Marek Trtik committed
    #include <maker/presenter.hpp>
    #include <maker/index.hpp>
    
    #include <maker/handlers/button_handler.hpp>
    
    Marek Trtik's avatar
    Marek Trtik committed
    #include <gfx/index.hpp>
    #include <osi/index.hpp>
    #include <com/math_files.hpp>
    
    #include <utils/context_utils.hpp>
    #include <iostream>
    #include <sstream>
    #include <fstream>
    
    Marek Trtik's avatar
    Marek Trtik committed
    
    namespace mak {
    
    Presenter::Presenter()
        : com::Runner{ self_name() }
    {}
    
    Presenter::~Presenter()
    {}
    
    void Presenter::initialize()
    {
        osi::presenters()->push_back<com::Folder>("maker")->push_back<com::Link>(self_name() + ".link", this);
    
    
        gfx::Shader* forward_uniform_color_shader =  gfx::shader_generators()->insert_shader_forward_uniform_color();
    
    
    Marek Trtik's avatar
    Marek Trtik committed
        gfx::object_system()->push_frame_back(
            gfx::object_system()->insert_object(
                { "grid" },
                gfx::shader_generators()->insert_shader_forward_varying_color(),
                gfx::buffer_generators()->insert_procedural_grid()
                ),
            grid_frame()
            );
    
        // --- BEGIN test_box ---
        com::Folder* const test_box = gfx::object_system()->insert_object(
                { "maker", "test_box" },
    
                forward_uniform_color_shader,
    
    Marek Trtik's avatar
    Marek Trtik committed
                gfx::buffer_generators()->insert_procedural_box_solid({ 0.5f, 0.5f, 0.5f } , "test_box", { "maker" })
                );
        gfx::object_system()->push_frame_back(
            test_box ,
            root()->push_back<com::Folder>("test_box")->push_back<com::Frame>()
            );
        gfx::object_system()->get_uniforms(test_box)->push_back<com::FileVec3>("material_ambient_color", vec3{ 0.75f, 0.75f, 0 });
    
        // The second instance
        com::Frame* const frame2 = root()->push_back<com::Folder>("test_box_2")->push_back<com::Frame>();
        frame2->move_origin({ 0.5f, 0, 1.5f });
        gfx::object_system()->push_frame_back(test_box, frame2);
    
        // --- END test_box ---
    
    
        // UI
        float z_coord = -gfx::camera_system()->active_camera()->near() - 0.001f;
        // Create gfx/ui/ and maker/ui/ folders
        com::Folder* ui_elem1 = gfx::ui_system()->insert_element("ui_elem1");
        com::Folder* ui_elem2 = gfx::ui_system()->insert_element("ui_elem2");
        com::Folder* ui_elem_folder1 = root()->push_back<com::Folder>(ui_elem1->name());
        com::Folder* ui_elem_folder2 = root()->push_back<com::Folder>(ui_elem2->name());
        gfx::ui_system()->set_parent(ui_elem1, camera_frame());
        gfx::ui_system()->set_parent(ui_elem2, camera_frame());
    
        // Create buffer
        gfx::Buffer* ui_buffer = gfx::buffer_generators()->insert_procedural_rectangle({ 1.0f, 1.0f }, "ui_buffer", { "ui" });
    
        // Set variables
        gfx::ui_system()->get_transform(ui_elem1)->push_back<com::FileVec3>("pivot", vec3{ 0.03f, 0.02f, z_coord });
        gfx::ui_system()->get_transform(ui_elem1)->push_back<com::FileScalar>("width", 0.01f);
        gfx::ui_system()->get_transform(ui_elem1)->push_back<com::FileScalar>("height", 0.005f);
        gfx::ui_system()->get_transform(ui_elem2)->push_back<com::FileVec3>("pivot", vec3{ -0.03f, 0.04f, z_coord });
        gfx::ui_system()->get_transform(ui_elem2)->push_back<com::FileScalar>("width", 0.01f);
        gfx::ui_system()->get_transform(ui_elem2)->push_back<com::FileScalar>("height", 0.005f);
    
        // Insert UI elements into object system
        gfx::ui_system()->insert_object(ui_elem1, ui_elem_folder1, forward_uniform_color_shader, ui_buffer);
        gfx::ui_system()->insert_object(ui_elem2, ui_elem_folder2, forward_uniform_color_shader, ui_buffer);
    
    Marek Trtik's avatar
    Marek Trtik committed
        // --- BEGIN directional light ---
    
        com::Frame* const frame_light = root()->push_back<com::Folder>("directional_light")->push_back<com::Frame>();
        com::Folder* const dir_light = gfx::light_system()->insert_light<gfx::DirectionalLight>(
                { "global", "directional" },
                frame_light,
                vec4{ 0.75f, 0.75f, 0.75f, 1 }
                );
    
        // --- END directional light ---
    
    
        // BEGIN Handlers
        ButtonHandler* button_handler = root()->find<ButtonHandler>(ButtonHandler::self_name());
        gfx::ui_system()->add_handler(ui_elem1, button_handler);
        gfx::ui_system()->add_handler(ui_elem2, button_handler);
    
        // END handlers
    
    
    Marek Trtik's avatar
    Marek Trtik committed
        gfx::object_system()->insert_light(test_box, dir_light);
        gfx::light_system()->insert_shadow_caster(dir_light, test_box);
    
        print_tree(*gfx::root(), std::cout);
        print_tree(*mak::root(), std::cout);
    
    Marek Trtik's avatar
    Marek Trtik committed
    }
    
    void Presenter::release()
    {
        osi::presenters()->find<com::Folder>("maker")->erase(self_name() + ".link");
    
        root()->erase("test_box");
    
        root()->erase("test_box2");
        root()->erase("ui_elem1");
        root()->erase("ui_elem2");
    
    Marek Trtik's avatar
    Marek Trtik committed
    }
    
    void Presenter::next_round()
    {
        gfx::renderer()->clear_render_buffers();
        gfx::renderer()->present_collection(gfx::object_system()->objects());
    }
    
    }