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;
static inline void toggle(bool& b)
static inline void toggle(bool &b)
{
b = !b;
}
@@ -32,7 +32,8 @@ static float getFPS()
return fps;
}
static void updatePointSize(float dir) {
static void updatePointSize(float dir)
{
float size, granularity;
glGetFloatv(GL_POINT_SIZE, &size);
glGetFloatv(GL_POINT_SIZE_GRANULARITY, &granularity);
@@ -127,7 +128,7 @@ void RenderingState::handleKey(unsigned char x)
glutPostRedisplay();
}
std::string RenderingState::debugInfo()const
std::string RenderingState::debugInfo() const
{
std::stringstream buffer;
buffer << "Pos(x,y,z) = (" << pos[0] << ", " << pos[1] << ", " << pos[2] << ")\n";
@@ -146,7 +147,7 @@ void RenderingState::reset()
angle[1] = 0;
}
void RenderingState::configureRenderingContext()const
void RenderingState::configureRenderingContext() const
{
glLoadIdentity();
glRotatef(angle[1], 1, 0, 0);
@@ -163,9 +164,12 @@ RenderingState &RenderingState::instance()
void RenderingState::registerControls()
{
reset();
glutPassiveMotionFunc([](int x, int y) {
auto motionFunc = [](int x, int y) {
RenderingState::instance().handleMouseAt(x, y);
});
};
glutPassiveMotionFunc(motionFunc);
glutMotionFunc(motionFunc);
glutKeyboardFunc([](auto key, auto, auto) {
RenderingState::instance().handleKey(key);
});
@@ -174,13 +178,26 @@ void RenderingState::registerControls()
RenderingState::instance().render(time);
});
glutIdleFunc([]() {
checkGLErrors();
throttleIdleFunc();
glutPostRedisplay();
RenderingState::instance().idleFunc();
});
glutSpecialFunc([](int key, int, int) {
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)
@@ -381,3 +398,17 @@ float RenderingState::layerAlpha() const
{
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 interactionAlpha;
Color pathColor;
bool mouse_1_pressed = false;
bool mouse_2_pressed = false;
} options;
std::array<float, 3> pos;
std::array<float, 2> angle;
@@ -83,6 +85,7 @@ namespace fmri
void move(unsigned char key, bool sprint);
void updateVisualisers();
void idleFunc();
std::string debugInfo() const;
void renderOverlayText() const;

View File

@@ -40,7 +40,7 @@ static void loadSimulationData(const Options &options)
if (optLabels) {
auto& labels = *optLabels;
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()));
LOG(INFO) << "Got answer: " << labels[bestIndex] << endl;
}