Advertisement

OpenGL Issues

Started by March 24, 2003 08:57 PM
10 comments, last by Roy Fokker 21 years, 11 months ago
Hey, i've been working on a very simple opengl template, based on nehe's oop code, for my applications. the problem i am having is when i try to render stuff in fullscreen opengl window, every thing is very wonky. Where as in windowed mode it works perfectly. i have looked over and over the code, not being able to find out what i did worng. If some of you may enlighten me. I have managed to narrow it down to OpenGL Initialization routine, at least thats where i think the problem is. few things to note in the code below, gfxWindow, and pTrace are 2 global variable/classes, gfxWindow contains all the window info, like resolution, bits per pixel, etc, where are pTrace is just a loging class that i use. And sorry if anyone thinks this is messy way of doing things, neat ness is not top of my priorities list

bool COpenGL::InitGL()
{
	pTrace->toFile("\nInitializing OpenGL States");
	
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	if (gfxWindow.winInit.bFullscreen)
	{
		pTrace->toFile("\n\tWindow is set to FullScreen mode");
	}
	else
		pTrace->toFile("\n\tWindow is set to Windowed mode");

	return true;
}

bool COpenGL::CreateGfxWindow()
{
	int			extGLError;
	GLuint		PixelFormat;
	WNDCLASSEX	wndClass;
	DWORD		dwExStyle;
	DWORD		dwStyle;
	
	wndClass.cbSize			= sizeof(wndClass);
	wndClass.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wndClass.lpfnWndProc	= procWin;
	wndClass.cbClsExtra		= 0;
	wndClass.cbWndExtra		= 0;
	wndClass.hInstance		= gfxWindow.winInit.hInstance;
	wndClass.hIcon			= LoadIcon(gfxWindow.winInit.hInstance, IDI_APPLICATION);
	wndClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground	= NULL;
	wndClass.lpszMenuName	= NULL;
	wndClass.lpszClassName	= "Main Window";
	wndClass.hIconSm		= LoadIcon(NULL, IDI_WINLOGO);

	if (!RegisterClassEx(&wndClass))
	{
		pTrace->toFile("\n* Failed to Register the Window Class.");
		return false;
	}

	if (gfxWindow.winInit.bFullscreen)
	{
		pTrace->toFile("\n\tSetting dmScreenSettings values");
		DEVMODE dmScreenSettings;
		ZeroMemory(&dmScreenSettings, sizeof(DEVMODE));
		dmScreenSettings.dmSize			= sizeof(DEVMODE);
		dmScreenSettings.dmPelsHeight	= gfxWindow.winInit.iHeight;
		dmScreenSettings.dmPelsWidth	= gfxWindow.winInit.iWidth;
		dmScreenSettings.dmBitsPerPel	= gfxWindow.winInit.iBitDepth;
		dmScreenSettings.dmFields		= DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			pTrace->toFile("\n* Resolution of %d x %d is not supported.",gfxWindow.winInit.iWidth, gfxWindow.winInit.iWidth);
			return false;
		}
	}
	if (gfxWindow.winInit.bFullscreen)
	{
		pTrace->toFile("\n\tSetting dwExStyle values");
		dwExStyle	= WS_EX_TOPMOST;
		dwStyle		= WS_POPUP;
	}
	else
	{
		dwExStyle	= WS_EX_APPWINDOW |WS_EX_WINDOWEDGE;
		dwStyle		= WS_CAPTION;
	}

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

	if (!(gfxWindow.hWnd = CreateWindowEx(dwExStyle, "Main Window", 
										gfxWindow.winInit.cTitle, 
										dwStyle | WS_CLIPSIBLINGS |WS_CLIPCHILDREN,
										0, 0, gfxWindow.winInit.iWidth, gfxWindow.winInit.iHeight,
										NULL, NULL, gfxWindow.winInit.hInstance, NULL)))
	{
		pTrace->toFile("\n* Window Creation ERROR.");
		KillWindow();
		return false;
	}
	
	if (!(gfxWindow.hDC = GetDC(gfxWindow.hWnd)))
	{
		pTrace->toFile("\n* Failed to create a GL Device Context");
		KillWindow();
		return false;
	}

	if (!(PixelFormat = ChoosePixelFormat(gfxWindow.hDC, &pfd)))
	{
		pTrace->toFile("\n* Can't find a suitable pixel format");
		KillWindow();
		return false;
	}

	if (!SetPixelFormat(gfxWindow.hDC, PixelFormat, &pfd))
	{
		pTrace->toFile("\n* Failed to Set Pixel Format");
		KillWindow();
		return false;
	}

	if (!(gfxWindow.hRC = wglCreateContext(gfxWindow.hDC)))
	{
		pTrace->toFile("\n* Can not create a GL Rendering Context");
		KillWindow();
		return false;
	}

	if (!wglMakeCurrent(gfxWindow.hDC, gfxWindow.hRC))
	{
		pTrace->toFile("\n* Cannot Activate the GL Rendering Context");
		KillWindow();
		return false;
	}

	extGLError = extgl_Initialize();
	if (extGLError != 0)
	{
		pTrace->toFile("\nExtGL exited with code %d", extGLError);
	}
	else
	{
		pTrace->toFile("\nCheck Supported GL Versions"); 
		if (extgl_Extensions.OpenGL12)
			pTrace->toFile("\n\tOpenGL 1.2 is Supported");
		if (extgl_Extensions.OpenGL13)
			pTrace->toFile("\n\tOpenGL 1.3 is Supported");
		if (extgl_Extensions.OpenGL14)
			pTrace->toFile("\n\tOpenGL 1.4 is Supported");
	}
	
	ShowWindow(gfxWindow.hWnd, SW_SHOW);
	SetForegroundWindow(gfxWindow.hWnd);
	SetFocus(gfxWindow.hWnd);
	ShowCursor(false);

	if(!InitGL())
	{
		pTrace->toFile("\nFailed to Initialize OpenGL Functions");
		KillWindow();
		return false;
	}
	pTrace->toFile("\nOpenGL Window Created");
	return true;
}
 
