Sign in to follow this  

glClear problem

This topic is 3485 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

At the moment, the only things in the main loop in my program (other than message pumping) are glClear and glLoadIdentity, to clear the color and depth buffers. Ever since I added these lines, the program has been taking around 1.5 seconds just to complete a single loop, and it doesn't even clear the screen. Has this happened to anyone else? Here is some of my initialization code:
glEnable( GL_DEPTH_TEST );
glEnable( GL_TEXTURE_2D );

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0f );
glDepthFunc( GL_LEQUAL );

This code comes before any other OpenGL code. Following is window creation code, then glViewport, then glOrtho, then the loop begins. I'm not sure whether the order of initialization makes a difference. Thanks in advance. [Edited by - Daggerbot on May 27, 2008 11:32:22 PM]

Share this post


Link to post
Share on other sites
Thanks for the reply. Here's a bit of the code:


// initialize OpenGL

void InitGL()
{
if( g_gl_init ) return;
else g_gl_init = true;

glEnable( GL_DEPTH_TEST );
glEnable( GL_TEXTURE_2D );

glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClearDepth( 1.0f );
glDepthFunc( GL_LEQUAL );
}





// initialize window

bool TV_GameWindow::SetGameMode( Dimsu &size, bool full_screen )
{
#define END { EndGameMode(); return false; }
_TV_GameWindow *ws = (_TV_GameWindow*) m_handle;
InitGL();

if( ws->hwnd ) DestroyWindow( ws->hwnd );

// create window
ws->hwnd = CreateGameWindow( *m_text, size.width, size.height, full_screen );
if( !ws->hwnd ) return false;

// associate window
SetWindowLongW( ws->hwnd, 0, 1 );
if( !SetWindowLongW( ws->hwnd, 0, (LONG) this ) ) END

// rendering context
GLuint pf;

static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA, 32,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, 0, 0,
PFD_MAIN_PLANE,
0, 0, 0
};

if( !( ws->hdc = GetDC( ws->hwnd ) ) ) END
if( !( pf = ChoosePixelFormat( ws->hdc, &pfd ) ) ) END
if( !SetPixelFormat( ws->hdc, pf, &pfd ) ) END
if( !( ws->hrc = wglCreateContext( ws->hdc ) ) ) END

// finish
Show( IsWindowVisible( ws->hwnd ) == TRUE );
return true;
#undef END
}





// setup rendering plane

void TV_GameWindow::SelectSurface()
{
wglMakeCurrent( ((_TV_GameWindow*)m_handle)->hdc, ((_TV_GameWindow*)m_handle)->hrc );
}

void TV_SelectSurface( TV_GameWindow *window )
{
window->SelectSurface();
}

void TV_SelectViewport( const Rect &viewport )
{
glViewport( viewport.left, viewport.top, viewport.right - viewport.left,
viewport.bottom - viewport.top );
}

void TV_SelectPlane( const Dims &size, double znear, double zfar )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, size.width, size.height, 0.0, znear, zfar );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}





// main

bool TC_PumpInput()
{
MSG msg;

if( PeekMessageW( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );

if( msg.message == WM_QUIT ) return false;
else DispatchMessageW( &msg );
}

return true;
}

void TV_ClearAll()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
}

int WINAPI WinMain( HINSTANCE inst, HINSTANCE prev_inst, char *cmd_line, int show_cmd )
{
// coords
Dimsu size = { 640, 480 };
Rect viewport = { 0, 0, 640, 480 };
Dims plane = { 640, 480 };

// init
TC_InitInput();
TV_GameWindow *window = new TV_GameWindow( L"test", size, false );

if( !window->IsValid() )
{
MessageBoxW( NULL, L"Can't create game window!", NULL, MB_OK );
return 1;
}

TV_SelectSurface( window );
TV_SelectViewport( viewport );
TV_SelectPlane( plane, 0.0, 1.0 );

// run
window->Show();

while( TC_PumpInput() )
{
TV_ClearAll();
}

// end
delete window;
return 0;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by nhatkthanh
think you need to swap the back buffer, with

SwapBuffers(hdc); where hdc is the handle to the device context that you created.


It works fine now. I can't believe I forgot to add that one line of code. Amateur mistake. Thanks for the help. rate++;

Share this post


Link to post
Share on other sites

This topic is 3485 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.

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