Creating two windows

Dear friends…

I am trying to create two windows, but the program close right after I run it.

This is the code of the program:

 
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>

void redraw(void);
void keyboardPress(unsigned char, int, int);

int main()
{
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(100, 100); 
	glutCreateWindow("Window 1");

	glutInitWindowPosition(250, 100);
	glutInitWindowSize(100, 100); 
	glutCreateWindow("Window 2");

	glutDisplayFunc(redraw);
	glutKeyboardFunc(keyboardPress);
	
	glutMainLoop();
	return 0;
}

void redraw(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_POLYGON);
	{
		glVertex2f(-0.5, -0.5);
		glVertex2f(0.5, -0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(-0.5, 0.5);
	}
	glEnd();

	glFlush();
}

void keyboardPress(unsigned char key, int x, int y)
{
	switch(key)
	{
		case '1':
			glutSetWindow(1);
			glutPostRedisplay();
			break;

		case '2':
			glutSetWindow(2);
			glutPostRedisplay();
			break;
	}
}

Thanks in advance for your help…

Here with freeglut, I get:

freeglut ERROR: Function <glutCreateWindow> called without first calling ‘glutInit’.

Instaed of the previous code you can use the following one, if you are using Linux


#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>

void redraw(void);
void keyboardPress(unsigned char, int, int);

int main( int argc, char** argv )
{
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

	glutInitWindowPosition(100, 100);
	glutInitWindowSize(100, 100); 
	glutCreateWindow("Window 1");

	glutInitWindowPosition(250, 100);
	glutInitWindowSize(100, 100); 
	glutCreateWindow("Window 2");

	glutDisplayFunc(redraw);
	glutKeyboardFunc(keyboardPress);
	
	glutMainLoop();
	return 0;
}

void redraw(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_POLYGON);
	{
		glVertex2f(-0.5, -0.5);
		glVertex2f(0.5, -0.5);
		glVertex2f(0.5, 0.5);
		glVertex2f(-0.5, 0.5);
	}
	glEnd();

	glFlush();
}

void keyboardPress(unsigned char key, int x, int y)
{
	switch(key)
	{
		case '1':
			glutSetWindow(1);
			glutPostRedisplay();
			break;

		case '2':
			glutSetWindow(2);
			glutPostRedisplay();
			break;
	}
}

Dear friends,

My main question is how we can create more than one window in OpenGL. I tryed the previous programs, but it did not work.

Thanks in advance for your help.

You have to have a callback for both windows.
That seems to work.
Thanks for the topic, I couldnt figure this either.

glutInitDisplayMode,GLUT_DOUBLE or GLUT_RGBA or GLUT_BORDERLESS ;(freeglut)

glutInitWindowPosition, 100,100
glutInitWindowSize,100,100
glutCreateWindow,offset GMS1
glutDisplayFunc, offset display1

glutInitWindowPosition, 250,100
glutInitWindowSize,100,100
glutCreateWindow,offset GMS3
glutDisplayFunc, offset display2

display1 PROC
glClear, GL_COLOR_BUFFER_BIT
draw blah blah
glutSwapBuffers
ret
display1 ENDP

display2 PROC
glClear, GL_COLOR_BUFFER_BIT
draw blah blah
glutSwapBuffers
ret
display2 ENDP