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

Revision 103, 5.2 KB checked in by mattausch, 19 years ago (diff)

fixed bug with shadow maps

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