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