Unify drawing of layers and interactions into "drawables".

This commit is contained in:
2018-04-06 13:06:03 +02:00
parent 9dc4f1546d
commit 2138ce440a
14 changed files with 63 additions and 54 deletions

View File

@@ -38,14 +38,16 @@ ActivityAnimation::ActivityAnimation(
startingPos.reserve(bufferLength);
vector<float> endPos;
endPos.reserve(bufferLength);
colorBuf.reserve(interactions.size());
transform(interactions.begin(), interactions.end(), back_inserter(colorBuffer), [&coloring](auto e) {
return coloring(e.first);
});
colorBuffer.reserve(interactions.size());
for (auto &entry : interactions) {
auto *aPos = &aPositions[3 * entry.second.first];
auto *bPos = &bPositions[3 * entry.second.second];
colorBuf.push_back(coloring(entry.first));
for (auto i : Range(3)) {
startingPos.emplace_back(aPos[i]);
endPos.emplace_back(bPos[i] + (i % 3 ? 0 : LAYER_X_OFFSET));
@@ -59,7 +61,7 @@ ActivityAnimation::ActivityAnimation(
lineIndices.push_back(i + interactions.size());
}
patchTransparancy(colorBuf.begin(), colorBuf.end());
patchTransparancy();
}
void ActivityAnimation::draw(float timeScale)
@@ -68,7 +70,7 @@ void ActivityAnimation::draw(float timeScale)
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(std::tuple_size<Color>::value, GL_FLOAT, 0, colorBuf.data());
glColorPointer(std::tuple_size<Color>::value, GL_FLOAT, 0, colorBuffer.data());
glVertexPointer(3, GL_FLOAT, 0, vertexBuffer.data());
glDrawArrays(GL_POINTS, 0, bufferLength / 3);
glDisableClientState(GL_COLOR_ARRAY);

View File

@@ -27,7 +27,6 @@ namespace fmri
private:
std::size_t bufferLength;
std::vector<Color> colorBuf;
std::vector<float> startingPos;
std::vector<float> delta;
std::vector<int> lineIndices;

View File

@@ -2,31 +2,16 @@
#include "utils.hpp"
#include "Drawable.hpp"
namespace fmri
{
class Animation
class Animation : public Drawable
{
public:
virtual ~Animation() = default;
virtual void draw(float step) = 0;
private:
static float getAlpha();
protected:
template<typename It>
void patchTransparancy(It begin, It end)
{
if constexpr (std::tuple_size<Color>::value >= 4) {
const auto alpha = getAlpha();
for (; begin != end; ++begin) {
Color &color = *begin;
color[3] = alpha;
}
}
}
float getAlpha() override;
};
}

16
src/fmri/Drawable.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "Drawable.hpp"
void fmri::Drawable::patchTransparancy()
{
if constexpr (std::tuple_size<Color>::value < 4) {
// Not compiling with alpha support
return;
}
const auto alpha = getAlpha();
const auto end = colorBuffer.end();
for (auto it = colorBuffer.begin(); it != end; ++it) {
(*it)[3] = alpha;
}
}

22
src/fmri/Drawable.hpp Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "utils.hpp"
namespace fmri
{
class Drawable
{
public:
virtual ~Drawable() = default;
virtual void draw(float time) = 0;
virtual void patchTransparancy();
protected:
std::vector<Color> colorBuffer;
virtual float getAlpha() = 0;
};
}

View File

@@ -10,7 +10,7 @@ namespace fmri
class DummyLayerVisualisation : public LayerVisualisation
{
public:
void render() override
void draw(float time) override
{};
};
}

View File

@@ -29,7 +29,6 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
LayerVisualisation(layer.numEntries()),
ordering(ordering),
vertexBuffer(layer.numEntries() * NODE_FACES.size()),
colorBuffer(layer.numEntries() * VERTICES_PER_NODE),
indexBuffer(layer.numEntries() * NODE_FACES.size())
{
auto &shape = layer.shape();
@@ -45,7 +44,7 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
auto scalingMax = std::max(abs(*minElem), abs(*maxElem));
auto colorPos = colorBuffer.begin();
auto colorPos = std::back_inserter(colorBuffer);
auto indexPos = indexBuffer.begin();
for (int i : Range(limit)) {
@@ -67,12 +66,10 @@ FlatLayerVisualisation::FlatLayerVisualisation(const LayerData &layer, Ordering
}
assert(indexPos == indexBuffer.end());
assert(colorPos == colorBuffer.end());
patchTransparancy(colorBuffer.begin(), colorBuffer.end());
patchTransparancy();
}
void FlatLayerVisualisation::render()
void FlatLayerVisualisation::draw(float time)
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
@@ -85,7 +82,7 @@ void FlatLayerVisualisation::render()
glDisableClientState(GL_COLOR_ARRAY);
// Now draw wireframe
glColor4f(0, 0, 0, 1);
glColor4f(0, 0, 0, getAlpha());
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, indices.data());
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

View File

@@ -12,12 +12,11 @@ namespace fmri
public:
explicit FlatLayerVisualisation(const LayerData &layer, Ordering ordering);
void render() override;
void draw(float time) override;
private:
Ordering ordering;
std::vector<float> vertexBuffer;
std::vector<Color> colorBuffer;
std::vector<int> indexBuffer;
std::vector<int> activeIndexBuffer;

View File

@@ -67,7 +67,7 @@ InputLayerVisualisation::InputLayerVisualisation(const LayerData &data)
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_FLOAT, imageData.data());
}
void InputLayerVisualisation::render()
void InputLayerVisualisation::draw(float time)
{
const float vertices[] = {
0, 0, 0,

View File

@@ -11,7 +11,7 @@ namespace fmri
public:
explicit InputLayerVisualisation(const LayerData &data);
void render() override;
void draw(float time) override;
private:
Texture texture;

View File

@@ -2,10 +2,12 @@
#include <vector>
#include "utils.hpp"
#include "Drawable.hpp"
namespace fmri
{
class LayerVisualisation
: public Drawable
{
public:
enum class Ordering {
@@ -17,28 +19,15 @@ namespace fmri
explicit LayerVisualisation(size_t numNodes);
virtual ~LayerVisualisation() = default;
virtual void render() = 0;
virtual const std::vector<float>& nodePositions() const;
private:
static float getAlpha();
protected:
float getAlpha() override;
protected:
std::vector<float> nodePositions_;
template<Ordering Order>
void initNodePositions(size_t n, float spacing);
template<typename It>
void patchTransparancy(It begin, It end)
{
if constexpr (std::tuple_size<Color>::value >= 4) {
const auto alpha = getAlpha();
for (; begin != end; ++begin) {
Color &color = *begin;
color[3] = alpha;
}
}
}
};
}

View File

@@ -25,7 +25,7 @@ MultiImageVisualisation::MultiImageVisualisation(const fmri::LayerData &layer)
texCoordBuffer = getTexCoords(channels);
}
void MultiImageVisualisation::render()
void MultiImageVisualisation::draw(float time)
{
float alpha = RenderingState::instance().layerAlpha();
drawImageTiles(vertexBuffer.size() / 3, vertexBuffer.data(), texCoordBuffer.data(), texture, alpha);

View File

@@ -20,7 +20,7 @@ namespace fmri
explicit MultiImageVisualisation(const LayerData&);
void render() override;
void draw(float time) override;
static vector<float> getVertices(const std::vector<float> &nodePositions, float scaling = 1);
static std::vector<float> getTexCoords(int n);

View File

@@ -242,7 +242,7 @@ void RenderingState::render(float time) const
glPushMatrix();
renderLayerName(currentData->at(i).name());
if (options.renderLayers) {
layerVisualisations[i]->render();
layerVisualisations[i]->draw(time);
}
if (options.renderInteractions && i < interactionAnimations.size() && interactionAnimations[i]) {
interactionAnimations[i]->draw(time);