2015-08-09 23:00:51 -04:00
|
|
|
#include "WaterfallPanel.h"
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
WaterfallPanel::WaterfallPanel() : GLPanel(), fft_size(0), waterfall_lines(0), waterfall_slice(NULL), activeTheme(NULL) {
|
2015-08-17 21:52:38 -04:00
|
|
|
setFillColor(RGBA4f(0,0,0));
|
2015-01-20 22:26:34 -05:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
waterfall[i] = 0;
|
|
|
|
}
|
2014-12-28 05:13:46 -05:00
|
|
|
}
|
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
void WaterfallPanel::setup(int fft_size_in, int num_waterfall_lines_in) {
|
2014-12-28 05:13:46 -05:00
|
|
|
waterfall_lines = num_waterfall_lines_in;
|
|
|
|
fft_size = fft_size_in;
|
2015-08-09 23:00:51 -04:00
|
|
|
|
|
|
|
if (points.size() != fft_size) {
|
|
|
|
points.resize(fft_size);
|
|
|
|
}
|
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
if (waterfall[i]) {
|
|
|
|
glDeleteTextures(1, &waterfall[i]);
|
|
|
|
waterfall[i] = 0;
|
|
|
|
}
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-08 13:49:12 -05:00
|
|
|
waterfall_ofs[i] = waterfall_lines - 1;
|
2015-01-20 22:26:34 -05:00
|
|
|
}
|
2014-11-15 23:41:41 -05:00
|
|
|
}
|
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
void WaterfallPanel::refreshTheme() {
|
2015-02-08 13:49:12 -05:00
|
|
|
glEnable (GL_TEXTURE_2D);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[i]);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
|
|
|
|
glPixelMapfv(GL_PIXEL_MAP_I_TO_R, 256, &(ThemeMgr::mgr.currentTheme->waterfallGradient.getRed())[0]);
|
|
|
|
glPixelMapfv(GL_PIXEL_MAP_I_TO_G, 256, &(ThemeMgr::mgr.currentTheme->waterfallGradient.getGreen())[0]);
|
|
|
|
glPixelMapfv(GL_PIXEL_MAP_I_TO_B, 256, &(ThemeMgr::mgr.currentTheme->waterfallGradient.getBlue())[0]);
|
|
|
|
}
|
2015-01-11 03:09:59 -05:00
|
|
|
}
|
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
void WaterfallPanel::setPoints(std::vector<float> &points) {
|
|
|
|
int halfPts = points.size()/2;
|
|
|
|
if (halfPts == fft_size) {
|
|
|
|
for (int i = 0; i < fft_size; i++) {
|
|
|
|
this->points[i] = points[i*2+1];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->points.assign(points.begin(), points.end());
|
|
|
|
}
|
|
|
|
}
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
void WaterfallPanel::step() {
|
2015-02-08 13:49:12 -05:00
|
|
|
int half_fft_size = fft_size / 2;
|
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
if (!waterfall[0]) {
|
|
|
|
glGenTextures(2, waterfall);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-08 13:49:12 -05:00
|
|
|
unsigned char *waterfall_tex;
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-08 13:49:12 -05:00
|
|
|
waterfall_tex = new unsigned char[half_fft_size * waterfall_lines];
|
|
|
|
memset(waterfall_tex, 0, half_fft_size * waterfall_lines);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[i]);
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
2015-01-20 22:26:34 -05:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-08 13:49:12 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, half_fft_size, waterfall_lines, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, (GLvoid *) waterfall_tex);
|
2015-01-20 22:26:34 -05:00
|
|
|
}
|
2015-08-09 23:00:51 -04:00
|
|
|
|
|
|
|
if (waterfall_slice != NULL) {
|
|
|
|
delete waterfall_slice;
|
|
|
|
}
|
|
|
|
waterfall_slice = new unsigned char[half_fft_size];
|
|
|
|
|
2015-05-30 00:09:51 -04:00
|
|
|
delete[] waterfall_tex;
|
2015-01-20 22:26:34 -05:00
|
|
|
}
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2014-11-15 23:41:41 -05:00
|
|
|
if (points.size()) {
|
2015-01-20 22:26:34 -05:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
for (int i = 0, iMax = half_fft_size; i < iMax; i++) {
|
2015-08-09 23:00:51 -04:00
|
|
|
float v = points[j * half_fft_size + i];
|
|
|
|
|
2015-01-22 21:32:32 -05:00
|
|
|
float wv = v < 0 ? 0 : (v > 0.99 ? 0.99 : v);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-09 22:01:33 -05:00
|
|
|
waterfall_slice[i] = (unsigned char) floor(wv * 255.0);
|
2015-01-20 22:26:34 -05:00
|
|
|
}
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-10-18 13:44:31 -04:00
|
|
|
int newBufSize = (half_fft_size*lines_buffered+half_fft_size);
|
|
|
|
if (lineBuffer[j].size() < newBufSize) {
|
|
|
|
lineBuffer[j].resize(newBufSize);
|
|
|
|
rLineBuffer[j].resize(newBufSize);
|
|
|
|
}
|
|
|
|
memcpy(&(lineBuffer[j][half_fft_size*lines_buffered]), waterfall_slice, sizeof(unsigned char) * half_fft_size);
|
|
|
|
}
|
|
|
|
lines_buffered++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaterfallPanel::update() {
|
|
|
|
int half_fft_size = fft_size / 2;
|
|
|
|
|
|
|
|
for (int i = 0; i < lines_buffered; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
memcpy(&(rLineBuffer[j][i*half_fft_size]),
|
|
|
|
&(lineBuffer[j][((lines_buffered-1)*half_fft_size)-(i*half_fft_size)]), sizeof(unsigned char) * half_fft_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int run_ofs = 0;
|
|
|
|
while (lines_buffered) {
|
|
|
|
int run_lines = lines_buffered;
|
|
|
|
if (run_lines > waterfall_ofs[0]) {
|
|
|
|
run_lines = waterfall_ofs[0];
|
|
|
|
}
|
|
|
|
for (int j = 0; j < 2; j++) {
|
2015-02-08 13:49:12 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[j]);
|
2015-10-18 13:44:31 -04:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, waterfall_ofs[j]-run_lines, half_fft_size, run_lines,
|
|
|
|
GL_COLOR_INDEX, GL_UNSIGNED_BYTE, (GLvoid *) &(rLineBuffer[j][run_ofs]));
|
|
|
|
|
|
|
|
waterfall_ofs[j]-=run_lines;
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-22 21:32:32 -05:00
|
|
|
if (waterfall_ofs[j] == 0) {
|
|
|
|
waterfall_ofs[j] = waterfall_lines;
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 13:44:31 -04:00
|
|
|
run_ofs += run_lines*half_fft_size;
|
|
|
|
lines_buffered-=run_lines;
|
2015-01-20 22:26:34 -05:00
|
|
|
}
|
2015-08-09 23:00:51 -04:00
|
|
|
}
|
2014-11-15 23:41:41 -05:00
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
void WaterfallPanel::drawPanelContents() {
|
|
|
|
if (!waterfall[0]) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 18:45:46 -04:00
|
|
|
|
2015-08-09 23:00:51 -04:00
|
|
|
int half_fft_size = fft_size / 2;
|
|
|
|
|
|
|
|
glLoadMatrixf(transform);
|
|
|
|
|
|
|
|
glEnable (GL_TEXTURE_2D);
|
|
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
|
|
|
|
|
|
|
|
if (activeTheme != ThemeMgr::mgr.currentTheme) {
|
|
|
|
refreshTheme();
|
|
|
|
activeTheme = ThemeMgr::mgr.currentTheme;
|
|
|
|
}
|
2014-11-15 23:41:41 -05:00
|
|
|
glColor3f(1.0, 1.0, 1.0);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-22 21:32:32 -05:00
|
|
|
GLint vp[4];
|
2015-02-08 13:49:12 -05:00
|
|
|
glGetIntegerv(GL_VIEWPORT, vp);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-22 21:32:32 -05:00
|
|
|
float viewWidth = (float) vp[2];
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-22 21:32:32 -05:00
|
|
|
// some bias to prevent seams at odd scales
|
2015-02-08 13:49:12 -05:00
|
|
|
float half_pixel = 1.0 / viewWidth;
|
2015-01-22 21:32:32 -05:00
|
|
|
float half_texel = 1.0 / (float) half_fft_size;
|
2015-02-08 13:49:12 -05:00
|
|
|
float vtexel = 1.0 / (float) waterfall_lines;
|
|
|
|
float vofs = (float) (waterfall_ofs[0] + 1) * vtexel;
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[0]);
|
2015-02-08 13:49:12 -05:00
|
|
|
glBegin (GL_QUADS);
|
|
|
|
glTexCoord2f(0.0 + half_texel, 1.0 + vofs);
|
2014-11-15 23:41:41 -05:00
|
|
|
glVertex3f(-1.0, -1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(1.0 - half_texel, 1.0 + vofs);
|
2015-01-22 21:32:32 -05:00
|
|
|
glVertex3f(0.0 + half_pixel, -1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(1.0 - half_texel, 0.0 + vofs);
|
2015-01-22 21:32:32 -05:00
|
|
|
glVertex3f(0.0 + half_pixel, 1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(0.0 + half_texel, 0.0 + vofs);
|
2015-01-20 22:26:34 -05:00
|
|
|
glVertex3f(-1.0, 1.0, 0.0);
|
|
|
|
glEnd();
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-02-08 13:49:12 -05:00
|
|
|
vofs = (float) (waterfall_ofs[1] + 1) * vtexel;
|
2015-01-20 22:26:34 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, waterfall[1]);
|
|
|
|
glBegin(GL_QUADS);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(0.0 + half_texel, 1.0 + vofs);
|
2015-01-22 21:32:32 -05:00
|
|
|
glVertex3f(0.0 - half_pixel, -1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(1.0 - half_texel, 1.0 + vofs);
|
2014-11-15 23:41:41 -05:00
|
|
|
glVertex3f(1.0, -1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(1.0 - half_texel, 0.0 + vofs);
|
2014-11-15 23:41:41 -05:00
|
|
|
glVertex3f(1.0, 1.0, 0.0);
|
2015-02-08 13:49:12 -05:00
|
|
|
glTexCoord2f(0.0 + half_texel, 0.0 + vofs);
|
2015-01-22 21:32:32 -05:00
|
|
|
glVertex3f(0.0 - half_pixel, 1.0, 0.0);
|
2014-11-15 23:41:41 -05:00
|
|
|
glEnd();
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-20 22:26:34 -05:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2015-08-09 23:00:51 -04:00
|
|
|
|
2015-01-03 18:45:34 -05:00
|
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
2015-01-20 22:26:34 -05:00
|
|
|
glDisable(GL_TEXTURE_2D);
|
2015-08-10 18:45:46 -04:00
|
|
|
}
|