Click here to Skip to main content
Click here to Skip to main content

A WTL class for developping OpenGL programs using WTL

By , 1 Oct 2001
 

Sample Image

Overview

Recently, I studied up on how to write OpenGL programs. I have tried GLUT, a window system independent toolkit for writing OpenGL programs. But I don't like it since it is C-style and need an additional glut32.dll. I have played with WTL for a while, I really like this great framework for developing Win32 program, so I decided to write OpenGL programs using WTL.

Actually, WTL DOES provide OpenGL support in atlgdi.h. If you didn't define _ATL_NO_OPENGL, you should be able to use ChoosePixelFormat, wglCreateContext, etc. However, you have to do a lot of work every time to write an OpenGL program. For simplification, I developed a class COpenGL which can be easily plugged into your WTL programs.

Using COpenGL

COpenGL is a template class which implements most necessary work for OpenGL. It is defined in atlopengl.h. For using it in your program, you have to do the following steps: (assume your main program is Cube.cpp, your OpenGL window is CMainFrame.)

  • copy atlopengl.h to WTL\include.
  • add #include <atlopengl.h> in Cube.cpp file.
  • derive your OpenGL window CMainFrame from COpenGL<CMainFrame>.
  • chain message map to COpenGL by adding CHAIN_MSG_MAP(COpenGL<CMainFrame>) in CMainFrame's message map.
  • if you have implemented OnCreate function in CMainFrame, set bHandled = FALSE; before return 0;
  • implement three functions in CMainFrame: OnInit(), OnRender(), OnResize()
  • (optional) if you want to do animation, derive CMainFrame from CIdleHandler and implement OnIdle. In additional, change CMessageLoop to CGLMessageLoop in Cube.cpp. (you have to do this, otherwise, you cannot get animation).
Similarly you can use it in your view windows. Read atlopengl.h and the demo project for more details.

Conclusion

atlopengl.h is still very simple, but it is a good start for writing OpenGL programs using WTL.

Comment

Partial codes in demo project generated by "OpenGL AppWizard" developed by Ulf Öhlén. See here for details.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Zilin
Web Developer
United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generala small correction of COpenGL::OnSize(..)memberHunt Chang5-Oct-09 18:42 
After the execution of COpenGL::OnDestroy(..), the OpenGL rendering context, m_hRC, was reset to NULL in this routine. However, there are still two WM_SIZE message were generated by Windows before exit, so part of code in the OnSize(..) function should be corrected as below,
 
if (cx==0 || cy==0 || NULL==pT->m_hWnd || NULL==m_hRC)
return 0;

 
Thanks to the author for this fantastic class. Poke tongue | ;-P
 
Hunt Chang
GeneralRe: a small correction of COpenGL::OnSize(..)memberbible~9-Aug-11 18:28 
That's quite strange. There's definitely NO OnSize called after OnDestroy. Can you show us the log of that call?
Questionwhat is the function of CGLMessageLoop? [modified]memberHunt Chang29-Sep-09 18:40 
Dear Zilin,
 
I just read your article and test the code recently, it still running very well under WTL8 and VS 2008 without any bit of problem. Laugh | :laugh:
 
Though I have noticed that you wrote this article in year 2001 which means a long long time has been passed away. But I am still curious what is the magic behind the class of CGLMessageLoop?
 
Inside that class, you have only wrote one instruction, return !CMessageLoop::OnIdle(nIdleCount); I have tried to replace the class with some other methods to run the drawing during idle time but they are all not so effective like yours. Could you please explain the code magics behind it?
 
Thanks in advance and best regards,
 
Hunt Chang Smile | :)
 
modified on Wednesday, September 30, 2009 9:22 PM

Generalatlres.hmemberpeterdrozd20-Dec-05 8:16 
can not find atlres.h
 
-pete
GeneralOpengl errorsussAnonymous24-Apr-05 10:10 
Everytime I try too use one of the event handlers like OnFileOpen I immediatly get an invalid operation opengl error.
GeneralRe: Opengl errormembertdziki11-Jan-07 10:25 

This is because current rendering context is set to NULL.
Try to implement PreTranslateMessage as showed below.
Now OpenGL command works in message handlers;
 
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
CMainFrame* pFrame = static_cast(this);
CPaintDC dc(pFrame->m_hWnd);
dc.wglMakeCurrent(m_hRC);

BOOL res = FrameWindowImpl::PreTranslateMessage(pMsg);

dc.wglMakeCurrent(NULL);

return res;

}
 
TD
GeneralI use my view derived from COpenGL&lt;&gt;, and it works wellmembergu mingqiu13-Sep-04 2:50 
I use my view derived from COpenGL<>, and it works well
QuestionFullscreen/Windowed switch?member_leech_25-May-04 20:01 
How would that be accomplished with WTL and this OpenGL class?
QuestionHow to draw openGl in dialog box use WTLsussAnonymous7-May-04 21:22 
thanks
GeneralSome problems on Win2Kmembercdr8-Oct-01 17:18 
This is a great idea and I'm psyched to get it working.
 
The demo executable does not update the client area. I assume that it should display an animated cube?
 
The source code compiles okay, but subsequently asserts in atlopengl.h line 95. Expression m_hRC. The same binary runs fine on Win98 for what it's worth.
 
Have you tested on Win2K or have I done something dumb? Any suggestions would be appreciated because I think this will be very useful to me.
 

 
- Thanks and regards
Chris Russell

 
---
cdr@encapsule.com
http://www.encapsule.com
novus ordo seclorum

GeneralRe: Some problems on Win2KmemberZilin10-Oct-01 6:26 
I built it under win2k! And I just tried to download the demo executable and run it under win2k, it animated!
 
As the asserts in atlopengl.h line 95, it is necessary, I think it may due to other problem.
 
As I know, it seemed has some problem if you use OpenGL SDK from SGI. I think soem functions like wglCreateContext(), are micosoft-related. so you check your SDK version, use opengl32.lib, glu32.lib, opengl32.dll, glu32.dll from microsoft instead of opengl.lib glu.lib, opengl.dll, glu.dll from SGI.
 

GeneralRe: Some problems on Win2Kmembercdr11-Oct-01 19:17 
Okay - that's fair enough. It would be useful to automatically detect which version of OpenGL is installed and respond accordingly. Or, at least to throw up a dialog that states that the incorrect OpenGL libs are installed and that the control can't initialize. Anyway - thanks.
 
- Chris
 
---
cdr@encapsule.com
http://www.encapsule.com
novus ordo seclorum

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 2 Oct 2001
Article Copyright 2001 by Zilin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid