Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

An OpenGL Wrapper for Win32


December 1998/An OpenGL Wrapper for Win32/Figure 2

Figure 2: Implementations of classes COpenGLCtrl and COpenGLCtrls

// File OGLCtrl.cpp

#include "stdafx.h"
#include "OGLCtrl.h"

COpenGLCtrl::COpenGLCtrl()
            :m_hRC(NULL),m_hDC(NULL)
{
    RegisterOpenGLWindowClass();
}

BOOL COpenGLCtrl::PreCreateWindow(CREATESTRUCT& cs) 
{
    cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
    return CWnd::PreCreateWindow(cs);
}

void COpenGLCtrl::PreSubclassWindow() 
{
   ModifyStyle(0,WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
   ASSERT((GetClassLong(m_hWnd,GCL_STYLE)&CS_PARENTDC)==0);
   InitializeOpenGLWindow();

   CRect Rect;
   GetClientRect(&Rect);

   // If we are subclassing an existing window 
   // we might have missed the WM_SIZE message
   if (!Rect.IsRectEmpty())
      ResizeOpenGLWindow(Rect.Width(),Rect.Height());

   CWnd::PreSubclassWindow();
}

void COpenGLCtrl::OnSize(UINT nType, int cx, int cy) 
{
    CWnd::OnSize(nType, cx, cy);
    if (cx>0 && cy>0)
       ResizeOpenGLWindow(cx,cy);
}

void COpenGLCtrl::OnPaint() 
{
    CPaintDC DC(this);
    RedrawOpenGLWindow();
}   

BOOL COpenGLCtrl::OnEraseBkgnd(CDC* pDC) 
{
    return TRUE; // Don't clear background, OpenGL will
}

void COpenGLCtrl::OnDestroy() 
{
    UninitializeOpenGLWindow();
    CWnd::OnDestroy();
}

void COpenGLCtrl::SetupPixelFormat(HDC hDC)
{
    PIXELFORMATDESCRIPTOR pfd = 
    {
        sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd
        1,                      // version number
        PFD_DRAW_TO_WINDOW      // support window
        | PFD_SUPPORT_OPENGL    // support OpenGL
        | PFD_DOUBLEBUFFER      // double buffered 
        , PFD_TYPE_RGBA,        // RGBA type
        24,                     // 24-bit 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,             // accum bits ignored
        32,                     // 32-bit z-buffer
        0,                      // no stencil buffer
        0,                      // no auxiliary buffer
        PFD_MAIN_PLANE,         // main layer
        0,                      // reserved
        0, 0, 0                 // layer masks ignored
    };

    // Let user change the pfd structure
    CustomizePixelFormat(&pfd);

    // Set the pixel format
    int pixelformat=ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC,pixelformat,&pfd);
}

void COpenGLCtrl::InitializeOpenGLWindow()
{
    // Get the DC and set its pixel format
    m_hDC=::GetDC(m_hWnd);
    SetupPixelFormat(m_hDC);

    // Create the Rendering Context and make it current
    m_hRC=wglCreateContext(m_hDC);
    wglMakeCurrent(m_hDC,m_hRC);

    // Let user do his one-time initialization
    OnPrepareRC();
}

void COpenGLCtrl::UninitializeOpenGLWindow()
{
    // Delete the RC
    if (m_hRC)
    {   
        OnDestroyRC(); // Let derived classes clean up
        wglMakeCurrent(NULL,NULL);
        wglDeleteContext(m_hRC);
        m_hRC=NULL;
    }
    // Release the DC
    if (m_hDC)
    {
        ::ReleaseDC(m_hWnd,m_hDC);
        m_hDC=NULL;
    }
}

void COpenGLCtrl::ResizeOpenGLWindow(int cx, int cy) 
{
    // Let user set his viewport transformation
    OnViewport(cx, cy);
    // Select Projection matrix and initialize it
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    // Get the aspect ratio and set perspective transform
    GLdouble AspectRatio=(GLdouble)cx/(GLdouble)cy;
    OnProjection(AspectRatio);
    // Select ModelView matrix and initialize it
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 
    // Let user set his viewing transformation
    OnViewing();
}

void COpenGLCtrl::RedrawOpenGLWindow() 
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glPushMatrix();
    OnRender(); // Draw your scene
    glPopMatrix();
    glFinish();
    SwapBuffers(m_hDC);
}

//... Other members implement the default scene

//==================================================

COpenGLCtrls::COpenGLCtrls()
{
}

void COpenGLCtrls::InitializeOpenGLWindow()
{
    COpenGLCtrl::InitializeOpenGLWindow();
    wglMakeCurrent(NULL,NULL);
    ::ReleaseDC(m_hWnd,m_hDC);
    m_hDC=NULL;
}

void COpenGLCtrls::UninitializeOpenGLWindow()
{
    m_hDC=::GetDC(m_hWnd);
    wglMakeCurrent(m_hDC,m_hRC);
    COpenGLCtrl::UninitializeOpenGLWindow();
}

void COpenGLCtrls::BeginOpenGLDrawing()
{
    m_hDC=::GetDC(m_hWnd);
    wglMakeCurrent(m_hDC,m_hRC);
}

void COpenGLCtrls::EndOpenGLDrawing()
{
    wglMakeCurrent(NULL,NULL);
    ::ReleaseDC(m_hWnd,m_hDC);
    m_hDC=NULL; 
}

void COpenGLCtrls::ResizeOpenGLWindow(int cx, int cy) 
{
    BeginOpenGLDrawing();
    COpenGLCtrl::ResizeOpenGLWindow(cx,cy);
    EndOpenGLDrawing();
}

void COpenGLCtrls::RedrawOpenGLWindow() 
{
    BeginOpenGLDrawing();
    COpenGLCtrl::RedrawOpenGLWindow();
    EndOpenGLDrawing();
}
//End of File


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.