GLUT 和 gnuplot window 没有同时出现

GLUT and gnuplot window not showing up at the same time

我有以下函数,它们 objective 用于显示 GLUT window 显示 3D 对象和 Gnuplot window 用于显示绘图。

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

关闭 GLUT window 后,以下代码将仅显示 Gnuplot 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";
}

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

我也试过为 Gnuplot 的创建设置条件 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(){

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

我找到了一个解决方案,使用不同的 C++ 接口连接到 Gnuplot,即 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 window 并且两者都可以在用户发出命令时更新。