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.
Related
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;
}
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 ...
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();
}
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);
Here's my initialization code:
const int WIN_HEIGHT = 640;
const int WIN_WIDTH = 640;
void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
/* lines in question */
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glutInitWindowSize(WIN_WIDTH, WIN_HEIGHT);
glutCreateWindow("OpenGL");
glutDisplayFunc(Draw);
glutKeyboardFunc(HandleInput);
Initialize();
glutMainLoop();
}
So, most of that code is pretty boilerplate for a basic 3D program. The problem is, if I put all the glEnable() lines before glutCreateWindow() they are reset. It's an easy enough fix to move them after creating the window (I moved them to my own Initialize() function), but why would glutCreateWindow() disable these?
I would say it's because a context has not yet been created before your call to glutCreateWindow(). You are then able to set them after one is created, as can be interpreted from the following text:
In order for any OpenGL commands to work, a context must be current; all OpenGL commands affect the state of whichever context is current.
Since there was no context (or an old, invalid one), your calls to glInit() don't affect the current window's context.
Because glEnable is being done to the current window. Once you call glutCreateWindow you've made a new window and replaced your current one with it. This new window has a new opengl context. After creating a new window you can go ahead and enable and modify it's context as you want.
Reference: http://www.opengl.org/documentation/specs/glut/spec3/node16.html