Window Resizing

Introduction

Window ResizingYou 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|ESlibrary 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 glMatrixModeglLoadIdentity 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.

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

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.

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

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 theugReshapeFunc 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值