From 33a8e3af8b50084e9b4dadc8ae0294a216807cbd Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 26 Feb 2018 20:49:27 -0800 Subject: [PATCH] working on a more versitile fps counter, but need to move to the newest imgui first --- CMakeLists.txt | 2 +- include/Camera.h | 4 +- include/imgui/imgui-multilines.hpp | 186 +++++++++++++++++++++++++++++ include/util.hpp | 31 +++-- src/Camera.cpp | 63 +++++++++- 5 files changed, 270 insertions(+), 16 deletions(-) create mode 100644 include/imgui/imgui-multilines.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 735e316..edbfb09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,7 @@ file(GLOB_RECURSE KERNELS "kernels/*.cl") file(GLOB_RECURSE SHADERS "shaders/*.vert" "shaders/*.tesc" "shaders/*.tese" "shaders/*.geom" "shaders/*.frag" "shaders/*.comp") -add_executable(${PNAME} ${SOURCES} ${HEADERS} ${KERNELS} ${SHADERS}) +add_executable(${PNAME} ${SOURCES} ${HEADERS} ${KERNELS} ${SHADERS} include/imgui/imgui-multilines.hpp) # Follow the sub directory structure to add sub-filters in VS # Gotta do it one by one unfortunately diff --git a/include/Camera.h b/include/Camera.h index 86c7392..36173b7 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -65,8 +65,8 @@ public: private: - float friction_coefficient = 0.001f; - float default_impulse = 0.1f; + float friction_coefficient = 0.1f; + float default_impulse = 0.01f; // 3D vector sf::Vector3f movement; diff --git a/include/imgui/imgui-multilines.hpp b/include/imgui/imgui-multilines.hpp new file mode 100644 index 0000000..9ad3d5e --- /dev/null +++ b/include/imgui/imgui-multilines.hpp @@ -0,0 +1,186 @@ + + +#include "imgui.h" +#include "imgui_internal.h" + + +namespace ImGui { + + static ImU32 InvertColorU32(ImU32 in) + { + ImVec4 in4 = ColorConvertU32ToFloat4(in); + in4.x = 1.f - in4.x; + in4.y = 1.f - in4.y; + in4.z = 1.f - in4.z; + return GetColorU32(in4); + } + + static void PlotMultiEx( + ImGuiPlotType plot_type, + const char* label, + int num_datas, + const char** names, + const ImColor* colors, + float(*getter)(const void* data, int idx), + const void * const * datas, + int values_count, + float scale_min, + float scale_max, + ImVec2 graph_size) + { + const int values_offset = 0; + + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true); + if (graph_size.x == 0.0f) + graph_size.x = CalcItemWidth(); + if (graph_size.y == 0.0f) + graph_size.y = label_size.y + (style.FramePadding.y * 2); + + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y)); + const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); + const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, NULL)) + return; + + // Determine scale from values if not specified + if (scale_min == FLT_MAX || scale_max == FLT_MAX) + { + float v_min = FLT_MAX; + float v_max = -FLT_MAX; + for (int data_idx = 0; data_idx < num_datas; ++data_idx) + { + for (int i = 0; i < values_count; i++) + { + const float v = getter(datas[data_idx], i); + v_min = ImMin(v_min, v); + v_max = ImMax(v_max, v); + } + } + if (scale_min == FLT_MAX) + scale_min = v_min; + if (scale_max == FLT_MAX) + scale_max = v_max; + } + + RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); + + int res_w = ImMin((int) graph_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); + int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); + + // Tooltip on hover + int v_hovered = -1; + if (IsHovered(inner_bb, 0)) + { + const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); + const int v_idx = (int) (t * item_count); + IM_ASSERT(v_idx >= 0 && v_idx < values_count); + + // std::string toolTip; + ImGui::BeginTooltip(); + const int idx0 = (v_idx + values_offset) % values_count; + if (plot_type == ImGuiPlotType_Lines) + { + const int idx1 = (v_idx + 1 + values_offset) % values_count; + Text("%8d %8d | Name", v_idx, v_idx+1); + for (int dataIdx = 0; dataIdx < num_datas; ++dataIdx) + { + const float v0 = getter(datas[dataIdx], idx0); + const float v1 = getter(datas[dataIdx], idx1); + TextColored(colors[dataIdx], "%8.4g %8.4g | %s", v0, v1, names[dataIdx]); + } + } + else if (plot_type == ImGuiPlotType_Histogram) + { + for (int dataIdx = 0; dataIdx < num_datas; ++dataIdx) + { + const float v0 = getter(datas[dataIdx], idx0); + TextColored(colors[dataIdx], "%d: %8.4g | %s", v_idx, v0, names[dataIdx]); + } + } + ImGui::EndTooltip(); + v_hovered = v_idx; + } + + for (int data_idx = 0; data_idx < num_datas; ++data_idx) + { + const float t_step = 1.0f / (float) res_w; + + float v0 = getter(datas[data_idx], (0 + values_offset) % values_count); + float t0 = 0.0f; + ImVec2 tp0 = ImVec2(t0, 1.0f - ImSaturate((v0 - scale_min) / (scale_max - scale_min))); // Point in the normalized space of our target rectangle + + const ImU32 col_base = colors[data_idx]; + const ImU32 col_hovered = InvertColorU32(colors[data_idx]); + + //const ImU32 col_base = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram); + //const ImU32 col_hovered = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered); + + for (int n = 0; n < res_w; n++) + { + const float t1 = t0 + t_step; + const int v1_idx = (int) (t0 * item_count + 0.5f); + IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); + const float v1 = getter(datas[data_idx], (v1_idx + values_offset + 1) % values_count); + const ImVec2 tp1 = ImVec2(t1, 1.0f - ImSaturate((v1 - scale_min) / (scale_max - scale_min))); + + // NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. + ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); + ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, 1.0f)); + if (plot_type == ImGuiPlotType_Lines) + { + window->DrawList->AddLine(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + } + else if (plot_type == ImGuiPlotType_Histogram) + { + if (pos1.x >= pos0.x + 2.0f) + pos1.x -= 1.0f; + window->DrawList->AddRectFilled(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + } + + t0 = t1; + tp0 = tp1; + } + } + + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); +} + +void PlotMultiLines( + const char* label, + int num_datas, + const char** names, + const ImColor* colors, + float(*getter)(const void* data, int idx), + const void * const * datas, + int values_count, + float scale_min, + float scale_max, + ImVec2 graph_size) +{ + PlotMultiEx(ImGuiPlotType_Lines, label, num_datas, names, colors, getter, datas, values_count, scale_min, scale_max, graph_size); +} + +void PlotMultiHistograms( + const char* label, + int num_hists, + const char** names, + const ImColor* colors, + float(*getter)(const void* data, int idx), + const void * const * datas, + int values_count, + float scale_min, + float scale_max, + ImVec2 graph_size) +{ + PlotMultiEx(ImGuiPlotType_Histogram, label, num_hists, names, colors, getter, datas, values_count, scale_min, scale_max, graph_size); +} + +} // namespace ImGui \ No newline at end of file diff --git a/include/util.hpp b/include/util.hpp index 6faf77d..7a6ef81 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -18,7 +18,15 @@ const float PI_F = 3.14159265358979f; struct fps_counter { public: - fps_counter() {}; + fps_counter() { + + }; + + ~fps_counter() { + for (auto i: fps_vectors){ + delete[] i; + } + }; void frame(double delta_time) { @@ -29,27 +37,36 @@ public: } frame_count++; fps_average += (delta_time - fps_average) / frame_count; + instant_fps = delta_time; } void draw() { - if (arr_pos == 200) + if (arr_pos == 1000) arr_pos = 0; - fps_array[arr_pos] = static_cast(1.0 / fps_average); + fps_array[arr_pos] = static_cast(1.0 / instant_fps); arr_pos++; ImGui::Begin("Performance"); - ImGui::PlotLines("FPS", fps_array, 200, 0, std::to_string(1.0 / fps_average).c_str(), 0.0f, 150.0f, ImVec2(200, 80)); + ImVec2 wh = ImGui::GetContentRegionAvail(); + ImGui::PlotLines("FPS", fps_array, 1000, 0, + std::to_string(1.0 / fps_average).c_str(), + 0.0f, 150.0f, wh); ImGui::End(); } private: - float fps_array[200]{60}; - int arr_pos = 0; + const unsigned int FPS_ARRAY_LENGTH = 1000; + std::vector fps_vectors; + + + float fps_array[1000]{60}; + int arr_pos = 0; - int frame_count = 0; + double instant_fps = 0; + double frame_count = 0; double fps_average = 0; }; diff --git a/src/Camera.cpp b/src/Camera.cpp index c88f001..8105217 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -76,12 +76,53 @@ int Camera::update(double delta_time) { double multiplier = 80; + +// +// +// if (movement.x < 0) +// { +// movement.x = std::min( 0.0f, movement.x + 0.01f); +// } +// else if (movement.x > 0) +// { +// movement.x = std::min( 0.0f, movement.x - 0.01f); +// } +// +// if (movement.y < 0) +// { +// movement.y = std::min( 0.0f, movement.y + 0.01f); +// } +// else if (movement.y > 0) +// { +// movement.y = std::min( 0.0f, movement.y - 0.01f); +// } +// +// +// +// +//// if (movement.x > 0) +//// movement.x -= friction_coefficient * delta_time * multiplier; +//// else if (movement.x < 0) +//// movement.x += friction_coefficient * delta_time * multiplier; +//// +//// if (movement.y > 0) +//// movement.y -= friction_coefficient * delta_time * multiplier; +//// else if (movement.y < 0) +//// movement.y += friction_coefficient * delta_time * multiplier; +//// +//// if (abs(movement.x) > 0.02f) +//// movement.x = ((movement.x > 0) - (movement.x < 0)) * 0.02f; +//// if (abs(movement.y) > 0.02f) +//// movement.y = ((movement.y > 0) - (movement.y < 0)) * 0.02f; +// //movement.x = +// //movement.y = abs(movement.y) - -((movement.y > 0) - (movement.y < 0)) * friction_coefficient * delta_time * multiplier; + position.x += static_cast(movement.x * delta_time * multiplier); position.y += static_cast(movement.y * delta_time * multiplier); position.z += static_cast(movement.z * delta_time * multiplier); - - movement.x *= static_cast(friction_coefficient * delta_time * multiplier); - movement.y *= static_cast(friction_coefficient * delta_time * multiplier); + + movement.x *= static_cast(friction_coefficient * delta_time * multiplier); + movement.y *= static_cast(friction_coefficient * delta_time * multiplier); if (position.z < 3.0f){ position.z = 3.0f; @@ -214,9 +255,12 @@ void Camera::render_gui() { ImGui::Text("Camera Inclination"); ImGui::Text("Camera Azimuth"); - ImGui::Text("Camera Pos_X"); - ImGui::Text("Camera Poz_Y"); - ImGui::Text("Camera Poz_Z"); + ImGui::Text("Camera Pos X"); + ImGui::Text("Camera Pos Y"); + ImGui::Text("Camera Pos Z"); + ImGui::Text("Camera Accel X"); + ImGui::Text("Camera Accel Y"); + ImGui::Text("Camera Accel Z"); ImGui::NextColumn(); @@ -225,6 +269,9 @@ void Camera::render_gui() { ImGui::Text("%f", position.x); ImGui::Text("%f", position.y); ImGui::Text("%f", position.z); + ImGui::Text("%f", movement.x); + ImGui::Text("%f", movement.y); + ImGui::Text("%f", movement.z); ImGui::Separator(); @@ -232,6 +279,10 @@ void Camera::render_gui() { ImGui::SliderFloat("Camera Speed", &default_impulse, 0, 4); + ImGui::Separator(); + + ImGui::SliderFloat("Camera Friction", &friction_coefficient, 0, 1); + ImGui::End(); }