Implement movement with the mouse.

This commit is contained in:
2018-04-11 13:03:34 +02:00
parent 0145391450
commit 63e487afce
3 changed files with 44 additions and 10 deletions

View File

@@ -9,7 +9,7 @@
using namespace fmri; using namespace fmri;
static inline void toggle(bool& b) static inline void toggle(bool &b)
{ {
b = !b; b = !b;
} }
@@ -32,7 +32,8 @@ static float getFPS()
return fps; return fps;
} }
static void updatePointSize(float dir) { static void updatePointSize(float dir)
{
float size, granularity; float size, granularity;
glGetFloatv(GL_POINT_SIZE, &size); glGetFloatv(GL_POINT_SIZE, &size);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &granularity); glGetFloatv(GL_POINT_SIZE_GRANULARITY, &granularity);
@@ -127,7 +128,7 @@ void RenderingState::handleKey(unsigned char x)
glutPostRedisplay(); glutPostRedisplay();
} }
std::string RenderingState::debugInfo()const std::string RenderingState::debugInfo() const
{ {
std::stringstream buffer; std::stringstream buffer;
buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n"; buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n";
@@ -146,7 +147,7 @@ void RenderingState::reset()
angle[1] = 0; angle[1] = 0;
} }
void RenderingState::configureRenderingContext()const void RenderingState::configureRenderingContext() const
{ {
glLoadIdentity(); glLoadIdentity();
glRotatef(angle[1], 1, 0, 0); glRotatef(angle[1], 1, 0, 0);
@@ -163,9 +164,12 @@ RenderingState &RenderingState::instance()
void RenderingState::registerControls() void RenderingState::registerControls()
{ {
reset(); reset();
glutPassiveMotionFunc([](int x, int y) { auto motionFunc = [](int x, int y) {
RenderingState::instance().handleMouseAt(x, y); RenderingState::instance().handleMouseAt(x, y);
}); };
glutPassiveMotionFunc(motionFunc);
glutMotionFunc(motionFunc);
glutKeyboardFunc([](auto key, auto, auto) { glutKeyboardFunc([](auto key, auto, auto) {
RenderingState::instance().handleKey(key); RenderingState::instance().handleKey(key);
}); });
@@ -174,13 +178,26 @@ void RenderingState::registerControls()
RenderingState::instance().render(time); RenderingState::instance().render(time);
}); });
glutIdleFunc([]() { glutIdleFunc([]() {
checkGLErrors(); RenderingState::instance().idleFunc();
throttleIdleFunc();
glutPostRedisplay();
}); });
glutSpecialFunc([](int key, int, int) { glutSpecialFunc([](int key, int, int) {
RenderingState::instance().handleSpecialKey(key); RenderingState::instance().handleSpecialKey(key);
}); });
glutMouseFunc([](int button, int state, int, int) {
auto& options = RenderingState::instance().options;
switch (button) {
case GLUT_LEFT_BUTTON:
options.mouse_1_pressed = state == GLUT_DOWN;
break;
case GLUT_RIGHT_BUTTON:
options.mouse_2_pressed = state == GLUT_DOWN;
break;
default:
// Do nothing.
break;
}
});
} }
void RenderingState::handleMouseAt(int x, int y) void RenderingState::handleMouseAt(int x, int y)
@@ -381,3 +398,17 @@ float RenderingState::layerAlpha() const
{ {
return options.layerAlpha; return options.layerAlpha;
} }
void RenderingState::idleFunc()
{
if (options.mouse_1_pressed) {
move('w', false);
}
if (options.mouse_2_pressed) {
move('s', false);
}
checkGLErrors();
throttleIdleFunc();
glutPostRedisplay();
}

View File

@@ -68,6 +68,8 @@ namespace fmri
float layerAlpha; float layerAlpha;
float interactionAlpha; float interactionAlpha;
Color pathColor; Color pathColor;
bool mouse_1_pressed = false;
bool mouse_2_pressed = false;
} options; } options;
std::array<float, 3> pos; std::array<float, 3> pos;
std::array<float, 2> angle; std::array<float, 2> angle;
@@ -83,6 +85,7 @@ namespace fmri
void move(unsigned char key, bool sprint); void move(unsigned char key, bool sprint);
void updateVisualisers(); void updateVisualisers();
void idleFunc();
std::string debugInfo() const; std::string debugInfo() const;
void renderOverlayText() const; void renderOverlayText() const;

View File

@@ -40,7 +40,7 @@ static void loadSimulationData(const Options &options)
if (optLabels) { if (optLabels) {
auto& labels = *optLabels; auto& labels = *optLabels;
for (const auto& result : results) { for (const auto& result : results) {
auto& last = *result.rbegin(); auto &last = *result.rbegin();
auto bestIndex = std::distance(last.data(), max_element(last.data(), last.data() + last.numEntries())); auto bestIndex = std::distance(last.data(), max_element(last.data(), last.data() + last.numEntries()));
LOG(INFO) << "Got answer: " << labels[bestIndex] << endl; LOG(INFO) << "Got answer: " << labels[bestIndex] << endl;
} }