How to do 3d plots? #1057
Replies: 1 comment
-
Here is the associated code: Below is a “tour” of the snippet, line-by-line, so you can see how the little 3-D mesh that follows the mouse is produced. The code is one of the FTXUI Canvas demos, but the ideas are transferable to any immediate-mode GUI. Below is a “tour” of the snippet, line-by-line, so you can see how the little 3-D mesh that follows the mouse is produced. The code is one of the FTXUI Canvas demos, but the ideas are transferable to any immediate-mode GUI. 1. Renderer wrapperauto renderer_plot_3 = Renderer([&] {
2. A drawing surfaceauto c = Canvas(100, 100);
c.DrawText(0, 0, "A 2D gaussian plot");
3. Grid sizeint size = 15; We will sample a 15 × 15 lattice. 4. Convert mouse screen-space → grid-space// mouse_x = 5mx + 3*my
// mouse_y = -5my + 90
float my = (mouse_y - 90) / -5.f;
float mx = (mouse_x - 3 * my) / 5.f; Later, every grid point is projected to screen with Assuming the plane height
So 5. Height (z) field – a 2-D Gaussian “bump”std::vector<std::vector<float>> ys(size, std::vector<float>(size));
...
ys[y][x] = -1.5 + 3.0 * std::exp(-0.2f * (dx*dx + dy*dy));
Result: a smooth hill that rises under the mouse. 6. Draw the mesh (two nested loops)For every interior edge we draw a tiny line segment in screen space. if (x != 0) { … } // horizontal edge (from (x-1,y) → (x,y))
if (y != 0) { … } // vertical edge (from (x,y-1) → (x,y)) Projection used in both calls screen_x = 5 * x + 3 * y
screen_y = 90 - 5 * y - 5 * z // z = ys[y][x]
Because each edge is rendered twice (once per loop direction) all adjacent quads are connected, giving the retina-style wireframe. 7. Why it “moves”Every frame the lambda:
So as the pointer travels, the Gaussian’s peak follows, and the projection math makes the hill appear to rise out of a flat square. TL;DR
Change the constants ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I came across this library on pinterest, where there's a gif of multiple things made with this and one of them's a 3d plot (square plane that gets peaks as the mouse moves over it). I can't find how it's made in the actual repo code. Any guidance would be appreciated, thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions