Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/Viewport.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
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,0,0,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 (!Has_GL_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     // save writemask status to be restored later
00104     GLboolean color_write_mask[4] = {0,0,0,0};
00105     glGetBooleanv(GL_COLOR_WRITEMASK, color_write_mask);
00106 
00107     GLboolean depth_write_mask = 0;
00108     glGetBooleanv(GL_DEPTH_WRITEMASK, &depth_write_mask );
00109 
00110     GLboolean stencil_write_mask = 0;
00111     glGetBooleanv(GL_STENCIL_WRITEMASK, &stencil_write_mask );
00112 
00113     // make sure we clear everything
00114     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
00115     glDepthMask(GL_TRUE);
00116     glStencilMask(GL_TRUE);
00117 
00118     // setup scissor
00119     glEnable(GL_SCISSOR_TEST);
00120     glScissor(x, y, w, h);
00121 
00122     switch( clearColorMode() )
00123     {
00124       case CCM_Float: glClearColor(        mClearColor.r(),     mClearColor.g(),     mClearColor.b(),     mClearColor.a()    );  break;
00125       case CCM_Int:   glClearColorIiEXT(   mClearColorInt.r(),  mClearColorInt.g(),  mClearColorInt.b(),  mClearColorInt.a() );  break;
00126       case CCM_UInt:  glClearColorIuiEXT(  mClearColorUInt.r(), mClearColorUInt.g(), mClearColorUInt.b(), mClearColorUInt.a() ); break;
00127     }
00128 
00129     glClearDepth( mClearDepth );
00130 
00131     glClearStencil( mClearStencil );
00132 
00133     glClear(mClearFlags);
00134 
00135     // restore writemasks
00136     glColorMask(color_write_mask[0], color_write_mask[1], color_write_mask[2], color_write_mask[3]);
00137     glDepthMask(depth_write_mask);
00138     glStencilMask(stencil_write_mask);
00139 
00140     VL_CHECK_OGL()
00141   }
00142 }
00143 //-----------------------------------------------------------------------------
00144 bool Viewport::isPointInside(int x, int y, int framebuffer_height) const
00145 {
00146   // set x/y relative to the viewport
00147   x -= this->x();
00148   y -= framebuffer_height - 1 - (this->y() + height() -1);
00149 
00150   // check that the click is in the viewport
00151 
00152   if (x<0 || y<0 || x>=this->width() || y>=this->height())
00153     return false;
00154   else
00155     return true;
00156 }
00157 //-----------------------------------------------------------------------------

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:42.
Permission is granted to use this page to write and publish articles regarding Visualization Library.