GLUT和gnuplot窗口不会同时显示

时间:2017-12-21 19:27:55

标签: c++ opengl glut gnuplot-iostream

我有以下功能,其目的是显示一个显示3D对象的GLUT窗口和一个显示绘图的Gnuplot窗口。

为此,我使用Gnuplot-Iostream Interface。绘图代码位于函数内部,因为当用户在键盘上键入时,它将被更新。

以下代码仅在关闭GLUT窗口后显示Gnuplot窗口:

#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";
}

什么有效是在displayGraph函数中声明Gnuplot实例。不幸的是,这对我的情况不适用,因为每次调用displayGraph函数时都会创建一个新的Gnuplot窗口,而我只想更新Gnuplot窗口。

我还尝试在创建Gnuplot窗口时设置条件无济于事:

#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";
}

1 个答案:

答案 0 :(得分:0)

我找到了一个使用与Gnuplot不同的C ++接口的解决方案,即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();
}

此解决方案适用于我,因为它同时显示GLUT和gnuplot窗口,并且在用户发出命令时都可以更新。