Problem with multiple viewports with animation

I want to 2 Viewports on the screen. In first one, i have some animation and i am calling gluttimerFunc for this. In other viewport i have some text displayed which updates continuously with by calling gluttimerfunc. Now when I resize the window, i need to get updated height and width of a viewport from my reshape function. I have the separate modules of the program working fine. but i am not able to integrate it to see the animation with both the viewports.


static unsigned int timer = 0;
void display()
{


	glClear(GL_COLOR_BUFFER_BIT);

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glColor3f(0.5, 0.5, 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();

	
	glBegin(GL_LINES);
	glColor3f(1.0, 0.0, 0.0);
	glVertex2f(-0.75, -0.75);
	glColor3f(0.0, 0.0, 1.0);
	glVertex2f(0.75, -0.75);
	glEnd();


}
void DrawHexagon(int o)
{
	
	static float angle = 0;
	float distance_x, distance_y;
	glClear(GL_COLOR_BUFFER_BIT);

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glColor3f(0.0f, 0.3f, 0.2f); 

	//glViewport(0, 0, 240, 240);
	glBegin(GL_POLYGON);
	glVertex2f(-0.9, -0.9);
	glVertex2f(-0.9, -0.7);
	glVertex2f(-0.7, -0.7);
	glVertex2f(-0.7, -0.9);
	glEnd();

	glBegin(GL_POLYGON);
	glVertex2f(-0.3, -0.3);
	glVertex2f(0.3, -0.3);
	glVertex2f(0.3, 0.3);
	glVertex2f(-0.3, 0.3);
	glEnd();

	static int i = 0;
	distance_x = 0.5 * cos(i*3.14 / 180);
	distance_y = 0.5 * sin(i*3.14 / 180);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(distance_x, distance_y, 0.0);	
	
	
	glBegin(GL_POLYGON);
	glVertex2f(-0.2, -0.2);
	glVertex2f(0.2, -0.2);
	glVertex2f(0.2, 0.2);
	glVertex2f(-0.2, 0.2);
	glEnd();
	
	distance_x = 0.3 * cos(i*3.14 / 180);
	distance_y = 0.3 * sin(i*3.14 / 180);
	i++;

	if (i > 360)
		i = 0;

	glTranslatef(distance_x, distance_y, 0.0);
	
	glBegin(GL_POLYGON);
	glVertex2f(-0.1, -0.1);
	glVertex2f(0.1, -0.1);
	glVertex2f(0.1, 0.1);
	glVertex2f(-0.1, 0.1);
	glEnd();

	
	angle = angle + 0.01f;
	glPopMatrix();

	glutSwapBuffers();
	glutTimerFunc(10, DrawHexagon, 0);
}
void DisplayTimer(int time)
{
	glViewport(480, 480, 240, 240);
	timer++;
	glRasterPos2f(0.75f, 0.75f);
	int q = 12345;
	char p[30];
	_itoa_s(timer, p, 10);
	for (int i = 0; i < 5; i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, p[i]);
	glutTimerFunc(100, DisplayTimer, 0);
	glutSwapBuffers();
	glutPostRedisplay();
}

void Resize(int w, int h)
{
	GLdouble aspect_ratio = (GLdouble) w / (GLdouble) h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluOrtho2D(-1.0 * aspect_ratio, 1.0 * aspect_ratio, -1.0, 1.0);

	glMatrixMode(GL_MODELVIEW);
	glViewport(0, 0, w, h);
}
void init()
{

	glClearColor(1.0, 0.0, 1.0, 0.0);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluOrtho2D(-1.0, 1.0, -1.0, 1.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int main(int argc, char** argv)
{

	/* create a window, position it, and name it */
	glutInit(&argc, argv);

	//Changed single to double buffer rendering

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE |GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(200, 200);
	glutCreateWindow("basics");

	/* create a callback routine for (re-)display */
	glutDisplayFunc(display);

	glutTimerFunc(1000, DrawHexagon, 0);

	// This gets called whenever window gets resized
	glutReshapeFunc(Resize);
	
	

	/* init some states */
	init();

	/* entering the event loop */
	glutMainLoop();

	
}


[ol]
[li] Don’t execute drawing commands outside of the display function. If you want animation, have a timer call glutPostRedisplay().
[/li][li] If both viewports are part of the same window, you have to redraw everything. glutSwapBuffers() applies to the entire window, not a region, and it leaves the contents of the back buffer undefined (so you have to call glClear() before drawing anything).
[/li][li] You can use glutCreateSubWindow() to create a separate subwindow with its own buffers, context etc. Note that you have to register display/reshape functions for the subwindow separately. Also, you need to use glutSetWindow() to ensure that functions such as glutPostRedisplay() affect the correct window (display/reshape callbacks already have the correct window active when they are called).
[/li][li] The OpenGL viewport specifies a transformation between clip coordinates and window coordinates. It doesn’t establish a clipping rectangle. While geometry will be clipped to the frustum, rasterisation can extend beyond it (e.g. wide lines and points, and bitmaps can extend outside the viewport). Use glScissor() to set a clip rectangle.
[/li][/ol]

could you please tell me the correction in my code. ?

[QUOTE=GClements;1254607][ol]
[li] Don’t execute drawing commands outside of the display function. If you want animation, have a timer call glutPostRedisplay().
[/li][li] If both viewports are part of the same window, you have to redraw everything. glutSwapBuffers() applies to the entire window, not a region, and it leaves the contents of the back buffer undefined (so you have to call glClear() before drawing anything).
[/li][li] You can use glutCreateSubWindow() to create a separate subwindow with its own buffers, context etc. Note that you have to register display/reshape functions for the subwindow separately. Also, you need to use glutSetWindow() to ensure that functions such as glutPostRedisplay() affect the correct window (display/reshape callbacks already have the correct window active when they are called).
[/li][li] The OpenGL viewport specifies a transformation between clip coordinates and window coordinates. It doesn’t establish a clipping rectangle. While geometry will be clipped to the frustum, rasterisation can extend beyond it (e.g. wide lines and points, and bitmaps can extend outside the viewport). Use glScissor() to set a clip rectangle.
[/li][/ol][/QUOTE]This is a very good explanation.
Took me years to learn the stuff you’ve listed.

[QUOTE=GClements;1254607][ol]
[li] Don’t execute drawing commands outside of the display function. If you want animation, have a timer call glutPostRedisplay().[/li][li] If both viewports are part of the same window, you have to redraw everything. glutSwapBuffers() applies to the entire window, not a region, and it leaves the contents of the back buffer undefined (so you have to call glClear() before drawing anything).[/li][li] You can use glutCreateSubWindow() to create a separate subwindow with its own buffers, context etc. Note that you have to register display/reshape functions for the subwindow separately. Also, you need to use glutSetWindow() to ensure that functions such as glutPostRedisplay() affect the correct window (display/reshape callbacks already have the correct window active when they are called).[/li][li] The OpenGL viewport specifies a transformation between clip coordinates and window coordinates. It doesn’t establish a clipping rectangle. While geometry will be clipped to the frustum, rasterisation can extend beyond it (e.g. wide lines and points, and bitmaps can extend outside the viewport). Use glScissor() to set a clip rectangle.[/li][/ol][/QUOTE]

Thanks a lot… Animation part of it done now.
I have rendering code in display and timer calling postRedisplay. Now I am stuck up with multiple viewports part.
I dont want to create subwindow as you have mentioned.

In Reshapefunction, i am saving width and height retrieved in global variables. Now i am using these variables to set viewport in my display call like this:


void display()
{

	//glViewport(0, 0, 500, 500);
	/* clear the screen*/
	static float angle = 0;
	float distance_x, distance_y;
	glClear(GL_COLOR_BUFFER_BIT);

	glViewport(0, 0, 3 * width / 4, 3 * height / 4);


	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glColor3f(0.0f, 0.3f, 0.2f);

	//glViewport(0, 0, 240, 240);
	glBegin(GL_POLYGON);
	glVertex2f(-0.9, -0.9);
	glVertex2f(-0.9, -0.7);
	glVertex2f(-0.7, -0.7);
	glVertex2f(-0.7, -0.9);
	glEnd();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();
	glTranslatef(distance_x, distance_y, 0.0);


	render();

	glTranslatef(distance_x, distance_y, 0.0);
	render();
	glPopMatrix();


    glViewport( 3 * width / 4, 3 * height / 4,width,height);
	
	glRasterPos2f(0.0f, 0.0f);
	
	
	int q = 12345;
	char p[30];
	_itoa_s(timer, p, 10);
	for (int i = 0; i < 5; i++)
		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, p[i]);
	timer++;
	

	// call swapbuffer as we are using double buffer rendering
	glutSwapBuffers();
	glutTimerFunc(10, animation, 0);

}  

void Resize(int w, int h)
{
	GLdouble aspect_ratio = (GLdouble) w / (GLdouble) h;

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluOrtho2D(-1.0 * aspect_ratio, 1.0 * aspect_ratio, -1.0, 1.0);

	glMatrixMode(GL_MODELVIEW);
	width = w;
	height = h;
}
glutReshapeFunc(Resize);

after 2nd viewport call if i draw anything, nothing is getting displayed.

This places most of the viewport outside of the window. It should probably be:


	glViewport( 3 * width / 4, 3 * height / 4,width - 3 * width / 4,height - 3 * height / 4);

The last two parameters to glViewport() are the width and height of the viewport, not the right X and top Y.

[QUOTE=GClements;1254626]This places most of the viewport outside of the window. It should probably be:


	glViewport( 3 * width / 4, 3 * height / 4,width - 3 * width / 4,height - 3 * height / 4);

The last two parameters to glViewport() are the width and height of the viewport, not the right X and top Y.[/QUOTE]

great, thank you so much…!!