Sign in to follow this  

OpenGL retardednesss....

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

Ok, so i just recently got Beginning OpenGL Game Programming (by our buddies Dave and Kevin), and have been messing around with some really basic things. I have this source code i am playing with (below), and it runs great, except for one thing. It is running the while loop - but the screen just 'captures' whatever was there before it and that is all you see, even if you move the window around. So, i have like a 'screenshot' of Visual Studio .NET floating around doing me no good. Anyway, i think it has something to do with the buffer updating or flipping, but i dunno. Any help would be appreciated. Thanks! Code..
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>

#pragma warning(disable:4305)

bool				done = false;
long				windowWidth = 800;
long				windowHeight = 600;
int					m_windowWidth;
int					m_windowHeight;
long				windowBits = 32;
bool				fullscreen = false;
float				m_angle;

HDC					hDC;

void SetupPixelFormat( HDC hDc )
{
	int pixelFormat;

	PIXELFORMATDESCRIPTOR pfd =
	{   
		sizeof(PIXELFORMATDESCRIPTOR),  // size
			1,                          // version
			PFD_SUPPORT_OPENGL |        // OpenGL window
			PFD_DRAW_TO_WINDOW |        // render to window
			PFD_DOUBLEBUFFER,           // support double-buffering
			PFD_TYPE_RGBA,              // color type
			32,                         // prefered color depth
			0, 0, 0, 0, 0, 0,           // color bits (ignored)
			0,                          // no alpha buffer
			0,                          // alpha bits (ignored)
			0,                          // no accumulation buffer
			0, 0, 0, 0,                 // accum bits (ignored)
			16,                         // depth buffer
			0,                          // no stencil buffer
			0,                          // no auxiliary buffers
			PFD_MAIN_PLANE,             // main layer
			0,                          // reserved
			0, 0, 0,                    // no layer, visible, damage masks
	};

	pixelFormat = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, pixelFormat, &pfd);
}

LRESULT CALLBACK MainWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
//	fprintf( stderr, "MainWindowProc enter." );
	static HDC hDC;
	static HGLRC hRC;
	int height, width;

	switch( uMsg )
	{
	case WM_CREATE:
		hDC = GetDC( hWnd );
		SetupPixelFormat( hDC );
		hRC = wglCreateContext( hDC );
		wglMakeCurrent( hDC, hRC );
		break;

	case WM_DESTROY:
	case WM_QUIT:
	case WM_CLOSE:

		wglMakeCurrent( hDC, NULL );
		wglDeleteContext( hRC );

		PostQuitMessage(0);
		break;

	case WM_SIZE:

		height = HIWORD( lParam );
		width = LOWORD( lParam );

		if ( height == 0 )
		{
			height = 1;
		}

		glViewport( 0, 0, width, height );
		glMatrixMode( GL_PROJECTION );
		glLoadIdentity();

		gluPerspective( 52.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f );

		glMatrixMode( GL_MODELVIEW );
		glLoadIdentity();

		m_windowHeight = height;
		m_windowWidth = width;
		
		break;

	case WM_ACTIVATEAPP:
		break;

	case WM_PAINT:
		PAINTSTRUCT ps;
		BeginPaint( hWnd, &ps );
		EndPaint( hWnd, &ps );
		break;

	case WM_LBUTTONDOWN:
		break;

	case WM_RBUTTONDOWN:
		break;

	case WM_MOUSEMOVE:
		break;

	case WM_LBUTTONUP:
		break;

	case WM_RBUTTONUP:
		break;

	case WM_KEYUP:
		break;

	case WM_KEYDOWN:
		int fwKeys;
		LPARAM keyData;

		fwKeys = (int)wParam;
		keyData = lParam;

		switch ( fwKeys )
		{
		case VK_ESCAPE:
			PostQuitMessage(0);
			break;
		default:
			break;
		}

		break;

	default:
		break;
	}	

	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
