From c59c92199702355d00090bf9ffafb87a32e97ac0 Mon Sep 17 00:00:00 2001 From: lenn Date: Mon, 2 Mar 2026 17:21:14 +0800 Subject: [PATCH] update --- main.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++ shader/heatmap.frag | 2 +- shader/skirt.frag | 37 +++++++++++++++++++++++++++++++++ shader/skirt.vert | 18 ++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 shader/skirt.frag create mode 100644 shader/skirt.vert diff --git a/main.cpp b/main.cpp index 510b208..0762b3b 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include "camera.h" #include #include +#include #include #include #include @@ -249,7 +250,55 @@ private: const int vertex_count = cols * rows; std::vector verts; + for (int i = 0; i < rows; ++i) { + const float v = (rows > 1) ? (float)(i) / (float)(rows - 1) : 0.0F; // normailze to 0-1 + const float y = (v - 0.5F) * panelHeight_; // -0.5 - 0.5 + for (int j = 0; j < cols; ++j) { + const float u = (cols > 1) ? (float)(j) / (float)(cols - 1) : 0.0F; + const float x = (u - 0.5F) * panelWidth_; + verts.push_back(x); + verts.push_back(y); + verts.push_back(0.0F); + verts.push_back(u); + verts.push_back(v); + } + } + std::vector idx; + idx.reserve((cols - 1) * (rows - 1) * 6); + for (int r = 0; r < rows - 1; ++r) { + for (int c = 0; c < cols - 1; ++c) { + auto i0 = r * cols + c; + auto i1 = r * cols + c + 1; + auto i2 = (r + 1) * cols + c + 1; + auto i3 = (r + 1) * cols + c; + idx.push_back(i0); + idx.push_back(i1); + idx.push_back(i2); + idx.push_back(i0); + idx.push_back(i2); + idx.push_back(i3); + } + } + + heatmapIndexCount = int(idx.size()); + glGenVertexArrays(1, &heatmapVao_); + glBindVertexArray(heatmapVao_); + + glGenBuffers(1, &heatmapVbo_); + glBindBuffer(GL_ARRAY_BUFFER, heatmapVbo_); + glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(float), verts.data(), GL_STATIC_DRAW); + + glGenBuffers(1, &heatmapIbo_); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, heatmapIbo_); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx.size() * sizeof(unsigned int), idx.data(), GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0)); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); + + glBindVertexArray(0); } bool initBgProgram() { @@ -509,6 +558,7 @@ private: unsigned int heatmapVao_ = 0; unsigned int heatmapVbo_ = 0; unsigned int heatmapIbo_ = 0; + int heatmapIndexCount = 0; ViewPort viewport_{800, 600}; Camera camera_{glm::vec3(0.0F, 0.0F, 2.0F)}; diff --git a/shader/heatmap.frag b/shader/heatmap.frag index f5abd8d..55c1d74 100644 --- a/shader/heatmap.frag +++ b/shader/heatmap.frag @@ -114,7 +114,7 @@ void main() { float vC = texture(uHeightTex, vUV).r; bool isZero = abs(vC) <= 1e-6; float t = value01(vC); - float m = maxNeighborValue(vUV); + float m = maxNeighborValueK(vUV, 2); float eps = max(1e-4, 0.001 * (uMaxV - uMinV)); float force = smoothstep(uMaxV - eps, uMaxV, m); t = max(t, force); diff --git a/shader/skirt.frag b/shader/skirt.frag new file mode 100644 index 0000000..4226f05 --- /dev/null +++ b/shader/skirt.frag @@ -0,0 +1,37 @@ +#version 330 core + +in vec3 vNormal; +in vec3 vWorldPos; +in vec3 vUV; +out vec4 FragColor; + +uniform vec3 uCameraPos; +uniform vec3 uLightDir; +uniform sampler2D uHeightTex; +uniform float uMinV; +uniform float uMaxV; +uniform vec2 uTexelSize; +uniform vec3 uColorZero; +uniform vec3 uColorLow; +uniform vec3 uColorMid; +uniform vec3 uColorHeigh; + +float saturate(float x) { + return clamp(x, 0.0, 1.0); +} + +float value01(float v) { + return saturate((v - uMinV) / max(uMaxV - uMinV, 1e-6)); +} + +vec3 colorRamp(float t) { + if (t < 0.5) { + return mix(uColorLow, uColorMid, t / 0.5); + } + + return mix(uColorHeigh, uColorMid, (t - 0.5) / 0.5); +} + +float maxNeighborValue(vec2 uv) { + +} \ No newline at end of file diff --git a/shader/skirt.vert b/shader/skirt.vert new file mode 100644 index 0000000..21ee00f --- /dev/null +++ b/shader/skirt.vert @@ -0,0 +1,18 @@ +#version 330 core + +layout(location=0) in vec3 aPos; +layout(location=1) in vec3 aNormal; +layout(location=2) in vec2 aUV; + +out vec3 vNormal; +out vec3 vWorldPos; +out vec2 vUV; + +uniform mat4 uMVP; + +void main() { + vWorldPos = aPos; + vNormal = aNormal; + vUV = aUV; + gl_Position = uMVP * vec4(aPos, 1.0); +}