Regarding glutReshapeFunc()

I have small code drawing a square initially but when I maximize the window it changes to rectangle. I know this has do with aspect ratio and when I add glutReshapeFunc(Reshape); call it works perfectly, I mean after maximizing the window, it remains square only. ReshapFunc is called every time display is modified and before the first display as well.
I am not getting just by adding reshapefunc, how it maintains aspect ratio. Please help me understanding this. I am copying my code here:

void display()

	glColor3f(0.5, 0.5, 1.0);

	glVertex2f(-0.5, -0.5);
	glVertex2f(0.5, -0.5);
	glVertex2f(0.5, 0.5);
	glVertex2f(-0.5, 0.5);

void Reshape(int w, int h) {


void init()
	glClearColor(1.0, 0.0, 1.0, 0.0);

	glColor3f(1.0, 1.0, 1.0);

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


int main(int argc, char** argv)

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(200, 200);
	// If I comment this, it will become rectangle.



I’m in the same boat. That probably doesn’t help much :slight_smile:


When you create a window, the viewport’s width & height are set by default to the dimensions of your window.
If you provide a reshape callback, it’s up to you to change the viewport using a call to glViewport() within the callback, which you are not doing hence the ratio of the rendering surface remaining square.
If you don’t provide any reshape callback, a default one is used : this default callback calls glViewport(0,0,width,height), width & height being the new dimensions requested by the reshape event, thus in your case this is why the rendering surface becomes rectangular if you comment “glutReshapeFunc(Reshape);”.

1 Like

I am calling reshape function and i am not calling glviewport in it, which means it should be of default window size which is of 500 500 in my case. In this case it should be rectangle to maintain aspect ratio. but this is not the case here.

This is the call to glViewport() which will modify the aspect ratio.

If you never call glViewport(), you will keep forever the initial aspect ratio set by default when creating the window ( here x=0 y=0 width=500 height=500, so initial aspect ratio is 500 / 500 = 1 = square ).
This is what you are doing when you uncomment “glutReshapeFunc(Reshape);”, because you don’t modify the viewport nor in Reshape() nor somewhere else.

If you comment “glutReshapeFunc(Reshape);”, glut will instead call its own reshape callback which is calling glViewport() with the new dimensions of the window (so here aspect ratio will take the aspect ratio of the maximized window, thus becoming rectangular).
You will have the same behaviour if you uncomment “glutReshapeFunc(Reshape);” and call “glViewport(0, 0, w, h);” in Reshape().

Nonsense. The viewport merely describes how normalized device coodinates are transformed to window coordinates. The ratio of width and height of the area covered by the viewports rectangle to which NDCs are mapped is called the aspect ratio. However, the GL doesn’t have any state representing an aspect ratio which is actually set by glViewport.

Also, if you need aspect ratio correction, you need to factor a corresponding term into the projection to equalize the effects of viewport transformation. Therefore, you can adapt your viewport to new window sizes all you want and still not get a correct visual. That’s why there is a parameter to good ol’ gluPerspective names aspect.

Disclaimer: I’m not advocating the use of any legacy GL features or libraries layered on top of legacy GL, such as GLU. I recommend using GLM for mathematical problems (and subsequently glm::perspective) instead.

Yes, this first sentence was a quick answer to point what is leading to this behaviour here, as there are multiple ways to handle rendering aspect ratio, considering he doesn’t handle it nor in projection or modelview matrix, nor in vertices coordinates. So yes, while there is no OpenGL aspect ratio state, this is an implicit behaviour due to projection matrix mapping the viewport both horizontally and vertically to [-1.0 , 1.0].

Sorry if i’m not accurate enough in my answers.

if i’m not accurate enough

Inaccuracy, as in missing important details, isn’t the problem. The problem is stating stuff that is not true at all - however unintentionally. I’m guilty of the same crime, so there is no need to apologize.