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

Revision 97, 5.1 KB checked in by mattausch, 19 years ago (diff)

ogre: changed from occlusion_query_nv to occlusion_query_arb (in ogre_changes)

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