GLUT: Rendering Mulitple Windows in the loop - opengl

I am trying to rendering multiple GLUT windows in a loop in my main function.
Before entering the loop, I use the following code to define the window properties.
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
int windows1 = glutCreateWindow("GlutWindow 1");
int windows2 = glutCreateWindow("GlutWindow 2");
But I found later, in my loop, when I call
glutDisplayFunc (display1);
glutReshapeFunc (reshape1);
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
It will only render the shape in display2 on the windoew2 function.
I also try to render the two images in the loop by putting the all the code below inside the loop
int windows1 = glutCreateWindow("GlutWindow 1");
glutDisplayFunc (display1);
glutReshapeFunc (reshape1);
int windows2 = glutCreateWindow("GlutWindow 2");
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
It does render the 2 windows content but it will keep create windows1 and 2 again and again.
So how to let GLUT to render both windows in this case? Is there any function to let glutDisplayFunc (display1) "smartly" knows that it should be rendered in windows1?

glutDisplayFunc and friends set callbacks for the current window. You can change the current window using glutSetWindow():
glutSetWindow sets the current window; glutGetWindow returns the identifier of the current window. If no windows exist or the previously current window was destroyed, glutGetWindow returns zero. glutSetWindow does not change the layer in use for the window; this is done using glutUseLayer.
So:
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
int windows1 = glutCreateWindow("GlutWindow 1");
int windows2 = glutCreateWindow("GlutWindow 2");
...
glutSetWindow(windows1);
glutDisplayFunc (display1);
glutReshapeFunc (reshape1);
glutSetWindow(windows2);
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);

Related

How realize two different windows (no contemporary) in OpenGL

I want to create in OpenGL two differents windows but they should appears not together.
I mean: when I execute my code I visualize both of windows contemporarily.
My aim instead is to visualize at the beginning the first window, then the first window should be closed and the second will appear. My pseudocode is the following (this realeses only the two windows together ntemporarily):
int main(int argc, char** argv)
{
glutInit(&argc, argv);
initRendering();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
//window1=
glutCreateWindow("First window");
glutDisplayFunc(drawScene1);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
cout << "Before inserting 'choice' I want to visualize the first window\n
After the insert of 'choice' I want to visualize the second window
and close the first one !";
cin >> choice;
//create the second window
//window2 =
glutCreateWindow("Second Window");
//define a window position for second window
glutPositionWindow(540, 40);
// register callbacks for second window, which is now current
glutReshapeFunc(handleResize);
glutDisplayFunc(drawScene2);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
return 0;
}

GLEW/GLUT: After calling init and creating a window, deinitialize and re-init?

