problem clearing screen for next render

I have a window 32 + opengl application. in Window WS_PAINT message, I am calling GLInit() and render(), no problem drawing stuff on the screen, but also in my window WM_KEY message, if KEY is LEFT ARROW, I am again calling render(), this time I was thinking glClear( GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT ) would clear the screen and redraw. but it is NOT clearing the screen. Does anyone know why it is doing that? how can I clear and redraw.

void GLInit()
glViewport( 0,0, width , height );
//Set the matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();//reset projection matrix
//gluPerspective( 54.0, (GLfloat)width/(GLfloat)height, 1.0, 1000.0);
glOrtho( -10.0, 10.0, -10.0, 10.0, 1.0, 1000.0 );

void prerender()

  glEnable( GL_TEXTURE_2D );
glBlendFunc( GL_SRC_ALPHA, GL_ONE );
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClearDepth( 1.0f );//clear depth buffer
glDepthFunc( GL_LESS );
glEnable( GL_DEPTH_TEST);//enable depth testing
glShadeModel( GL_SMOOTH );
glDepthFunc( GL_LEQUAL );//Passes if the incoming z value is less than or equal to the stored z value.
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );//really nice perspective calculation


void render( void )

glLoadIdentity();//Identity Matrix

gluLookAt(  0.0f, 0.0f, 10.0f, //eye position
			0.0f, 0.0f, 0.0f, //position where object is at origin,
			0.0f, 1.0f, 0.0f//up vector
	setColor( 0.0, 0.0, 0.0 );
	drawVertices( 10, 10, 1 );
	SwapBuffers( hdc );

Can you verify that SwapBuffers is doing its job?

glClear() will clear the write buffer (back buffer in most cases), allowing you to render into a clean slate.

If your image isn’t being updated, they may not be being swapped over properly.

May be so. I am calling render() in event WM_PAINT and also in WM_KEY, therefore calling SwapBuffers() in both events. may be that is the problem, i dont know. I even tried to call my glInit() in WM_KEY event, still not updating the drawing ( or not clearing the screen with glClear() ). I dont know what is going on.

actually I was checking the return value from SwapBuffers(), first time when render() is called, it returns TRUE ( success), next time with WM_KEYDOWN event, SwapBuffes() returned FALSE. So I tried GetLastError() right after SwapBuffers(), error code is 6, which is ERROR_INVALID_HANDLE.
SO i dont know what is going on, i am checking hdc HDC it is still a valid pointer.

I think i solved the problem. In WM_KEYBOARD event, instead of calling render() again, I called SendMessage( HDC, WM_PAINT, NULL, NULL ). which actually forced to call render() again, and that did it… :slight_smile: :slight_smile: