How realize two different windows (no contemporary) in OpenGL - c++

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

Related

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();
}

GLUT: Rendering Mulitple Windows in the loop

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);

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.

Win7 GLUT window doesn't receive events

I have created a simple Visual Studio Express 2010 C++ project using GLUT and OpenGL,
it compiles and runs ok, except that the window it creates receives no events..
the close/minimize buttons don't do anything (not even mouseover) no context menu on the task bar on right click, and the window doesn't come to the foreground when clicked if partially covered.
The project is set up as a console application, I can close the program by closing the console.
I have this in main:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
glutIdleFunc(main_loop_function);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
if (GLEW_VERSION_1_3)
{
fprintf(stdout, "OpenGL 1.3 is supported \n");
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
GL_Setup(window_width, window_height);
glutMainLoop();
}
You miss a display callback. Could you try:
void display();
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
/// glutIdleFunc(main_loop_function);
glutDisplayFunc(display);
// ...
glutMainLoop();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
Also, your idle function seems bad, if you effectively loop inside that function. Glut is callback oriented. You don't need to create a loop, but rather rely on idle, mouse, keyboard, display and resize callbacks. If you don't do so, you are going to miss window manager events.
EDIT:
If you want to create an animation, you could use:
glutIdleFunc(idleFunc);
void idleFunc(){
glutPostRedisplay();
}