# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/Viewport.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #include <vlGraphics/Viewport.hpp>
    00033 #include <vlGraphics/OpenGL.hpp>
    00034 #include <vlCore/Log.hpp>
    00035 #include <vlCore/Say.hpp>
    00036
    00037 using namespace vl;
    00038
    00039 //-----------------------------------------------------------------------------
    00040 // Viewport
    00041 //-----------------------------------------------------------------------------
    00042 Viewport::Viewport()
    00043 {
    00044   VL_DEBUG_SET_OBJECT_NAME()
    00045   mX = 0;
    00046   mY = 0;
    00047   mWidth = 0;
    00048   mHeight = 0;
    00049   mClearColor = fvec4(0.8f,0,0.1f,1);
    00050   mClearDepth = 1.0f;
    00051   mClearStencil = 0;
    00052   mClearFlags = CF_CLEAR_COLOR_DEPTH;
    00053   mClearColorMode = CCM_Float;
    00054 }
    00055 //-----------------------------------------------------------------------------
    00056 Viewport::Viewport(int x, int y, int w, int h)
    00057 {
    00058   VL_DEBUG_SET_OBJECT_NAME()
    00059   mX = x;
    00060   mY = y;
    00061   mWidth  = w;
    00062   mHeight = h;
    00063   mClearColor = fvec4(0.8f,0,0.1f,1);
    00064   mClearDepth = 1.0f;
    00065   mClearStencil = 0;
    00066   mClearFlags = CF_CLEAR_COLOR_DEPTH;
    00067   mClearColorMode = CCM_Float;
    00068 }
    00069 //-----------------------------------------------------------------------------
    00070 void Viewport::activate() const
    00071 {
    00072   VL_CHECK_OGL()
    00073
    00074   // viewport
    00075   int x = mX;
    00076   int y = mY;
    00077   int w = mWidth;
    00078   int h = mHeight;
    00079
    00080   if (w < 1) w = 1;
    00081   if (h < 1) h = 1;
    00082
    00083   glViewport(x, y, w, h);
    00084
    00085   // clear viewport
    00086   if (mClearFlags)
    00087   {
    00088     #ifndef NDEBUG
    00089       if (!GLEW_EXT_texture_integer)
    00090       {
    00091         switch( clearColorMode() )
    00092         {
    00093         case CCM_Int:
    00094         case CCM_UInt:
    00095           Log::bug("Viewport::activate(): GL_EXT_texture_integer not supported.\n");
    00096           break;
    00097         default:
    00098           break;
    00099         }
    00100       }
    00101     #endif
    00102
    00103     // this are deliberately all initialized to true
    00104     glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
    00105     glDepthMask(GL_TRUE);
    00106     glStencilMask(GL_TRUE);
    00107
    00108     // save scissor settings
    00109     GLboolean scissor_on = glIsEnabled(GL_SCISSOR_TEST);
    00110     int scissor_box[4] = {0,0,-1,-1};
    00111     glGetIntegerv(GL_SCISSOR_BOX, scissor_box);
    00112
    00113     // setup scissor
    00114     glScissor(x, y, w, h);
    00115     glEnable(GL_SCISSOR_TEST);
    00116
    00117     switch( clearColorMode() )
    00118     {
    00119       case CCM_Float: glClearColor(        mClearColor.r(),     mClearColor.g(),     mClearColor.b(),     mClearColor.a()    );  break;
    00120       case CCM_Int:   glClearColorIiEXT(   mClearColorInt.r(),  mClearColorInt.g(),  mClearColorInt.b(),  mClearColorInt.a() );  break;
    00121       case CCM_UInt:  glClearColorIuiEXT(  mClearColorUInt.r(), mClearColorUInt.g(), mClearColorUInt.b(), mClearColorUInt.a() ); break;
    00122     }
    00123
    00124     glClearDepth( mClearDepth );
    00125     glClearStencil( mClearStencil );
    00126
    00127     glClear(mClearFlags);
    00128
    00129     // restore scissor settings
    00130     if (!scissor_on)
    00131       glDisable(GL_SCISSOR_TEST);
    00132     glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]); VL_CHECK_OGL()
    00133
    00134     VL_CHECK_OGL()
    00135   }
    00136 }
    00137 //-----------------------------------------------------------------------------
    00138 bool Viewport::isPointInside(int x, int y, int render_target_height) const
    00139 {
    00140   // set x/y relative to the viewport
    00141   x -= this->x();
    00142   y -= render_target_height - 1 - (this->y() + height() -1);
    00143
    00144   // check that the click is in the viewport
    00145
    00146   if (x<0 || y<0 || x>=this->width() || y>=this->height())
    00147     return false;
    00148   else
    00149     return true;
    00150 }
    00151 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:25.
Permission is granted to use this page to write and publish articles regarding Visualization Library.