MathGL have possibility to draw resulting plot using OpenGL. This produce resulting plot a bit faster, but with some limitations (especially at use of transparency and lighting). Generally, you need to prepare OpenGL window and call MathGL functions to draw it. There is GLUT interface (see Widget classes) to do it by simple way. Below I show example of OpenGL usage basing on Qt libraries (i.e. by using QGLWidget
widget).
First, one need to define widget class derived from QGLWidget
and implement a few methods: resizeGL()
called after each window resize, paintGL()
for displaying the image on the screen, and initializeGL()
for initializing OpenGL. The header file looks as following.
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QGLWidget> #include <mgl2/mgl.h> class MainWindow : public QGLWidget { Q_OBJECT protected: mglGraph *gr; // pointer to MathGL core class void resizeGL(int nWidth, int nHeight); // Method called after each window resize void paintGL(); // Method to display the image on the screen void initializeGL(); // Method to initialize OpenGL public: MainWindow(QWidget *parent = 0); ~MainWindow(); }; #endif // MAINWINDOW_H
The class implementation is rather straightforward. One need to recreate the instance of mglGraph at initializing OpenGL, and ask MathGL to use OpenGL output (set argument 1
in mglGraph constructor). Of course, the mglGraph object should be deleted at destruction. The method resizeGL()
just pass new sizes to OpenGL and update viewport sizes. All plotting functions are located in the method paintGL()
. At this, one need to add 2 calls: gr->Clf()
at beginning for clearing previous OpenGL primitives; and swapBuffers()
for showing output on the screen. The source file looks as following.
#include "qgl_example.h" #include <QApplication> //#include <QtOpenGL> //----------------------------------------------------------------------------- MainWindow::MainWindow(QWidget *parent) : QGLWidget(parent) { gr=0; } //----------------------------------------------------------------------------- MainWindow::~MainWindow() { if(gr) delete gr; } //----------------------------------------------------------------------------- void MainWindow::initializeGL() // recreate instance of MathGL core { if(gr) delete gr; gr = new mglGraph(1); // use '1' for argument to force OpenGL output in MathGL } //----------------------------------------------------------------------------- void MainWindow::resizeGL(int w, int h) // standard resize replace { QGLWidget::resizeGL(w, h); glViewport (0, 0, w, h); } //----------------------------------------------------------------------------- void MainWindow::paintGL() // main drawing function { gr->Clf(); // clear previous OpenGL primitives gr->SubPlot(1,1,0); gr->Rotate(40,60); gr->Light(true); gr->AddLight(0,mglPoint(0,0,10),mglPoint(0,0,-1)); gr->Axis(); gr->Box(); gr->FPlot("sin(pi*x)","i2"); gr->FPlot("cos(pi*x)","|"); gr->FSurf("cos(2*pi*(x^2+y^2))"); gr->Finish(); swapBuffers(); // show output on the screen } //----------------------------------------------------------------------------- int main(int argc, char *argv[]) // create application { mgl_textdomain(argv?argv[0]:NULL,""); QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } //-----------------------------------------------------------------------------