Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://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 mSetupScissor = true; 00055 } 00056 //----------------------------------------------------------------------------- 00057 Viewport::Viewport(int x, int y, int w, int h) 00058 { 00059 VL_DEBUG_SET_OBJECT_NAME() 00060 mX = x; 00061 mY = y; 00062 mWidth = w; 00063 mHeight = h; 00064 mClearColor = fvec4(0.8f,0,0.1f,1); 00065 mClearDepth = 1.0f; 00066 mClearStencil = 0; 00067 mClearFlags = CF_CLEAR_COLOR_DEPTH; 00068 mClearColorMode = CCM_Float; 00069 mSetupScissor = true; 00070 } 00071 //----------------------------------------------------------------------------- 00072 void Viewport::activate() const 00073 { 00074 VL_CHECK_OGL() 00075 00076 // viewport 00077 int x = mX; 00078 int y = mY; 00079 int w = mWidth; 00080 int h = mHeight; 00081 00082 if (w < 1) w = 1; 00083 if (h < 1) h = 1; 00084 00085 glViewport(x, y, w, h); 00086 00087 // clear viewport 00088 if (mClearFlags) 00089 { 00090 #ifndef NDEBUG 00091 if (!Has_GL_EXT_texture_integer) 00092 { 00093 switch( clearColorMode() ) 00094 { 00095 case CCM_Int: 00096 case CCM_UInt: 00097 Log::bug("Viewport::activate(): GL_EXT_texture_integer not supported.\n"); 00098 break; 00099 default: 00100 break; 00101 } 00102 } 00103 #endif 00104 00105 // save writemask status to be restored later 00106 GLboolean color_write_mask[4] = {0,0,0,0}; 00107 glGetBooleanv(GL_COLOR_WRITEMASK, color_write_mask); 00108 00109 GLboolean depth_write_mask = 0; 00110 glGetBooleanv(GL_DEPTH_WRITEMASK, &depth_write_mask ); 00111 00112 GLboolean stencil_write_mask = 0; 00113 glGetBooleanv(GL_STENCIL_WRITEMASK, &stencil_write_mask ); 00114 00115 // make sure we clear everything 00116 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 00117 glDepthMask(GL_TRUE); 00118 glStencilMask(GL_TRUE); 00119 00120 // setup scissor 00121 if (mSetupScissor) 00122 { 00123 glEnable(GL_SCISSOR_TEST); 00124 glScissor(x, y, w, h); 00125 } 00126 else { 00127 glDisable(GL_SCISSOR_TEST); 00128 } 00129 00130 switch( clearColorMode() ) 00131 { 00132 case CCM_Float: glClearColor( mClearColor.r(), mClearColor.g(), mClearColor.b(), mClearColor.a() ); break; 00133 case CCM_Int: glClearColorIiEXT( mClearColorInt.r(), mClearColorInt.g(), mClearColorInt.b(), mClearColorInt.a() ); break; 00134 case CCM_UInt: glClearColorIuiEXT( mClearColorUInt.r(), mClearColorUInt.g(), mClearColorUInt.b(), mClearColorUInt.a() ); break; 00135 } 00136 00137 glClearDepth( mClearDepth ); 00138 00139 glClearStencil( mClearStencil ); 00140 00141 glClear(mClearFlags); 00142 00143 // restore writemasks 00144 glColorMask(color_write_mask[0], color_write_mask[1], color_write_mask[2], color_write_mask[3]); 00145 glDepthMask(depth_write_mask); 00146 glStencilMask(stencil_write_mask); 00147 00148 VL_CHECK_OGL() 00149 } 00150 } 00151 //----------------------------------------------------------------------------- 00152 bool Viewport::isPointInside(int x, int y, int framebuffer_height) const 00153 { 00154 // set x/y relative to the viewport 00155 x -= this->x(); 00156 y -= framebuffer_height - 1 - (this->y() + height() -1); 00157 00158 // check that the click is in the viewport 00159 00160 if (x<0 || y<0 || x>=this->width() || y>=this->height()) 00161 return false; 00162 else 00163 return true; 00164 } 00165 //----------------------------------------------------------------------------- 00166 void Viewport::enableScissorSetup(bool enable) 00167 { 00168 mSetupScissor = enable; 00169 } 00170 //----------------------------------------------------------------------------- 00171