3

I have written a c++ program in Xcode to implement Symbolic Regression & Genetic Programming. I'd like to create a window to visualize the reference data (an array of 2d points) and the best function that the program generates each generation.

To put it simply, I'd like the window to show 2 graphs, and have it be updated with a for loop. From what I understand, GLUT seems like a good framework, and I've written a function to display the reference data (std::vector is how I'm storing the "referenceDataSet" variable):

void renderScene(){
    // The min/max variables are just for scaling & centering the graph
    double minX, maxX, minY, maxY;
    minX = referenceDataSet[0].first;
    maxX = referenceDataSet[0].first;
    minY = referenceDataSet[0].second;
    maxY = referenceDataSet[0].second;
    for (int i = 0; i < referenceDataSet.size(); i++) {
        minX = min(minX, referenceDataSet[i].first);
        maxX = max(maxX, referenceDataSet[i].first);
        minY = min(minY, referenceDataSet[i].second);
        maxY = max(maxY, referenceDataSet[i].second);
    }
    glLoadIdentity ();
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin( GL_LINE_STRIP );
    glColor4f( 1.0, 0.0, 0.0, 1.0);
    for (int i = 0; i < referenceDataSet.size(); i++) {
        glVertex2f((referenceDataSet[i].first-minX)/(maxX-minX)-0.5, (referenceDataSet[i].second-minY)/(maxY-minY)-0.5);
    }
    glEnd();
    glFlush();
}

void renderInit(int argc, char **argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(600, 600);
    glutCreateWindow("");
    glutDisplayFunc(renderScene);
    glutCheckLoop();
}

The problem with this is that I'm not sure how I should go about updating the window, or drawing a second graph that constantly changes.

Also, this is my first question on Stack Overflow, so I apologize if I'm not doing something correctly here, or if any of this is difficult to understand. I searched as best I could for the answer, but couldn't really find anything relevant.

4

1 回答 1

2

在 glut 或 OpenGL 中,glutIdleFunc(void (*func)(void))用于更新场景。glutDisplayFunc每次场景刷新时,idle 函数都会调用。

参考在这里http://www.opengl.org/resources/libraries/glut/spec3/node63.html

我猜renderScene()是你的glutDisplayFunc。并且您需要使用glutIdleFunc. 在空闲函数中,您可以更改不断变化的第二张图的参数,并renderScene()在空闲函数更改完成后再次调用。

于 2012-12-09T15:58:44.123 回答