From fe2e56231db1e18a401cfa8e07bffc5a93d5e81d Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 11 Nov 2014 08:11:12 -0500 Subject: [PATCH] gradient util, started waterfall implementation --- CMakeLists.txt | 6 ++-- src/CubicSDRDefs.h | 2 +- src/Gradient.cpp | 2 ++ src/Gradient.h | 66 ++++++++++++++++++++++++++++++++++++++++ src/PrimaryGLContext.cpp | 62 ++++++++++++++++++++++++++++++++++--- src/PrimaryGLContext.h | 14 ++++++++- 6 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 src/Gradient.cpp create mode 100644 src/Gradient.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 72ad941..db83905 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/CubicSDRDefs.h b/src/CubicSDRDefs.h index ed1593c..62c2d3a 100644 --- a/src/CubicSDRDefs.h +++ b/src/CubicSDRDefs.h @@ -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 diff --git a/src/Gradient.cpp b/src/Gradient.cpp new file mode 100644 index 0000000..fa33aba --- /dev/null +++ b/src/Gradient.cpp @@ -0,0 +1,2 @@ +#include "Gradient.h" + diff --git a/src/Gradient.h b/src/Gradient.h new file mode 100644 index 0000000..ce3b974 --- /dev/null +++ b/src/Gradient.h @@ -0,0 +1,66 @@ +#pragma once + +#include + +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 *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 colors; +}; \ No newline at end of file diff --git a/src/PrimaryGLContext.cpp b/src/PrimaryGLContext.cpp index 636a852..58d7a03 100644 --- a/src/PrimaryGLContext.cpp +++ b/src/PrimaryGLContext.cpp @@ -62,7 +62,7 @@ PrimaryGLContext::PrimaryGLContext(wxGLCanvas *canvas) : CheckGLError(); } -void PrimaryGLContext::Plot(std::vector &points, std::vector &points2) { +void PrimaryGLContext::Plot(std::vector &points, std::vector &points2, GLuint tex) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); @@ -91,7 +91,21 @@ void PrimaryGLContext::Plot(std::vector &points, std::vector &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 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 *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); } } diff --git a/src/PrimaryGLContext.h b/src/PrimaryGLContext.h index 9d25844..dd6b914 100644 --- a/src/PrimaryGLContext.h +++ b/src/PrimaryGLContext.h @@ -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 &points, std::vector &points2); + void Plot(std::vector &points, std::vector &points2, GLuint tex); private: }; @@ -44,6 +48,14 @@ private: std::vector fft_result_ma; std::vector fft_result_maa; + Gradient grad; + + std::vector color_map; + + unsigned char waterfall_tex[FFT_SIZE * NUM_WATERFALL_LINES]; + + GLuint waterfall; + Demodulator test_demod; wxDECLARE_EVENT_TABLE(); };