Patch 2793067: fix trunk with OGRE_THREAD_SUPPORT=1 on non-Windows platforms (don...
[ogre3d.git] / OgreMain / src / OgreWireBoundingBox.cpp
blob62f6b41bea9cf924ed2f619eb6180837f9390715
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/
7 Copyright (c) 2000-2006 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
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.
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.
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.
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 -----------------------------------------------------------------------------
29 #include "OgreStableHeaders.h"
30 #include "OgreWireBoundingBox.h"
32 #include "OgreSimpleRenderable.h"
33 #include "OgreHardwareBufferManager.h"
34 #include "OgreCamera.h"
36 namespace Ogre {
37 #define POSITION_BINDING 0
39 WireBoundingBox::WireBoundingBox()
41 mRenderOp.vertexData = OGRE_NEW VertexData();
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;
49 VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration;
50 VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding;
52 decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
55 HardwareVertexBufferSharedPtr vbuf =
56 HardwareBufferManager::getSingleton().createVertexBuffer(
57 decl->getVertexSize(POSITION_BINDING),
58 mRenderOp.vertexData->vertexCount,
59 HardwareBuffer::HBU_STATIC_WRITE_ONLY);
61 // Bind buffer
62 bind->setBinding(POSITION_BINDING, vbuf);
64 // set basic white material
65 this->setMaterial("BaseWhiteNoLighting");
71 WireBoundingBox::~WireBoundingBox()
73 OGRE_DELETE mRenderOp.vertexData;
76 void WireBoundingBox::setupBoundingBox(const AxisAlignedBox& aabb)
78 // init the vertices to the aabb
79 setupBoundingBoxVertices(aabb);
81 // setup the bounding box of this SimpleRenderable
82 setBoundingBox(aabb);
86 // Override this method to prevent parent transforms (rotation,translation,scale)
87 void WireBoundingBox::getWorldTransforms( Matrix4* xform ) const
89 // return identity matrix to prevent parent transforms
90 *xform = Matrix4::IDENTITY;
92 //-----------------------------------------------------------------------
93 void WireBoundingBox::setupBoundingBoxVertices(const AxisAlignedBox& aab) {
95 Vector3 vmax = aab.getMaximum();
96 Vector3 vmin = aab.getMinimum();
98 Real sqLen = std::max(vmax.squaredLength(), vmin.squaredLength());
99 mRadius = Math::Sqrt(sqLen);
104 Real maxx = vmax.x;
105 Real maxy = vmax.y;
106 Real maxz = vmax.z;
108 Real minx = vmin.x;
109 Real miny = vmin.y;
110 Real minz = vmin.z;
112 // fill in the Vertex buffer: 12 lines with 2 endpoints each make up a box
113 HardwareVertexBufferSharedPtr vbuf =
114 mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING);
116 float* pPos = static_cast<float*>(
117 vbuf->lock(HardwareBuffer::HBL_DISCARD));
119 // line 0
120 *pPos++ = minx;
121 *pPos++ = miny;
122 *pPos++ = minz;
123 *pPos++ = maxx;
124 *pPos++ = miny;
125 *pPos++ = minz;
126 // line 1
127 *pPos++ = minx;
128 *pPos++ = miny;
129 *pPos++ = minz;
130 *pPos++ = minx;
131 *pPos++ = miny;
132 *pPos++ = maxz;
133 // line 2
134 *pPos++ = minx;
135 *pPos++ = miny;
136 *pPos++ = minz;
137 *pPos++ = minx;
138 *pPos++ = maxy;
139 *pPos++ = minz;
140 // line 3
141 *pPos++ = minx;
142 *pPos++ = maxy;
143 *pPos++ = minz;
144 *pPos++ = minx;
145 *pPos++ = maxy;
146 *pPos++ = maxz;
147 // line 4
148 *pPos++ = minx;
149 *pPos++ = maxy;
150 *pPos++ = minz;
151 *pPos++ = maxx;
152 *pPos++ = maxy;
153 *pPos++ = minz;
154 // line 5
155 *pPos++ = maxx;
156 *pPos++ = miny;
157 *pPos++ = minz;
158 *pPos++ = maxx;
159 *pPos++ = miny;
160 *pPos++ = maxz;
161 // line 6
162 *pPos++ = maxx;
163 *pPos++ = miny;
164 *pPos++ = minz;
165 *pPos++ = maxx;
166 *pPos++ = maxy;
167 *pPos++ = minz;
168 // line 7
169 *pPos++ = minx;
170 *pPos++ = maxy;
171 *pPos++ = maxz;
172 *pPos++ = maxx;
173 *pPos++ = maxy;
174 *pPos++ = maxz;
175 // line 8
176 *pPos++ = minx;
177 *pPos++ = maxy;
178 *pPos++ = maxz;
179 *pPos++ = minx;
180 *pPos++ = miny;
181 *pPos++ = maxz;
182 // line 9
183 *pPos++ = maxx;
184 *pPos++ = maxy;
185 *pPos++ = minz;
186 *pPos++ = maxx;
187 *pPos++ = maxy;
188 *pPos++ = maxz;
189 // line 10
190 *pPos++ = maxx;
191 *pPos++ = miny;
192 *pPos++ = maxz;
193 *pPos++ = maxx;
194 *pPos++ = maxy;
195 *pPos++ = maxz;
196 // line 11
197 *pPos++ = minx;
198 *pPos++ = miny;
199 *pPos++ = maxz;
200 *pPos++ = maxx;
201 *pPos++ = miny;
202 *pPos++ = maxz;
203 vbuf->unlock();
206 //-----------------------------------------------------------------------
207 Real WireBoundingBox::getSquaredViewDepth(const Camera* cam) const
209 Vector3 min, max, mid, dist;
210 min = mBox.getMinimum();
211 max = mBox.getMaximum();
212 mid = ((max - min) * 0.5) + min;
213 dist = cam->getDerivedPosition() - mid;
216 return dist.squaredLength();