Click here to Skip to main content
15,867,488 members
Articles / Multimedia / OpenGL
Article

OGLTools, A OOP solution to OpenGL programming

Rate me:
Please Sign up or sign in to vote.
4.43/5 (12 votes)
15 Jul 20023 min read 118.8K   3.5K   50   11
A set of classes designed to make programming of OpenGL easier

Sample Image - ogltools.jpg

Introduction

This article presents a set of helper classes that encapsulates OpenGL handling and really makes the programmer's life easier (and happier).

This piece of code was originally been written by W.Weyna 'Voytec'. All I did was to publish it to CodeProject.

Here are the main features of the package:

  • GL context creation and destruction,
  • Double buffering support (of course),
  • Rendering to bitmap (Woww that's nice feature),
  • Export to clipboard, jpeg, bmp, dib,
  • Texture loading and management,

The main class: CWGL

CWGL is a Windows OpenGL rendering interface class. This header defines also a few interfacing wgl_ inlines.

The idea behind the CWGL is to make use of OpenGL in Microsoft Windows as simple as possible.

Under Windows only one OpenGL rendering context may be active in a single thread. CWGL helps you write a single threaded Windows application which need more than one OpenGL rendering context.

Usage:

  • Optionally, set different pixelformat if you are not satisfied with CWGL's default one.
  • Enable OpenGL, telling it where it should render.
    wgl.Begin(&windowOrBitmapDC);
  • Call OpenGL API functions to do the rendering.
  • Disable OpenGL, first optionally showing the rendered image.
    wgl.End();

Examples:

  1. In MFC view, render to window's backbuffer and swap buffers.
    //  CView.h
    
    CWGL m_wgl;
    //  CView.cpp
    CView::OnDraw(pDC)
    {
        m_wgl.Begin(pDC);
        glClearColor(1.0, 1.0, 1.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        m_wgl.End();
    }
  2. Render on DIB section GDI object and copy it to clipboard.
    CRGBSurface tmpSurf;    // CRGBSurface encapsulates a DIB section GDI object
    tmpSurf.Create(10, 10);
    
    CWGL wgl;
    wgl.Begin(tmpSurf.GetDC());
    glClearColor(1.0, 1.0, 1.0, 0.0)
        glClear(GL_COLOR_BUFFER_BIT);
    wgl.End();
    
    tmpSurf.CopyToClipboard();
  3. Rendering contexts may be 'nested' if different CWGL objects are used, but remember that only one RC can be created for one window.
    wgl1.Begin(&windowDC);
    {
        CWGL wgl2;
        wgl2.Begin(&bitmapDC);
        wgl2.End();
        // here wgl2 destructor delete's wgl2 RC and makes
        // wgl1 RC current again.
    }
    // here all the display lists and textures of wgl1 RC
    // remain valid
    wgl1.End(); 
  4. If you need an RC for a window for which you no longer have a device context available, you may call wgl.Begin() with no DC to make last used RC of this window current again.
    wgl.Begin(&windowDC);
    wgl.End(); 
    ...
    
    // some time in the future...
    wgl.Begin();
    wgl.End();
  5. After a call to CWGL::End() you may ask what the rendering time was with GetRenderingTime().

Notes:

When rendering on different DIB sections or bitmaps, a new RC with PFD_DRAW_TO_BITMAP pixelformat is always created for that bitmap.

WindowsNT: CWGL synchronizes GDI and OpenGL access to rendering surface automatically.

When switching RC's of different pixelformats with CWGL, all textures and display lists must be recreated in a new RC. Display list sharing is possible when RCs are of the same pixelformat (this means also that you cannot share between window and bitmap RC).

Please also note that under Windows, only one RC may be created for a given window and this RC cannot be used for another window.

Working with textures:

CGLTexture is a simple wrapper to OpenGL texture object. It enables easy changing of texture images, reusing existing texture object when possible.

  • Call ChangeImage to create/recreate/reuse a texture and copy CGLImage image to the texture object.
  • Use Bind() to bind a texture.
  • Use Invalidate() before switching to a new RC. If Invalidate() is called and then Bind() is called in a new RC, the texture will be automatically recreated using a backup copy of the image.
  • If you are not going to use the same texture in multiple RC's, call ChangeImage() with second parameter set to false so it won't make unnecessary backup copy of the image.
    if(bChangeImage)
    {
        // Handle a request to use a different image 
        // for texture.
    
        bChangeImage = false;
        CGLImage glImage;
        glImage.Create(m_strNewImageFilePath, GL_RGBA);
        m_tex1.ChangeImage(&glImage, false); 
    }
    
    m_tex1.Bind();  
  • Use Coordn() function to generate texture coordinates which point to the four corners of current subimage inside a possibly larger texture.
    glBegin(GL_QUADS);
    m_tex1.Coord0(); glVertex3f(-5.0, -5.0, 0.0);
    m_tex1.Coord1(); glVertex3f(-5.0, 5.0, 0.0);
    m_tex1.Coord2(); glVertex3f(5.0, 5.0, 0.0);
    m_tex1.Coord3(); glVertex3f(5.0, -5.0, 0.0);
    glEnd();
    
    glFlush();
    glDisable(GL_TEXTURE_2D);

More examples and Doc

A documentation of the classes has been generated and is available with the source code distribution.

Using the library in your app

  1. Include the OGLTools header in your StdAfx.h:
    #include "OGLT.h"
  2. Make sure the .h and .lib are available, if not add the corresponding directories to the project settings.
  3. That's it, the lib's will be automatically inserted.

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


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions

 
Generalusing OpenGL to read, save,.. file .bmp Pin
1vs18-Apr-05 19:55
1vs18-Apr-05 19:55 
GeneralProblem encountered with CGLImage Pin
Allem779-Sep-04 21:46
Allem779-Sep-04 21:46 
Hello!

I downloaded and tried this sw. Then I wanted to put a texture to the cube. But as soon as I define a CGLImage object I get the following error from the linker. Has somebody a clue? I use VC++ 6.0 under Winnt 4.0. The Tool seems to be really great! Would be a pitty if I couldn't use it Blush | :O )

Tanks for your help!

Allem77


--------------------Konfiguration: OGLDemo - Win32 Debug--------------------
Kompilierung läuft...
StdAfx.cpp
_Adding library: Jpegmt.lib, OpenGL32.lib, glu32.lib
_Adding library: OGLToolsd.lib (Debug version)
Kompilierung läuft...
ChildFrm.cpp
MainFrm.cpp
OGLDemo.cpp
OGLDemoDoc.cpp
OGLDemoView.cpp
Generieren von Code...
Linker-Vorgang läuft...
libcmt.lib(crt0dat.obj) : error LNK2005: _exit bereits in msvcrtd.lib(MSVCRTD.dll) definiert
libcmt.lib(crt0dat.obj) : error LNK2005: __exit bereits in msvcrtd.lib(MSVCRTD.dll) definiert
libcmt.lib(strcat.obj) : error LNK2005: _strcpy bereits in msvcrtd.lib(MSVCRTD.dll) definiert
libcmt.lib(crt0init.obj) : error LNK2005: ___xc_z bereits in msvcrtd.lib(cinitexe.obj) definiert
libcmt.lib(crt0init.obj) : error LNK2005: ___xc_a bereits in msvcrtd.lib(cinitexe.obj) definiert
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_z bereits in msvcrtd.lib(cinitexe.obj) definiert
libcmt.lib(crt0init.obj) : error LNK2005: ___xi_a bereits in msvcrtd.lib(cinitexe.obj) definiert
libcmt.lib(winxfltr.obj) : error LNK2005: __XcptFilter bereits in msvcrtd.lib(MSVCRTD.dll) definiert
libcmt.lib(fclose.obj) : error LNK2005: _fclose bereits in msvcrtd.lib(MSVCRTD.dll) definiert
msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __setmbcp bereits in libcmt.lib(mbctype.obj) definiert
LINK : warning LNK4098: Standardbibliothek "msvcrtd.lib" steht in Konflikt mit anderen Bibliotheken; /NODEFAULT:Bibliothek verwenden
msvcrtd.lib(cinitexe.obj) : warning LNK4098: Standardbibliothek "libcmt.lib" steht in Konflikt mit anderen Bibliotheken; /NODEFAULT:Bibliothek verwenden
libcmt.lib(crt0.obj) : error LNK2001: Nichtaufgeloestes externes Symbol _main
Debug/OGLDemo.exe : fatal error LNK1120: 1 unaufgeloeste externe Verweise
Fehler beim Ausführen von link.exe.

OGLDemo.exe - 12 Fehler, 2 Warnung(en)


Allem77
GeneralRe: Problem encountered with CGLImage Pin
Ian Miller7-Jul-05 4:12
Ian Miller7-Jul-05 4:12 
GeneralRe: Problem encountered with CGLImage Pin
MasterGohan9-May-06 10:58
MasterGohan9-May-06 10:58 
QuestionHow to run 2 different(or same) OpenGL objects in one DialogBox? Pin
werter15-Jun-04 21:53
werter15-Jun-04 21:53 
GeneralAbout FullScreen Pin
int29-Nov-03 3:53
int29-Nov-03 3:53 
GeneralUsing OGTL with dialog items Pin
Zeruel10-Jan-03 7:26
Zeruel10-Jan-03 7:26 
GeneralRe: Using OGTL with dialog items Pin
Jonathan de Halleux13-Jan-03 21:32
Jonathan de Halleux13-Jan-03 21:32 
GeneralHelp Pin
Ali Khanlarkhani19-Dec-02 1:47
Ali Khanlarkhani19-Dec-02 1:47 
GeneralRe: Help Pin
Jonathan de Halleux19-Dec-02 1:56
Jonathan de Halleux19-Dec-02 1:56 
GeneralRe: Help Pin
Ali Khanlarkhani21-Dec-02 0:33
Ali Khanlarkhani21-Dec-02 0:33 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.