gradient util, started waterfall implementation

This commit is contained in:
Charles J. Cliffe 2014-11-11 08:11:12 -05:00
parent b270379361
commit fe2e56231d
6 changed files with 143 additions and 9 deletions

View File

@ -83,7 +83,7 @@ endif (DEFINED WIN32)
SET (cubicsdr_sources
src/CubicSDR.cpp
src/CubicSDR.cpp
src/SDRThread.cpp
src/IQBufferThread.cpp
src/PrimaryGLContext.cpp
@ -91,10 +91,11 @@ SET (cubicsdr_sources
src/SDRThreadQueue.cpp
src/SDRThreadTask.cpp
src/Demodulator.cpp
src/Gradient.cpp
)
SET (cubicsdr_headers
src/CubicSDR.h
src/CubicSDR.h
src/SDRThread.h
src/IQBufferThread.h
src/PrimaryGLContext.h
@ -103,6 +104,7 @@ SET (cubicsdr_headers
src/SDRThreadQueue.h
src/SDRThreadTask.h
src/Demodulator.h
src/Gradient.h
)
#configure_files(${PROJECT_SOURCE_DIR}/shaders ${PROJECT_BINARY_DIR}/shaders COPYONLY)
#configure_files(${PROJECT_SOURCE_DIR}/png ${PROJECT_BINARY_DIR}/png COPYONLY)

View File

@ -2,7 +2,7 @@
#define BUF_SIZE (16 * 32 * 256)
#define SRATE 2500000
#define FFT_SIZE 8192
#define FFT_SIZE 4096
#define DEFAULT_FREQ 107500000

2
src/Gradient.cpp Normal file
View File

@ -0,0 +1,2 @@
#include "Gradient.h"

66
src/Gradient.h Normal file
View File

@ -0,0 +1,66 @@
#pragma once
#include <vector>
class GradientColor {
public:
float r,g,b;
float w;
GradientColor(float r_in, float g_in, float b_in) : r(r_in), g(g_in), b(b_in) {
};
};
class Gradient {
public:
Gradient() {
}
void addColor(GradientColor c) {
colors.push_back(c);
}
void generate(std::vector<unsigned char> *out, unsigned int len) {
int chunk_size = len/(colors.size()-1);
out->resize(len*3);
int p = 0;
for (unsigned int j = 0, jMax = colors.size()-1; j < jMax; j++) {
if (chunk_size*3 < len && j == jMax-1) {
chunk_size += len-chunk_size*3;
}
for (unsigned int i = 0; i < chunk_size; i++) {
float idx = (float)(i+1)/(float)chunk_size;
float r1 = colors[j].r;
float g1 = colors[j].g;
float b1 = colors[j].b;
float r2 = colors[j+1].r;
float g2 = colors[j+1].g;
float b2 = colors[j+1].b;
float r = r1 + (r2-r1) * idx;
float g = g1 + (g2-g1) * idx;
float b = b1 + (b2-b1) * idx;
(*out)[p*3] = (unsigned char)(r*255.0);
(*out)[p*3+1] = (unsigned char)(g*255.0);
(*out)[p*3+2] = (unsigned char)(b*255.0);
p++;
}
}
}
~Gradient() {
}
private:
std::vector<GradientColor> colors;
};

View File

@ -62,7 +62,7 @@ PrimaryGLContext::PrimaryGLContext(wxGLCanvas *canvas) :
CheckGLError();
}
void PrimaryGLContext::Plot(std::vector<float> &points, std::vector<float> &points2) {
void PrimaryGLContext::Plot(std::vector<float> &points, std::vector<float> &points2, GLuint tex) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
@ -91,7 +91,21 @@ void PrimaryGLContext::Plot(std::vector<float> &points, std::vector<float> &poin
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}
glEnable(GL_TEXTURE_2D);
// glEnable(GL_COLOR_TABLE);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex3f(-0.8,-1.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex3f(1.0,-1.0,0.0);
glTexCoord2f(1.0,1.0);
glVertex3f(1.0,1.0,0.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-1.0,1.0,0.0);
glEnd();
glFlush();
CheckGLError();
@ -120,7 +134,40 @@ TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
fft_ceil_ma = fft_ceil_maa = 1.0;
grad.addColor(GradientColor(0,0.5,1.0));
grad.addColor(GradientColor(1.0,0,0));
grad.addColor(GradientColor(0,1.0,1.0));
grad.generate(&color_map,256);
glGenTextures(1, &waterfall);
std::cout << waterfall << std::endl;
std::cout << waterfall << std::endl;
std::cout << waterfall << std::endl;
std::cout << waterfall << std::endl;
// for (int i = 0; i < c.size()/3; i++) {
// std::cout << i << ": r[" << (int)c[i*3] << "] g[" << (int)c[i*3+1] << "] b[" << (int)c[i*3+2] << "] " << std::endl;
// }
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,waterfall);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// glTexImage2D(GL_TEXTURE_2D,0,GL_INTENSITY,FFT_SIZE,NUM_WATERFALL_LINES,0,GL_COLOR_INDEX,GL_UNSIGNED_BYTE,(GLvoid *)waterfall_tex);
float clr[16] = { 255, 0, 0, 0,
0, 255, 0, 0,
0, 0, 255, 0,
255, 255, 255, 0 };
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,2,2,0,GL_RGBA,GL_UNSIGNED_BYTE,clr);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glColorTable(GL_TEXTURE_2D,GL_RGB8,256,GL_RGB,GL_UNSIGNED_BYTE,&color_map[0]);
}
TestGLCanvas::~TestGLCanvas() {
@ -134,9 +181,8 @@ void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
PrimaryGLContext& canvas = wxGetApp().GetContext(this);
glViewport(0, 0, ClientSize.x, ClientSize.y);
std::vector<float> null_pts;
canvas.Plot(spectrum_points, test_demod.waveform_points);
canvas.Plot(spectrum_points, test_demod.waveform_points, waterfall);
SwapBuffers();
}
@ -233,12 +279,18 @@ void TestGLCanvas::setData(std::vector<signed char> *data) {
// fftw_execute(plan[1]);
memmove(waterfall_tex+NUM_WATERFALL_LINES,waterfall_tex,(NUM_WATERFALL_LINES-1)*FFT_SIZE);
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
spectrum_points[i * 2 + 1] = log10(fft_result_maa[i]) / log10(fft_ceil_maa);
// spectrum_points[i * 2 + 1] = (fft_result_maa[i]) / (fft_ceil_maa);
spectrum_points[i * 2] = ((double) i / (double) iMax);
float v = ((float) i / (float) iMax);
spectrum_points[i * 2] = v;
waterfall_tex[i] = (unsigned char)(v*255.0);
}
test_demod.writeBuffer(data);
}
}

View File

@ -10,11 +10,15 @@
#include "fftw3.h"
#include "Demodulator.h"
#include "Gradient.h"
#define NUM_WATERFALL_LINES 256
class PrimaryGLContext: public wxGLContext {
public:
PrimaryGLContext(wxGLCanvas *canvas);
void Plot(std::vector<float> &points, std::vector<float> &points2);
void Plot(std::vector<float> &points, std::vector<float> &points2, GLuint tex);
private:
};
@ -44,6 +48,14 @@ private:
std::vector<float> fft_result_ma;
std::vector<float> fft_result_maa;
Gradient grad;
std::vector<unsigned char> color_map;
unsigned char waterfall_tex[FFT_SIZE * NUM_WATERFALL_LINES];
GLuint waterfall;
Demodulator test_demod;
wxDECLARE_EVENT_TABLE();
};