//	fprintf( stderr, "WinMain run." );

	WNDCLASSEX			windowClass;
	HWND				hWnd;
	MSG					msg;
	DWORD				dwExStyle;
	DWORD				dwStyle;
	RECT				windowRect;

	windowRect.left = (long)0;
	windowRect.right = (long)windowWidth;
	windowRect.top = (long)0;
	windowRect.bottom = (long)windowHeight;

	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	windowClass.lpfnWndProc = MainWindowProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	windowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
	windowClass.hbrBackground = NULL;
	windowClass.lpszMenuName = NULL;
	windowClass.lpszClassName = "GLClass";
	windowClass.hIconSm = LoadIcon( NULL, IDI_WINLOGO );

	if ( !RegisterClassEx( &windowClass ))
		return 0;

	if ( fullscreen )
	{
		DEVMODE dmScreenSettings;
		memset( &dmScreenSettings, 0, sizeof( dmScreenSettings ));
		dmScreenSettings.dmSize = sizeof( dmScreenSettings );
		dmScreenSettings.dmPelsWidth = windowWidth;
		dmScreenSettings.dmPelsHeight = windowHeight;
		dmScreenSettings.dmBitsPerPel = windowBits;
		dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		if ( ChangeDisplaySettings( &dmScreenSettings, CDS_FULLSCREEN ) != DISP_CHANGE_SUCCESSFUL )
		{
			MessageBox( NULL, "Display mode failed.", NULL, MB_OK );
			fullscreen = FALSE;
		}
	}

	if ( fullscreen )
	{
		dwExStyle = WS_EX_APPWINDOW;
		dwStyle = WS_POPUP;
		ShowCursor( FALSE );
	}
	else
	{
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPEDWINDOW;
	}

	AdjustWindowRectEx( &windowRect, dwStyle, FALSE, dwExStyle );

	hWnd = CreateWindowEx(NULL,                                 // extended style
		"GLClass",                          // class name
		"BOGLGP - Chapter 2 - OpenGL Application",      // app name
		dwStyle | WS_CLIPCHILDREN |
		WS_CLIPSIBLINGS,
		0, 0,                               // x,y coordinate
		windowRect.right - windowRect.left,
		windowRect.bottom - windowRect.top, // width, height
		NULL,                               // handle to parent
		NULL,                               // handle to menu
		hInstance,                          // application instance
		NULL); 
	hDC = GetDC( hWnd );

	if ( !hWnd )
		return 0;

	ShowWindow( hWnd, SW_SHOW );
	UpdateWindow( hWnd );

	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	m_angle = 0.0f;

	while( !done )
	{
		m_angle += 0.1f;

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
		glLoadIdentity();
		
		glTranslatef( 0.0, 0.0, -5.0f );
		glRotatef( m_angle, 1.0f, 0.0f, 0.0f );
		glRotatef( m_angle, 0.0f, 1.0f, 0.0f );
		glRotatef( m_angle, 0.0f, 0.0f, 1.0f );

		glColor3f( 0.7f, 1.0f, 0.3f );

		glBegin( GL_TRIANGLES );
			glVertex3f( 1.0f, -1.0f, 0.0f );
			glVertex3f( -1.0f, -1.0f, 0.0f );
			glVertex3f( 0.0f, 1.0f, 0.0f );
		glEnd();

		SwapBuffers( hDC );

		while ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
		{
			if ( !GetMessage( &msg, NULL, 0, 0 ) )
			{
				done = true;
				break;
			}

			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}

	}

	if ( fullscreen )
	{
		ChangeDisplaySettings( NULL, 0 );
		ShowCursor( TRUE );
	}

	return (int)msg.wParam;

}

Share this post


Link to post
Share on other sites
I was going to suggest that you're missing the command to clear the screen, but it's definately there in your code. Since the command is there (and, I assume, being run) my guess is that it's not drawing to the screen, possibly because the screen stuff hasn't been acquired properly. Try putting in error checking on that initialisation stuff?

Share this post


Link to post
Share on other sites
Ok i did some errorchecking and it turns out that OpenGL is throwing a 0x502 error ( or GL_INVALID_OPERATION ). I am still tracking down where exactly, but i think it has something to do with WGL. Thanks for the help...

Anyone see any invalid operations? Neither do i....

Edit: I found out that OpenGL is throwing that error from the beginning of the program. How is that possible? I put glGetError(); right as WinMain begins, and it gives me that.

Any ideas?

Share this post


Link to post
Share on other sites
I'd say use a different OpenGL framework to work with, such as NeHe's. I compared the two and they are similar, except that posted code does not work :/ well at all. It has to do with the order of creation.

I think the way they did it based on the windows messages might be throwing you off. Just compare it to NeHe's and do a little rewriting, and I think you will be able to find one little difference somewhere. I'm sure it has to do with the rendering contexts though, that is why the screen cannot be cleared as well as be drawn to.

I tried figuring it out, but I just gave up - no sense in rewriting someone elses code that is known to work on other computers. You may want to check the errata for the book and see if any changes were made, but I did not see any of use.

- Drew

Share this post


Link to post
Share on other sites

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