source: trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp @ 118

Revision 118, 5.4 KB checked in by mattausch, 19 years ago (diff)
Line 
1#include "OgreStableHeaders.h"
2#include "OgreSolidBoundingBox.h"
3
4#include <OgreSimpleRenderable.h>
5#include <OgreHardwareBufferManager.h>
6#include <OgreCamera.h>
7#include <OgreMaterialManager.h>
8#include <OgreMaterial.h>
9#include <OgrePass.h>
10#include <OgreTechnique.h>
11
12namespace Ogre {
13
14#define POSITION_BINDING 0
15//-----------------------------------------------------------------------
16SolidBoundingBox::SolidBoundingBox()
17{
18         mRenderOp.vertexData = new VertexData();
19         mRenderOp.indexData = new IndexData();
20
21         mRenderOp.indexData->indexStart = 0;
22         mRenderOp.vertexData->vertexStart = 0;
23
24         mRenderOp.vertexData->vertexCount = 8;
25         mRenderOp.indexData->indexCount  = 36;
26
27         mRenderOp.operationType = RenderOperation::OT_TRIANGLE_LIST;
28         mRenderOp.useIndexes = true;
29
30     VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration;
31     VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding;
32
33     decl->addElement(POSITION_BINDING, 0, VET_FLOAT3, VES_POSITION);
34         //decl->addElement(INDEX_BINDING, 0, VET_, VES_POSITION);
35
36         // create the vertex buffer
37     HardwareVertexBufferSharedPtr vbuf =
38            HardwareBufferManager::getSingleton().createVertexBuffer(
39                decl->getVertexSize(POSITION_BINDING),
40                mRenderOp.vertexData->vertexCount,
41                HardwareBuffer::HBU_STATIC_WRITE_ONLY);
42
43     // Bind buffer
44     bind->setBinding(POSITION_BINDING, vbuf);
45
46         //-- index buffer
47         unsigned short faces[36] =
48         {
49        0,2,3,
50        0,1,2,
51        1,6,2,
52        1,5,6,
53        4,6,5,
54        4,7,6,
55        0,7,4,
56        0,3,7,
57        0,5,1,
58        0,4,5,
59        2,7,3,
60        2,6,7
61         };
62
63         /// Allocate index buffer of the requested number of vertices
64     HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().
65         createIndexBuffer(
66             HardwareIndexBuffer::IT_16BIT,
67             mRenderOp.indexData->indexCount,
68             HardwareBuffer::HBU_STATIC_WRITE_ONLY);
69
70         /// Upload the index data to the card
71         ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true);
72         //mRenderOp.useSharedVertices = true;
73         mRenderOp.indexData->indexBuffer = ibuf;
74         // set material with no lighting, no color, no depth write
75         SetOcclusionQueryMaterial();
76         //setMaterial("BaseWhiteNoLighting");
77}
78//-----------------------------------------------------------------------
79SolidBoundingBox::~SolidBoundingBox()
80{
81        delete mRenderOp.vertexData;
82        delete mRenderOp.indexData;
83}
84//-----------------------------------------------------------------------
85void SolidBoundingBox::SetupBoundingBoxVertices(const AxisAlignedBox& aab)
86{
87        const Vector3& min = aab.getMinimum();
88    const Vector3& max = aab.getMaximum();
89
90        // fill the vertex buffer: 12 lines with 2 endpoints each make up a box
91    HardwareVertexBufferSharedPtr vbuf =
92        mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING);     
93
94    float* pPos = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
95
96        *pPos++ = min.x; *pPos++ = max.y; *pPos++ = min.z;
97        *pPos++ = max.x; *pPos++ = max.y; *pPos++ = min.z;
98        *pPos++ = max.x; *pPos++ = min.y; *pPos++ = min.z;
99        *pPos++ = min.x; *pPos++ = min.y; *pPos++ = min.z;
100               
101        *pPos++ = min.x; *pPos++ = max.y; *pPos++ = max.z;
102        *pPos++ = max.x; *pPos++ = max.y; *pPos++ = max.z;
103        *pPos++ = max.x; *pPos++ = min.y; *pPos++ = max.z;
104        *pPos++ = min.x; *pPos++ = min.y; *pPos++ = max.z;
105
106    vbuf->unlock();
107}
108//-----------------------------------------------------------------------
109void SolidBoundingBox::SetupBoundingBox(const AxisAlignedBox& aab)
110{
111        // init the vertices to the aabb
112        SetupBoundingBoxVertices(aab);
113
114        Real sqLen = std::max(aab.getMaximum().squaredLength(),
115                                                  aab.getMinimum().squaredLength());
116    mRadius = Math::Sqrt(sqLen);
117
118    // setup the bounding box of this SimpleRenderable
119        setBoundingBox(aab);
120}
121//-----------------------------------------------------------------------
122void SolidBoundingBox::SetOcclusionQueryMaterial()
123{
124        m_pMaterial = MaterialManager::getSingleton().getByName("Visibility/QueryMaterial");
125
126        if (m_pMaterial.isNull())
127        {
128                m_pMaterial = MaterialManager::getSingleton().
129                        create("Visibility/QueryMaterial", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
130
131                Pass *queryPass = m_pMaterial->getTechnique(0)->getPass(0);
132                queryPass->setColourWriteEnabled(false);
133                queryPass->setDepthWriteEnabled(false);
134                queryPass->setLightingEnabled(false);
135        }
136
137        setMaterial("Visibility/QueryMaterial");
138}
139
140// Override this method to prevent parent transforms (rotation,translation,scale)
141void SolidBoundingBox::getWorldTransforms(Matrix4* xform) const
142{
143        // return identity matrix to prevent parent transforms
144    *xform = Matrix4::IDENTITY;
145}
146//-----------------------------------------------------------------------
147const Quaternion& SolidBoundingBox::getWorldOrientation() const
148{
149        return Quaternion::IDENTITY;
150}
151//-----------------------------------------------------------------------
152const Vector3& SolidBoundingBox::getWorldPosition() const
153{
154        return Vector3::ZERO;
155}
156
157Real SolidBoundingBox::getSquaredViewDepth(const Camera* cam) const
158{
159        Vector3 min, max, mid, dist;
160
161        min = mBox.getMinimum();
162        max = mBox.getMaximum();
163        mid = ((min - max) * 0.5) + min;
164        dist = cam->getDerivedPosition() - mid;
165
166        return dist.squaredLength();
167}
168
169} // namespace Ogre
170
Note: See TracBrowser for help on using the repository browser.