source: OGRE/trunk/ogrenew/OgreMain/src/OgreWireBoundingBox.cpp @ 657

Revision 657, 6.3 KB checked in by mattausch, 19 years ago (diff)

added ogre dependencies and patched ogre sources

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