【问题标题】:GLUT and gnuplot window not showing up at the same timeGLUT 和 gnuplot 窗口没有同时显示
【发布时间】:2017-12-22 07:59:31
【问题描述】:

我有以下函数,其目的是显示一个显示 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";
}

【问题讨论】:

  • 为什么你认为gnuplot可以使用OpenGL?
  • 我可以看到我的代码有点混乱,我已经对其进行了更新以将 GLUT 和 Gnuplot 的使用分开。我想要的是让 Gnuplot 和 GLUT 窗口同时出现。他们并没有真正相互交流。

标签: c++ opengl glut gnuplot-iostream


【解决方案1】:

我找到了一个使用与 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 窗口,并且两者都可以在用户发出命令时更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多