Reputation: 33
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.
Upvotes: 3
Views: 613
Reputation: 463
In glut or OpenGL, glutIdleFunc(void (*func)(void))
is used to update the scene.
The idle func will call the glutDisplayFunc
every time the scene refreshes.
Reference is here http://www.opengl.org/resources/libraries/glut/spec3/node63.html
I guess renderScene()
is your glutDisplayFunc
. And you need to register an idle function using glutIdleFunc
. In the idle function, you can change the parameters for the second graph that constantly changes, and renderScene()
will be called again after changes in the idle function are done.
Upvotes: 2