and in the window Proc i have this

		case WM_SIZE:
			if (height ==0)
				height =1;
			pTrace->toFile("\n+++Entered WM_SIZE");
			
			glViewport(0,0,width,height);
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 500.0f);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
			
			return 0;
 
[edited by - Roy Fokker on March 24, 2003 10:05:51 PM]
I think you''re gonna have to elaborate on ''wonky''. Give a better description of the problem and even post some links of screenshots off the problem. Just by guessing about your problem (because you didn''t given enough info for me to deduce much more) and looking over your code real quick, I''ve never seen the flag WS_EX_TOPMOST ever used. Try taking that out and replacing it with WS_EX_APPWINDOW. That probably won''t help much tho...you''re just gonna more specific about ''wonky''.

-------------------
Realm Games Company
-------------------Realm Games Company
Advertisement
1) What graphics card are you using?
2) What bitdepth are you setting the window to?
3) What bitdepth is your desktop set to?

I have found that unless my desktop bitdepth is set to 32 bit, MANY 3d programs run rediculously slow. I believe it has to do with using blending. Try settings your desktop bitdepth to 32 bit and see if it works any better. Also request 32 bit when you are creating the PFD.
i have tried running program in 16 and 32 bit depths, set via the program it self

the results are like this
this is what it looks like in windowed mode (this is what it is supposed to look like)
Bit depth 32 bits, Window size 800x600
Windowed Screenshot

this is what it look like in full screen mode
same bitdepth and res
Fullscreen Screenshot
The vid card is not the problem, (i have GF3 latest drivers)
I experince the same problem with Nehe Lesson 3 tutorial, it works fine in windowed mode. but Fullscreen is very wonky



[edited by - Roy Fokker on March 25, 2003 9:48:21 AM]
Set your near plane in gluPerspective to 1.0f, this solves many problems on many graphics cards...
Setting gluPerspective''s near plane to 1 instead of 0.1 doesn''t help
Advertisement
does it occur when u switch between screen modes or even when u directly start the app in fullscreen?
- To learn, we share... Give some to take some -
i have no switch between mode thing set up.

at the begining of the application, you get a simple dialog box with choices like resolution, bitdepth and where you want fullscreen or not.

which the application puts in the the gfxWindow(global) structure

and the application makes use of that, once its set you can't change during the course the application is running


[edited by - Roy Fokker on March 25, 2003 5:25:08 PM]
Is your windows desktop running in the same bit-depth as your application ... I can tell you from experience that this is a common OpenGL problem, and is quite elusive. For many video cards, this has been an issue even for publicly released games ... ie Tribes / Tribes 2 actually stated (correctly) that you must set you desktop resolution to the correct depth, prior to running the program.

Obviously there is code that can fix this, which some programs use ... which I think involves setting the desktop disply with Win32 API functions, prior to registering your window class, and your opengl window, but I''m not sure.
The only thing that makes me wonder is your case WM_SIZE section it doesn''t change you height and width variables when the window resizes. Try adding these lines right after case...

case WM_SIZE:
width = LOWORD(lParam);
height = HIWORD(lParam);
...

The rest of WM_SIZE looks fine...

I think that might fix your problem... Hope it helps!
I'm don't know much but I can try to help... just email me at... Shadow_0f_Light@Yahoo.com(the '0' in 'Of' is a zero :P)

This topic is closed to new replies.

Advertisement