[5] | 1 | /* |
---|
| 2 | ----------------------------------------------------------------------------- |
---|
| 3 | This source file is part of OGRE |
---|
| 4 | (Object-oriented Graphics Rendering Engine) |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
| 13 | version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
| 23 | |
---|
| 24 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | #include "OgreStableHeaders.h" |
---|
| 30 | #include "OgreWireBoundingBox.h" |
---|
| 31 | |
---|
| 32 | #include "OgreSimpleRenderable.h" |
---|
| 33 | #include "OgreHardwareBufferManager.h" |
---|
| 34 | #include "OgreCamera.h" |
---|
| 35 | |
---|
| 36 | namespace Ogre { |
---|
| 37 | #define POSITION_BINDING 0 |
---|
| 38 | |
---|
| 39 | WireBoundingBox::WireBoundingBox() |
---|
| 40 | { |
---|
| 41 | mRenderOp.vertexData = new VertexData(); |
---|
| 42 | |
---|
| 43 | mRenderOp.indexData = 0; |
---|
| 44 | mRenderOp.vertexData->vertexCount = 24; |
---|
| 45 | mRenderOp.vertexData->vertexStart = 0; |
---|
| 46 | mRenderOp.operationType = RenderOperation::OT_LINE_LIST; |
---|
| 47 | mRenderOp.useIndexes = false; |
---|
| 48 | |
---|
| 49 | VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration; |
---|
| 50 | VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding; |
---|
| 51 | |
---|
| 52 | decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION); |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | HardwareVertexBufferSharedPtr vbuf = |
---|
| 56 | HardwareBufferManager::getSingleton().createVertexBuffer( |
---|
| 57 | decl->getVertexSize(POSITION_BINDING), |
---|
| 58 | mRenderOp.vertexData->vertexCount, |
---|
| 59 | HardwareBuffer::HBU_STATIC_WRITE_ONLY); |
---|
| 60 | |
---|
| 61 | // Bind buffer |
---|
| 62 | bind->setBinding(POSITION_BINDING, vbuf); |
---|
| 63 | |
---|
| 64 | // set basic white material |
---|
| 65 | this->setMaterial("BaseWhiteNoLighting"); |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | WireBoundingBox::~WireBoundingBox() |
---|
| 72 | { |
---|
| 73 | delete mRenderOp.vertexData; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void WireBoundingBox::setupBoundingBox(const AxisAlignedBox& aabb) |
---|
| 77 | { |
---|
| 78 | // init the vertices to the aabb |
---|
| 79 | setupBoundingBoxVertices(aabb); |
---|
| 80 | |
---|
| 81 | // setup the bounding box of this SimpleRenderable |
---|
| 82 | setBoundingBox(aabb); |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // Override this method to prevent parent transforms (rotation,translation,scale) |
---|
| 87 | void WireBoundingBox::getWorldTransforms( Matrix4* xform ) const |
---|
| 88 | { |
---|
| 89 | // return identity matrix to prevent parent transforms |
---|
| 90 | *xform = Matrix4::IDENTITY; |
---|
| 91 | } |
---|
| 92 | //----------------------------------------------------------------------- |
---|
| 93 | const Quaternion& WireBoundingBox::getWorldOrientation(void) const |
---|
| 94 | { |
---|
| 95 | return Quaternion::IDENTITY; |
---|
| 96 | } |
---|
| 97 | //----------------------------------------------------------------------- |
---|
| 98 | const Vector3& WireBoundingBox::getWorldPosition(void) const |
---|
| 99 | { |
---|
| 100 | return Vector3::ZERO; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | //----------------------------------------------------------------------- |
---|
| 104 | void WireBoundingBox::setupBoundingBoxVertices(const AxisAlignedBox& aab) { |
---|
| 105 | |
---|
| 106 | Vector3 vmax = aab.getMaximum(); |
---|
| 107 | Vector3 vmin = aab.getMinimum(); |
---|
| 108 | |
---|
| 109 | Real sqLen = std::max(vmax.squaredLength(), vmin.squaredLength()); |
---|
| 110 | mRadius = Math::Sqrt(sqLen); |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | Real maxx = vmax.x; |
---|
| 116 | Real maxy = vmax.y; |
---|
| 117 | Real maxz = vmax.z; |
---|
| 118 | |
---|
| 119 | Real minx = vmin.x; |
---|
| 120 | Real miny = vmin.y; |
---|
| 121 | Real minz = vmin.z; |
---|
| 122 | |
---|
| 123 | // fill in the Vertex buffer: 12 lines with 2 endpoints each make up a box |
---|
| 124 | HardwareVertexBufferSharedPtr vbuf = |
---|
| 125 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING); |
---|
| 126 | |
---|
| 127 | float* pPos = static_cast<float*>( |
---|
| 128 | vbuf->lock(HardwareBuffer::HBL_DISCARD)); |
---|
| 129 | |
---|
| 130 | // line 0 |
---|
| 131 | *pPos++ = minx; |
---|
| 132 | *pPos++ = miny; |
---|
| 133 | *pPos++ = minz; |
---|
| 134 | *pPos++ = maxx; |
---|
| 135 | *pPos++ = miny; |
---|
| 136 | *pPos++ = minz; |
---|
| 137 | // line 1 |
---|
| 138 | *pPos++ = minx; |
---|
| 139 | *pPos++ = miny; |
---|
| 140 | *pPos++ = minz; |
---|
| 141 | *pPos++ = minx; |
---|
| 142 | *pPos++ = miny; |
---|
| 143 | *pPos++ = maxz; |
---|
| 144 | // line 2 |
---|
| 145 | *pPos++ = minx; |
---|
| 146 | *pPos++ = miny; |
---|
| 147 | *pPos++ = minz; |
---|
| 148 | *pPos++ = minx; |
---|
| 149 | *pPos++ = maxy; |
---|
| 150 | *pPos++ = minz; |
---|
| 151 | // line 3 |
---|
| 152 | *pPos++ = minx; |
---|
| 153 | *pPos++ = maxy; |
---|
| 154 | *pPos++ = minz; |
---|
| 155 | *pPos++ = minx; |
---|
| 156 | *pPos++ = maxy; |
---|
| 157 | *pPos++ = maxz; |
---|
| 158 | // line 4 |
---|
| 159 | *pPos++ = minx; |
---|
| 160 | *pPos++ = maxy; |
---|
| 161 | *pPos++ = minz; |
---|
| 162 | *pPos++ = maxx; |
---|
| 163 | *pPos++ = maxy; |
---|
| 164 | *pPos++ = minz; |
---|
| 165 | // line 5 |
---|
| 166 | *pPos++ = maxx; |
---|
| 167 | *pPos++ = miny; |
---|
| 168 | *pPos++ = minz; |
---|
| 169 | *pPos++ = maxx; |
---|
| 170 | *pPos++ = miny; |
---|
| 171 | *pPos++ = maxz; |
---|
| 172 | // line 6 |
---|
| 173 | *pPos++ = maxx; |
---|
| 174 | *pPos++ = miny; |
---|
| 175 | *pPos++ = minz; |
---|
| 176 | *pPos++ = maxx; |
---|
| 177 | *pPos++ = maxy; |
---|
| 178 | *pPos++ = minz; |
---|
| 179 | // line 7 |
---|
| 180 | *pPos++ = minx; |
---|
| 181 | *pPos++ = maxy; |
---|
| 182 | *pPos++ = maxz; |
---|
| 183 | *pPos++ = maxx; |
---|
| 184 | *pPos++ = maxy; |
---|
| 185 | *pPos++ = maxz; |
---|
| 186 | // line 8 |
---|
| 187 | *pPos++ = minx; |
---|
| 188 | *pPos++ = maxy; |
---|
| 189 | *pPos++ = maxz; |
---|
| 190 | *pPos++ = minx; |
---|
| 191 | *pPos++ = miny; |
---|
| 192 | *pPos++ = maxz; |
---|
| 193 | // line 9 |
---|
| 194 | *pPos++ = maxx; |
---|
| 195 | *pPos++ = maxy; |
---|
| 196 | *pPos++ = minz; |
---|
| 197 | *pPos++ = maxx; |
---|
| 198 | *pPos++ = maxy; |
---|
| 199 | *pPos++ = maxz; |
---|
| 200 | // line 10 |
---|
| 201 | *pPos++ = maxx; |
---|
| 202 | *pPos++ = miny; |
---|
| 203 | *pPos++ = maxz; |
---|
| 204 | *pPos++ = maxx; |
---|
| 205 | *pPos++ = maxy; |
---|
| 206 | *pPos++ = maxz; |
---|
| 207 | // line 11 |
---|
| 208 | *pPos++ = minx; |
---|
| 209 | *pPos++ = miny; |
---|
| 210 | *pPos++ = maxz; |
---|
| 211 | *pPos++ = maxx; |
---|
| 212 | *pPos++ = miny; |
---|
| 213 | *pPos++ = maxz; |
---|
| 214 | vbuf->unlock(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | //----------------------------------------------------------------------- |
---|
| 218 | Real WireBoundingBox::getSquaredViewDepth(const Camera* cam) const |
---|
| 219 | { |
---|
| 220 | Vector3 min, max, mid, dist; |
---|
| 221 | min = mBox.getMinimum(); |
---|
| 222 | max = mBox.getMaximum(); |
---|
| 223 | mid = ((max - min) * 0.5) + min; |
---|
| 224 | dist = cam->getDerivedPosition() - mid; |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | return dist.squaredLength(); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | } |
---|
| 233 | |
---|