glfwSwapBuffers() and window resize events

geago wrote on Tuesday, May 01, 2012:

Hello…I was hoping someone might be able to comment on my code below so I can
better understand why it works the way it works.

I’ve simplified and changed the code as much as possible to replicate a
behavior I have noticed in my other programs.

In the code below I have basically 4 sets of 3 lines of code that are:
glClear; glClearColor and glfwSwapBuffers.

I’ve commented them out below with // 1, // 2, // 3, and // 4

If I have all the //1 code uncommented and run the program, and drag to a new
window size I get the new window size background completely filled with old
color black ( not the new color ).

If I uncomment all the // 2 code then if I do the same I will get the new
sized window filled with the new color.

So at this point my question is why would it take 2 iterations of the same
code to get this result.

But if that is the case where in my 2 iterations of glClear, glClearColor and
glfwSwapBuffers is that information ( new window size ) being passed on to
opengl that I am getting the results as expected ie. the full window sized
filled with the new color?

What makes it even more confusing is the commented code I’m talking about
doesn’t behave the same within my callback function window_size_change.

If recomment the // 1 and // 2 code and then uncomment the // 3 code what
happens is if I resize the window the original window size area remains black
( original color ) while the new window resized area is transparent. Each
resize afterwards fills the old windows with the new color with every new
window resized area remaining transparent.

If I uncomment // 4 code I get the same results with the exception that first
window resize incorporates the new color and as before new window areas are

So I guess my question is why do I need to call code that is in // 1 and // 2
to get my result ( I would have thought just glClear, glclearColor and
glfwSwapBuffers would have sufficed)?

And secondly, why does repeating the same code within my function
"Window_size_change" yield different results?

Any comments on the matter is much appreciated, thank you.

#include <windows.h>
#include <stdbool.h>
#include <stdio.h>
#include <GL/glfw.h>
//****** Functions Declared  *********************
bool setup_GL(int, int);
void GLFWCALL Window_size_change( int, int);
//******** Variables to use ***********************
int running, new_window_width, new_window_height;
bool testswap;
//********** Program starts ************************
int main( void )
new_window_width = 200;
new_window_height = 200;
glfwOpenWindow( new_window_width, new_window_height, 0,0,0,0,0,0, GLFW_WINDOW ); 
setup_GL(new_window_width, new_window_height);
running = GL_TRUE;
testswap = FALSE;
while( running )
	glfwSetWindowSizeCallback( Window_size_change );
	// 1	glClear(GL_COLOR_BUFFER_BIT);
	// 1	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);			
	// 1	glfwSwapBuffers();
	// 2	glClear(GL_COLOR_BUFFER_BIT);
	// 2	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
	// 2 	glfwSwapBuffers();
		testswap = FALSE;
	if( glfwGetWindowParam( GLFW_OPENED) == FALSE)                   
		running = FALSE;
//******************* Functions Defined ********************************
bool setup_GL(int width, int height)
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glOrtho( -(width/2), width/2, -(height/2), height/2, -1, 1 );
if( glGetError() != GL_NO_ERROR )
	return false;
return true;
void GLFWCALL Window_size_change(int width, int height )
if( new_window_width != width  ||  new_window_height != height )
		new_window_width = width;
		new_window_height = height;
	// 3	glClear(GL_COLOR_BUFFER_BIT);
	// 3	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);			
	// 3	glfwSwapBuffers();
	// 4	glClear(GL_COLOR_BUFFER_BIT);
	// 4	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  	// 4	glfwSwapBuffers();
		testswap = TRUE;