diff --git a/src/AnimGraph/AnimGraphEditor.cc b/src/AnimGraph/AnimGraphEditor.cc index 0f5513f..d82e8f2 100644 --- a/src/AnimGraph/AnimGraphEditor.cc +++ b/src/AnimGraph/AnimGraphEditor.cc @@ -61,10 +61,10 @@ void RemoveBlendTreeConnectionsForSocket( BlendTreeResource& blend_tree_resource, AnimNodeResource* node_resource, Socket& socket) { - std::vector::iterator iter = - blend_tree_resource.m_connections.begin(); + std::vector::const_iterator iter = + blend_tree_resource.GetConnections().begin(); - while (iter != blend_tree_resource.m_connections.end()) { + while (iter != blend_tree_resource.GetConnections().end()) { // TODO adjust for refactor assert(false); @@ -299,11 +299,11 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { #if 1 for (size_t node_id = 0, - n = sGraphGresource.m_blend_tree_resource.m_nodes.size(); + n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); node_id < n; node_id++) { AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.m_nodes[node_id]; + sGraphGresource.m_blend_tree_resource.GetNode(node_id); if (node_id == 0 || node_id == 1) { // continue; @@ -346,31 +346,31 @@ void AnimGraphEditorUpdate(ax::NodeEditor::EditorContext* context) { int link_id = 0; for (size_t connection_id = 0, - n = sGraphGresource.m_blend_tree_resource.m_connections.size(); + n = sGraphGresource.m_blend_tree_resource.GetNumConnections(); connection_id < n; connection_id++) { - const BlendTreeConnectionResource& connection_resource = - sGraphGresource.m_blend_tree_resource.m_connections[connection_id]; + const BlendTreeConnectionResource* connection_resource = + sGraphGresource.m_blend_tree_resource.GetConnection(connection_id); const AnimNodeResource* source_node_resource = - sGraphGresource.m_blend_tree_resource - .m_nodes[connection_resource.source_node_index]; + sGraphGresource.m_blend_tree_resource.GetNode( + connection_resource->source_node_index); int source_socket_index = source_node_resource->m_socket_accessor->GetOutputIndex( - connection_resource.source_socket_name.c_str()); + connection_resource->source_socket_name.c_str()); const AnimNodeResource* target_node_resource = - sGraphGresource.m_blend_tree_resource - .m_nodes[connection_resource.target_node_index]; + sGraphGresource.m_blend_tree_resource.GetNode( + connection_resource->target_node_index); int target_socket_index = target_node_resource->m_socket_accessor->GetInputIndex( - connection_resource.target_socket_name.c_str()); + connection_resource->target_socket_name.c_str()); int source_socket_id = GetNodeOutputSocketId( - static_cast(connection_resource.source_node_index), + static_cast(connection_resource->source_node_index), source_socket_index); int target_socket_id = GetNodeInputSocketId( - static_cast(connection_resource.target_node_index), + static_cast(connection_resource->target_node_index), target_socket_index); ax::NodeEditor::Link(link_id++, source_socket_id, target_socket_id); @@ -407,11 +407,11 @@ void LegacyAnimGraphEditorUpdate() { if (ImGui::Button("Load")) { sGraphGresource.LoadFromFile("editor_graph.json"); - for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.m_nodes.size(); + for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); i < n; i++) { const AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.m_nodes[i]; + sGraphGresource.m_blend_tree_resource.GetNode(i); ImNodes::SetNodeGridSpacePos( i, ImVec2(node_resource->m_position[0], node_resource->m_position[1])); @@ -485,9 +485,9 @@ void LegacyAnimGraphEditorUpdate() { if (node_type_name != "") { AnimNodeResource* node_resource = AnimNodeResourceFactory(node_type_name); - size_t node_id = sGraphGresource.m_blend_tree_resource.m_nodes.size(); + size_t node_id = sGraphGresource.m_blend_tree_resource.GetNumNodes(); ImNodes::SetNodeScreenSpacePos(node_id, ImGui::GetMousePos()); - sGraphGresource.m_blend_tree_resource.m_nodes.push_back(node_resource); + sGraphGresource.m_blend_tree_resource.AddNode(node_resource); } ImGui::EndPopup(); @@ -496,11 +496,11 @@ void LegacyAnimGraphEditorUpdate() { ImGui::PopStyleVar(ImGuiStyleVar_WindowPadding); } - for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.m_nodes.size(); + for (size_t i = 0, n = sGraphGresource.m_blend_tree_resource.GetNumNodes(); i < n; i++) { AnimNodeResource* node_resource = - sGraphGresource.m_blend_tree_resource.m_nodes[i]; + sGraphGresource.m_blend_tree_resource.GetNode(i); ImNodes::BeginNode(i); ImGui::PushItemWidth(110.0f); @@ -624,30 +624,30 @@ void LegacyAnimGraphEditorUpdate() { } for (size_t i = 0, - n = sGraphGresource.m_blend_tree_resource.m_connections.size(); + n = sGraphGresource.m_blend_tree_resource.GetNumConnections(); i < n; i++) { - const BlendTreeConnectionResource& connection = - sGraphGresource.m_blend_tree_resource.m_connections[i]; + const BlendTreeConnectionResource* connection = + sGraphGresource.m_blend_tree_resource.GetConnection(i); int start_attr, end_attr; const AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource - .m_nodes[connection.source_node_index]; + sGraphGresource.m_blend_tree_resource.GetNode( + connection->source_node_index); int source_socket_index = source_node->m_socket_accessor->GetOutputIndex( - connection.source_socket_name.c_str()); + connection->source_socket_name.c_str()); const AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource - .m_nodes[connection.target_node_index]; + sGraphGresource.m_blend_tree_resource.GetNode( + connection->target_node_index); int target_socket_index = target_node->m_socket_accessor->GetInputIndex( - connection.target_socket_name.c_str()); + connection->target_socket_name.c_str()); start_attr = GenerateOutputAttributeId( - connection.source_node_index, + connection->source_node_index, source_socket_index); end_attr = GenerateInputAttributeId( - connection.target_node_index, + connection->target_node_index, target_socket_index); ImNodes::Link(i, start_attr, end_attr); @@ -672,18 +672,22 @@ void LegacyAnimGraphEditorUpdate() { BlendTreeConnectionResource connection; connection.source_node_index = node_start_id; const AnimNodeResource* source_node = - sGraphGresource.m_blend_tree_resource.m_nodes[node_start_id]; + sGraphGresource.m_blend_tree_resource.GetNode(node_start_id); connection.source_socket_name = source_node->m_socket_accessor->m_outputs[node_start_output_index] .m_name; connection.target_node_index = node_end_id; const AnimNodeResource* target_node = - sGraphGresource.m_blend_tree_resource.m_nodes[node_end_id]; + sGraphGresource.m_blend_tree_resource.GetNode(node_end_id); connection.target_socket_name = target_node->m_socket_accessor->m_inputs[node_end_input_index].m_name; - sGraphGresource.m_blend_tree_resource.m_connections.push_back(connection); + sGraphGresource.m_blend_tree_resource.ConnectSockets( + source_node, + connection.source_socket_name, + target_node, + connection.target_socket_name); } if (ImGui::IsKeyPressed(ImGuiKey_Delete, false)) { @@ -693,8 +697,19 @@ void LegacyAnimGraphEditorUpdate() { // Handle link detachements. int link_id = 0; if (ImNodes::IsLinkDestroyed(&link_id)) { - sGraphGresource.m_blend_tree_resource.m_connections.erase( - sGraphGresource.m_blend_tree_resource.m_connections.begin() + link_id); + BlendTreeConnectionResource* connection = + sGraphGresource.m_blend_tree_resource.GetConnection(link_id); + AnimNodeResource* source_node = + sGraphGresource.m_blend_tree_resource.GetNode( + connection->source_node_index); + AnimNodeResource* target_node = + sGraphGresource.m_blend_tree_resource.GetNode( + connection->target_node_index); + sGraphGresource.m_blend_tree_resource.DisconnectSockets( + source_node, + connection->source_socket_name, + target_node, + connection->target_socket_name); } int selected_nodes[ImNodes::NumSelectedNodes()]; @@ -707,9 +722,9 @@ void LegacyAnimGraphEditorUpdate() { if (ImNodes::NumSelectedNodes() == 1) { if (selected_nodes[0] - < sGraphGresource.m_blend_tree_resource.m_nodes.size()) { + < sGraphGresource.m_blend_tree_resource.GetNumNodes()) { AnimNodeResource* selected_node = - sGraphGresource.m_blend_tree_resource.m_nodes[selected_nodes[0]]; + sGraphGresource.m_blend_tree_resource.GetNode(selected_nodes[0]); AnimGraphEditorRenderSidebar( sGraphGresource.m_blend_tree_resource, selected_node); diff --git a/src/main.cc b/src/main.cc index b509de4..846946d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -16,13 +16,16 @@ #define GLFW_INCLUDE_NONE #include +#include "3rdparty/imgui-node-editor/imgui_node_editor.h" #include "3rdparty/json/json.hpp" +#include "AnimGraph/AnimGraphBlendTree.h" +#include "AnimGraph/AnimGraphData.h" #include "Camera.h" #include "GLFW/glfw3.h" #include "SkinnedMesh.h" +#include "SkinnedMeshRenderer.h" #include "SkinnedMeshResource.h" #include "src/AnimGraph/AnimGraphEditor.h" -#include "3rdparty/imgui-node-editor/imgui_node_editor.h" const int Width = 1024; const int Height = 768; @@ -49,7 +52,6 @@ static void draw_imgui(ImDrawData*); #include // fmodf #include -#include "SkinnedMeshRenderer.h" #include "ozz/animation/runtime/animation.h" #include "ozz/animation/runtime/sampling_job.h" #include "ozz/animation/runtime/skeleton.h" @@ -156,14 +158,10 @@ struct Viewport { this->pass = sg_make_pass(&offscreen_pass_desc); sg_pipeline_desc gl_pipeline_desc = { - .depth = { - .compare = SG_COMPAREFUNC_LESS_EQUAL, - .write_enabled = true - }, + .depth = {.compare = SG_COMPAREFUNC_LESS_EQUAL, .write_enabled = true}, .cull_mode = SG_CULLMODE_BACK, - .sample_count = cMSAASampleCount - }; -// this->pip = sg_make_pipeline(gl_pipeline_desc); + .sample_count = cMSAASampleCount}; + // this->pip = sg_make_pipeline(gl_pipeline_desc); } }; @@ -186,7 +184,7 @@ struct ApplicationConfig { ax::NodeEditor::EditorContext* context = nullptr; }; GraphEditor graph_editor; - + struct SkinnedMeshWidget { bool visible = false; int position[2] = {20, 20}; @@ -224,8 +222,10 @@ void to_json(nlohmann::json& j, const ApplicationConfig& config) { j["main_window"]["size"][1] = config.window_size[1]; j["legacy_graph_editor"]["visible"] = config.legacy_graph_editor.visible; - j["legacy_graph_editor"]["position"][0] = config.legacy_graph_editor.position[0]; - j["legacy_graph_editor"]["position"][1] = config.legacy_graph_editor.position[1]; + j["legacy_graph_editor"]["position"][0] = + config.legacy_graph_editor.position[0]; + j["legacy_graph_editor"]["position"][1] = + config.legacy_graph_editor.position[1]; j["legacy_graph_editor"]["size"][0] = config.legacy_graph_editor.size[0]; j["legacy_graph_editor"]["size"][1] = config.legacy_graph_editor.size[1]; @@ -234,7 +234,7 @@ void to_json(nlohmann::json& j, const ApplicationConfig& config) { j["graph_editor"]["position"][1] = config.graph_editor.position[1]; j["graph_editor"]["size"][0] = config.graph_editor.size[0]; j["graph_editor"]["size"][1] = config.graph_editor.size[1]; - + j["skinned_mesh_widget"]["visible"] = config.skinned_mesh_widget.visible; j["skinned_mesh_widget"]["position"][0] = config.skinned_mesh_widget.position[0]; @@ -287,14 +287,18 @@ void from_json(const nlohmann::json& j, ApplicationConfig& config) { if (j["legacy_graph_editor"].contains("position") and j["legacy_graph_editor"]["position"].size() == 2) { - config.legacy_graph_editor.position[0] = j["legacy_graph_editor"]["position"].at(0); - config.legacy_graph_editor.position[1] = j["legacy_graph_editor"]["position"].at(1); + config.legacy_graph_editor.position[0] = + j["legacy_graph_editor"]["position"].at(0); + config.legacy_graph_editor.position[1] = + j["legacy_graph_editor"]["position"].at(1); } if (j["legacy_graph_editor"].contains("size") and j["legacy_graph_editor"]["size"].size() == 2) { - config.legacy_graph_editor.size[0] = j["legacy_graph_editor"]["size"].at(0); - config.legacy_graph_editor.size[1] = j["legacy_graph_editor"]["size"].at(1); + config.legacy_graph_editor.size[0] = + j["legacy_graph_editor"]["size"].at(0); + config.legacy_graph_editor.size[1] = + j["legacy_graph_editor"]["size"].at(1); } } @@ -302,7 +306,7 @@ void from_json(const nlohmann::json& j, ApplicationConfig& config) { if (j["graph_editor"].contains("visible")) { config.graph_editor.visible = j["graph_editor"]["visible"]; } - + if (j["graph_editor"].contains("position") and j["graph_editor"]["position"].size() == 2) { config.graph_editor.position[0] = j["graph_editor"]["position"].at(0); @@ -315,7 +319,7 @@ void from_json(const nlohmann::json& j, ApplicationConfig& config) { config.graph_editor.size[1] = j["graph_editor"]["size"].at(1); } } - + if (j.contains("skinned_mesh_widget")) { if (j["skinned_mesh_widget"].contains("visible")) { config.skinned_mesh_widget.visible = j["skinned_mesh_widget"]["visible"]; @@ -529,7 +533,9 @@ int main() { // setup sokol_gfx and sokol_time stm_setup(); - sg_desc desc = {.logger = {.func = slog_func}, .context {.sample_count = cMSAASampleCount}}; + sg_desc desc = { + .logger = {.func = slog_func}, + .context{.sample_count = cMSAASampleCount}}; sg_setup(&desc); assert(sg_isvalid()); @@ -550,7 +556,7 @@ int main() { skinned_mesh_resource.createInstance(skinned_mesh); skinned_mesh.SetCurrentAnimation(0); - AnimGraph anim_graph; + AnimGraphBlendTree anim_graph; AnimGraphContext anim_graph_context; AnimData anim_graph_output; anim_graph_output.m_local_matrices.resize( @@ -673,7 +679,8 @@ int main() { // Graph Editor gApplicationConfig.graph_editor.config.SettingsFile = "graph_editor.json"; gApplicationConfig.graph_editor.config.NavigateButtonIndex = 2; - gApplicationConfig.graph_editor.context = ax::NodeEditor::CreateEditor(&gApplicationConfig.graph_editor.config); + gApplicationConfig.graph_editor.context = + ax::NodeEditor::CreateEditor(&gApplicationConfig.graph_editor.config); // draw loop while (!glfwWindowShouldClose(w)) { @@ -811,12 +818,12 @@ int main() { AnimGraphEditorGetRuntimeGraph(anim_graph); anim_graph_context.m_skeleton = &skinned_mesh.m_skeleton; - anim_graph.init(anim_graph_context); + anim_graph.Init(anim_graph_context); // For simplicity use first animation data output const std::vector& graph_output_sockets = - anim_graph.getGraphOutputs(); - for (const auto & output : graph_output_sockets) { + anim_graph.GetGraphOutputs(); + for (const auto& output : graph_output_sockets) { if (output.m_type == SocketType::SocketTypeAnimation) { anim_graph.SetOutput(output.m_name.c_str(), &anim_graph_output); } @@ -845,7 +852,8 @@ int main() { ImGui::Begin("Viewport", &gApplicationConfig.viewport_widget.visible); - if (ImGui::IsWindowHovered() && ImGui::IsMouseDown(ImGuiMouseButton_Right)) { + if (ImGui::IsWindowHovered() + && ImGui::IsMouseDown(ImGuiMouseButton_Right)) { if (gControlMode == ControlMode::ControlModeNone) { gControlMode = ControlMode::ControlModeFPS; Camera_CalcFromMatrix(&state.camera, &state.camera.mtxView[0]); @@ -854,8 +862,10 @@ int main() { } ImVec2 viewport_widget_size = ImGui::GetWindowSize(); - gApplicationConfig.viewport_widget.size[0] = static_cast(viewport_widget_size.x); - gApplicationConfig.viewport_widget.size[1] = static_cast(viewport_widget_size.y); + gApplicationConfig.viewport_widget.size[0] = + static_cast(viewport_widget_size.x); + gApplicationConfig.viewport_widget.size[1] = + static_cast(viewport_widget_size.y); ImGui::Text( "Viewport size: %d, %d", @@ -869,12 +879,16 @@ int main() { if (static_cast(current_size[0]) != content_size[0] || static_cast(current_size[1]) != content_size[1] || offscreen_viewport.pass.id == 0) { - offscreen_viewport.Resize(static_cast(content_size[0]), static_cast(content_size[1])); + offscreen_viewport.Resize( + static_cast(content_size[0]), + static_cast(content_size[1])); } ImGui::Image( (ImTextureID)(uintptr_t)offscreen_viewport.color_image.id, - ImVec2(static_cast(offscreen_viewport.size[0]), static_cast(offscreen_viewport.size[1])), + ImVec2( + static_cast(offscreen_viewport.size[0]), + static_cast(offscreen_viewport.size[1])), ImVec2(0.0f, 1.0f), ImVec2(1.0f, 0.0f)); @@ -897,13 +911,17 @@ int main() { if (gApplicationConfig.skinned_mesh_widget.visible) { ImGui::SetNextWindowPos( ImVec2( - static_cast(gApplicationConfig.skinned_mesh_widget.position[0]), - static_cast(gApplicationConfig.skinned_mesh_widget.position[1])), + static_cast( + gApplicationConfig.skinned_mesh_widget.position[0]), + static_cast( + gApplicationConfig.skinned_mesh_widget.position[1])), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize( ImVec2( - static_cast(gApplicationConfig.skinned_mesh_widget.size[0]), - static_cast(gApplicationConfig.skinned_mesh_widget.size[1])), + static_cast( + gApplicationConfig.skinned_mesh_widget.size[0]), + static_cast( + gApplicationConfig.skinned_mesh_widget.size[1])), ImGuiCond_FirstUseEver); ImGui::Begin( @@ -930,13 +948,17 @@ int main() { if (gApplicationConfig.animation_player_widget.visible) { ImGui::SetNextWindowPos( ImVec2( - static_cast(gApplicationConfig.animation_player_widget.position[0]), - static_cast(gApplicationConfig.animation_player_widget.position[1])), + static_cast( + gApplicationConfig.animation_player_widget.position[0]), + static_cast( + gApplicationConfig.animation_player_widget.position[1])), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize( ImVec2( - static_cast(gApplicationConfig.animation_player_widget.size[0]), - static_cast(gApplicationConfig.animation_player_widget.size[1])), + static_cast( + gApplicationConfig.animation_player_widget.size[0]), + static_cast( + gApplicationConfig.animation_player_widget.size[1])), ImGuiCond_FirstUseEver); ImGui::Begin( @@ -1015,8 +1037,8 @@ int main() { if (state.ozz.animation != nullptr) { state.ozz.sampling_job.animation = state.ozz.animation; - state.ozz.sampling_job.ratio = - static_cast(state.time.absolute) / state.ozz.animation->duration(); + state.ozz.sampling_job.ratio = static_cast(state.time.absolute) + / state.ozz.animation->duration(); state.ozz.sampling_job.context = &skinned_mesh.m_sampling_context; state.ozz.sampling_job.output = ozz::make_span(skinned_mesh.m_local_matrices); @@ -1031,9 +1053,10 @@ int main() { if (state.time.use_graph && !anim_graph.m_nodes.empty() && state.time.anim_update_time > 0.) { - anim_graph.markActiveNodes(); - anim_graph.updateTime(state.time.anim_update_time); - anim_graph.evaluate(anim_graph_context); + // TODO: update for new API after embedding refactor + // anim_graph.MarkActiveNodes(); + // anim_graph.UpdateTime()pdateTime(state.time.anim_update_time); + // anim_graph.evaluate(anim_graph_context); skinned_mesh.m_local_matrices = anim_graph_output.m_local_matrices; skinned_mesh.CalcModelMatrices(); @@ -1099,8 +1122,10 @@ int main() { ImGuiWindowFlags_MenuBar); ImVec2 graph_editor_position = ImGui::GetWindowPos(); - gApplicationConfig.legacy_graph_editor.position[0] = graph_editor_position.x; - gApplicationConfig.legacy_graph_editor.position[1] = graph_editor_position.y; + gApplicationConfig.legacy_graph_editor.position[0] = + graph_editor_position.x; + gApplicationConfig.legacy_graph_editor.position[1] = + graph_editor_position.y; ImVec2 graph_editor_size = ImGui::GetWindowSize(); gApplicationConfig.legacy_graph_editor.size[0] = graph_editor_size.x;