mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2024-11-10 14:23:27 -05:00
127 lines
2.5 KiB
C++
127 lines
2.5 KiB
C++
|
#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"
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
|
||
|
void PrimaryGLContext::PlotIQ() {
|
||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||
|
|
||
|
glMatrixMode(GL_MODELVIEW);
|
||
|
glLoadIdentity();
|
||
|
|
||
|
glBegin(GL_LINE_STRIP);
|
||
|
glColor3f(1.0f, 1.0f, 1.0f);
|
||
|
glVertex3f(-1.0f, -0.5f, 0.0f);
|
||
|
glVertex3f(1.0f, -0.5f, 0.0f);
|
||
|
glEnd();
|
||
|
|
||
|
glBegin(GL_LINE_STRIP);
|
||
|
glColor3f(1.0f, 1.0f, 1.0f);
|
||
|
glVertex3f(-1.0f, 0.5f, 0.0f);
|
||
|
glVertex3f(1.0f, 0.5f, 0.0f);
|
||
|
glEnd();
|
||
|
|
||
|
glFlush();
|
||
|
|
||
|
CheckGLError();
|
||
|
}
|
||
|
|
||
|
wxBEGIN_EVENT_TABLE(TestGLCanvas, wxGLCanvas) EVT_PAINT(TestGLCanvas::OnPaint)
|
||
|
EVT_KEY_DOWN(TestGLCanvas::OnKeyDown)
|
||
|
wxEND_EVENT_TABLE()
|
||
|
|
||
|
TestGLCanvas::TestGLCanvas(wxWindow *parent, int *attribList) :
|
||
|
wxGLCanvas(parent, wxID_ANY, attribList, wxDefaultPosition, wxDefaultSize,
|
||
|
wxFULL_REPAINT_ON_RESIZE) {
|
||
|
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
|
||
|
canvas.PlotIQ();
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|