Making an OpenGL window from a MDI child

Started by
6 comments, last by Endar 19 years, 2 months ago
Would the following function be enough to make a window an openGL window? The handle that is being passed to it is the handle of an child window of an MDI client. When I run the program,, pretty much nothing different happens from when I wasn't trying to make the MDI child an OpenGL window.

/** 
 * Create GLWindow
 */
bool MakeOpenGLWindow( HWND hwnd, int width, int height, int bits )
{
	GLuint PixelFormat;		// Hold results after searching for a match

	// pfd tells windows how we want things to be
	static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof( PIXELFORMATDESCRIPTOR ),		// Size of this pixel format descriptor
			1,							// Version number
			PFD_DRAW_TO_WINDOW |		// Format must support window
			PFD_SUPPORT_OPENGL |		// Format must support OpenGL
			PFD_DOUBLEBUFFER,		// Format must support double buffering
			PFD_TYPE_RGBA,			// Request an RGBA format
			bits,				// Select our Color depth
			0, 0, 0, 0, 0, 0,		// Color bits ignored
			0,				// No alpha buffer
			0,				// Shift bit ignored
			0,				// No accumulation bit
			0, 0, 0, 0,			// Accumulation bits ignored
			16,				// 16Bit Z-Buffer (Depth buffer)
			0,				// No stencil buffer
			0,				// No auxillary buffer
			PFD_MAIN_PLANE,			// Main drawing layer
			0,				// reserved
			0, 0, 0				// Layer masks ignored.
	};

	// Did We Get A Device Context?
	if (!(hDC=GetDC(hwnd))){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}

	// Did Windows Find A Matching Pixel Format?
	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}

	// Are We Able To Set The Pixel Format?
	if(!SetPixelFormat(hDC,PixelFormat,&pfd)){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}

	// Are We Able To Get A Rendering Context?
	if (!(hRC=wglCreateContext(hDC))){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}

	// Try To Activate The Rendering Context
	if(!wglMakeCurrent(hDC,hRC)){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}


	ShowWindow(hwnd,SW_SHOW);			// Show The Window
	SetForegroundWindow(hwnd);			// Slightly Higher Priority
	SetFocus(hwnd);						// Sets Keyboard Focus To The Window
	ReSizeGLScene(width, height);		// Set Up Our Perspective GL Screen

	// Initialize Our Newly Created GL Window
	if (!InitGL()){
		KillGLWindow();			// Reset The Display
		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;			// Return FALSE
	}

	// success
	return true;

}


