diff --git a/src/presenter.cpp b/src/presenter.cpp index 8b41e385c8ad883511583ee87b0a01ac2e654257..bc4fa5beebc7074ff436fa849f1258f42daa83e5 100644 --- a/src/presenter.cpp +++ b/src/presenter.cpp @@ -13,43 +13,101 @@ Presenter::Presenter() Presenter::~Presenter() {} -void Presenter::initialize() -{ - osi::presenters()->push_back<com::Folder>("maker")->push_back<com::Link>(self_name() + ".link", this); +static com::Folder *generate_grid() { + auto shader = gfx::shader_system()->insert_shader("grid_shader", gfx::Shader::Pipeline::FORWARD, gfx::Shader::UNLIT, {"shaders"}); + auto color = shader->insert<gfx::Shader::UniformNode>(gfx::Shader::Node::SlotDefinition{"color", "color", gfx::Shader::Node::DataType::ElementType::VEC3}); + shader->connect(shader->root(), gfx::Shader::MasterNodeUnlit::inputs::color, color, 0); + + auto material = gfx::material_system()->insert_material("grid_material", shader, {gfx::material_system()->materials()->name()}); + + auto grid = gfx::object_system()->insert_object( + { "grid" }, + material, + gfx::buffer_generators()->insert_procedural_grid() + ); + gfx::object_system()->push_frame_back(grid, grid_frame()); + + return grid; +} + +static com::Folder *generate_box(vec3 position, vec3 half_sizes_along_axes) { + auto shader = gfx::shader_system()->insert_shader("box_shader", gfx::Shader::Pipeline::FORWARD, gfx::Shader::LIT, {"shaders"}); + auto texture = shader->insert<gfx::Shader::TextureNode>("/home/pbabic/Repositories/age_app_template/data/age/texture/UV.jpeg"); + auto container = shader->insert<gfx::Shader::TextureNode>("/home/pbabic/Repositories/age_app_template/data/age/texture/container.jpg"); + + auto tex_coords = shader->insert<gfx::Shader::NodeVaryingTexcoord>(); + shader->connect(texture, gfx::Shader::TextureNode::inputs::tex_coord, tex_coords, 0); + shader->connect(container, gfx::Shader::TextureNode::inputs::tex_coord, tex_coords, 0); + + auto add = shader->insert<gfx::Shader::NodeVectorAdd>(gfx::Shader::Node::DataType::ElementType::VEC3); + shader->connect(add, 0, texture, 0); + shader->connect(add, 1, container, 0); + + shader->connect(shader->root(), gfx::Shader::MasterNodeLit::inputs::ambient, add, 0); + shader->connect(shader->root(), gfx::Shader::MasterNodeLit::inputs::diffuse, add, 0); + shader->connect(shader->root(), gfx::Shader::MasterNodeLit::inputs::specular, add, 0); + + + auto material = gfx::material_system()->insert_material("box_material", shader, {gfx::material_system()->materials()->name()}); + + auto box = gfx::object_system()->insert_object( + { "test_box" }, + material, + gfx::buffer_generators()->insert_procedural_box_solid(half_sizes_along_axes , "test_box", { "maker" }) + ); + auto frame = root()->push_back<com::Folder>("test_box")->push_back<com::Frame>(); + frame->set_origin(position); + gfx::object_system()->push_frame_back(box, frame); + + return box; +} + +static com::Folder *generate_point_light(vec3 position, vec3 color, float radius) { + auto point_light_frame = root()->push_back<com::Folder>("point_light")->push_back<com::Frame>(); + point_light_frame->move_origin(position); + auto point_light = gfx::light_system()->insert_light<gfx::PointLight>({"point_light"}, point_light_frame, color, radius); - // Generate grid - auto grid_shader = gfx::shader_system()->insert_shader("grid_shader", gfx::Shader::Pipeline::FORWARD, {"shaders"}); - auto grid_color = grid_shader->insert<gfx::Shader::NodeConstant>(vec3{0, 0, 0}); - grid_shader->connect(grid_shader->root(), gfx::Shader::MasterNode::inputs::color, grid_color, 0); + auto shader = gfx::shader_system()->insert_shader("point_light_shader", gfx::Shader::Pipeline::FORWARD, gfx::Shader::UNLIT, {"shaders"}); + auto color_node = shader->insert<gfx::Shader::UniformNode>(gfx::Shader::Node::SlotDefinition{"color", "color", gfx::Shader::Node::DataType::ElementType::VEC3}); + shader->connect(shader->root(), gfx::Shader::MasterNodeUnlit::inputs::color, color_node, 0); + auto material = gfx::material_system()->insert_material("point_light_material", shader, {gfx::material_system()->materials()->name()}); + material->set_uniform(color_node->name(), color); + + // point light visualizer box gfx::object_system()->push_frame_back( gfx::object_system()->insert_object( - { "grid" }, - grid_shader, - gfx::buffer_generators()->insert_procedural_grid() + { "point_light_box" }, + material, + gfx::buffer_generators()->insert_procedural_box_solid({0.1, 0.1, 0.1}, "point_light_box", {"maker"}) ), - grid_frame() + point_light_frame ); - // Generate box - auto box_shader = gfx::shader_system()->insert_shader("box_shader", gfx::Shader::Pipeline::FORWARD, {"shaders"}); - - auto blue = box_shader->insert<gfx::Shader::NodeConstant>(vec3{0, 0, 1}); - auto green = box_shader->insert<gfx::Shader::NodeConstant>(vec3{0, 0.8, 0}); - auto box_color = box_shader->insert<gfx::Shader::NodeVectorAdd>(gfx::Shader::Node::DataType::ElementType::VEC3); - box_shader->connect(box_color, gfx::Shader::NodeVectorAdd::left, blue, 0); - box_shader->connect(box_color, gfx::Shader::NodeVectorAdd::right, green, 0); - box_shader->connect(box_shader->root(), gfx::Shader::MasterNode::inputs::color, box_color, 0); - - com::Folder* const test_box = gfx::object_system()->insert_object( - { "maker", "test_box" }, - box_shader, - 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>() - ); + return point_light; +} + +void Presenter::initialize() +{ + osi::presenters()->push_back<com::Folder>("maker")->push_back<com::Link>(self_name() + ".link", this); + + auto grid = generate_grid(); + auto box = generate_box(vec3{0}, vec3{0.5}); + + auto point_light = generate_point_light({2, 1, 1}, {0, 1, 1}, 20); + gfx::object_system()->insert_light(box, point_light); + + // directional light + auto light_frame = root()->push_back<com::Folder>("dir_light")->push_back<com::Frame>(); + light_frame->set_rotation(quat{{1.2, 0.8, 0.3}}); + auto light = gfx::light_system()->insert_light<gfx::DirectionalLight>({"dir_light"}, light_frame, vec3{0.75, 0.5, 0.5}); + gfx::object_system()->insert_light(box, light); + + // ambient light + gfx::object_system()->insert_light(box, gfx::light_system()->insert_light<gfx::AmbientLight>({"ambient_light"}, nullptr, vec3{0.1, 1, 0.5})); + + print_tree(*root()); + print_tree(*gfx::material_system()->folder()); } void Presenter::release()