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/Clear.hpp> 00033 #include <vlGraphics/Camera.hpp> 00034 #include <vlCore/Vector4.hpp> 00035 #include <vlCore/Log.hpp> 00036 00037 using namespace vl; 00038 00039 //----------------------------------------------------------------------------- 00040 Clear::Clear(): mClearColorMode(CCM_Float), mClearDepthValue(1.0f), mClearStencilValue(0), 00041 mClearColorBuffer(false), mClearDepthBuffer(false), mClearStencilBuffer(false) 00042 { 00043 VL_DEBUG_SET_OBJECT_NAME() 00044 // no scissor box by default 00045 mScissorBox[0] = 0; 00046 mScissorBox[1] = 0; 00047 mScissorBox[2] = -1; 00048 mScissorBox[3] = -1; 00049 } 00050 //----------------------------------------------------------------------------- 00051 void Clear::render_Implementation(const Actor*, const Shader*, const Camera* camera, OpenGLContext*) const 00052 { 00053 // build buffer bit mask 00054 GLbitfield mask = 0; 00055 mask = mask | (mClearColorBuffer ? GL_COLOR_BUFFER_BIT : 0); 00056 mask = mask | (mClearDepthBuffer ? GL_DEPTH_BUFFER_BIT : 0); 00057 mask = mask | (mClearStencilBuffer ? GL_STENCIL_BUFFER_BIT : 0); 00058 00059 // check for integer texture support 00060 if ( (clearColorMode() == CCM_Int || clearColorMode() == CCM_UInt) && !Has_GL_EXT_texture_integer) 00061 { 00062 Log::error("Clear::render(): glClearColorIiEXT and glClearColorIuiEXT not supported.\n"); 00063 return; 00064 } 00065 00066 if (mask) 00067 { 00068 int viewport[] = { camera->viewport()->x(), camera->viewport()->y(), camera->viewport()->width(), camera->viewport()->height() }; 00069 00070 // save scissor settings 00071 GLboolean scissor_on = glIsEnabled(GL_SCISSOR_TEST); 00072 int scissor_box_save[4] = {0,0,-1,-1}; 00073 glGetIntegerv(GL_SCISSOR_BOX, scissor_box_save); 00074 00075 int scissor_box[4] = {0,0,-1,-1}; 00076 00077 // fit to the viewport 00078 if (mScissorBox[2] == -1 || mScissorBox[3] == -1) 00079 { 00080 scissor_box[0] = viewport[0]; 00081 scissor_box[1] = viewport[1]; 00082 scissor_box[2] = viewport[2]; 00083 scissor_box[3] = viewport[3]; 00084 } 00085 else 00086 // scissor box in viewport coords 00087 { 00088 scissor_box[0] = mScissorBox[0] + viewport[0]; 00089 scissor_box[1] = mScissorBox[1] + viewport[1]; 00090 scissor_box[2] = mScissorBox[2]; 00091 scissor_box[3] = mScissorBox[3]; 00092 } 00093 00094 // viewport from x,y,w,h -> x,y,x2,y2 00095 viewport[2] = viewport[0] + viewport[2] -1; 00096 viewport[3] = viewport[1] + viewport[3] -1; 00097 // scissor_box from x,y,w,h -> x,y,x2,y2 00098 scissor_box[2] = scissor_box[0] + scissor_box[2] -1; 00099 scissor_box[3] = scissor_box[1] + scissor_box[3] -1; 00100 // clip scissor box 00101 if (scissor_box[0] < viewport[0]) scissor_box[0] = viewport[0]; 00102 if (scissor_box[1] < viewport[1]) scissor_box[1] = viewport[1]; 00103 if (scissor_box[2] > viewport[2]) scissor_box[2] = viewport[2]; 00104 if (scissor_box[3] > viewport[3]) scissor_box[3] = viewport[3]; 00105 // culling 00106 if (scissor_box[0] > scissor_box[2]) 00107 return; 00108 if (scissor_box[1] > scissor_box[3]) 00109 return; 00110 // scissor_box from x,y,x2,y2 -> x,y,w,h 00111 scissor_box[2] = scissor_box[2] -scissor_box[0] +1; 00112 scissor_box[3] = scissor_box[3] -scissor_box[1] +1; 00113 00114 // enable scissor test 00115 glEnable(GL_SCISSOR_TEST); 00116 glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]); VL_CHECK_OGL() 00117 00118 // defines the clear values 00119 if (mClearColorBuffer) 00120 { 00121 switch(clearColorMode()) 00122 { 00123 case CCM_Float: glClearColor( mClearColorValue.r(), mClearColorValue.g(), mClearColorValue.b(), mClearColorValue.a()); break; 00124 case CCM_Int: glClearColorIiEXT( mClearColorValueInt.r(), mClearColorValueInt.g(), mClearColorValueInt.b(), mClearColorValueInt.a()); break; 00125 case CCM_UInt: glClearColorIuiEXT(mClearColorValueUInt.r(), mClearColorValueUInt.g(), mClearColorValueUInt.b(), mClearColorValueUInt.a()); break; 00126 } 00127 } 00128 if (mClearDepthBuffer) 00129 glClearDepth(mClearDepthValue); 00130 if (mClearStencilBuffer) 00131 glClearStencil(mClearStencilValue); 00132 00133 // clear! 00134 glClear(mask); 00135 00136 // restore scissor settings 00137 if (!scissor_on) 00138 glDisable(GL_SCISSOR_TEST); 00139 glScissor(scissor_box_save[0], scissor_box_save[1], scissor_box_save[2], scissor_box_save[3]); VL_CHECK_OGL() 00140 } 00141 } 00142 //-----------------------------------------------------------------------------