From aeccd2b772b5eb65230f44fe8be060a09e45a2d1 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Mon, 22 Jan 2018 14:05:35 +0100 Subject: [PATCH] Add new animation class. --- src/ActivityAnimation.cpp | 48 +++++++++++++++++++++++++++++++++++++++ src/ActivityAnimation.hpp | 20 ++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/ActivityAnimation.cpp create mode 100644 src/ActivityAnimation.hpp diff --git a/src/ActivityAnimation.cpp b/src/ActivityAnimation.cpp new file mode 100644 index 0000000..c286aaf --- /dev/null +++ b/src/ActivityAnimation.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include "ActivityAnimation.hpp" +#include "utils.hpp" + +using namespace fmri; + +static inline float correct_timescale(float original) +{ + float ignore; + return std::modf(original, &ignore); +} + +ActivityAnimation::ActivityAnimation(std::size_t count, const float *aPos, const float *bPos, const float xDist) : + bufferLength(3 * count), + startingPos(new float[bufferLength]), + delta(new float[bufferLength]), + offset(new float[count]) +{ + memcpy(startingPos.get(), aPos, sizeof(aPos[0]) * bufferLength); + for (int i = 0; i < bufferLength; ++i) { + delta[i] = bPos[i] - aPos[i]; + } + + auto& random = rng(); + std::uniform_real_distribution rd; + for (int i = 0; i < count; ++i) { + offset[i] = rd(random); + delta[3 * i] += xDist; + } +} + +void ActivityAnimation::draw(float timeScale) const +{ + std::unique_ptr vertexBuffer(new float[bufferLength]); + + for (auto i = 0; i < bufferLength; ++i) { + vertexBuffer[i] = startingPos[i] + correct_timescale(offset[i/3] + timeScale) * delta[i]; + } + + glColor3f(1, 1, 1); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.get()); + glDrawArrays(GL_POINTS, bufferLength / 3, bufferLength); + glDisableClientState(GL_VERTEX_ARRAY); +} diff --git a/src/ActivityAnimation.hpp b/src/ActivityAnimation.hpp new file mode 100644 index 0000000..bad3051 --- /dev/null +++ b/src/ActivityAnimation.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace fmri +{ + class ActivityAnimation + { + public: + ActivityAnimation(std::size_t count, const float* aPos, const float* bPos, const float xDist); + void draw(float timeScale) const; + + private: + std::size_t bufferLength; + std::unique_ptr startingPos; + std::unique_ptr delta; + std::unique_ptr offset; + }; +} \ No newline at end of file