Advertisement

HELP with OpenGL Basecode with MFC

Started by May 26, 2002 01:22 AM
6 comments, last by henryws 22 years, 9 months ago
hi guys, I have a problem with setting up an OpenGL basecode with MFC. At the moment, I initialize openGL in CAppView::OnCreate(..) but it just doesn''t do anything for me I am trying to get sth with this simple code but just doesn''t work anyone know why ? so, the below code is called once in OnCreate() void CBasicSkeletonView::EnableOpenGL() { PIXELFORMATDESCRIPTOR pfd; int format; CClientDC clientDC(this); // set the pixel format for the DC ZeroMemory( &pfd, sizeof( pfd ) ); pfd.nSize = sizeof( pfd ); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; format = ChoosePixelFormat( clientDC.m_hDC, &pfd ); SetPixelFormat( clientDC.m_hDC, format, &pfd ); // create and enable the render context (RC) m_hRC = wglCreateContext( clientDC.m_hDC ); wglMakeCurrent( clientDC.m_hDC, m_hRC ); } then in OnDraw this is the simple code to draw polygon { glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); glClear( GL_COLOR_BUFFER_BIT ); glColor3f( 1.0f, 1.0f, 1.0f ); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, 0.75, 0.0); glVertex3f(0.25, 0.75, 0.0); glEnd(); glFlush(); } but nothing come to my screen. Do you know why ? What do i need to do ? thanks
look to the left of nehe''s site, and down the side you will find a tut on using GL in a mfc document based window.
You still can use MFC with normal window creation routines though.
(which I myself do)
Advertisement
Many things can go wrong when using mfc (it makes a lot of stuff in the background), but I can see you''re using a temporary DC (local to the function/method) for the GL context. It gets destroyed when leaving this function.

The variable "clientDC" should be a member of CBasicSkeletonView,
and it can be initialized exactly where you have it.

quote:
Original post by t0y
Many things can go wrong when using mfc


Much fewer than you think if you're using it properly.
quote:

(it makes a lot of stuff in the background)


Not when you're in GameLoop()/OnIdle(). With proper code organization MFC code takes <1% of your apptime.
quote:

but I can see you're using a temporary DC (local to the function/method) for the GL context. It gets destroyed when leaving this function.


While this is likely the case, client DC isn't always temporary.

henryws: override PreCreateWindow and add CS_OWNDC to the class styles. You also appear to be missing a call to SwapBuffers. Finally, OnDraw is not the best place to do rendering. Have a separate function that is called from your OnIdle handler to perform rendering.

Edit: changed wording a bit. MFC is old and some things do go wrong with it.

[edited by - IndirectX on May 26, 2002 12:25:22 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
quote:
Original post by IndirectX
Much fewer than you think if you're using it properly.

Not when you're in GameLoop()/OnIdle(). With proper code




I meant that during window creation/destruction (the actual hWnd), MFC makes more things besides calling CreateWindowEx (or whatever).

This week I created a class to encapsulate the same functionality of nehe's basecode in mfc, and I had to check out MFC's source to catch bugs. It takes care of the precreatewindow and creates a class with CS_OWNDC and vertical/horizontal size updating, so I know what you mean...
One thing I noticed was that if the context wasn't created in OnCreate() it simply wouldn't work...


Right now it works ok, but some GDI objects get lost in mode switching ... Probably allocated somewhere in MFC code



[edited by - t0y on May 26, 2002 1:09:28 PM]
well. as I said, you can still link to the MFC dlls, in place of all the defualt libs. There are a couple of dissadvantages of this. loss of generic RTTI is a good example, but otherwise, the pros outweigh the cons. (you can still use generic GL window creation/setup stuff like this too)
Advertisement
quote:
Original post by RipTorn
well. as I said, you can still link to the MFC dlls, in place of all the defualt libs. There are a couple of dissadvantages of this. loss of generic RTTI is a good example, but otherwise, the pros outweigh the cons. (you can still use generic GL window creation/setup stuff like this too)


Or, you can use WTL (Windows Template Library). It provides a framework similar to that of MFC, but is significantly less intrusive. I switched to it a while ago and haven''t regretted it.
---visit #directxdev on afternet <- not just for directx, despite the name
In most cases using OpenGL with MFC is relatively easy.

See www.mfcogl.com

There you will find a tut on setting up OpenGL in MFC, 20-30 examples and more.


RipTorn: I looked to the left of nehe''s site as suggested and couldn''t find a tut on using GL in a mfc document based window.

This topic is closed to new replies.

Advertisement