Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC

Rubber-Banding with OpenGL

Rate me:
Please Sign up or sign in to vote.
4.36/5 (11 votes)
7 Jan 2012CPOL2 min read 109.2K   4.2K   50   15
Rubber-banding with OpenGL - A utility class
Image 1

Introduction

This article shows how we can perform rubber-banding in an OpenGL application.

Background

Rubber-banding is frequently used by drawing programs. The objective is to draw something such as a rectangle, then erase it without disturbing what has already been rendered. The rubber-banding rectangle can then be used for selecting objects. For an OpenGL application, rubber-banding can be achieved by rendering with the logic op enabled and set to XOR mode.

The source code here includes a simple C++ class called jxglTracker. The two main member functions in the class are DrawTrackRect() and Track(). In the DrawTrackRect() function, the logic op is enabled by calling glEnable(GL_COLOR_LOGIC_OP) and the XOR mode is set by calling glLogicOp(GL_XOR). The rubber-banding rectangle is drawn using glRecti().

C++
void jxglTracker::DrawTrackRect(int x1, int y1, int x2, int y2)
{
    CRect rectClient;
    m_pWnd->GetClientRect(&rectClient);
    
    glEnable(GL_COLOR_LOGIC_OP);
    glLogicOp(GL_XOR);
    // drawing different rubber-banding rectangle
    // depending on the mouse movement x-direction
    if(x1 < x2)
    {
        glColor4f(0.0, 0.0, 1.0, 0.5);
    }
    else
    {
        glColor4f(1.0, 0.0, 0.0, 0.5);
    }
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    // OpenGL window coordinates are different from GDI's
    glRecti(x1, rectClient.Height() - y1, x2, 
                rectClient.Height() - y2);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glFlush(); // must flush here
    glDisable(GL_COLOR_LOGIC_OP);
}

In the Track() function, we first set the drawing buffer to the front-buffer instead of the default back-buffer. This is needed because we do not want to disturb what has already been drawn (which could be expensive to redraw) while the rubber-banding rectangle is constantly being drawn and erased. Here, we also set up a convenient projection matrix so the pixels on the window client rectangle corresponds to the OpenGL model coordinate system. The DrawTrackRect() is called in an infinite for loop until WM_LBUTTONUP, WM_RBUTTONDOWN or the ESC WM_KEYDOWN message is received. The Track() function takes CWnd* pWnd and CPoint point as parameters, and is generally called from the WM_LBUTTONDOWN message handler of the client window pWnd.

C++
BOOL jxglTracker::Track(CWnd* pWnd, CPoint point)
{
    m_pWnd = pWnd;
    ASSERT(m_pWnd != 0);
    CRect rectClient;
    m_pWnd->GetClientRect(&rectClient);

    // set drawing mode to front-buffer
    glDrawBuffer(GL_FRONT);

    // set up a convenient projection matrix
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, rectClient.Width(), 0, 
               rectClient.Height(), -1, 1);
    glViewport(-1, -1, rectClient.Width() + 2, 
                       rectClient.Height() + 2);

    if (::GetCapture() != NULL)
    { 
        return FALSE;
    }

    // set mouse capture because we
    // are going to work on this window
    pWnd->SetCapture();
    ASSERT(pWnd == CWnd::GetCapture());
    pWnd->UpdateWindow();

    BOOL bMoved = FALSE;
    CPoint ptOld = point;
    CRect rectOld = CRect(ptOld, ptOld);
    CPoint ptNew;

    BOOL bStop = FALSE;
    for (;;)
    {
        // loop forever until LButtonUp,
        // RButtonDown or ESC keyDown
        MSG msg;
        VERIFY(::GetMessage(&msg, NULL, 0, 0));
    
        if (CWnd::GetCapture() != pWnd)
        {
            break;
        }

        if(msg.message == WM_LBUTTONUP || msg.message == WM_MOUSEMOVE)
        {
            ptNew.x = (int)(short)LOWORD(msg.lParam);
            ptNew.y = (int)(short)HIWORD(msg.lParam);
            m_rect = CRect(ptOld, ptNew);
    
            if (bMoved)
            {
                m_bErased = TRUE;
                DrawTrackRect(&rectOld);
            }
            rectOld = m_rect;
            if (msg.message != WM_LBUTTONUP)
            {
                bMoved = TRUE;
            }
    
            if (msg.message == WM_MOUSEMOVE)
            {
                m_bErased = FALSE;
                DrawTrackRect(&m_rect);
            }
            else
            {
                bStop = TRUE;
                ASSERT(msg.message == WM_LBUTTONUP);
            }
        }
        else if(msg.message == WM_KEYDOWN)
        {
            if (msg.wParam == VK_ESCAPE)
            {
                bStop = TRUE;
            }
        }
        else if(msg.message == WM_RBUTTONDOWN)
        { 
            bStop = TRUE;
        }
        else
        {
            DispatchMessage(&msg);
        }

        if(bStop)
        {
            break;
        }
    
    } // for (;;)

    // release mouse capture
    ReleaseCapture();
    
    if(!m_bErased)
    {
        // do a final erase if needed
        DrawTrackRect(m_rect);
    }

    glPopMatrix();
    // restore drawing mode to back-buffer
    glDrawBuffer(GL_BACK);

    return TRUE;
}