Essentially, I am attempting to discover the controls of the GLEW-GLUT setup. The first objective here is the "hello-world" case where a window is initialized:
int argc = 0;
char ** argv = (char **) calloc(1,sizeof(char **));
argv[0] = (char *) calloc(1,sizeof(char *));
argv[0][0] = '\0';
glutInit(&argc, argv);
glutInitWindowSize( 500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("foo");
std::this_thread::sleep_for(std::chrono::seconds(1));
glutDestroyWindow(glutGetWindow());
free(argv[0]);
free(argv);
From a "controls" perspective, it seems as if I cannot achieve a runtime state that existed prior to calling glutInit in the sense that I cannot "re-initialize" glut without getting a segmentation fault.
So, once glut is initialized, it appears as if glut is always initialized. This seems strange.
How does one tear everything down after GLUT is initialized, so that it can be re-initialized? Every method and setting I have tries leaves a window that will not close until the code exits.
There must be some sort of "GLEW/GLUT Teardown and Exit Everything" function...?
Or is every GLEW/GLUT window a one way ticket?
Firstly, freeglut cannot cause a segmentation fault on re-initialization. It will simply notify you of this attempt and terminate the program, but this is not a segmentation fault.
Secondly, yes, it is possible to deinitialize and reinitialize freeglut. The fgDeinitialize() function should do this.
Something like this:
void fgDeinitialize( void ); // put it above main function
int main (int argc, char** argv, char** env)
{
int Window, i = 0;
glutInit(&i, NULL); // i don't want send arguments
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
Window = glutCreateWindow("frog"); // init window
glutDestroyWindow(Window); // destroy window
printf("reinitialize freeglut\n");
fgDeinitialize(); // destroy glut
glutInit(&i, NULL); // initialize it again
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
Window = glutCreateWindow("Second Window!");
if (!Window) exit(-1); // if no window returned - error
... some other code ...

C++ update OpenGL/Glut window

I've tried some OpenGL C++ training.
But I have a logic problem, how can I update my OpenGL Windows window.
It should draw text one, then delay 1-2sec, then draw text 2, but now it draws same time. Can anyone help or give a hint.
void text () {
wait(1);
Sleep(1000);
std::string text_one;
text_one = "Text 1";
glColor3f(1,01, 0);
drawText(text_one.data(), text_one.size(), 050, 150);
glutPostRedisplay();
wait (1)
std::string text_two;
text_two = "Text 2";
glColor3f(1,0, 0);
drawText(text_two.data(), text_two.size(), 250, 150);
}
and here the main
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,640);
glutCreateWindow("Test 001");
// register callbacks
glutDisplayFunc(renderScene);
glutIdleFunc(text);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
You should render in renderScene callback. It will be called automatically in you screen refresh rate. If you want some delay you need to implement it inside this callback (functions called from this callback).
So basically you need to re-render everything every 1/60 second.
If you want to implement easy delay you can do something like this:
void renderScene() {
time += deltaTime;
RenderText1();
if (time > delayTime)
RenderText2();
glutSwapBuffers();
}

OpenGL Optimization or...?

I'm making a OpenGL game, and I have problem with optimization. When I start it, it does not respond. If in Update() I just put a for loop and _time += 0.1f, I get a blank screen.
void Update(){
for(; ;){
_time += 0.1f;
Render();
}
}
void Render() {
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
_colorProgram.use();
GLuint timeLocation = _colorProgram.getUniformLocation("time");
glUniform1f(timeLocation, _time);
_sprite.Render();
_colorProgram.unuse();
glutSwapBuffers();
}
int main(int argc, char** argv) {
std::printf("OpenGL version is %s",glGetString(GL_VERSION));
// Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(520, 200);
glutInitWindowSize(800, 600);
glutInitDisplayMode(GLUT_DOUBLE);
glutCreateWindow("OpenGL [ Shader #1 error pt 3 ]");
// Setup GLEW
if( GLEW_OK != glewInit()){
return 1;
} while( GL_NO_ERROR != glGetError() );
// After creating window
Init();
glutDisplayFunc(Render);
Update();
glutMainLoop();
}
The infinite loop in Update() never lets GLUT pump the event queue.
Use glutTimerFunc() or glutIdleFunc() to call Update() instead. That way execution flow periodically returns to GLUT and GLUT can do what it needs to keep the OS happy.
The proper way to run an animation with GLUT is to use a timer function. This way, GLUT can get back to its main loop, and call your display function. You're not supposed to call the display function directly from your own code.
For example, register a timer function during initialization (the first argument is a time in milliseconds):
glutTimerFunc(10, timerFunc, 0);
Then in the timer function:
void timerFunc(int value) {
_time += 0.1f;
glutPostRedisplay();
glutTimerFunc(10, timerFunc, 0);
}
There are two critical pieces in the code fragment above:
You do not call your Render() function directly. Instead, you call glutPostRedisplay() to tell GLUT that a redisplay is needed. It will then call your Render() function because you registered it as the display function with glutDisplayFunc().
You have to register the timer again. glutTimerFunc() fires the timer only once, not periodically. So you have to re-register it every time it fired.
There is one other problem in your code. You have these calls in your main():
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
...
glutInitDisplayMode(GLUT_DOUBLE);
The flags passed in the second call will override the ones from the first call. While GL_RGBA is the default anyway, you will not get a depth buffer because GL_DEPTH is missing in the second call. You can simply remove the second call, since the first one is most likely what you want.

OpenGL Issues with GLUT_DOUBLE and two windows

I'm using Sumanta Guha's code sample and I'm trying to create two windows. Using the following code:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
// First top-level window definition.
glutInitWindowSize(250, 500);
glutInitWindowPosition(100, 100);
// Create the first window and return id.
id1 = glutCreateWindow("windows.cpp - window 1");
// Initialization, display, and other routines of the first window.
setup1();
glutDisplayFunc(drawScene1);
glutReshapeFunc(resize1);
glutKeyboardFunc(keyInput); // Routine is shared by both windows.
// Second top-level window definition.
glutInitWindowSize(250, 500);
glutInitWindowPosition(400, 100);
// Create the second window and return id.
id2 = glutCreateWindow("windows.cpp - window 2");
// Initialization, display, and other routines of the second window.
setup2();
glutDisplayFunc(drawScene2);
glutReshapeFunc(resize2);
glutKeyboardFunc(keyInput); // Routine is shared by both windows.
glutMainLoop();
return 0;
}
I'm using Windows 7, and normally it should display two windows. But as you can see, only one Window displays properly and the other one doesn't seem to work quite as well. Are there additional steps that I have to take other than GLUT_DOUBLE and buffer swap?
Are there additional steps that I have to take other than GLUT_DOUBLE and buffer swap?
Since you are creating multiple windows, you have to call glutSetWindow() in your callbacks.
freeglut has an extension (which doesn't work) to create a shared opengl context, but the original glut doesn't support it.