Skip to content
Snippets Groups Projects
Commit 57585a5b authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

PixelInfo object, pre-parse XYZ point into 32-bit float

parent 995b5d31
No related branches found
No related tags found
No related merge requests found
...@@ -43,6 +43,23 @@ ...@@ -43,6 +43,23 @@
#include "rapidjson/document.h" #include "rapidjson/document.h"
// Information about one LED pixel
class PixelInfo {
public:
PixelInfo(unsigned index, const rapidjson::Value &layout);
// Point coordinates
float x, y, z;
// Index in the framebuffer
unsigned index;
// Parsed JSON for this pixel's layout
const rapidjson::Value &layout;
};
// Abstract base class for one LED effect
class Effect { class Effect {
public: public:
virtual void nextFrame(float timeDelta); virtual void nextFrame(float timeDelta);
...@@ -50,7 +67,7 @@ public: ...@@ -50,7 +67,7 @@ public:
// Calculate a pixel value, using floating point RGB in the range [0, 1]. // Calculate a pixel value, using floating point RGB in the range [0, 1].
// Caller is responsible for clamping if necessary. This supports effects // Caller is responsible for clamping if necessary. This supports effects
// that layer with other effects using greater than 8-bit precision. // that layer with other effects using greater than 8-bit precision.
virtual void calculatePixel(rapidjson::Value &layout, float rgb[3]) = 0; virtual void calculatePixel(float rgb[3], const PixelInfo &p) = 0;
}; };
...@@ -85,11 +102,25 @@ private: ...@@ -85,11 +102,25 @@ private:
Effect *effect; Effect *effect;
struct timeval lastTime; struct timeval lastTime;
std::vector<uint8_t> frameBuffer; std::vector<uint8_t> frameBuffer;
std::vector<PixelInfo> pixelInfo;
int usage(const char *name); int usage(const char *name);
}; };
inline PixelInfo::PixelInfo(unsigned index, const rapidjson::Value& layout)
: x(0), y(0), z(0), index(index), layout(layout)
{
if (layout.IsObject()) {
const rapidjson::Value& point = layout["point"];
if (point.IsArray()) {
if (point.Size() >= 1) x = point[0u].GetDouble();
if (point.Size() >= 2) y = point[1u].GetDouble();
if (point.Size() >= 3) z = point[2u].GetDouble();
}
}
}
inline void Effect::nextFrame(float timeDelta) inline void Effect::nextFrame(float timeDelta)
{ {
// Default implementation; do nothing. // Default implementation; do nothing.
...@@ -138,6 +169,13 @@ inline bool EffectRunner::setLayout(const char *filename) ...@@ -138,6 +169,13 @@ inline bool EffectRunner::setLayout(const char *filename)
frameBuffer.resize(sizeof(OPCClient::Header) + frameBytes); frameBuffer.resize(sizeof(OPCClient::Header) + frameBytes);
OPCClient::Header::view(frameBuffer).init(0, opc.SET_PIXEL_COLORS, frameBytes); OPCClient::Header::view(frameBuffer).init(0, opc.SET_PIXEL_COLORS, frameBytes);
// Set up PixelInfo instances
pixelInfo.clear();
for (unsigned i = 0; i < layout.Size(); i++) {
PixelInfo p(i, layout[i]);
pixelInfo.push_back(p);
}
return true; return true;
} }
...@@ -196,13 +234,12 @@ inline void EffectRunner::doFrame(float timeDelta) ...@@ -196,13 +234,12 @@ inline void EffectRunner::doFrame(float timeDelta)
uint8_t *dest = OPCClient::Header::view(frameBuffer).data(); uint8_t *dest = OPCClient::Header::view(frameBuffer).data();
for (unsigned i = 0; i < layout.Size(); i++) { for (std::vector<PixelInfo>::iterator i = pixelInfo.begin(), e = pixelInfo.end(); i != e; ++i) {
float rgb[3] = { 0, 0, 0 }; float rgb[3] = { 0, 0, 0 };
const PixelInfo &p = *i;
rapidjson::Value &pixelLayout = layout[i]; if (p.layout.IsObject()) {
effect->calculatePixel(rgb, p);
if (pixelLayout.IsObject()) {
effect->calculatePixel(pixelLayout, rgb);
} }
for (unsigned i = 0; i < 3; i++) { for (unsigned i = 0; i < 3; i++) {
......
...@@ -20,15 +20,10 @@ public: ...@@ -20,15 +20,10 @@ public:
angle = fmodf(angle + timeDelta * speed, 2 * M_PI); angle = fmodf(angle + timeDelta * speed, 2 * M_PI);
} }
virtual void calculatePixel(rapidjson::Value &layout, float rgb[3]) virtual void calculatePixel(float rgb[3], const PixelInfo &p)
{ {
float x = layout["point"][0u].GetDouble(); float distance = sqrtf(p.x * p.x + p.y * p.y + p.z * p.z);
float y = layout["point"][1u].GetDouble(); float wave = sinf(3.0 * distance - angle) + noise3(p.x, p.y, p.z);
float z = layout["point"][2u].GetDouble();
float distance = sqrtf(x*x + y*y + z*z);
float wave = sinf(3.0 * distance - angle) + noise3(x, y, z);
hsv2rgb(rgb, 0.2, 0.3, wave); hsv2rgb(rgb, 0.2, 0.3, wave);
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment