00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #include "OgreStableHeaders.h" 00026 #include "OgreWireBoundingBox.h" 00027 00028 #include "OgreSimpleRenderable.h" 00029 #include "OgreHardwareBufferManager.h" 00030 #include "OgreCamera.h" 00031 00032 namespace Ogre { 00033 #define POSITION_BINDING 0 00034 00035 WireBoundingBox::WireBoundingBox() 00036 { 00037 mRenderOp.vertexData = new VertexData(); 00038 00039 mRenderOp.indexData = 0; 00040 mRenderOp.vertexData->vertexCount = 24; 00041 mRenderOp.vertexData->vertexStart = 0; 00042 mRenderOp.operationType = RenderOperation::OT_LINE_LIST; 00043 mRenderOp.useIndexes = false; 00044 00045 VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration; 00046 VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding; 00047 00048 decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION); 00049 00050 00051 HardwareVertexBufferSharedPtr vbuf = 00052 HardwareBufferManager::getSingleton().createVertexBuffer( 00053 decl->getVertexSize(POSITION_BINDING), 00054 mRenderOp.vertexData->vertexCount, 00055 HardwareBuffer::HBU_STATIC_WRITE_ONLY); 00056 00057 // Bind buffer 00058 bind->setBinding(POSITION_BINDING, vbuf); 00059 00060 // set basic white material 00061 this->setMaterial("BaseWhiteNoLighting"); 00062 00063 00064 00065 } 00066 00067 WireBoundingBox::~WireBoundingBox() 00068 { 00069 delete mRenderOp.vertexData; 00070 } 00071 00072 void WireBoundingBox::setupBoundingBox(const AxisAlignedBox& aabb) 00073 { 00074 // init the vertices to the aabb 00075 setupBoundingBoxVertices(aabb); 00076 00077 // setup the bounding box of this SimpleRenderable 00078 setBoundingBox(aabb); 00079 00080 } 00081 00082 // Override this method to prevent parent transforms (rotation,translation,scale) 00083 void WireBoundingBox::getWorldTransforms( Matrix4* xform ) const 00084 { 00085 // return identity matrix to prevent parent transforms 00086 *xform = Matrix4::IDENTITY; 00087 } 00088 //----------------------------------------------------------------------- 00089 const Quaternion& WireBoundingBox::getWorldOrientation(void) const 00090 { 00091 return Quaternion::IDENTITY; 00092 } 00093 //----------------------------------------------------------------------- 00094 const Vector3& WireBoundingBox::getWorldPosition(void) const 00095 { 00096 return Vector3::ZERO; 00097 } 00098 00099 00100 void WireBoundingBox::setupBoundingBoxVertices(const AxisAlignedBox& aab) { 00101 00102 Vector3 vmax = aab.getMaximum(); 00103 Vector3 vmin = aab.getMinimum(); 00104 00105 Real sqLen = std::max(vmax.squaredLength(), vmin.squaredLength()); 00106 mRadius = Math::Sqrt(sqLen); 00107 00108 // inflate the wire bounding box just a bit so that it will set apart from 00109 // a solid object 00110 Real maxx = vmax.x + 1.0; 00111 Real maxy = vmax.y + 1.0; 00112 Real maxz = vmax.z + 1.0; 00113 00114 Real minx = vmin.x - 1.0; 00115 Real miny = vmin.y - 1.0; 00116 Real minz = vmin.z - 1.0; 00117 00118 // fill in the Vertex buffer: 12 lines with 2 endpoints each make up a box 00119 HardwareVertexBufferSharedPtr vbuf = 00120 mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING); 00121 00122 Real* pPos = static_cast<Real*>( 00123 vbuf->lock(HardwareBuffer::HBL_DISCARD)); 00124 00125 // line 0 00126 *pPos++ = minx; 00127 *pPos++ = miny; 00128 *pPos++ = minz; 00129 *pPos++ = maxx; 00130 *pPos++ = miny; 00131 *pPos++ = minz; 00132 // line 1 00133 *pPos++ = minx; 00134 *pPos++ = miny; 00135 *pPos++ = minz; 00136 *pPos++ = minx; 00137 *pPos++ = miny; 00138 *pPos++ = maxz; 00139 // line 2 00140 *pPos++ = minx; 00141 *pPos++ = miny; 00142 *pPos++ = minz; 00143 *pPos++ = minx; 00144 *pPos++ = maxy; 00145 *pPos++ = minz; 00146 // line 3 00147 *pPos++ = minx; 00148 *pPos++ = maxy; 00149 *pPos++ = minz; 00150 *pPos++ = minx; 00151 *pPos++ = maxy; 00152 *pPos++ = maxz; 00153 // line 4 00154 *pPos++ = minx; 00155 *pPos++ = maxy; 00156 *pPos++ = minz; 00157 *pPos++ = maxx; 00158 *pPos++ = maxy; 00159 *pPos++ = minz; 00160 // line 5 00161 *pPos++ = maxx; 00162 *pPos++ = miny; 00163 *pPos++ = minz; 00164 *pPos++ = maxx; 00165 *pPos++ = miny; 00166 *pPos++ = maxz; 00167 // line 6 00168 *pPos++ = maxx; 00169 *pPos++ = miny; 00170 *pPos++ = minz; 00171 *pPos++ = maxx; 00172 *pPos++ = maxy; 00173 *pPos++ = minz; 00174 // line 7 00175 *pPos++ = minx; 00176 *pPos++ = maxy; 00177 *pPos++ = maxz; 00178 *pPos++ = maxx; 00179 *pPos++ = maxy; 00180 *pPos++ = maxz; 00181 // line 8 00182 *pPos++ = minx; 00183 *pPos++ = maxy; 00184 *pPos++ = maxz; 00185 *pPos++ = minx; 00186 *pPos++ = miny; 00187 *pPos++ = maxz; 00188 // line 9 00189 *pPos++ = maxx; 00190 *pPos++ = maxy; 00191 *pPos++ = minz; 00192 *pPos++ = maxx; 00193 *pPos++ = maxy; 00194 *pPos++ = maxz; 00195 // line 10 00196 *pPos++ = maxx; 00197 *pPos++ = miny; 00198 *pPos++ = maxz; 00199 *pPos++ = maxx; 00200 *pPos++ = maxy; 00201 *pPos++ = maxz; 00202 // line 11 00203 *pPos++ = minx; 00204 *pPos++ = miny; 00205 *pPos++ = maxz; 00206 *pPos++ = maxx; 00207 *pPos++ = miny; 00208 *pPos++ = maxz; 00209 vbuf->unlock(); 00210 } 00211 00212 00213 Real WireBoundingBox::getSquaredViewDepth(const Camera* cam) const 00214 { 00215 Vector3 min, max, mid, dist; 00216 min = mBox.getMinimum(); 00217 max = mBox.getMaximum(); 00218 mid = ((min - max) * 0.5) + min; 00219 dist = cam->getDerivedPosition() - mid; 00220 00221 00222 return dist.squaredLength(); 00223 } 00224 00225 00226 00227 } 00228
Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:32 2004