Skip to content

Commit 7c6397d

Browse files
committed
Add a neuron on Space
Default area size is a cube of 20 neurons per side
1 parent 081cec1 commit 7c6397d

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

graphics/neurons/network.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "render/neuron_render.hpp"
55
#include "render/network_render.h"
66

7+
Network::Network() {
8+
layers.push_back({});
9+
}
710

811
void Network::addLayer(const NeuronLayer&& layer, const std::vector<size_t>& area_size) {
912
layers.push_back(layer);
@@ -21,10 +24,11 @@ void Network::addLayer(const NeuronLayer&& layer, const std::vector<size_t>& are
2124
}
2225
}
2326

24-
void Network::addNeuron(const std::vector<size_t>& pos, const NeuronLayer& connected_to) {
27+
void Network::addNeuron(const std::vector<int32_t>& pos, const NeuronLayer& connected_to) {
2528
uint32_t layer = pos[2];
2629
if (layer >= layers.size()) {
27-
std::cerr << "wrong layer position\n";
30+
layers.insert(layers.end(), layers.size() - layer + 1, {});
31+
// std::cerr << "wrong layer position\n";
2832
return;
2933
}
3034

@@ -106,7 +110,7 @@ void Network::init() {
106110
if (render) render->init();
107111
}
108112

109-
void Network::draw(float time) const {
113+
void Network::draw(float time) {
110114
if (render) render->draw(time);
111115
}
112116

graphics/neurons/network.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ struct Network {
2626

2727
std::shared_ptr<RenderStrategy> render;
2828

29-
explicit Network() = default;
29+
explicit Network();
3030

3131
void addLayer(const NeuronLayer&& layer, const std::vector<size_t>& area_size);
32-
void addNeuron(const std::vector<size_t>& pos, const NeuronLayer& connected_to);
32+
void addNeuron(const std::vector<int32_t>& pos, const NeuronLayer& connected_to);
3333
void addConnection(const std::shared_ptr<Neuron>& n1,
3434
const std::shared_ptr<Neuron>& n2);
3535

@@ -39,7 +39,7 @@ struct Network {
3939
std::vector<float> get_current_voltage_state() const;
4040
void step(std::vector<uint8_t> inputs);
4141
void init();
42-
void draw(float time) const;
42+
void draw(float time);
4343
void update(float dt);
4444
void destroy() const;
4545

graphics/neurons/neuron.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Neuron {
2626
if (render) render->update(dt);
2727
}
2828

29-
void draw(float time) const {
29+
void draw(float time) {
3030
if (render) render->draw(time);
3131
}
3232

graphics/neurons/render/network_render.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "network_render.h"
55
#include "neuron_render.hpp"
66

7+
78
void NetworkRenderStrategy::init() {
89
m_lastFrameMissing = 0;
910
// 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
@@ -92,18 +93,26 @@ void NetworkRenderStrategy::update(float dt) {
9293
if (ImGui::IsKeyPressed(ImGuiKey_W))
9394
selected_y = std::max(0, selected_y - 1);
9495
if (ImGui::IsKeyPressed(ImGuiKey_S))
95-
selected_y = std::min(area_size_y - 1, selected_y + 1);
96+
selected_y = std::min(max_area_size - 1, selected_y + 1);
9697

9798
if (ImGui::IsKeyPressed(ImGuiKey_A))
9899
selected_x = std::max(0, selected_x - 1);
99100
if (ImGui::IsKeyPressed(ImGuiKey_D))
100-
selected_x = std::min(area_size_x - 1, selected_x + 1);
101+
selected_x = std::min(max_area_size - 1, selected_x + 1);
101102

102103
if (ImGui::IsKeyPressed(ImGuiKey_R))
103-
selected_x = std::max(0, selected_z - 1);
104+
selected_z = std::max(0, selected_z - 1);
104105
if (ImGui::IsKeyPressed(ImGuiKey_F))
105-
selected_x = std::min(area_size_z - 1, selected_z + 1);
106+
selected_z = std::min(max_area_size - 1, selected_z + 1);
107+
108+
if (ImGui::IsKeyPressed(ImGuiKey_Space)) {
109+
std::vector<int32_t> pos = { selected_x, selected_y, selected_z };
110+
if (selected_neuron == nullptr) {
111+
// create neuron
112+
ctx->net.addNeuron(pos, {});
113+
}
106114

115+
}
107116
// to total number of instances to draw
108117
uint32_t totalCubes = ctx->net.neurons.size();
109118

@@ -119,7 +128,7 @@ void NetworkRenderStrategy::update(float dt) {
119128
}
120129
}
121130

122-
void NetworkRenderStrategy::drawNeurons(float time) const {
131+
void NetworkRenderStrategy::drawNeurons(float time) {
123132
bgfx::InstanceDataBuffer idb;
124133
bgfx::allocInstanceDataBuffer(&idb, drawnCubes, instanceStride);
125134

@@ -130,14 +139,22 @@ void NetworkRenderStrategy::drawNeurons(float time) const {
130139
const float start_y = - area_size_y * offset / 2.0f;
131140
const float start_z = - area_size_z * offset / 2.0f;
132141

142+
// reset the selection
143+
selected_neuron = nullptr;
144+
133145
for (uint32_t ii = 0; ii < drawnCubes; ++ii)
134146
{
135147
const auto& neuron = ctx->net.neurons[ii];
136148

137149
std::shared_ptr<NeuronRenderStrategy>& s = std::dynamic_pointer_cast<NeuronRenderStrategy>(neuron->render);
138150
bx::Vec3 pos = s->get_position();
139-
uint32_t yy = pos.y;
140151
uint32_t xx = pos.x;
152+
uint32_t yy = pos.y;
153+
uint32_t zz = pos.z;
154+
155+
if (xx == selected_x && yy == selected_y && zz == selected_z) {
156+
selected_neuron = neuron;
157+
}
141158

142159
float* mtx = (float*)data;
143160
time = 0.0f;
@@ -156,7 +173,7 @@ void NetworkRenderStrategy::drawNeurons(float time) const {
156173

157174
mtx[12] = start_x + float(xx) * offset;
158175
mtx[13] = start_y + float(yy) * offset;
159-
mtx[14] = 0.0f;
176+
mtx[14] = start_z + float(zz) * offset;
160177

161178
float* color = (float*)&data[64];
162179

@@ -187,7 +204,7 @@ void NetworkRenderStrategy::drawNeurons(float time) const {
187204
bgfx::submit(0, m_program);
188205
}
189206

190-
void NetworkRenderStrategy::drawSelection(float time) const {
207+
void NetworkRenderStrategy::drawSelection(float time) {
191208

192209
const float offset = 3.0f;
193210
const float start_x = - area_size_x * offset / 2.0f;
@@ -215,7 +232,7 @@ void NetworkRenderStrategy::drawSelection(float time) const {
215232
bgfx::submit(0, m_selection_program);
216233
}
217234

218-
void NetworkRenderStrategy::draw(float time) const {
235+
void NetworkRenderStrategy::draw(float time) {
219236
if (drawnCubes > 0) {
220237
drawNeurons(time);
221238
}

graphics/neurons/render/network_render.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "network.h"
1010
#include "render_strategy.h"
1111

12-
12+
const int32_t max_area_size = 20;
1313

1414
struct NetworkVisualContext : VisualContext {
1515
// std::vector<bx::Vec3> positions;
@@ -34,11 +34,11 @@ struct NetworkRenderStrategy : RenderStrategy {
3434
void init() override;
3535
void addLayer(const NeuronLayer& layer, const std::vector<size_t>& area_size);
3636
void update(float dt) override;
37-
void draw(float time) const override;
37+
void draw(float time) override;
3838
void destroy() override;
3939

40-
void drawNeurons(float time) const;
41-
void drawSelection(float time) const;
40+
void drawNeurons(float time);
41+
void drawSelection(float time);
4242

4343
bgfx::VertexBufferHandle m_vbh;
4444
bgfx::IndexBufferHandle m_ibh;
@@ -57,6 +57,7 @@ struct NetworkRenderStrategy : RenderStrategy {
5757
int32_t selected_x = 0;
5858
int32_t selected_y = 0;
5959
int32_t selected_z = 0;
60+
std::shared_ptr<Neuron> selected_neuron;
6061

6162
float m_color[4];
6263
bgfx::UniformHandle u_color;

graphics/neurons/render/neuron_render.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct NeuronRenderStrategy : RenderStrategy {
6969
// Optional: nothing for now
7070
}
7171

72-
void draw(float time) const override {
72+
void draw(float time) override {
7373
// size_t idx = ctx->idx;
7474
// const bx::Vec3& pos = ctx->positions[idx];
7575

graphics/neurons/render_strategy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct VisualContext {};
1111
struct RenderStrategy {
1212
virtual void init() = 0;
1313
virtual void update(float dt) = 0;
14-
virtual void draw(float time) const = 0;
14+
virtual void draw(float time) = 0;
1515
virtual void destroy() = 0;
1616
virtual ~RenderStrategy() = default;
1717
};

0 commit comments

Comments
 (0)