Sign in to follow this  
Seabolt

OpenGL glClear locks system

Recommended Posts

Seabolt    781
Hey guys, I'm reposting this since the last one didn't get many replies and I'm at my wits end.

So basically I am working on an EXTREMELY basic OpenGL program. All i want it to do is clear the screen blue going through my pipeline. Now I have my HDC and HGLRC all set up and not NULL. Glew doesn't throw any errors, Set and ChoosePixelFormat have no problems, and wglCreateContext and wglMakeCurrent both work without throwing any errors. But when the game starts calling glClear, the entire game freezes up. The message pump is still running, and if I step through it, I'm still receiving WM_PAINT messages. The system will still respond, but its really slow, like ten to forty-five seconds slow. I'm posting all relevant code

This is my window set up

[code]// Create a window
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)MainWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("Thunderclad Engine"); // Should be unique
wcex.hIconSm = NULL;

// Select window styles
UINT unStyle,unStyleX;
unStyleX = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
unStyle = WS_OVERLAPPEDWINDOW;

// Describe our window placement
WINDOWPLACEMENT wndPlacement;
wndPlacement.length = sizeof(WINDOWPLACEMENT);
wndPlacement.ptMaxPosition.x = 0;
wndPlacement.ptMaxPosition.y = 0;
wndPlacement.ptMinPosition.x = 0;
wndPlacement.ptMinPosition.y = 0;
wndPlacement.rcNormalPosition.bottom = g_nWindowHeight;
wndPlacement.rcNormalPosition.left = 0;
wndPlacement.rcNormalPosition.top = 0;
wndPlacement.rcNormalPosition.right = g_nWindowWidth;
[/code]

Here's where I create the window

[code]// First we must register the class
assert( RegisterClassEx( in_wndClassDescription ) != 0 );

RECT windowRect; // Grabs Rectangle Upper Left / Lower Right Values
windowRect.left=(long)0; // Set Left Value To 0
windowRect.right=(long)in_nWindowWidth; // Set Right Value To Requested Width
windowRect.top=(long)0; // Set Top Value To 0
windowRect.bottom=(long)in_nWindowHeight; // Set Bottom Value To Requested Height

// Get a pixel perfect window
AdjustWindowRectEx( &windowRect, in_unWindowStyle, FALSE, in_unWindowExStyle );

// Then we must create the window from the data provided;
out_handleWindow = CreateWindowEx( in_unWindowExStyle,
in_wndClassDescription->lpszClassName,
in_szProgramName,
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | in_unWindowStyle,
0,
0,
in_nWindowWidth,
in_nWindowHeight,
NULL,
NULL,
in_handleCurrentInstance,
NULL );

assert( out_handleWindow != NULL );


// Show the window and start updating it
ShowWindow( out_handleWindow, SW_SHOW );
UpdateWindow( out_handleWindow );

[/code]




Here's where I init the device

[code]

//////////////////////////////////////////////////////////////////////////
// Platform Specific Initialization (Windows)
#if USING_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
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER );
m_tPixelFormat.iPixelType = PFD_TYPE_RGBA; // Set the color format
m_tPixelFormat.cColorBits = 32; // Set the color depth
m_tPixelFormat.cDepthBits = 32; // 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,
nPixelFormat,
&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
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
//and it shall be forward compatible so that we can only use up to date functionality
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0}; //zero indicates the end of the array

m_handleGLRenderContext = wglCreateContextAttribsARB( m_handleDeviceContext, 0, attribs );
ASSERT_NULL( m_handleGLRenderContext );
ASSERT_FALSE( wglMakeCurrent( NULL, NULL ) );
ASSERT_FALSE( wglDeleteContext( tempContext ) );
ASSERT_FALSE( wglMakeCurrent( m_handleDeviceContext, m_handleGLRenderContext ) );
}
else
{
m_handleGLRenderContext = tempContext;
}
//////////////////////////////////////////////////////////////////////////
#endif

Resize( programWindow->GetWindowWidth(), programWindow->GetWindowHeight() )
[/code]




Here's where I init the renderer

[code]

glEnable(GL_DEPTH_TEST); // Enable depth testing
glDepthFunc( GL_LEQUAL ); // Reject anything less than or equal to the current depth
glClearDepth( 1.0f ); // We clear to full depth
glClearStencil( 0 ); // We clear the stencil buffer to false
glCullFace(GL_FRONT); // Enable culling
glClearColor(0.5f, 0.5f, 1.0f, 1.0f); // The color to clear the background to
[/code]




And lastly here's my WndProc

