CubicSDR/src/visual/PrimaryGLContext.cpp

451 lines
13 KiB
C++
Raw Normal View History

#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"
#include "CubicSDRDefs.h"
#include "AppFrame.h"
#include <algorithm>
2014-11-09 04:38:33 -05:00
wxString PrimaryGLContext::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);
}
void PrimaryGLContext::CheckGLError() {
GLenum errLast = GL_NO_ERROR;
for (;;) {
GLenum err = glGetError();
if (err == GL_NO_ERROR)
return;
if (err == errLast) {
std::cout << "OpenGL error state couldn't be reset." << std::endl;
return;
}
errLast = err;
std::cout << "OpenGL Error " << err << std::endl;
}
}
PrimaryGLContext::PrimaryGLContext(wxGLCanvas *canvas, wxGLContext *sharedContext) :
wxGLContext(canvas, sharedContext), hoverAlpha(1.0) {
//#ifndef __linux__
// SetCurrent(*canvas);
// // Pre-load fonts
// for (int i = 0; i < GLFONT_MAX; i++) {
// getFont((GLFontSize) i);
// }
// CheckGLError();
//#endif
}
void PrimaryGLContext::DrawDemodInfo(DemodulatorInstance *demod, RGBA4f color, long long center_freq, long long srate) {
if (!demod) {
return;
}
2015-01-11 17:08:16 -05:00
if (!srate) {
srate = wxGetApp().getSampleRate();
}
GLint vp[4];
glGetIntegerv( GL_VIEWPORT, vp);
float viewHeight = (float) vp[3];
float viewWidth = (float) vp[2];
if (center_freq == -1) {
center_freq = wxGetApp().getFrequency();
}
float uxPos = (float) (demod->getFrequency() - (center_freq - srate / 2)) / (float) srate;
uxPos = (uxPos - 0.5) * 2.0;
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
2015-01-15 20:37:51 -05:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(color.r, color.g, color.b, 0.6);
float ofs = ((float) demod->getBandwidth()) / (float) srate;
float ofsLeft = (demod->getDemodulatorType()!="USB")?ofs:0, ofsRight = (demod->getDemodulatorType()!="LSB")?ofs:0;
2015-01-06 19:15:27 -05:00
float labelHeight = 20.0 / viewHeight;
float hPos = -1.0 + labelHeight;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (demod->isMuted()) {
glColor4f(0.8, 0, 0, 0.35);
} else {
glColor4f(0, 0, 0, 0.35);
}
2015-01-06 19:15:27 -05:00
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, hPos + labelHeight, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
2015-01-06 19:15:27 -05:00
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, hPos + labelHeight, 0.0);
2015-01-06 19:15:27 -05:00
glEnd();
2015-01-20 22:26:34 -05:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
2015-01-06 19:15:27 -05:00
glColor4f(color.r, color.g, color.b, 0.2);
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, 1.0, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, 1.0, 0.0);
glEnd();
if (ofs * 2.0 < 16.0 / viewWidth) {
glColor4f(color.r, color.g, color.b, 0.2);
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, hPos + labelHeight, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, hPos + labelHeight, 0.0);
glEnd();
}
glColor4f(1.0, 1.0, 1.0, 0.8);
std::string demodLabel = demod->getLabel();
if (demod->isMuted()) {
2015-08-17 01:11:42 -04:00
demodLabel = std::string("[M] ") + demodLabel;
}
if (demod->getDemodulatorType() == "USB") {
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_LEFT, GLFont::GLFONT_ALIGN_CENTER);
} else if (demod->getDemodulatorType() == "LSB") {
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_RIGHT, GLFont::GLFONT_ALIGN_CENTER);
} else {
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER);
}
glDisable(GL_BLEND);
}
2014-12-08 21:08:03 -05:00
void PrimaryGLContext::DrawFreqBwInfo(long long freq, int bw, RGBA4f color, long long center_freq, long long srate, bool stack) {
if (!srate) {
srate = wxGetApp().getSampleRate();
}
GLint vp[4];
glGetIntegerv( GL_VIEWPORT, vp);
float viewHeight = (float) vp[3];
float viewWidth = (float) vp[2];
if (center_freq == -1) {
center_freq = wxGetApp().getFrequency();
}
float uxPos = (float) (freq - (center_freq - srate / 2)) / (float) srate;
uxPos = (uxPos - 0.5) * 2.0;
std::string lastType = wxGetApp().getDemodMgr().getLastDemodulatorType();
float ofs = (float) bw / (float) srate;
float ofsLeft = (lastType!="USB")?ofs:0, ofsRight = (lastType!="LSB")?ofs:0;
float labelHeight = 20.0 / viewHeight;
float hPos = -1.0 + (stack?(labelHeight*3.0):labelHeight);
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0, 0, 0, 0.35);
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, hPos + labelHeight, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, hPos + labelHeight, 0.0);
glEnd();
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(color.r, color.g, color.b, 0.1);
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, 1.0, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, 1.0, 0.0);
glEnd();
if (ofs * 2.0 < 16.0 / viewWidth) {
glColor4f(color.r, color.g, color.b, 0.1);
glBegin(GL_QUADS);
glVertex3f(uxPos - ofsLeft, hPos + labelHeight, 0.0);
glVertex3f(uxPos - ofsLeft, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, -1.0, 0.0);
glVertex3f(uxPos + ofsRight, hPos + labelHeight, 0.0);
glEnd();
}
glColor4f(1.0, 1.0, 1.0, 0.8);
std::string demodLabel = std::to_string((double)freq/1000000.0);
double shadowOfsX = 3.0 / viewWidth, shadowOfsY = 3.0 / viewHeight;
if (lastType == "USB") {
glColor4f(0,0,0, 1.0);
glBlendFunc(GL_ONE, GL_ZERO);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos+shadowOfsX, hPos-shadowOfsY, 16, GLFont::GLFONT_ALIGN_LEFT, GLFont::GLFONT_ALIGN_CENTER);
glColor4f(color.r, color.g, color.b, 1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_LEFT, GLFont::GLFONT_ALIGN_CENTER);
} else if (lastType == "LSB") {
glBlendFunc(GL_ONE, GL_ZERO);
glColor4f(0,0,0, 1.0);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos+shadowOfsX, hPos-shadowOfsY, 16, GLFont::GLFONT_ALIGN_RIGHT, GLFont::GLFONT_ALIGN_CENTER);
glColor4f(color.r, color.g, color.b, 1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_RIGHT, GLFont::GLFONT_ALIGN_CENTER);
} else {
glBlendFunc(GL_ONE, GL_ZERO);
glColor4f(0,0,0, 1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos+shadowOfsX, hPos-shadowOfsY, 16, GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER);
glColor4f(color.r, color.g, color.b, 1.0);
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodLabel, uxPos, hPos, 16, GLFont::GLFONT_ALIGN_CENTER, GLFont::GLFONT_ALIGN_CENTER);
}
glDisable(GL_BLEND);
}
void PrimaryGLContext::DrawDemod(DemodulatorInstance *demod, RGBA4f color, long long center_freq, long long srate) {
2014-12-08 21:08:03 -05:00
if (!demod) {
return;
}
2015-01-11 17:08:16 -05:00
if (!srate) {
srate = wxGetApp().getSampleRate();
}
2014-12-08 21:08:03 -05:00
if (center_freq == -1) {
center_freq = wxGetApp().getFrequency();
}
float uxPos = (float) (demod->getFrequency() - (center_freq - srate / 2)) / (float) srate;
2014-12-08 21:08:03 -05:00
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
2015-01-20 22:26:34 -05:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(color.r, color.g, color.b, 0.6);
2014-12-08 21:08:03 -05:00
float ofs = ((float) demod->getBandwidth()) / (float) srate;
float ofsLeft = (demod->getDemodulatorType()!="USB")?ofs:0, ofsRight = (demod->getDemodulatorType()!="LSB")?ofs:0;
2014-12-08 21:08:03 -05:00
glBegin(GL_LINES);
glVertex3f((uxPos - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0, -1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofsLeft, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofsLeft, -1.0, 0.0);
2014-12-08 21:08:03 -05:00
glVertex3f((uxPos - 0.5) * 2.0 + ofsRight, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 + ofsRight, -1.0, 0.0);
2014-12-08 21:08:03 -05:00
glEnd();
2015-01-20 22:26:34 -05:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(color.r, color.g, color.b, 0.2*hoverAlpha);
2014-12-08 21:08:03 -05:00
glBegin(GL_QUADS);
glVertex3f((uxPos - 0.5) * 2.0 - ofsLeft, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofsLeft, -1.0, 0.0);
2014-12-08 21:08:03 -05:00
glVertex3f((uxPos - 0.5) * 2.0 + ofsRight, -1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 + ofsRight, 1.0, 0.0);
2014-12-08 21:08:03 -05:00
glEnd();
2015-01-03 18:45:34 -05:00
GLint vp[4];
glGetIntegerv( GL_VIEWPORT, vp);
float viewHeight = (float) vp[3];
float viewWidth = (float) vp[2];
float labelHeight = 20.0 / viewHeight;
float xOfs = (2.0 / viewWidth);
float yOfs = (2.0 / viewHeight);
float hPos = labelHeight;
glDisable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
2015-05-30 00:09:51 -04:00
std::string demodStr = "";
GLFont::Align demodAlign = GLFont::GLFONT_ALIGN_CENTER;
2015-01-03 18:45:34 -05:00
demodStr = demod->getDemodulatorType();
demodAlign = GLFont::GLFONT_ALIGN_CENTER;
if (demodStr == "LSB") {
2015-01-03 18:45:34 -05:00
demodAlign = GLFont::GLFONT_ALIGN_RIGHT;
uxPos -= xOfs;
} else if (demodStr == "USB") {
2015-01-03 18:45:34 -05:00
demodAlign = GLFont::GLFONT_ALIGN_LEFT;
uxPos += xOfs;
2015-01-03 18:45:34 -05:00
}
// advanced demodulators start here
2015-01-03 18:45:34 -05:00
// if (demod->getDemodulatorCons() > 0) {
// demodStr = demodStr + std::to_string(demod->getDemodulatorCons());
// }
2015-06-05 03:51:46 -04:00
// add lock to string if we have an lock
if(demod->getDemodulatorLock()) {
demodStr = demodStr + " Lock";
}
// else {
// demodStr = demodStr + " UnLock";
// }
2015-01-03 18:45:34 -05:00
glColor3f(0, 0, 0);
2015-06-30 23:07:39 -04:00
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodStr, 2.0 * (uxPos - 0.5) + xOfs, -1.0 + hPos - yOfs, 16, demodAlign,
2015-01-03 18:45:34 -05:00
GLFont::GLFONT_ALIGN_CENTER);
2015-08-17 01:11:42 -04:00
glColor3f(1, 1, 1);
2015-06-30 23:07:39 -04:00
GLFont::getFont(GLFont::GLFONT_SIZE16).drawString(demodStr, 2.0 * (uxPos - 0.5), -1.0 + hPos, 16, demodAlign, GLFont::GLFONT_ALIGN_CENTER);
2015-01-03 18:45:34 -05:00
2014-12-08 21:08:03 -05:00
glDisable(GL_BLEND);
2015-01-03 18:45:34 -05:00
2014-12-08 21:08:03 -05:00
}
void PrimaryGLContext::DrawFreqSelector(float uxPos, RGBA4f color, float w, long long center_freq, long long srate) {
DemodulatorInstance *demod = wxGetApp().getDemodMgr().getLastActiveDemodulator();
long long bw = 0;
2014-12-08 21:08:03 -05:00
std::string last_type = wxGetApp().getDemodMgr().getLastDemodulatorType();
2014-12-08 21:08:03 -05:00
if (!demod) {
bw = wxGetApp().getDemodMgr().getLastBandwidth();
} else {
bw = demod->getBandwidth();
2014-12-08 21:08:03 -05:00
}
2015-01-11 17:08:16 -05:00
if (!srate) {
srate = wxGetApp().getSampleRate();
}
2014-12-08 21:08:03 -05:00
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
2015-01-20 22:26:34 -05:00
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(color.r, color.g, color.b, 0.6);
2014-12-08 21:08:03 -05:00
glBegin(GL_LINES);
2014-12-08 21:08:03 -05:00
glVertex3f((uxPos - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0, -1.0, 0.0);
float ofs;
if (w) {
ofs = w;
} else {
ofs = ((float) bw) / (float) srate;
}
2014-12-08 21:08:03 -05:00
if (last_type != "USB") {
glVertex3f((uxPos - 0.5) * 2.0 - ofs, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 - ofs, -1.0, 0.0);
}
2014-12-08 21:08:03 -05:00
if (last_type != "LSB") {
glVertex3f((uxPos - 0.5) * 2.0 + ofs, 1.0, 0.0);
glVertex3f((uxPos - 0.5) * 2.0 + ofs, -1.0, 0.0);
}
2014-12-08 21:08:03 -05:00
glEnd();
glDisable(GL_BLEND);
}
void PrimaryGLContext::DrawRangeSelector(float uxPos1, float uxPos2, RGBA4f color) {
if (uxPos2 < uxPos1) {
float temp = uxPos2;
uxPos2=uxPos1;
uxPos1=temp;
}
std::string last_type = wxGetApp().getDemodMgr().getLastDemodulatorType();
glDisable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glColor4f(color.r, color.g, color.b, 0.6);
glLineWidth((last_type == "USB")?2.0:1.0);
glBegin(GL_LINES);
glVertex3f((uxPos1 - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos1 - 0.5) * 2.0, -1.0, 0.0);
glEnd();
glLineWidth((last_type == "LSB")?2.0:1.0);
glBegin(GL_LINES);
glVertex3f((uxPos2 - 0.5) * 2.0, 1.0, 0.0);
glVertex3f((uxPos2 - 0.5) * 2.0, -1.0, 0.0);
glEnd();
glLineWidth(1.0);
glDisable(GL_BLEND);
}
2015-01-15 00:59:33 -05:00
void PrimaryGLContext::BeginDraw(float r, float g, float b) {
glClearColor(r,g,b, 1);
2014-12-08 21:08:03 -05:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void PrimaryGLContext::EndDraw() {
glFlush();
CheckGLError();
}
void PrimaryGLContext::setHoverAlpha(float hoverAlpha) {
this->hoverAlpha = hoverAlpha;
}