Advertisement

Can't get a HGLRC

Started by January 18, 2002 10:22 AM
2 comments, last by glvoid 23 years, 1 month ago
Ok, I''ve some experience coding for OpenGL using a Win32 surface. Now I tried to use it in my existing MFC Application (Own Software3D before...), and it compiles fine and stuff, but I can''t create a GL-Window. The code below runs fine (minimum security, I know) until the point I check if wglMakeCurrent really did create the rDC. The point is, wglMakeCurrent runs, but rDC stays NULL. Can anyone help me? I need to run it in MFC because of all the dialog and object stuff I use. void CChildView::CreateGLWindow() { #define alert(x) MessageBox(x, "Status", MB_OK); GLuint PixelFormat; 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, 0 }; CPaintDC paintdc(this); // I was fooling around with this a bit but it didn''t really help hDC = (HDC)paintdc; // no matter what type of DC I used PixelFormat = ChoosePixelFormat(hDC, &pfd); if (!SetPixelFormat(hDC, PixelFormat, &pfd)) alert("!SetPixelFormat"); if (!wglCreateContext(hDC)) alert("!wglCreateContext"); if (!wglMakeCurrent(hDC, hRC)) alert ("!wglMakeCurrent"); if (hDC == NULL) alert("hDC = NULL"); if (hRC == NULL) alert("hRC = NULL"); ResizeGLView(800, 600); InitGL(); }
change
if( !wglCreateContext( hDC ) ) alert( "!wglCreateContext" );
to
if( !(hRC = wglCreateContext( hDC )) ) alert( "!wglCreateContext" );
Advertisement
Well, that solved that problem. Still, I can''t get the app to initialise OGL. I have definitely set the background to different colors and cleared it, but it won''t show any activity...
I had thought it wasn''t too much of a problem, going on to MFC with it, but I am being taught the opposite...

Can you please look for more dummy-errors?

  CChildView::CChildView(){	hRC = NULL;	hDC = NULL;}CChildView::~CChildView(){	KillGL();}BEGIN_MESSAGE_MAP(CChildView,CWnd )	//{{AFX_MSG_MAP(CChildView)	ON_WM_MOUSEMOVE()	ON_WM_PAINT()	//}}AFX_MSG_MAPEND_MESSAGE_MAP()BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) {	if (!CWnd::PreCreateWindow(cs))		return FALSE;	cs.dwExStyle |= WS_EX_CLIENTEDGE;	cs.style &= ~WS_BORDER;	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 		::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);	return TRUE;}void CChildView::OnPaint() {	if (!created) CreateGLWindow();	Render();	}void CChildView::ResizeGLView(int h, int w){	if (h <= 0) h = 1;	glViewport(0, 0, w, h);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f, (GLfloat) w / (GLfloat) h, 0.1f, 500.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}void CChildView::InitGL(){	glShadeModel(GL_SMOOTH);	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);	glClearDepth(1.0f);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);}void CChildView::KillGL(){	if (hRC)	{		wglMakeCurrent(NULL, NULL);		wglDeleteContext(hRC);		hRC = NULL;	}	if (hDC)	{		ReleaseDC((CDC*)hDC);		hDC = NULL;	}}void CChildView::CreateGLWindow(){#define alert(x) MessageBox(x, "Status", MB_OK);	GLuint PixelFormat;	static	PIXELFORMATDESCRIPTOR pfd =	{		sizeof(PIXELFORMATDESCRIPTOR),		1,		PFD_DRAW_TO_WINDOW |		PFD_SUPPORT_OPENGL |		PFD_DOUBLEBUFFER,		PFD_TYPE_RGBA,		16,		0, 0, 0, 0, 0, 0,		0,		0,		0,		0, 0, 0, 0,		16,		0,		0,		PFD_MAIN_PLANE,		0,		0, 0, 0	};	CPaintDC paintdc(this);	hDC = paintdc;	PixelFormat = ChoosePixelFormat(hDC, &pfd);	if (!SetPixelFormat(hDC, PixelFormat, &pfd)) alert("!SetPixelFormat");	if ((hRC = wglCreateContext(hDC)) == NULL) alert("!wglCreateContext");	wglMakeCurrent(hDC, hRC);	if (hRC == NULL) alert("hRC = NULL");	ShowWindow(SW_SHOW);	SetForegroundWindow();	if (hDC == NULL) alert("hDC = NULL");	ResizeGLView(GetSystemMetrics(SM_CXFULLSCREEN), GetSystemMetrics(SM_CYFULLSCREEN));	InitGL();	created = true;}void CChildView::Render(){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        glLoadIdentity();// stuff...	SwapBuffers(hDC);}  
Dumb me...
I forgot to specify CS_OWNDC in the window styles list...
How should it draw to the main dc?

Anyway, if you add CS_OWNDC to the AfxRegisterWindowClass call in PreCreateWindow you have a neat tutorial page for using OpenGL in MFC applications :-)

This topic is closed to new replies.

Advertisement