Skip to content

Commit a5174a5

Browse files
committed
Render strategy (wip)
1 parent 61e383b commit a5174a5

File tree

8 files changed

+410
-341
lines changed

8 files changed

+410
-341
lines changed

graphics/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ add_executable(neurons_gui
8585
thirdparty/imgui/backends/imgui_impl_sdl2.cpp
8686
neurons/renderer.cpp
8787
neurons/network.hpp
88+
neurons/render/neuron_render.hpp
89+
neurons/render/neuron_render.cpp
90+
neurons/render/network_render.hpp
8891
neurons/neurons_gui.cpp
8992
)
9093

@@ -97,6 +100,7 @@ target_include_directories(neurons_gui PRIVATE
97100
thirdparty/imgui/backends
98101
bgfx_ext
99102
imgui_ext
103+
neurons
100104
)
101105

102106
if (MSVC)

graphics/neurons/network.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#pragma once
2+
3+
#include <memory>
24
#include <vector>
5+
36
#include "neuron.hpp"
47
#include "synapse.hpp"
8+
#include "render_strategy.h"
9+
510

611
struct Network {
712
std::vector<Neuron> neurons;
813
std::vector<std::vector<Synapse>> synapses;
914
float dt = 1.0f;
1015
float time = 0.0f;
1116

17+
std::shared_ptr<RenderStrategy> render;
18+
1219
explicit Network() = default;
1320

1421
Network(int N) {
@@ -52,4 +59,22 @@ struct Network {
5259

5360
time += dt;
5461
}
55-
};
62+
63+
void init() {
64+
if (render) render->init();
65+
}
66+
67+
void draw() const {
68+
if (render) render->draw();
69+
}
70+
71+
// void draw() const {
72+
// for (auto& n : neurons) {
73+
// n.draw();
74+
// }
75+
// }
76+
77+
void update(float input, float dt, float t) {
78+
if (render) render->update(dt);
79+
}
80+
};

graphics/neurons/neuron.hpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "render_strategy.h"
66

7+
78
struct Neuron {
89
size_t idx = 0;
910
float v = 0.0f;
@@ -13,16 +14,20 @@ struct Neuron {
1314
float spike_time = -1.0f;
1415
bool spiked = false;
1516

16-
std::shared_ptr<NeuronStrategy> strategy;
17+
std::shared_ptr<RenderStrategy> render;
18+
19+
void init() {
20+
if (render) render->init();
21+
}
1722

1823
void simulate(float input, float dt) {
1924
v += dt * (-v + input) / 20.0f;
2025
spiked = (v > 1.0f);
21-
if (strategy) strategy->update(*this, dt);
26+
if (render) render->update(dt);
2227
}
2328

2429
void draw() const {
25-
if (strategy) strategy->draw(*this);
30+
if (render) render->draw();
2631
}
2732

2833
void update(float input, float dt, float t) {
@@ -42,21 +47,3 @@ struct Neuron {
4247
}
4348
}
4449
};
45-
46-
struct RenderNeuronStrategy : NeuronStrategy {
47-
std::shared_ptr<VisualContext> ctx;
48-
49-
RenderNeuronStrategy(std::shared_ptr<VisualContext> ctx) : ctx(ctx) {}
50-
51-
void update(Neuron&, float) override {
52-
// Optional: nothing for now
53-
}
54-
55-
void draw(const Neuron& n) const override {
56-
size_t idx = n.idx;
57-
const bx::Vec3& pos = ctx->positions[idx];
58-
59-
bx::Vec3 c = n.spiked ? bx::Vec3{1, 1, 0} : bx::Vec3{n.v, 0, 1 - n.v};
60-
// draw_sphere(pos, 0.2f, c);
61-
}
62-
};

0 commit comments

Comments
 (0)