1919#include " render/neuron_render.hpp"
2020#include " render/network_render.hpp"
2121
22+ #include " camera.cpp"
23+ #include " mouse.cpp"
24+ #include " settings.cpp"
25+
2226// TODO:
2327// ImGui different look
2428// https://github.com/GraphicsProgramming/dear-imgui-styles
@@ -65,7 +69,7 @@ namespace
6569 : entry::AppI(" NEUF" , " spiking network simulator" , " " )
6670 {
6771 // TODO: data dimensions should come from MnistData
68- const uint32_t width = 28 , height = 28 ;
72+ const uint32_t width = 8 , height = 8 ;
6973
7074 net.setSize (width * height);
7175 auto ctx = std::make_shared<NetworkVisualContext>(net);
@@ -189,7 +193,13 @@ namespace
189193 // Check if instancing is supported.
190194 const bool instancingSupported = 0 != (BGFX_CAPS_INSTANCING & caps->supported );
191195
192- float time = (float )((bx::getHPCounter () - m_timeOffset) / double (bx::getHPFrequency ()));
196+ int64_t now = bx::getHPCounter ();
197+ static int64_t last = now;
198+ const int64_t frameTime = now - last;
199+ last = now;
200+ const double freq = double (bx::getHPFrequency ());
201+ float time = (float )((now - m_timeOffset) / freq);
202+ const float deltaTimeSec = float (double (frameTime)/freq);
193203
194204 ImGui::Text (" Time %.2f" , time);
195205
@@ -214,38 +224,89 @@ namespace
214224
215225 imguiEndFrame ();
216226
217- // Set view 0 default viewport.
218- bgfx::setViewRect (0 , 0 , 0 , uint16_t (m_width), uint16_t (m_height));
219-
220- // This dummy draw call is here to make sure that view 0 is cleared
221- // if no other draw calls are submitted to view 0.
222- bgfx::touch (0 );
223-
224-
225-
226- // if (!instancingSupported)
227227 // {
228- // // When instancing is not supported by GPU, implement alternative
229- // // code path that doesn't use instancing.
230- // bool blink = uint32_t(time * 3.0f) & 1;
231- // bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Instancing is not supported by GPU. ");
232-
228+ // // Set view 0 default viewport.
229+ // bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
230+
231+ // // This dummy draw call is here to make sure that view 0 is cleared
232+ // // if no other draw calls are submitted to view 0.
233+ // bgfx::touch(0);
234+
235+ // const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
236+ // const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
237+
238+ // // Set view and projection matrix for view 0.
239+ // {
240+ // float view[16];
241+ // bx::mtxLookAt(view, eye, at);
242+
243+ // float proj[16];
244+ // bx::mtxProj(
245+ // proj,
246+ // 60.0f,
247+ // float(m_width) / float(m_height),
248+ // 0.1f,
249+ // 100.0f,
250+ // bgfx::getCaps()->homogeneousDepth);
251+ // bgfx::setViewTransform(0, view, proj);
252+
253+ // // Set view 0 default viewport.
254+ // bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
255+ // }
233256 // }
234257
235- const bx::Vec3 at = { 0 .0f , 0 .0f , 0 .0f };
236- const bx::Vec3 eye = { 0 .0f , 0 .0f , -35 .0f };
237-
238- // Set view and projection matrix for view 0.
239258 {
259+ const bool mouseOverGui = ImGui::MouseOverArea ();
260+ m_mouse.update (float (m_mouseState.m_mx ), float (m_mouseState.m_my ), m_mouseState.m_mz , m_width, m_height);
261+ if (!mouseOverGui)
262+ {
263+ if (m_mouseState.m_buttons [entry::MouseButton::Left])
264+ {
265+ m_camera.orbit (m_mouse.m_dx , m_mouse.m_dy );
266+ }
267+ else if (m_mouseState.m_buttons [entry::MouseButton::Right])
268+ {
269+ m_camera.dolly (m_mouse.m_dx + m_mouse.m_dy );
270+ }
271+ else if (m_mouseState.m_buttons [entry::MouseButton::Middle])
272+ {
273+ m_settings.m_envRotDest += m_mouse.m_dx *2 .0f ;
274+ }
275+ else if (0 != m_mouse.m_scroll )
276+ {
277+ m_camera.dolly (float (m_mouse.m_scroll )*0 .05f );
278+ }
279+ }
280+ m_camera.update (deltaTimeSec);
281+ // bx::memCopy(m_uniforms.m_cameraPos, &m_camera.m_pos.curr.x, 3*sizeof(float) );
282+
283+ // View Transform 0.
240284 float view[16 ];
241- bx::mtxLookAt (view, eye, at );
285+ // bx::mtxIdentity (view);
242286
243287 float proj[16 ];
244- bx::mtxProj (proj, 60 .0f , float (m_width) / float (m_height), 0 .1f , 100 .0f , bgfx::getCaps ()->homogeneousDepth );
288+ // bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
289+ // bgfx::setViewTransform(0, view, proj);
290+
291+ // View Transform 1.
292+ m_camera.mtxLookAt (view);
293+ bx::mtxProj (proj, 60 .0f , float (m_width)/float (m_height), 0 .1f , 100 .0f , caps->homogeneousDepth );
245294 bgfx::setViewTransform (0 , view, proj);
246295
247- // Set view 0 default viewport.
248- bgfx::setViewRect (0 , 0 , 0 , uint16_t (m_width), uint16_t (m_height));
296+ // View rect.
297+ bgfx::setViewRect (0 , 0 , 0 , uint16_t (m_width), uint16_t (m_height) );
298+ // bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
299+
300+ // Env rotation.
301+ const float amount = bx::min (deltaTimeSec/0 .12f , 1 .0f );
302+ m_settings.m_envRotCurr = bx::lerp (m_settings.m_envRotCurr , m_settings.m_envRotDest , amount);
303+
304+ // Env mtx.
305+ float mtxEnvView[16 ];
306+ m_camera.envViewMtx (mtxEnvView);
307+ float mtxEnvRot[16 ];
308+ bx::mtxRotateY (mtxEnvRot, m_settings.m_envRotCurr );
309+
249310 }
250311
251312 m_lastFrameMissing = 0 ;
@@ -281,6 +342,11 @@ namespace
281342 bgfx::ProgramHandle m_program;
282343
283344 int64_t m_timeOffset;
345+
346+ Camera m_camera;
347+ Mouse m_mouse;
348+ Settings m_settings;
349+
284350 };
285351
286352} // namespace
0 commit comments