diff --git a/cpp/apps/shaders_lab/CMakeLists.txt b/cpp/apps/shaders_lab/CMakeLists.txt index 66dfe9f2..e3768411 100644 --- a/cpp/apps/shaders_lab/CMakeLists.txt +++ b/cpp/apps/shaders_lab/CMakeLists.txt @@ -1,5 +1,6 @@ add_imgui_app(shaders_lab main.cpp + ${CMAKE_SOURCE_DIR}/functions/gfx/gl_loader.cpp ${CMAKE_SOURCE_DIR}/functions/gfx/gl_shader.cpp ${CMAKE_SOURCE_DIR}/functions/gfx/gl_framebuffer.cpp ${CMAKE_SOURCE_DIR}/functions/gfx/fullscreen_quad.cpp @@ -9,3 +10,8 @@ add_imgui_app(shaders_lab target_include_directories(shaders_lab PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) + +if(WIN32) + # GUI app: sin consola al lanzar (subsystem:windows / -mwindows) + set_target_properties(shaders_lab PROPERTIES WIN32_EXECUTABLE TRUE) +endif() diff --git a/cpp/functions/gfx/fullscreen_quad.cpp b/cpp/functions/gfx/fullscreen_quad.cpp index a3bb1897..951845d1 100644 --- a/cpp/functions/gfx/fullscreen_quad.cpp +++ b/cpp/functions/gfx/fullscreen_quad.cpp @@ -1,7 +1,4 @@ -#define GL_GLEXT_PROTOTYPES -#include -#include - +#include "gfx/gl_loader.h" #include "gfx/fullscreen_quad.h" namespace fn::gfx { diff --git a/cpp/functions/gfx/gl_framebuffer.cpp b/cpp/functions/gfx/gl_framebuffer.cpp index fb28b855..1998eb4f 100644 --- a/cpp/functions/gfx/gl_framebuffer.cpp +++ b/cpp/functions/gfx/gl_framebuffer.cpp @@ -1,7 +1,4 @@ -#define GL_GLEXT_PROTOTYPES -#include -#include - +#include "gfx/gl_loader.h" #include "gfx/gl_framebuffer.h" namespace fn::gfx { diff --git a/cpp/functions/gfx/gl_loader.cpp b/cpp/functions/gfx/gl_loader.cpp new file mode 100644 index 00000000..1b8eef09 --- /dev/null +++ b/cpp/functions/gfx/gl_loader.cpp @@ -0,0 +1,78 @@ +#include "gl_loader.h" + +#ifdef _WIN32 + +PFNGLATTACHSHADERPROC fn_glAttachShader = nullptr; +PFNGLBINDBUFFERPROC fn_glBindBuffer = nullptr; +PFNGLBINDFRAMEBUFFERPROC fn_glBindFramebuffer = nullptr; +PFNGLBINDVERTEXARRAYPROC fn_glBindVertexArray = nullptr; +PFNGLCOMPILESHADERPROC fn_glCompileShader = nullptr; +PFNGLCREATEPROGRAMPROC fn_glCreateProgram = nullptr; +PFNGLCREATESHADERPROC fn_glCreateShader = nullptr; +PFNGLDELETEBUFFERSPROC fn_glDeleteBuffers = nullptr; +PFNGLDELETEFRAMEBUFFERSPROC fn_glDeleteFramebuffers = nullptr; +PFNGLDELETEPROGRAMPROC fn_glDeleteProgram = nullptr; +PFNGLDELETESHADERPROC fn_glDeleteShader = nullptr; +PFNGLDELETEVERTEXARRAYSPROC fn_glDeleteVertexArrays = nullptr; +PFNGLFRAMEBUFFERTEXTURE2DPROC fn_glFramebufferTexture2D = nullptr; +PFNGLGENBUFFERSPROC fn_glGenBuffers = nullptr; +PFNGLGENFRAMEBUFFERSPROC fn_glGenFramebuffers = nullptr; +PFNGLGENVERTEXARRAYSPROC fn_glGenVertexArrays = nullptr; +PFNGLGETPROGRAMINFOLOGPROC fn_glGetProgramInfoLog = nullptr; +PFNGLGETPROGRAMIVPROC fn_glGetProgramiv = nullptr; +PFNGLGETSHADERINFOLOGPROC fn_glGetShaderInfoLog = nullptr; +PFNGLGETSHADERIVPROC fn_glGetShaderiv = nullptr; +PFNGLGETUNIFORMLOCATIONPROC fn_glGetUniformLocation = nullptr; +PFNGLLINKPROGRAMPROC fn_glLinkProgram = nullptr; +PFNGLSHADERSOURCEPROC fn_glShaderSource = nullptr; +PFNGLUNIFORM1FPROC fn_glUniform1f = nullptr; +PFNGLUNIFORM2FPROC fn_glUniform2f = nullptr; +PFNGLUSEPROGRAMPROC fn_glUseProgram = nullptr; + +namespace fn::gfx { + +bool gl_loader_init() { + #define LOAD(name) \ + fn_##name = (decltype(fn_##name))wglGetProcAddress(#name); \ + if (!fn_##name) return false + + LOAD(glAttachShader); + LOAD(glBindBuffer); + LOAD(glBindFramebuffer); + LOAD(glBindVertexArray); + LOAD(glCompileShader); + LOAD(glCreateProgram); + LOAD(glCreateShader); + LOAD(glDeleteBuffers); + LOAD(glDeleteFramebuffers); + LOAD(glDeleteProgram); + LOAD(glDeleteShader); + LOAD(glDeleteVertexArrays); + LOAD(glFramebufferTexture2D); + LOAD(glGenBuffers); + LOAD(glGenFramebuffers); + LOAD(glGenVertexArrays); + LOAD(glGetProgramInfoLog); + LOAD(glGetProgramiv); + LOAD(glGetShaderInfoLog); + LOAD(glGetShaderiv); + LOAD(glGetUniformLocation); + LOAD(glLinkProgram); + LOAD(glShaderSource); + LOAD(glUniform1f); + LOAD(glUniform2f); + LOAD(glUseProgram); + + #undef LOAD + return true; +} + +} // namespace fn::gfx + +#else + +namespace fn::gfx { +bool gl_loader_init() { return true; } +} // namespace fn::gfx + +#endif diff --git a/cpp/functions/gfx/gl_loader.h b/cpp/functions/gfx/gl_loader.h new file mode 100644 index 00000000..3a9ca2db --- /dev/null +++ b/cpp/functions/gfx/gl_loader.h @@ -0,0 +1,77 @@ +#pragma once + +#ifdef _WIN32 + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + #include + #include + #include + + extern PFNGLATTACHSHADERPROC fn_glAttachShader; + extern PFNGLBINDBUFFERPROC fn_glBindBuffer; + extern PFNGLBINDFRAMEBUFFERPROC fn_glBindFramebuffer; + extern PFNGLBINDVERTEXARRAYPROC fn_glBindVertexArray; + extern PFNGLCOMPILESHADERPROC fn_glCompileShader; + extern PFNGLCREATEPROGRAMPROC fn_glCreateProgram; + extern PFNGLCREATESHADERPROC fn_glCreateShader; + extern PFNGLDELETEBUFFERSPROC fn_glDeleteBuffers; + extern PFNGLDELETEFRAMEBUFFERSPROC fn_glDeleteFramebuffers; + extern PFNGLDELETEPROGRAMPROC fn_glDeleteProgram; + extern PFNGLDELETESHADERPROC fn_glDeleteShader; + extern PFNGLDELETEVERTEXARRAYSPROC fn_glDeleteVertexArrays; + extern PFNGLFRAMEBUFFERTEXTURE2DPROC fn_glFramebufferTexture2D; + extern PFNGLGENBUFFERSPROC fn_glGenBuffers; + extern PFNGLGENFRAMEBUFFERSPROC fn_glGenFramebuffers; + extern PFNGLGENVERTEXARRAYSPROC fn_glGenVertexArrays; + extern PFNGLGETPROGRAMINFOLOGPROC fn_glGetProgramInfoLog; + extern PFNGLGETPROGRAMIVPROC fn_glGetProgramiv; + extern PFNGLGETSHADERINFOLOGPROC fn_glGetShaderInfoLog; + extern PFNGLGETSHADERIVPROC fn_glGetShaderiv; + extern PFNGLGETUNIFORMLOCATIONPROC fn_glGetUniformLocation; + extern PFNGLLINKPROGRAMPROC fn_glLinkProgram; + extern PFNGLSHADERSOURCEPROC fn_glShaderSource; + extern PFNGLUNIFORM1FPROC fn_glUniform1f; + extern PFNGLUNIFORM2FPROC fn_glUniform2f; + extern PFNGLUSEPROGRAMPROC fn_glUseProgram; + + #define glAttachShader fn_glAttachShader + #define glBindBuffer fn_glBindBuffer + #define glBindFramebuffer fn_glBindFramebuffer + #define glBindVertexArray fn_glBindVertexArray + #define glCompileShader fn_glCompileShader + #define glCreateProgram fn_glCreateProgram + #define glCreateShader fn_glCreateShader + #define glDeleteBuffers fn_glDeleteBuffers + #define glDeleteFramebuffers fn_glDeleteFramebuffers + #define glDeleteProgram fn_glDeleteProgram + #define glDeleteShader fn_glDeleteShader + #define glDeleteVertexArrays fn_glDeleteVertexArrays + #define glFramebufferTexture2D fn_glFramebufferTexture2D + #define glGenBuffers fn_glGenBuffers + #define glGenFramebuffers fn_glGenFramebuffers + #define glGenVertexArrays fn_glGenVertexArrays + #define glGetProgramInfoLog fn_glGetProgramInfoLog + #define glGetProgramiv fn_glGetProgramiv + #define glGetShaderInfoLog fn_glGetShaderInfoLog + #define glGetShaderiv fn_glGetShaderiv + #define glGetUniformLocation fn_glGetUniformLocation + #define glLinkProgram fn_glLinkProgram + #define glShaderSource fn_glShaderSource + #define glUniform1f fn_glUniform1f + #define glUniform2f fn_glUniform2f + #define glUseProgram fn_glUseProgram +#else + #define GL_GLEXT_PROTOTYPES + #include + #include +#endif + +namespace fn::gfx { + +// Load OpenGL 2.0+ function pointers. On Linux it's a no-op (symbols resolved +// statically via GL_GLEXT_PROTOTYPES). On Windows it uses wglGetProcAddress; +// the GL context must be current before calling. +bool gl_loader_init(); + +} // namespace fn::gfx diff --git a/cpp/functions/gfx/gl_loader.md b/cpp/functions/gfx/gl_loader.md new file mode 100644 index 00000000..da7196cd --- /dev/null +++ b/cpp/functions/gfx/gl_loader.md @@ -0,0 +1,54 @@ +--- +name: gl_loader +kind: function +lang: cpp +domain: gfx +version: "1.0.0" +purity: impure +signature: "bool gl_loader_init()" +description: "Loader minimo de simbolos OpenGL 2.0+ para cross-compile a Windows. En Linux es no-op (simbolos resueltos via GL_GLEXT_PROTOTYPES). En Windows resuelve punteros con wglGetProcAddress. Redirige las llamadas con macros para que el codigo fuente sea portable." +tags: [opengl, loader, windows, cross-compile, gfx] +uses_functions: [] +uses_types: [] +returns: [] +returns_optional: false +error_type: "error_go_core" +imports: [windows.h, GL/gl.h, GL/glext.h] +tested: false +tests: [] +test_file_path: "" +file_path: "cpp/functions/gfx/gl_loader.cpp" +framework: opengl +params: [] +output: "true si todos los simbolos se resolvieron (Linux siempre true; Windows depende de que el contexto GL este activo antes de llamar). false si algun simbolo no esta disponible en el driver." +--- + +# gl_loader + +Loader minimo de simbolos OpenGL 2.0+ sin dependencias externas (sin GLAD, GLEW, gl3w). Resuelve el problema de que `opengl32.dll` en Windows solo exporta OpenGL 1.1 — todo lo moderno (shaders, FBO, VAO) requiere resolucion dinamica via `wglGetProcAddress`. + +## Uso + +```cpp +#include "gfx/gl_loader.h" + +// Despues de crear el contexto GL (p.ej. tras el primer frame de ImGui): +fn::gfx::gl_loader_init(); + +// A partir de aqui, todos los simbolos funcionan identicamente en Linux y Windows: +glCreateShader(GL_FRAGMENT_SHADER); +glBindFramebuffer(GL_FRAMEBUFFER, fbo); +// ... +``` + +## Como funciona + +En Windows, el header declara `extern PFNGL...` por cada simbolo y los renombra con macros: `#define glCreateShader fn_glCreateShader`. El `.cpp` instancia los punteros y los resuelve con `wglGetProcAddress` en `gl_loader_init()`. + +En Linux, el header activa `GL_GLEXT_PROTOTYPES` e incluye `` + `` directamente; los simbolos son exportados por libGL y no hace falta resolver nada. + +## Anadir un simbolo nuevo + +1. Declarar `extern PFNGLPROC fn_gl;` en el `.h`. +2. Anadir `#define gl fn_gl` en el bloque `#ifdef _WIN32`. +3. Instanciar el puntero en el `.cpp` y anadir `LOAD(gl);` dentro de `gl_loader_init()`. diff --git a/cpp/functions/gfx/gl_shader.cpp b/cpp/functions/gfx/gl_shader.cpp index d06f9cd4..22cf7a86 100644 --- a/cpp/functions/gfx/gl_shader.cpp +++ b/cpp/functions/gfx/gl_shader.cpp @@ -1,7 +1,4 @@ -#define GL_GLEXT_PROTOTYPES -#include -#include - +#include "gfx/gl_loader.h" #include "gfx/gl_shader.h" #include diff --git a/cpp/functions/gfx/shader_canvas.cpp b/cpp/functions/gfx/shader_canvas.cpp index 4cc671d1..3b5ddc16 100644 --- a/cpp/functions/gfx/shader_canvas.cpp +++ b/cpp/functions/gfx/shader_canvas.cpp @@ -1,7 +1,4 @@ -#define GL_GLEXT_PROTOTYPES -#include -#include - +#include "gfx/gl_loader.h" #include "gfx/shader_canvas.h" #include "imgui.h" @@ -9,6 +6,7 @@ namespace fn::gfx { void canvas_init(ShaderCanvas& c) { if (c.initialized) return; + gl_loader_init(); fb_init(c.fb); quad_init(c.quad); c.initialized = true; diff --git a/cpp/functions/viz/bar_chart.cpp b/cpp/functions/viz/bar_chart.cpp index 75bb83d7..774f539b 100644 --- a/cpp/functions/viz/bar_chart.cpp +++ b/cpp/functions/viz/bar_chart.cpp @@ -3,24 +3,53 @@ #include -void bar_chart(const char* title, const char* const* labels, const float* values, int count, float bar_width) { - if (ImPlot::BeginPlot(title, ImVec2(-1, 0))) { +namespace { + +// Plot bars con ejes pineados (no se mueven entre frames) y labels categoricos. +// ImPlot por defecto auto-fitea Y en cada frame, lo que provoca oscilacion visual. +// Lo resolvemos calculando y_max una vez y forzandolo con ImPlotCond_Always. +template +void draw_bars(const char* title, const char* const* labels, const T* values, + int count, double bar_width) { + if (count <= 0) return; + + double y_max = 0.0; + for (int i = 0; i < count; i++) { + if (static_cast(values[i]) > y_max) y_max = static_cast(values[i]); + } + if (y_max <= 0.0) y_max = 1.0; + y_max *= 1.15; // 15% headroom sobre la barra mas alta + + const ImPlotFlags plot_flags = + ImPlotFlags_NoMenus | ImPlotFlags_NoBoxSelect | ImPlotFlags_NoMouseText; + const ImPlotAxisFlags x_flags = + ImPlotAxisFlags_NoMenus | ImPlotAxisFlags_Lock | ImPlotAxisFlags_NoGridLines; + const ImPlotAxisFlags y_flags = + ImPlotAxisFlags_NoMenus | ImPlotAxisFlags_Lock; + + if (ImPlot::BeginPlot(title, ImVec2(-1, 0), plot_flags)) { std::vector positions(count); for (int i = 0; i < count; i++) positions[i] = i; + ImPlot::SetupAxes(nullptr, nullptr, x_flags, y_flags); + ImPlot::SetupAxisLimits(ImAxis_X1, -0.5, static_cast(count) - 0.5, + ImPlotCond_Always); + ImPlot::SetupAxisLimits(ImAxis_Y1, 0.0, y_max, ImPlotCond_Always); ImPlot::SetupAxisTicks(ImAxis_X1, positions.data(), count, labels); + ImPlot::PlotBars("##data", values, count, bar_width); ImPlot::EndPlot(); } } -void bar_chart(const char* title, const char* const* labels, const double* values, int count, double bar_width) { - if (ImPlot::BeginPlot(title, ImVec2(-1, 0))) { - std::vector positions(count); - for (int i = 0; i < count; i++) positions[i] = i; +} // namespace - ImPlot::SetupAxisTicks(ImAxis_X1, positions.data(), count, labels); - ImPlot::PlotBars("##data", values, count, bar_width); - ImPlot::EndPlot(); - } +void bar_chart(const char* title, const char* const* labels, const float* values, + int count, float bar_width) { + draw_bars(title, labels, values, count, static_cast(bar_width)); +} + +void bar_chart(const char* title, const char* const* labels, const double* values, + int count, double bar_width) { + draw_bars(title, labels, values, count, bar_width); } diff --git a/cpp/functions/viz/kpi_card.cpp b/cpp/functions/viz/kpi_card.cpp index 516d3bb7..f3090aaa 100644 --- a/cpp/functions/viz/kpi_card.cpp +++ b/cpp/functions/viz/kpi_card.cpp @@ -1,44 +1,72 @@ #include "kpi_card.h" #include "sparkline.h" +#include "core/tokens.h" #include #include void kpi_card(const char* label, float value, float delta_percent, const float* history, int history_count, const char* format) { - ImGui::BeginGroup(); + using namespace fn_tokens; - // Label — small, muted - ImGui::TextDisabled("%s", label); + // Card container — surface bg, border, rounded, padding. + // Mirrors Mantine used in + // @fn_library/kpi_card.tsx, adapted for ImGui dark theme. + ImGui::PushStyleColor(ImGuiCol_ChildBg, colors::surface); + ImGui::PushStyleColor(ImGuiCol_Border, colors::border); + ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, radius::md); + ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 1.0f); + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(spacing::md, spacing::md)); - // Value — scaled up font - ImGui::SetWindowFontScale(1.8f); - char value_buf[64]; - snprintf(value_buf, sizeof(value_buf), format, value); - ImGui::Text("%s", value_buf); - ImGui::SetWindowFontScale(1.0f); + // Unique child id per label so multiple cards in the same window don't collide. + char child_id[96]; + std::snprintf(child_id, sizeof(child_id), "##kpi_%s", label); - // Delta badge — green up arrow / red down arrow - const bool positive = delta_percent >= 0.0f; - const ImVec4 delta_color = positive - ? ImVec4(0.20f, 0.80f, 0.35f, 1.0f) // green - : ImVec4(0.90f, 0.25f, 0.25f, 1.0f); // red + float width = ImGui::GetContentRegionAvail().x; + if (width < 1.0f) width = 1.0f; - char delta_buf[32]; - if (positive) { - snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xb2 +%.1f%%", delta_percent); - } else { - snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xbc %.1f%%", delta_percent); - } + ImGui::BeginChild(child_id, ImVec2(width, 0), + ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY); - ImGui::PushStyleColor(ImGuiCol_Text, delta_color); - ImGui::Text("%s", delta_buf); + // Label — muted (Mantine "dimmed" text) + ImGui::PushStyleColor(ImGuiCol_Text, colors::text_muted); + ImGui::TextUnformatted(label); ImGui::PopStyleColor(); - // Sparkline — matches delta color - if (history != nullptr && history_count > 0) { - sparkline(label, history, history_count, delta_color, 120.0f, 24.0f); + // Value — scaled up (Mantine fw={700} fontSize=1.875rem) + ImGui::SetWindowFontScale(1.8f); + char value_buf[64]; + std::snprintf(value_buf, sizeof(value_buf), format, value); + ImGui::TextUnformatted(value_buf); + ImGui::SetWindowFontScale(1.0f); + + // Delta badge — only render when meaningful + const bool has_delta = delta_percent != 0.0f; + const bool has_history = history != nullptr && history_count > 0; + + if (has_delta || has_history) { + const bool positive = delta_percent >= 0.0f; + const ImVec4 delta_color = positive ? colors::success : colors::error; + + if (has_delta) { + char delta_buf[32]; + if (positive) { + std::snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xb2 +%.1f%%", delta_percent); + } else { + std::snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xbc %.1f%%", delta_percent); + } + ImGui::PushStyleColor(ImGuiCol_Text, delta_color); + ImGui::TextUnformatted(delta_buf); + ImGui::PopStyleColor(); + } + + if (has_history) { + sparkline(label, history, history_count, delta_color, 120.0f, 24.0f); + } } - ImGui::EndGroup(); + ImGui::EndChild(); + + ImGui::PopStyleVar(3); + ImGui::PopStyleColor(2); }