/*****************************************************************************
 * $LastChangedDate: 2009-11-22 22:39:11 -0500 (Sun, 22 Nov 2009) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   OpenGL abstraction.
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define GFX_GFX_CC 1
#include "base/module.hh"
using namespace base;
#include "view/module.hh"
#include "view/window.hh"
using namespace view;
#include "gfx/module.hh"
#include "gfx/gfx.hh"
#include "gfx/graphics.hh"

namespace gfx {

////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////  GFX  ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * Begin/end 2D drawing mode.
 *****************************************************************************/
void
GFX::Begin2D( const Rect<int>& rect )
{
ASSERT( not rect.IfZero() );
    glLineWidth( 1.0f );
    glDisable( GL_DEPTH_TEST );
    glDisable( GL_LIGHTING );
    glDisable( GL_TEXTURE_2D );
    glEnable( GL_LINE_SMOOTH );
    glMatrixMode( GL_TEXTURE    ); glLoadIdentity();
    glMatrixMode( GL_PROJECTION ); glLoadIdentity();
    glMatrixMode( GL_MODELVIEW  ); glLoadIdentity();
    gluOrtho2D( rect.x1, rect.x2,
                rect.y1, rect.y2 );  // after the above
    glMatrixMode( GL_MODELVIEW  );
    glViewport( 0, 0, rect.x2, rect.y2 );
}

// Window's rectangle implied.
void
GFX::Begin2D( void )
{
    GFX::Begin2D( GET_WINDOW().GetWindowRect() );
}

void
GFX::End2D( void )
{
    // NOP.  OSG will undo the mode changes.
}

/*****************************************************************************
 * Draw 2D box.
 *****************************************************************************/
void
GFX::DrawBox( const Rect<int>& rect, const RGBA color, const bool drawX )
{
    // Set color.
    GLfloat color4f[4];
    color.Get4f( color4f );
    glColor4fv( color4f );  // GLfloat[4]

    // Each pair forms a separate line.
    GFX::Begin2D();
        glBegin( GL_LINES );
            GFX::Vertex2D( rect.x1, rect.y1 ); GFX::Vertex2D( rect.x2, rect.y1 );  // horz
            GFX::Vertex2D( rect.x1, rect.y2 ); GFX::Vertex2D( rect.x2, rect.y2 );  // horz
            GFX::Vertex2D( rect.x1, rect.y1 ); GFX::Vertex2D( rect.x1, rect.y2 );  // vert
            GFX::Vertex2D( rect.x2, rect.y1 ); GFX::Vertex2D( rect.x2, rect.y2 );  // vert
            // Draw X over box.
            if ( drawX )
            {
                GFX::Vertex2D( rect.x1, rect.y1 ); GFX::Vertex2D( rect.x2, rect.y2 );
                GFX::Vertex2D( rect.x2, rect.y1 ); GFX::Vertex2D( rect.x1, rect.y2 );
            }
        glEnd();
    GFX::End2D();
}

} // namespace gfx