Using the Code

The jxglTracker class can be simply used inside the WM_LBUTTONDOWN message handler like shown below:

C++
void COglRubberBandView::OnLButtonDown(UINT nFlags, CPoint point) 
{
    CPaintDC dc(this); // device context for painting
    wglMakeCurrent(dc.m_hDC, m_hRC);

    jxglTracker tracker;
    tracker.Track(this, point);

    CView::OnLButtonDown(nFlags, point);
}

Points of Interest

An MDI MFC-OpenGL application (oglRubberBand) is used to test the jxglTracker rubber-banding class. This application is generated by the MFC AppWizard (accepting default settings) using VC++ 6.0. It is beyond the scope of this article to explain the details of setting up OpenGL. The main logic is contained in the view class (COglRubberBandView) and should be pretty easy to follow. Of course, the jxglTracker.h and jxglTracker.cpp files are added to the project. OpenGL libraries are linked through #pragma comment(lib,"opengl32.lib") etc. in the stdafx.h file.

Depending on the graphics card speed of your system, you can change the number of geometry entities to draw, as shown below. Notice that the drawing speed of the rubber-banding should not be affected by the number of entities already drawn.

C++
void COglRubberBandView::OnPaint() 
{
    //...
    const int nLines = 10000; // let's draw quite a few lines
    //...
}

Happy coding!

History

  • 8th February, 2006: Initial post
  • 16th November, 2009: Article updated - code change that fixed a display bug in Windows 7 environment
  • 6th January, 2012: Article updated - code change that fixed a bug on Vista and Windows 7

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President Computations & Graphics, Inc.
United States United States
Junlin Xu is the founder of Computations & Graphics, Inc. (http://www.cg-inc.com). He is the author of Real3D finite element package, SolverBlaze finite element SDK, OpenGraph Library (OpenGL-based visualization and charting SDK for native and .NET environment), and double128 SDK (quad precision floating point math for C++ and .NET).

Junlin has 20+ years software development experiences in various industries. He has skills in Windows desktop and web application development using C++, C++/CLI, C#, Qt, MFC, STL, OpenGL, GLSL, COM/COM+, WinForms, MS SQL, MySQL, ASP.NET and .NET Core, CSS, jQuery and jQuery UI, Autodesk Revit API, Inno Setup. He is also an expert in mathematical, CAD and algorithmic software development.

Comments and Discussions

 
QuestionRubber rectangle is nearly invisible in light backgroundcolor Pin
egom18-Jul-21 17:04
egom18-Jul-21 17:04 
Praiseexcellent! Pin
Southmountain13-Mar-21 10:33
Southmountain13-Mar-21 10:33 
QuestionIs it possible to use it undecorated window in Linux? Pin
whatnextkenn21-Jul-14 21:45
whatnextkenn21-Jul-14 21:45 
AnswerRe: Is it possible to use it undecorated window in Linux? Pin
Junlin Xu14-Nov-15 17:25
Junlin Xu14-Nov-15 17:25 
GeneralOn Vista Pin
yjpengpeng7-Aug-08 5:25
yjpengpeng7-Aug-08 5:25 
GeneralRe: On Vista Pin
kemoon3-Nov-08 4:12
kemoon3-Nov-08 4:12 
GeneralRe: On Vista Pin
Peter Peng ROC15-Nov-08 21:34
Peter Peng ROC15-Nov-08 21:34 
GeneralRe: On Vista PinPopular
Junlin Xu4-Nov-09 14:56
Junlin Xu4-Nov-09 14:56 
GeneralCouple of issues with this code Pin
terapixel10-Oct-06 6:15
terapixel10-Oct-06 6:15 
GeneralRe: Couple of issues with this code [modified] Pin
Junlin Xu8-Mar-07 17:03
Junlin Xu8-Mar-07 17:03 
GeneralRe: Couple of issues with this code Pin
baoyibao12-Aug-09 17:12
baoyibao12-Aug-09 17:12 
GeneralRe: Couple of issues with this code Pin
Junlin Xu4-Nov-09 14:47
Junlin Xu4-Nov-09 14:47 
GeneralRe: Couple of issues with this code Pin
Le Roy9-Nov-11 2:28
Le Roy9-Nov-11 2:28 
How may I detect, that GL_LOGIC_OP is not supported by graphics card?
GeneralRe: Couple of issues with this code Pin
Junlin Xu10-Nov-11 13:37
Junlin Xu10-Nov-11 13:37 
GeneralDo you have this sample in VB6? Pin
Mr.CAD11-Nov-11 3:09
Mr.CAD11-Nov-11 3:09 

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.