1 | /*************************************************************************** |
---|
2 | |
---|
3 | This source file is part of OGREBULLET |
---|
4 | (Object-oriented Graphics Rendering Engine Bullet Wrapper) |
---|
5 | For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10 |
---|
6 | |
---|
7 | Copyright (c) 2007 tuan.kuranes@gmail.com (Use it Freely, even Statically, but have to contribute any changes) |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under |
---|
12 | the terms of the GPL General Public License with runtime exception as published by the Free Software |
---|
13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
14 | version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
18 | FOR A PARTICULAR PURPOSE. See the GPL General Public License with runtime exception for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GPL General Public License with runtime exception along with |
---|
21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
23 | http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
24 | ----------------------------------------------------------------------------- |
---|
25 | */ |
---|
26 | |
---|
27 | #include "OgreBulletCollisions.h" |
---|
28 | |
---|
29 | #include "Debug/OgreBulletCollisionsDebugLines.h" |
---|
30 | #include "Utils/OgreBulletConverter.h" |
---|
31 | |
---|
32 | using namespace OgreBulletCollisions; |
---|
33 | using namespace Ogre; |
---|
34 | |
---|
35 | //------------------------------------------------------------------------------------------------ |
---|
36 | bool DebugLines::_materials_created = false; |
---|
37 | //------------------------------------------------------------------------------------------------ |
---|
38 | DebugLines::DebugLines() : SimpleRenderable() |
---|
39 | { |
---|
40 | mRenderOp.vertexData = new Ogre::VertexData(); |
---|
41 | _drawn = false; |
---|
42 | |
---|
43 | if (!_materials_created) |
---|
44 | { |
---|
45 | MaterialPtr red = MaterialManager::getSingleton().create("OgreBulletCollisionsDebugLines/Disabled","OgreBulletCollisions"); |
---|
46 | MaterialPtr green = MaterialManager::getSingleton().create("OgreBulletCollisionsDebugLines/Enabled","OgreBulletCollisions"); |
---|
47 | MaterialPtr blue = MaterialManager::getSingleton().create("OgreBulletCollisionsDebugLines/Static","OgreBulletCollisions"); |
---|
48 | |
---|
49 | red->setReceiveShadows(false); |
---|
50 | red->getTechnique(0)->setLightingEnabled(true); |
---|
51 | red->getTechnique(0)->getPass(0)->setSelfIllumination(1,0,0); |
---|
52 | |
---|
53 | green->setReceiveShadows(false); |
---|
54 | green->getTechnique(0)->setLightingEnabled(true); |
---|
55 | green->getTechnique(0)->getPass(0)->setSelfIllumination(0,1,0); |
---|
56 | |
---|
57 | blue->setReceiveShadows(false); |
---|
58 | blue->getTechnique(0)->setLightingEnabled(true); |
---|
59 | blue->getTechnique(0)->getPass(0)->setSelfIllumination(0,0,1); |
---|
60 | |
---|
61 | _materials_created = true; |
---|
62 | } |
---|
63 | setCastShadows (false); |
---|
64 | this->setMaterial("OgreBulletCollisionsDebugLines/Enabled"); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | //------------------------------------------------------------------------------------------------ |
---|
69 | void DebugLines::clear() |
---|
70 | { |
---|
71 | if (_drawn) |
---|
72 | { |
---|
73 | _drawn = false; |
---|
74 | _points.clear(); |
---|
75 | delete mRenderOp.vertexData; |
---|
76 | |
---|
77 | mRenderOp.vertexData = new Ogre::VertexData(); |
---|
78 | } |
---|
79 | } |
---|
80 | //------------------------------------------------------------------------------------------------ |
---|
81 | DebugLines::~DebugLines(void) |
---|
82 | { |
---|
83 | clear(); |
---|
84 | |
---|
85 | delete mRenderOp.vertexData; |
---|
86 | } |
---|
87 | //------------------------------------------------------------------------------------------------ |
---|
88 | void DebugLines::draw() |
---|
89 | { |
---|
90 | if (_drawn || _points.empty()) |
---|
91 | return; |
---|
92 | else |
---|
93 | _drawn = true; |
---|
94 | |
---|
95 | // Initialization stuff |
---|
96 | mRenderOp.indexData = 0; |
---|
97 | mRenderOp.vertexData->vertexCount = _points.size(); |
---|
98 | mRenderOp.vertexData->vertexStart = 0; |
---|
99 | mRenderOp.operationType = RenderOperation::OT_LINE_LIST; |
---|
100 | mRenderOp.useIndexes = false; |
---|
101 | |
---|
102 | Ogre::VertexDeclaration *decl = mRenderOp.vertexData->vertexDeclaration; |
---|
103 | Ogre::VertexBufferBinding *bind = mRenderOp.vertexData->vertexBufferBinding; |
---|
104 | |
---|
105 | decl->addElement(0, 0, VET_FLOAT3, VES_POSITION); |
---|
106 | |
---|
107 | HardwareVertexBufferSharedPtr vbuf = |
---|
108 | HardwareBufferManager::getSingleton().createVertexBuffer( |
---|
109 | decl->getVertexSize(0), |
---|
110 | mRenderOp.vertexData->vertexCount, |
---|
111 | HardwareBuffer::HBU_STATIC_WRITE_ONLY); |
---|
112 | |
---|
113 | bind->setBinding(0, vbuf); |
---|
114 | |
---|
115 | // Drawing stuff |
---|
116 | unsigned int size = (unsigned int)_points.size(); |
---|
117 | Ogre::Vector3 vaabMin = _points[0]; |
---|
118 | Ogre::Vector3 vaabMax = _points[0]; |
---|
119 | |
---|
120 | float *prPos = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD)); |
---|
121 | |
---|
122 | for(unsigned int i = 0; i < size; i++) |
---|
123 | { |
---|
124 | *prPos++ = _points[i].x; |
---|
125 | *prPos++ = _points[i].y; |
---|
126 | *prPos++ = _points[i].z; |
---|
127 | |
---|
128 | if (_points[i].x < vaabMin.x) |
---|
129 | vaabMin.x = _points[i].x; |
---|
130 | if (_points[i].y < vaabMin.y) |
---|
131 | vaabMin.y = _points[i].y; |
---|
132 | if (_points[i].z < vaabMin.z) |
---|
133 | vaabMin.z = _points[i].z; |
---|
134 | |
---|
135 | if (_points[i].x > vaabMax.x) |
---|
136 | vaabMax.x = _points[i].x; |
---|
137 | if (_points[i].y > vaabMax.y) |
---|
138 | vaabMax.y = _points[i].y; |
---|
139 | if (_points[i].z > vaabMax.z) |
---|
140 | vaabMax.z = _points[i].z; |
---|
141 | } |
---|
142 | |
---|
143 | vbuf->unlock(); |
---|
144 | |
---|
145 | mBox.setExtents(vaabMin, vaabMax); |
---|
146 | } |
---|
147 | //------------------------------------------------------------------------------------------------ |
---|
148 | Real DebugLines::getSquaredViewDepth(const Camera *cam) const |
---|
149 | { |
---|
150 | Vector3 vMin, vMax, vMid, vDist; |
---|
151 | vMin = mBox.getMinimum(); |
---|
152 | vMax = mBox.getMaximum(); |
---|
153 | vMid = ((vMin - vMax) * 0.5) + vMin; |
---|
154 | vDist = cam->getDerivedPosition() - vMid; |
---|
155 | |
---|
156 | return vDist.squaredLength(); |
---|
157 | } |
---|
158 | //------------------------------------------------------------------------------------------------ |
---|
159 | Real DebugLines::getBoundingRadius() const |
---|
160 | { |
---|
161 | return Math::Sqrt(std::max(mBox.getMaximum().squaredLength(), mBox.getMinimum().squaredLength())); |
---|
162 | } |
---|