c++openglglutgnuplot-iostream

GLUT and gnuplot window not showing up at the same time


I have the following functions whose objective is to display a GLUT window displaying a 3D object and a Gnuplot window to display a plot.

For that I use the Gnuplot-Iostream Interface. The plotting code is located inside a function as it will be updated when the user types on the keyboard.

The following code will only display the Gnuplot window after I close the GLUT window:

#include "gnuplot-iostream.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void displayGraph();
void displayGnuplot();
Gnuplot gp;

int main(int argc, char** argv) {

    displayGnuplot();

    glutInit(&argc,argv);
    glutInitWindowSize(1024, 1024);
    glutInitWindowPosition(1080,10);
    glutCreateWindow("Continuum Data");
    glutDisplayFunc(displayGraph);

    glutMainLoop();
}

void displayGraph(){
    /*
    Code to display in Glut window that will be updated
    */
}

void displayGnuplot(){

    bool displayGnuplot = true;
    gp << "set xrange [-2:2]\nset yrange [-2:2]\n";
    gp << "plot '-' with vectors title 'pts_A', '-' with vectors title 'pts_B'\n";
}

What works is declaring the Gnuplot instance inside the displayGraph function. Unfortunately this wont work for my case as each time the displayGraph function is called a new Gnuplot window is created whereas I just want the Gnuplot window updated.

I've also tried putting a condition around the creation of the Gnuplot window to no avail:

#include "gnuplot-iostream.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void displayGraph();
void displayGnuplot();
Gnuplot gp;

int main(int argc, char** argv) {

    displayGnuplot();

    glutInit(&argc,argv);
    glutInitWindowSize(1024, 1024);
    glutInitWindowPosition(1080,10);
    glutCreateWindow("Continuum Data");
    glutDisplayFunc(displayGraph);

    glutMainLoop();
}

void displayGraph(){
    /*
    Code to display in Glut window that will be updated
    */
}

void displayGnuplot(){

    if(!gnuplotExists){
        Gnuplot gp;
        gnuplotExists = true;
    }
    gp << "set xrange [-2:2]\nset yrange [-2:2]\n";
    gp << "plot '-' with vectors title 'pts_A', '-' with vectors title 'pts_B'\n";
}

Solution

  • I've found a solution using a different C++ interface to Gnuplot, namely gnuplot-cpp

    #include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communikation with Gnuplot
    #include <GL/glut.h>
    
    void displayGraph();
    void displayGnuplot();
    Gnuplot g1("lines");
    
    int main(int argc, char** argv) {
    
        displayGnuplot();
    
        glutInit(&argc,argv);
        glutInitWindowSize(1024, 1024);
        glutInitWindowPosition(1080,10);
        glutCreateWindow("Continuum Data");
        glutDisplayFunc(displayGraph);
    
        glutMainLoop();
    }
    
    void displayGraph(){
    
    }
    
    void displayGnuplot(){
    
        g1.set_title("Slopes\\nNew Line");
        g1.plot_slope(1.0,0.0,"y=x");
        g1.plot_slope(2.0,0.0,"y=2x");
        g1.plot_slope(-1.0,0.0,"y=-x");
        g1.unset_title();
        g1.showonscreen();
    }
    

    This solution works for me as it displays at the same time the GLUT and gnuplot window and both can be updated when the user issues commands.