Any ideas? Or would you like some more info? (I'd like to cut down as much as possible, since the total code will be like 400 lines)
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Anybody have any ideas?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
[EDIT]
Oh no. I misread what you said and also was being distracted.

You cannot use the MDI client class for this.

Create an MDI Child using the MDICREATESTRUCT and get the hWnd by sending the MDI CLient a create message.

Then when you get the child window you can create an OpenGL context in the client area of the MDI child.



Quote:Original post by qesbit
[EDIT]
Oh no. I misread what you said and also was being distracted.

You cannot use the MDI client class for this.

Create an MDI Child using the MDICREATESTRUCT and get the hWnd by sending the MDI CLient a create message.

Then when you get the child window you can create an OpenGL context in the client area of the MDI child.


How would I go about creating the OpenGL context in the MDI child?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Would passing the window handle of an MDI child to this function make it an openGL window?

/**  * Create GLWindow */bool MakeOpenGLWindow( HWND hwnd, int width, int height, int bits ){	GLuint PixelFormat;		// Hold results after searching for a match	// pfd tells windows how we want things to be	static PIXELFORMATDESCRIPTOR pfd = 	{		sizeof( PIXELFORMATDESCRIPTOR ),		// Size of this pixel format descriptor			1,									// Version number			PFD_DRAW_TO_WINDOW |				// Format must support window			PFD_SUPPORT_OPENGL |				// Format must support OpenGL			PFD_DOUBLEBUFFER,					// Format must support double buffering			PFD_TYPE_RGBA,						// Request an RGBA format			bits,								// Select our Color depth			0, 0, 0, 0, 0, 0,					// Color bits ignored			0,									// No alpha buffer			0,									// Shift bit ignored			0,									// No accumulation bit			0, 0, 0, 0,							// Accumulation bits ignored			16,									// 16Bit Z-Buffer (Depth buffer)			0,									// No stencil buffer			0,									// No auxillary buffer			PFD_MAIN_PLANE,						// Main drawing layer			0,									// reserved			0, 0, 0								// Layer masks ignored.	};	// Did We Get A Device Context?	if (!(hDC=GetDC(hwnd))){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	// Did Windows Find A Matching Pixel Format?	if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	// Are We Able To Set The Pixel Format?	if(!SetPixelFormat(hDC,PixelFormat,&pfd)){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	// Are We Able To Get A Rendering Context?	if (!(hRC=wglCreateContext(hDC))){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	// Try To Activate The Rendering Context	if(!wglMakeCurrent(hDC,hRC)){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	ShowWindow(hwnd,SW_SHOW);			// Show The Window	SetForegroundWindow(hwnd);			// Slightly Higher Priority	SetFocus(hwnd);						// Sets Keyboard Focus To The Window	ReSizeGLScene(width, height);		// Set Up Our Perspective GL Screen	// Initialize Our Newly Created GL Window	if (!InitGL()){		KillGLWindow();			// Reset The Display		MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);		return false;			// Return FALSE	}	// success	return true;}
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Can anyone give me a reply to my last post?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
I would say that code is not appropriate for use with MDI windows because it doesn't handle releasing the DC and making the context current for the current window. This mean it wouldn't work with more than one MDI window.

Here is some sample code you might use instead:

// header file

#ifndef __OGLWINDOW_HPP__#define __OGLWINDOW_HPP__class OGLWindow{	static PIXELFORMATDESCRIPTOR pfd;	static PAINTSTRUCT ps;public:	static void InitializeStatics();	static void DeInitializeStatics();	static void MakeCurrent(HDC hDC) { wglMakeCurrent(hDC, wglGetCurrentContext()); }	static void SetupWindowForOGL(HWND hWnd);	static void StartRender(HWND hWnd, boolean defaultsettings = TRUE);	static void EndRender();};#endif


// source file
#include "globals.h"#include "OGLWindow.hpp"PIXELFORMATDESCRIPTOR OGLWindow::pfd = {	sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor		1,											// Version Number		PFD_DRAW_TO_WINDOW |						// Format Must Support Window		//PFD_DRAW_TO_BITMAP |		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL		//PFD_SUPPORT_GDI |		PFD_DOUBLEBUFFER,							// Must Support Double Buffering		PFD_TYPE_RGBA,								// Request An RGBA Format		32,											// Select Our Color Depth		0, 0, 0, 0, 0, 0,							// Color Bits Ignored		0,											// No Alpha Buffer		0,											// Shift Bit Ignored		0,											// No Accumulation Buffer		0, 0, 0, 0,									// Accumulation Bits Ignored		32,											// 32Bit Z-Buffer (Depth Buffer)  		0,											// No Stencil Buffer		0,											// No Auxiliary Buffer		PFD_MAIN_PLANE,								// Main Drawing Layer		0,											// Reserved		0, 0, 0										// Layer Masks Ignored};PAINTSTRUCT OGLWindow::ps;void OGLWindow::InitializeStatics(){	HWND SetupWindow = CreateWindowEx(NULL, WC_DIALOG, NULL, 0, 0, 0, 0, 0, 0, 0, g_hInst, NULL);	HDC hDC = GetDC(SetupWindow);	pfd.cDepthBits = GetDeviceCaps(hDC, BITSPIXEL);	SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);		wglMakeCurrent(hDC, wglCreateContext(hDC));	/* setup default opengl stuff */		//int c = GetSysColor(COLOR_3DFACE);		int c = RGB(255, 255, 255);		glClearColor(GetRValue(c) / 255.0f, GetGValue(c) / 255.0f, GetBValue(c) / 255.0f, 0.0f);		glShadeModel(GL_SMOOTH);		glClearDepth(1.0f);		glEnable(GL_DEPTH_TEST);		glDepthFunc(GL_LEQUAL);		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	MakeCurrent(NULL);	ReleaseDC(SetupWindow, hDC);	DestroyWindow(SetupWindow);}void OGLWindow::SetupWindowForOGL(HWND hWnd){	HDC hDC = GetDC(hWnd);	SetPixelFormat(hDC, ChoosePixelFormat(hDC, &pfd), &pfd);	ReleaseDC(hWnd, hDC);}void OGLWindow::DeInitializeStatics(){	wglDeleteContext(wglGetCurrentContext());}void OGLWindow::StartRender(HWND hWnd, boolean defaultsettings){	RECT rc;	GetClientRect(hWnd, &rc);	MakeCurrent(BeginPaint(hWnd, &ps));	glViewport(0, 0, rc.right, rc.bottom);	glPushAttrib(GL_ALL_ATTRIB_BITS);	if(defaultsettings)	{		glMatrixMode(GL_PROJECTION);		glLoadIdentity();		gluPerspective(90.0f, (GLfloat)rc.right /(GLfloat)rc.bottom, 0.1f, 128.0f);		glMatrixMode(GL_MODELVIEW);		glLoadIdentity();		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	}}void OGLWindow::EndRender(){	HDC hDC = wglGetCurrentDC();	HWND hWnd = WindowFromDC(hDC);	SwapBuffers(hDC);	EndPaint(hWnd, &ps);	glPopAttrib();	MakeCurrent(NULL);}


BTW - please excuse the poor style(this is 2 years old and I don't code like that anymore)but it's tested and it works.

sample usage:

main(){	OGLWindow::InitializeStatics();	MainLoop();	OGLWindow::DeInitializeStatics();}int MdiChildProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){	switch(msg)	{	case WM_CREATE:		OGLWindow::SetupWindowForOGL(hWnd);		break;	case WM_PAINT:		OGLWindow::StartRender(hWnd);		// draw stuff		OGLWindow::EndRender();	}	return DefMDIChildProc(hWnd, Msg, wParam, lParam);}


HTH.
-Melekor
Congratulations Melekor: you are now my new best friend. :D :D

Seriously, thanks a lot. It has helped me get rid of a lot of unweildy code. :D
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

This topic is closed to new replies.

Advertisement