augment_manager_v2.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "augment_manager_v2_impl.h"
  2. #include "core/imgui_utility.hpp"
  3. #include "core/math_helper.hpp"
  4. #include <glm/gtc/type_ptr.hpp>
  5. glm::mat4 augment_manager_v2::impl::item_store_type::extra_transform() const {
  6. return to_transform_mat(extra_offset, extra_rotation);
  7. }
  8. glm::mat4 augment_manager_v2::impl::item_store_type::get_transform() const {
  9. return base_transform * extra_transform();
  10. }
  11. void augment_manager_v2::impl::item_store_type::update_transform(impl *_pimpl) {
  12. if (transform_var.empty()) return;
  13. if (_pimpl->sophiar_conn == nullptr) return;
  14. auto trans = _pimpl->sophiar_conn->
  15. query_transform_variable(transform_var);
  16. if (trans.has_value()) {
  17. base_transform = to_mat4(*trans);
  18. is_tracked = true;
  19. } else {
  20. is_tracked = false;
  21. }
  22. }
  23. augment_manager_v2::impl::impl(const create_config &_conf) {
  24. conf = _conf;
  25. sophiar_conn = conf.sophiar_conn;
  26. stream = conf.stream;
  27. for (auto &item: conf.item_list) {
  28. auto &st = item_list.emplace_back(item);
  29. // special hacks
  30. if (item.type == ITEM_IMAGE) {
  31. st.flip_y = item.flip_y;
  32. }
  33. }
  34. ps_info = query_point_size_info();
  35. }
  36. void augment_manager_v2::impl::update(const camera_info &info, bool no_commit) {
  37. auto ren_info = std::make_shared<scene_render_info>();
  38. // load from scene_name in player mode
  39. if (conf.player_mode) {
  40. auto scene = OBJ_QUERY(scene_ptr, conf.scene_name);
  41. if (scene != nullptr) {
  42. *ren_info = *scene;
  43. }
  44. no_commit = true;
  45. }
  46. if (!conf.player_mode || player_allow_control) {
  47. ren_info->camera = info;
  48. ren_info->light.follow_camera = enable_light_follow_camera;
  49. ren_info->light.direction = to_vec3(light_direction);
  50. }
  51. ren_info->stream = stream;
  52. for (auto &item: item_list) {
  53. if (!item.visible) continue;
  54. item.update_transform(this);
  55. if (!item.is_tracked && !ignore_missing) continue;
  56. auto ren_item = scene_render_info::item_type();
  57. ren_item.alpha = item.alpha;
  58. ren_item.transform = item.get_transform();
  59. switch (item.type) {
  60. case ITEM_IMAGE: {
  61. auto image_info = scene_render_info::image_info{
  62. .img = to_image(item.name),
  63. .flip_y = item.flip_y,
  64. };
  65. ren_item.info = image_info;
  66. break;
  67. }
  68. case ITEM_MESH: {
  69. auto mesh_info = scene_render_info::mesh_info{
  70. .mesh = OBJ_QUERY(mesh_ptr, item.name),
  71. .enable_depth_alpha = item.enable_depth_alpha,
  72. .alpha_factor = item.alpha_factor,
  73. };
  74. mesh_info.material = {
  75. .ambient = item.color * item.ambient_factor,
  76. .diffuse = item.color
  77. };
  78. ren_item.info = mesh_info;
  79. break;
  80. }
  81. case ITEM_PC: {
  82. auto pc_info = scene_render_info::pc_info{
  83. .pc = OBJ_QUERY(pc_ptr, item.name),
  84. .point_size = item.point_size,
  85. .color = item.color,
  86. };
  87. ren_item.info = pc_info;
  88. break;
  89. }
  90. default: {
  91. RET_ERROR;
  92. }
  93. }
  94. ren_info->items.push_back(ren_item);
  95. }
  96. q_this->pre_render_sig(ren_info);
  97. last_scene_info = ren_info;
  98. if (!no_commit && conf.scene_name != invalid_obj_name) {
  99. OBJ_SAVE(conf.scene_name, ren_info);
  100. }
  101. }
  102. void augment_manager_v2::impl::render() {
  103. if (!enable) return;
  104. render_scene(last_scene_info);
  105. q_this->post_render_sig(last_scene_info);
  106. }
  107. void augment_manager_v2::impl::show() {
  108. ImGui::Checkbox("Enable", &enable);
  109. if (!enable) return;
  110. ImGui::SameLine();
  111. ImGui::Checkbox("Ignore Missing", &ignore_missing);
  112. if (conf.player_mode) {
  113. ImGui::SameLine();
  114. ImGui::Checkbox("Allow Control", &player_allow_control);
  115. }
  116. for (auto &item: item_list) {
  117. if (ImGui::TreeNode(item.disp_name.c_str())) {
  118. auto tree_close = sg::make_scope_guard([] { ImGui::TreePop(); });
  119. ImGui::Checkbox("Visibility", &item.visible);
  120. if (!item.visible) continue;
  121. ImGui::DragFloat("Alpha", &item.alpha, 0.005f, 0.0f, 1.0f);
  122. if (item.type == ITEM_MESH || item.type == ITEM_PC) {
  123. ImGui::ColorEdit3("Color", glm::value_ptr(item.color));
  124. ImGui::DragFloat3("Offset (mm)", glm::value_ptr(item.extra_offset),
  125. 0.05f, 0.0f, 0.0f, "%.02f");
  126. ImGui::DragFloat3("Offset (deg)", glm::value_ptr(item.extra_rotation),
  127. 0.1f, -180.0f, 180.0f, "%.01f");
  128. }
  129. if (item.type == ITEM_MESH) {
  130. ImGui::Checkbox("Depth Alpha", &item.enable_depth_alpha);
  131. if (item.enable_depth_alpha) {
  132. ImGui::DragFloat("Alpha Factor", &item.alpha_factor, 0.005f, 0.0f, 1.0f);
  133. }
  134. ImGui::DragFloat("Ambient Factor", &item.ambient_factor, 0.005f, 0.0f, 1.0f);
  135. }
  136. if (item.type == ITEM_PC) {
  137. ImGui::DragFloat("Point Size", &item.point_size, ps_info.delta,
  138. ps_info.min, ps_info.max, "%.01f");
  139. }
  140. }
  141. }
  142. if (ImGui::TreeNode("Light")) {
  143. ImGui::Checkbox("Follow Camera", &enable_light_follow_camera);
  144. if (!enable_light_follow_camera) {
  145. ImGui::gizmo3D("##direction", light_direction, ImGui::CalcItemWidth());
  146. }
  147. ImGui::TreePop();
  148. }
  149. }
  150. augment_manager_v2::augment_manager_v2(const create_config &conf)
  151. : pimpl(std::make_unique<impl>(conf)) {
  152. pimpl->q_this = this;
  153. }
  154. augment_manager_v2::~augment_manager_v2() = default;
  155. void augment_manager_v2::update(const camera_info &info) {
  156. pimpl->update(info);
  157. }
  158. void augment_manager_v2::render(const camera_info &info) {
  159. pimpl->update(info, true); // no commit
  160. pimpl->render();
  161. }
  162. void augment_manager_v2::show() {
  163. pimpl->show();
  164. }
  165. augment_manager_v2::item_list_type
  166. augment_manager_v2::item_list_from_v1(const item_list_v1_type &items) {
  167. auto ret = item_list_type();
  168. ret.reserve(items.size());
  169. auto hasher = std::hash<std::string>();
  170. using item_type = create_config::item_type;
  171. for (auto &v1: items) {
  172. auto item = item_type{
  173. .type = ITEM_MESH,
  174. .disp_name = v1.name,
  175. .name = next_name(),
  176. .transform_var = v1.transform_var,
  177. };
  178. // TODO: bg_name ignored
  179. ret.push_back(item);
  180. // create mesh
  181. auto mesh = mesh_type::create({.path = v1.model_path});
  182. mesh->set_meta_any(META_SERIES_NAME, (size_t) hasher(v1.name));
  183. mesh->set_meta_any(META_MESH_NO_TRANSFER, true);
  184. OBJ_SAVE(item.name, mesh);
  185. }
  186. return ret;
  187. }
  188. void augment_manager_v2::record_current_camera_helper(camera_augment_helper_v2 *cam) {
  189. pimpl->cam_helper = cam;
  190. }
  191. camera_augment_helper_v2 *augment_manager_v2::get_current_camera_helper() {
  192. return pimpl->cam_helper;
  193. }