[code] switch( unMessage )
{
case WM_SIZE:
// Resize the window
CDeviceManager::GetInstance()->Resize( LOWORD( lParam ),
HIWORD( lParam ) );
break;
case WM_PAINT:
if( CGame::GetInstance()->IsGameActive() )
{
CGame::GetInstance()->Render();
}
break;
case WM_CLOSE:
// Free up resources if the red x is pressed
CGame::GetInstance()->Destroy();
CDeviceManager::GetInstance()->Destroy();
g_cWindow.Destroy();

break;
default:
return DefWindowProc( handleWindow,
unMessage,
wParam,
lParam );
[/code]




Please guys, I'm at my wits end on this.

Also if I press close, when it registers about minute later, I do see the clear color for just a second :/

Share this post


Link to post
Share on other sites
NicoG    172
Hmm, do you require any specific Version of OpenGL?
But SFML or SDL or even GLUT might give you a better start into the OpenGL World. If you need to use a specific Version Context, you can download SFML 2, which let's you choose one. If not, download SFML 1.6. It is extreme easy to use OpenGL with SFML and you don't have to manage all this Windows Stuff.
It also makes your code platform independent to a certain degree.

Besides that:
[quote]
An application returns zero if it processes this message.
[/quote]
You must return zero if you handle WM_PAINT.
[url="http://msdn.microsoft.com/en-us/library/dd145213%28v=vs.85%29.aspx"]http://msdn.microsof...v=vs.85%29.aspx[/url]

That's what I meant with the Windows Stuff :).

edit:
SFML Window:

[code]
// Earlier in the class-definition:
sf::RenderWindow* m_window;

sf::ContextSettings context;
context.AntialiasingLevel = m_settings.fsaa;
context.DepthBits = m_settings.bpp;
context.MajorVersion = 3;
context.MinorVersion = 3;
context.StencilBits = 24;

m_window = new sf::RenderWindow(sf::VideoMode(static_cast<unsigned int>(m_settings.width), static_cast<unsigned int>(m_settings.height),
m_settings.bpp), m_settings.caption, flags, context);
[/code]

Share this post


Link to post
Share on other sites
Seabolt    781
@NicoG

I don't require any specific version or anything like that, this is mostly just educational. I just got a job where we use OpenGL for rendering and I've been a DirectX guy for awhile now, so I'm trying to get more intimate with OpenGL :) So I want to try and implement this without using someone else's code, though it seems almost impossible to avoid using GLUT and GLEW... :/


But thanks for that info! It didn't fix anything, but still, thanks!

Share this post


Link to post
Share on other sites
NicoG    172
Why do you want to avoid GLew?
Do you really want to write a hundred lines of Codes with wglGetProcAdress?
GLew is a fine lib and I don't want to live without it anymore as an OpenGL Programmer. Trust me.

Just download SFML 2 and create the Context as I showed you. If you want to have a 3.x Core Profile, so older Functions don't work do this:
Go to WglContext.cpp and go to line 252:
Change the Attributes to this:
[code]
int attributes[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, mySettings.MajorVersion,
WGL_CONTEXT_MINOR_VERSION_ARB, mySettings.MinorVersion,
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
0, 0
};
[/code]

That's a bit dirty hack and you need to comment out some other deprecated functions (fixed functions like glMatrixMode) inside SFML 2, but then it works. They don't support core profiles yet though.
For me, it works :).

But you could also just use GLUT, which is a fine Tool for quick programming.

[quote]
So I want to try and implement this without using someone else's code, though it seems almost impossible to avoid using GLUT and GLEW... :/
[/quote]
Yeah, but such thing as a window, is really no task to make a drama of. ^^ Be productive and don't reinvent the Wheel.

Share this post


Link to post
Share on other sites
Seabolt    781
Haha I understand what you mean by not reinventing the wheel, and of course I don't want to write a hundred lines of code to do what GLEW does, but that doesn't mean I don't want to learn what I can do or how these things work. I'm not doing this to be productive or to get a studio up and off the ground, I'm doing this to learn something new. if I use something like SFML, yes I can have things working faster, but I defeat the whole point of me tackling this thing in the first place. I'm only asking for help here because it seems like everything is saying its ok but the program is far from. I'm not trying to be disrespectful, but this is just what I'm trying to accomplish :)

Share this post


Link to post
Share on other sites
NicoG    172
Did you have a look here?:
[url="http://www.opengl.org/wiki/Creating_an_OpenGL_Context"]http://www.opengl.or..._OpenGL_Context[/url]

Share this post


Link to post
Share on other sites
Seabolt    781
I've seen tons like that, but that one has some new info in it. I never heard that I had to delete my window and re create it when using the dummy context. Thanks :)

Share this post


Link to post
Share on other sites
haegarr    7372
The only explanation I have is this one:

Render commands that are send to OpenGL are not executed immediately but enqueued and executed later. When you send a command that will alter a resource that is still locked by a previous command, then blocking will occur. Perhaps the current backbuffer is such a resource w.r.t. glClear. Now, glFlush instructs OpenGL to execute all pending commands as quick as possible. Therefore it is meaningful to send a glFlush (but not a glFinish in this case) before swapping buffers.

However, even without glFlush I would expect OpenGL to do its job, even if perhaps not at as performant as possible.

Share this post


Link to post
Share on other sites
Seabolt    781
Right, and that's why I'm trying to eliminate the glFlush() call.

What do you mean by w.r.t glClear() though?

Sorry I've never heard that acronym.

I do believe that somewhere the call is being blocked because when I run the program through glDEBugger I get some where in the tens of thousands of calls to the context in a frame, when all I call is two gl functions. Something is up there, I just can't seem to figure out what is eating all of those calls.

Share this post


Link to post
Share on other sites
Seabolt    781
[code]

// Start the message loop
MSG msg;
while( CGame::GetInstance()->IsGameActive() )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if( msg.message == WM_QUIT )
{
break;
}

TranslateMessage( &msg );
DispatchMessage( &msg );
}
else if( CGame::GetInstance()->IsGameActive() )
{
CGame::GetInstance()->Update();
CGame::GetInstance()->Render();
}
}



[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now