2014-10-27 21:22:29 -04:00
|
|
|
#include "PrimaryGLContext.h"
|
|
|
|
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !wxUSE_GLCANVAS
|
|
|
|
#error "OpenGL required: set wxUSE_GLCANVAS to 1 and rebuild the library"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "CubicSDR.h"
|
2014-10-30 22:51:33 -04:00
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include <algorithm>
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
wxString glGetwxString(GLenum name) {
|
|
|
|
const GLubyte *v = glGetString(name);
|
|
|
|
if (v == 0) {
|
|
|
|
// The error is not important. It is GL_INVALID_ENUM.
|
|
|
|
// We just want to clear the error stack.
|
|
|
|
glGetError();
|
|
|
|
|
|
|
|
return wxString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxString((const char*) v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckGLError() {
|
|
|
|
GLenum errLast = GL_NO_ERROR;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
GLenum err = glGetError();
|
|
|
|
if (err == GL_NO_ERROR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (err == errLast) {
|
|
|
|
wxLogError
|
|
|
|
(wxT("OpenGL error state couldn't be reset."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errLast = err;
|
|
|
|
|
|
|
|
wxLogError
|
|
|
|
(wxT("OpenGL error %d"), err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimaryGLContext::PrimaryGLContext(wxGLCanvas *canvas) :
|
|
|
|
wxGLContext(canvas) {
|
|
|
|
SetCurrent(*canvas);
|
|
|
|
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
CheckGLError();
|
|
|
|
}
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
void PrimaryGLContext::PlotIQ(std::vector<float> &i_points, std::vector<float> &q_points) {
|
2014-10-27 21:22:29 -04:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(0.0f, 0.5f, 0.0f);
|
|
|
|
if (q_points.size()) {
|
2014-10-30 22:51:33 -04:00
|
|
|
// glScalef(10.0f, 1.0f, 1.0f);
|
2014-10-28 22:59:17 -04:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &q_points[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, q_points.size() / 2);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
} else {
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
glColor3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 0.0f, 0.0f);
|
|
|
|
glVertex3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
glPopMatrix();
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(0.0f, -0.5f, 0.0f);
|
|
|
|
if (i_points.size()) {
|
2014-10-30 22:51:33 -04:00
|
|
|
// glScalef(10.0f, 1.0f, 1.0f);
|
2014-10-28 22:59:17 -04:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, &i_points[0]);
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, i_points.size() / 2);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
} else {
|
|
|
|
glBegin(GL_LINE_STRIP);
|
|
|
|
glColor3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 0.0f, 0.0f);
|
|
|
|
glVertex3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
glPopMatrix();
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
CheckGLError();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) EVT_PAINT(TestGLCanvas::OnPaint)
|
|
|
|
EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
|
2014-10-28 22:59:17 -04:00
|
|
|
EVT_IDLE(TestGLCanvas::OnIdle)
|
2014-10-27 21:22:29 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
|
|
|
|
|
|
|
TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
|
|
|
|
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
|
|
|
|
wxFULL_REPAINT_ON_RESIZE) {
|
|
|
|
|
2014-10-30 22:51:33 -04:00
|
|
|
int in_block_size = BUF_SIZE/2;
|
|
|
|
int out_block_size = FFT_SIZE;
|
|
|
|
|
|
|
|
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * in_block_size);
|
|
|
|
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * out_block_size);
|
|
|
|
plan = fftw_plan_dft_1d(out_block_size, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
|
|
|
|
|
2014-10-27 21:22:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestGLCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) {
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
const wxSize ClientSize = GetClientSize();
|
|
|
|
|
|
|
|
PrimaryGLContext& canvas = wxGetApp().GetContext(this);
|
|
|
|
glViewport(0, 0, ClientSize.x, ClientSize.y);
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
canvas.PlotIQ(i_points, q_points);
|
2014-10-27 21:22:29 -04:00
|
|
|
|
|
|
|
SwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGLCanvas::OnKeyDown(wxKeyEvent& event) {
|
|
|
|
float angle = 5.0;
|
|
|
|
|
|
|
|
switch (event.GetKeyCode()) {
|
|
|
|
case WXK_RIGHT:
|
|
|
|
break;
|
|
|
|
case WXK_LEFT:
|
|
|
|
break;
|
|
|
|
case WXK_DOWN:
|
|
|
|
break;
|
|
|
|
case WXK_UP:
|
|
|
|
break;
|
|
|
|
case WXK_SPACE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
void TestGLCanvas::setData(std::vector<signed char> *data) {
|
2014-10-30 22:51:33 -04:00
|
|
|
|
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
if (data && data->size()) {
|
2014-10-30 22:51:33 -04:00
|
|
|
|
|
|
|
if (i_points.size() < FFT_SIZE*2) {
|
|
|
|
i_points.resize(FFT_SIZE*2);
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
2014-10-30 22:51:33 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < BUF_SIZE / 2; i++) {
|
|
|
|
in[i][0] = (float) (*data)[i * 2] / 127.0f;
|
|
|
|
in[i][1] = (float) (*data)[i * 2 + 1] / 127.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for (int i = 0; i < BUF_SIZE / 2; i++) {
|
|
|
|
// double ang = (M_PI / (float) BUF_SIZE) * (float) i;
|
|
|
|
// double w = 0.5 * (1.0 - cos(ang));
|
|
|
|
//
|
|
|
|
// in[i][0] *= w;
|
|
|
|
// in[i][1] *= w;
|
|
|
|
// }
|
|
|
|
|
|
|
|
fftw_execute(plan);
|
|
|
|
|
|
|
|
float result[FFT_SIZE];
|
|
|
|
float fft_floor, fft_ceil;
|
|
|
|
|
|
|
|
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
|
|
|
|
double a = out[i][0];
|
|
|
|
double b = out[i][1];
|
|
|
|
|
|
|
|
double c = sqrt(a*a+b*b);
|
|
|
|
if (i==1) {
|
|
|
|
fft_floor=fft_ceil=c;
|
|
|
|
} else {
|
|
|
|
if (c<fft_floor) {
|
|
|
|
fft_floor = c;
|
|
|
|
}
|
|
|
|
if (c>fft_ceil) {
|
|
|
|
fft_ceil = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result[i] = c;
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
|
|
|
|
2014-10-30 22:51:33 -04:00
|
|
|
for (int i = 0, iMax = FFT_SIZE; i < iMax; i++) {
|
|
|
|
i_points[i * 2 + 1] = (result[i]-fft_floor)/(fft_ceil-fft_floor);
|
|
|
|
i_points[i * 2] = 2.0f * ((float) i / (float) iMax) - 1.0f;
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
2014-10-30 22:51:33 -04:00
|
|
|
|
2014-10-28 22:59:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestGLCanvas::OnIdle(wxIdleEvent &event) {
|
|
|
|
Refresh(false);
|
|
|
|
}
|
2014-10-30 22:51:33 -04:00
|
|
|
|