source: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreSolidBoundingBox.cpp @ 2455

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