OpenGL ES Tutorials


Window Resizing

Tutorials > OpenGL ES > Window Resizing

View Full Source


Window Resizing You may have noticed that when you open up the keyboard at the bottom of the screen, the window is resized and the shape moves off the top of the screen. As the GLUT|ES library always makes use of the same size window and you cannot make the keyboard popup, this tutorial is aimed mostly at the UG library. The tutorial has therefore been written for those using the UG library but it is still important for those using GLUT|ES to read through it. Also make sure that you download the correct source code at the bottom of this page.

This tutorial will show you how to change the view of the window so that no matter what size the window is, you will still be able to see everything on the screen.

Contents of main.cpp :

The first step is to remove the calls to glMatrixMode, glLoadIdentity and glOrthof in the init function. These will be placed in a new reshape function.

This function must accept a handle to an OpenGL window as well as the width and the height of the newly resized window. This function will get called every time a resize is made. With the UG library, the function would need to accept a UGWindow parameter.

void reshape(int width, int height)

As before, the GL_PROJECTION matrix is selected and set with the identity matrix.


A new function that we will cover is the glViewport function. This function is used to specify the position and size of the drawing region. This allows coordinates to be converted to screen coordinates in your OpenGL window.

The first 2 parameters specify the lower-left corner of the viewport. We want it to be the lower-left hand corner of the screen and therefore (0,0) is passed as the lower-left coordinate.

The third and fourth parameters specify the width and the height of the viewport. We want the width and the height of the viewport to be the same as the newly resized window.

	glViewport(0, 0, width, height);

The orthographic view is setup as per normal.

	glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);

We set the current matrix back to the GL_MODELVIEW matrix to allow model transformations to be made. Transformations will be covered in the next tutorial. This matrix is also initialized with the identity matrix.


In the main function, we specify the reshape function that will be used by calling ugReshapeFunc. This function takes 2 parameters. The first parameter takes the handle to the window that will be resized. The second parameter specifies the resizing function. If you are using the UG library, you would rather call the ugReshapeFunc function.

	glutReshapeFunc(uwin, reshape);

Well done. You can now create programs that will resize correctly when the windows size changes, specifically when the keyboard appears.

Please let me know of any comments you may have : Contact Me

GLUT|ES Source Files : Embedded Visual C++ 4.0
UG Source Files : Embedded Visual C++ 4.0

Last Updated : 20 November 2005

< Tutorial 08 - Color And Shading Tutorial 10 - Transformations >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer