Advertisement Jump to content
Sign in to follow this  

OpenGL Problem with glClear

This topic is 2837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

I'm relatively new to OpenGL rendering so I've decided to start an OpenGL renderer.

This is my Initialization code:

// Platform Specific Initialization (Windows)

// Store the HWND
m_handleWindowHandle = programWindow->GetWindowHandle();

// Do a NULL check if we are in Debug
ASSERT_NULL( m_handleWindowHandle );

// Get the Device Context
m_handleDeviceContext = GetDC( m_handleWindowHandle );

// Null check the DC
ASSERT_NULL( m_handleDeviceContext );

// Set the pixel format
m_tPixelFormat.nSize = sizeof( PIXELFORMATDESCRIPTOR ); // Size of the PFD
m_tPixelFormat.nVersion = 1; // Set the version number
m_tPixelFormat.dwFlags = ( PFD_DRAW_TO_WINDOW | // Set the formats
m_tPixelFormat.iPixelType = PFD_TYPE_RGBA; // Set the color format
m_tPixelFormat.cColorBits = 32; // Set the color depth
m_tPixelFormat.cDepthBits = 16; // Set the depth buffer depth
m_tPixelFormat.cStencilBits = 0; // Set the stencil buffer depth
m_tPixelFormat.iLayerType = PFD_MAIN_PLANE; // Set the layer for the app

// See if there is a pixel format that will match our requests
int nPixelFormat = 0;
ASSERT_FALSE( nPixelFormat = ChoosePixelFormat( m_handleDeviceContext,
&m_tPixelFormat ) );

ASSERT_FALSE( SetPixelFormat( m_handleDeviceContext,
&m_tPixelFormat ) );

// So far so good... Now we try to create our RenderContext (HRC)
HGLRC tempContext = wglCreateContext( m_handleDeviceContext );
ASSERT_NULL( tempContext );

// See if the tempContext will work
ASSERT_FALSE( wglMakeCurrent( m_handleDeviceContext, tempContext ) );

GLenum error = glewInit();
if( error != GLEW_OK )
fprintf(stderr, "Error: %s\n", glewGetErrorString(error));

if( wglewIsSupported( "WGL_ARB_create_context" ) == 1 )
// Define our attributes for using OpenGL 3.0 and forward
int attribs[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,//we want a 3.0 context
//and it shall be forward compatible so that we can only use up to date functionality
0}; //zero indicates the end of the array

m_handleGLRenderContext = wglCreateContextAttribsARB( m_handleDeviceContext, 0, attribs );
wglMakeCurrent( NULL, NULL );
wglDeleteContext( tempContext );
wglMakeCurrent( m_handleDeviceContext, m_handleGLRenderContext );
m_handleGLRenderContext = tempContext;

Everything initializes fine and my HDC and HGLRC are fine. My problem is that when I call glClear( COLOR_BUFFER_BIT ) the program locks up and it takes ten to fifteen seconds to close the program when I hit the red x. If I comment out glClear it will run fine. Any ideas?

Share this post

Link to post
Share on other sites
Other information:

I'm only calling glClear and the SwapBuffers every frame. It doesn't matter which bits I clear.

My resolution is 1280 by 720.

I set my clear color to be a non-black color.

Share this post

Link to post
Share on other sites
You've obviously got a bad context. Could you add some error checking to your 3.0 context creation and see what that gives you back? Also maybe ZeroMemory your m_tPixelFormatbefore doing any creation and make sure that you're not calling ReleaseDC on m_handleDeviceContext while your context exists. CS_OWNDC on your WNDCLASS(EX) shouldn't really be necessary any more, but I prefer to do it anyway just in case drivers expect it to be there (I've found one or two particularly poor ones that semi-explode without it).

Share this post

Link to post
Share on other sites
I am using ZeroMemory on the m_tPixelFormat.

At the time of initialization the HDC = 0x85012263 and the HGLRC = 0x00010001

When I call present the values are the same.

I haven't hit any ReleaseDC calls, accidentally or otherwise.

I'm using CS_OWNDC on my windows for backwards compatibility.

glewInit() returns GLEW_OK

wglMakeCurrent returns fine.

SwapBuffers returns fine.

Share this post

Link to post
Share on other sites
As of now, I can debug and step through all the functions fine with no errors being thrown. In fact, I can debug with no slow down either. But if I just let the program run the screen A.) doesn't clear to the color I specified and B.) doesn't respond to window messages very fast (like ten to fourty five seconds slow). This only happens if I call glClear. Someone must have ran into this before.

Share this post

Link to post
Share on other sites

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!