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

Revision 2184, 5.6 KB checked in by mattausch, 18 years ago (diff)

debug version: please don't check out

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.useSharedVertices = true;
74         mRenderOp.indexData->indexBuffer = ibuf;
75         // set material with no lighting, no color, no depth write
76         SetOcclusionQueryMaterial();
77         //setMaterial("BaseWhiteNoLighting");
78
79        /* std::stringstream d;
80        d << "vtx: " << mRenderOp.vertexData;
81        LogManager::getSingleton().logMessage(d.str());
82        */
83
84}
85//-----------------------------------------------------------------------
86SolidBoundingBox::~SolidBoundingBox()
87{
88        delete mRenderOp.vertexData;
89        delete mRenderOp.indexData;
90}
91//-----------------------------------------------------------------------
92void SolidBoundingBox::SetupBoundingBoxVertices(const AxisAlignedBox& aab)
93{
94        const Vector3& min = aab.getMinimum();
95    const Vector3& max = aab.getMaximum();
96
97        // fill the vertex buffer: 12 lines with 2 endpoints each make up a box
98    HardwareVertexBufferSharedPtr vbuf =
99        mRenderOp.vertexData->vertexBufferBinding->getBuffer(POSITION_BINDING);     
100
101    float* pPos = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
102
103        *pPos++ = min.x; *pPos++ = max.y; *pPos++ = min.z;
104        *pPos++ = max.x; *pPos++ = max.y; *pPos++ = min.z;
105        *pPos++ = max.x; *pPos++ = min.y; *pPos++ = min.z;
106        *pPos++ = min.x; *pPos++ = min.y; *pPos++ = min.z;
107               
108        *pPos++ = min.x; *pPos++ = max.y; *pPos++ = max.z;
109        *pPos++ = max.x; *pPos++ = max.y; *pPos++ = max.z;
110        *pPos++ = max.x; *pPos++ = min.y; *pPos++ = max.z;
111        *pPos++ = min.x; *pPos++ = min.y; *pPos++ = max.z;
112
113    vbuf->unlock();
114}
115//-----------------------------------------------------------------------
116void SolidBoundingBox::SetupBoundingBox(const AxisAlignedBox& aab)
117{
118        // init the vertices to the aabb
119        SetupBoundingBoxVertices(aab);
120
121        Real sqLen = std::max(aab.getMaximum().squaredLength(),
122                                                  aab.getMinimum().squaredLength());
123    mRadius = Math::Sqrt(sqLen);
124
125    // setup the bounding box of this SimpleRenderable
126        setBoundingBox(aab);
127}
128//-----------------------------------------------------------------------
129void SolidBoundingBox::SetOcclusionQueryMaterial()
130{
131        m_pMaterial = MaterialManager::getSingleton().getByName("Visibility/QueryMaterial");
132
133        if (m_pMaterial.isNull())
134        {
135                m_pMaterial = MaterialManager::getSingleton().
136                        create("Visibility/QueryMaterial", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
137
138                Pass *queryPass = m_pMaterial->getTechnique(0)->getPass(0);
139                queryPass->setColourWriteEnabled(false);
140                queryPass->setDepthWriteEnabled(false);
141                queryPass->setLightingEnabled(false);
142        }
143
144        setMaterial("Visibility/QueryMaterial");
145}
146
147// Override this method to prevent parent transforms (rotation,translation,scale)
148void SolidBoundingBox::getWorldTransforms(Matrix4* xform) const
149{
150        // return identity matrix to prevent parent transforms
151    *xform = Matrix4::IDENTITY;
152}
153//-----------------------------------------------------------------------
154const Quaternion& SolidBoundingBox::getWorldOrientation() const
155{
156        return Quaternion::IDENTITY;
157}
158//-----------------------------------------------------------------------
159const Vector3& SolidBoundingBox::getWorldPosition() const
160{
161        return Vector3::ZERO;
162}
163
164Real SolidBoundingBox::getSquaredViewDepth(const Camera* cam) const
165{
166        const Vector3 &min = mBox.getMinimum();
167        const Vector3 &max = mBox.getMaximum();
168        const Vector3 &mid = ((min - max) * 0.5) + min;
169        const Vector3 &dist = cam->getDerivedPosition() - mid;
170
171        return dist.squaredLength();
172}
173
174} // namespace Ogre
175
Note: See TracBrowser for help on using the repository browser.