GLUT Tutorial 3 : Preparing the window for a reshape

Download the following VC project ( glut0.zip) and run it. You'll see two windows: a console window and the OpenGL window. Now resize the window so that the height no longer matches the width. The triangle gets distorted. This occurs because you're not setting the perspective correctly. By default the perspective assumes that the ratio width/height is 1 and draws accordingly. So when the ratio is changed the perspective gets distorted. Therefore, every time the ratio changes the perspective needs to be recomputed.

 

GLUT provides a way to define which function should be called when the window is resized, i.e. to register a callback for recomputing the perspective. Furthermore, this function will also be called when the window is initially created so that even if you're initial window is not square things will look OK.

 

GLUT achieves this using the function glutReshapeFunc.


void glutReshapeFunc(void (*func)(int width, int height));

Parameters:
func - The name of the function that will be responsible for setting the correct perspective when the window changes size.


So the first thing that we must do is to go back to the main function we defined in the previous section and add a call to glutReshapeFunc. Lets call our own function to take care of window resizes changeSize. The code for the main function with the call to glutReshapeFunc added in is:


    
void main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("3D Tech- GLUT Tutorial");
	glutDisplayFunc(renderScene);
	
	// Here is our new entry in the main function 
	glutReshapeFunc(changeSize);
	
	glutMainLoop();
}



The next thing we need to do is to define the function that we'll take care of the perspective. As seen by the syntax of glutReshapeFunc, the changeSize function has two arguments, these are the new width and height, respectively, of the client area of the window, i.e. without the window decorations.


    
void changeSize(int w, int h) {

	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

	float ratio = 1.0* w / h;

	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
	glViewport(0, 0, w, h);

	// Set the correct perspective.
	gluPerspective(45,ratio,1,1000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0,0.0,5.0, 
		      0.0,0.0,-1.0,
			  0.0f,1.0f,0.0f);


}



A few functions we're introduced in this piece of code so let's go into a bit of detail in here before we all get lost. The first step was to compute the ratio between the width and the height. Note that in order for this to be done correctly we must take care of the case when the height of a window is actually zero to prevent a division by zero.

 

We then set the current matrix to be the projection matrix. This is the matrix that defines the viewing volume. We then load the identity matrix to initialize it. Afterwards we set the viewport to be the whole window with the function glViewport. You can try with different values to see what you come up with, the first two parameters are the top right corner, and the last two are the bottom left. Note that these coordinates are relative to the client area of the window, not the screen. If you do try with different values then don't forget that the ratio computed above should also use the new width and height values.

 

The gluPerspective function is part of another library for OpenGL, the OpenGL Utility Library, or GLU. GLU is a standard component of the implementation of OpenGL. The gluPerspective function establishes the perspective parameters. The first one defines the field of view angle in the yz plane, the ratio defines the relation between the width and height of the viewport. The last two parameters define the near and far clipping planes. Anything closer than the near value, or further away than the far value will be clipped away from the scene. Beware with these settings or you may end up not seeing anything at all.

 

Finally, setting the camera. First we set the GL_MODELVIEW as our current matrix. the modelview matrix is where we'll define both the camera settings and the modeling transformations. Before setting the camera it is always healthy to load the identity matrix. this avoids previous transformations to affect the camera settings. The gluLookAt function provides an easy and intuitive way to set the camera position and orientation. Basically it has three groups of parameters, each one is composed of 3 floating point values. The first three values indicate the camera position. The second set of values defines the point we're looking at. Actually it can be any point in our line of sight.The last group indicates the up vector, this is usually set to (0.0, 1.0, 0.0), meaning that the camera's is not tilted. If you want to tilt the camera just play with these values. For example, to see everything upside down try (0.0, -1.0, 0.0).

 

OK, here's the new VC project ( glut1.zip), try playing with the window size and check if the proportions of our triangle remain constant. Furthermore, do play with the parameters in the new functions now introduced to get a grip of what is really happening.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值