| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/ShadowVolumeOccluder> |
|---|
| 14 | #include <osg/CullStack> |
|---|
| 15 | |
|---|
| 16 | #include <osg/Group> |
|---|
| 17 | #include <osg/Geode> |
|---|
| 18 | |
|---|
| 19 | using namespace osg; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | typedef std::pair<unsigned int,Vec3> Point; |
|---|
| 24 | typedef std::vector<Point> PointList; |
|---|
| 25 | typedef std::vector<Vec3> VertexList; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | void copyVertexListToPointList(const VertexList& in,PointList& out) |
|---|
| 30 | { |
|---|
| 31 | out.reserve(in.size()); |
|---|
| 32 | for(VertexList::const_iterator itr=in.begin(); |
|---|
| 33 | itr!=in.end(); |
|---|
| 34 | ++itr) |
|---|
| 35 | { |
|---|
| 36 | out.push_back(Point(0,*itr)); |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | void copyPointListToVertexList(const PointList& in,VertexList& out) |
|---|
| 41 | { |
|---|
| 42 | out.reserve(in.size()); |
|---|
| 43 | for(PointList::const_iterator itr=in.begin(); |
|---|
| 44 | itr!=in.end(); |
|---|
| 45 | ++itr) |
|---|
| 46 | { |
|---|
| 47 | out.push_back(itr->second); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | unsigned int clip(const Plane& plane,const PointList& in, PointList& out,unsigned int planeMask) |
|---|
| 54 | { |
|---|
| 55 | std::vector<float> distance; |
|---|
| 56 | distance.reserve(in.size()); |
|---|
| 57 | for(PointList::const_iterator itr=in.begin(); |
|---|
| 58 | itr!=in.end(); |
|---|
| 59 | ++itr) |
|---|
| 60 | { |
|---|
| 61 | distance.push_back(plane.distance(itr->second)); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | out.clear(); |
|---|
| 65 | |
|---|
| 66 | for(unsigned int i=0;i<in.size();++i) |
|---|
| 67 | { |
|---|
| 68 | unsigned int i_1 = (i+1)%in.size(); |
|---|
| 69 | |
|---|
| 70 | if (distance[i]>=0.0f) |
|---|
| 71 | { |
|---|
| 72 | out.push_back(in[i]); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | if (distance[i_1]<0.0f) |
|---|
| 76 | { |
|---|
| 77 | unsigned int mask = (in[i].first & in[i_1].first) | planeMask; |
|---|
| 78 | float r = distance[i_1]/(distance[i_1]-distance[i]); |
|---|
| 79 | out.push_back(Point(mask,in[i].second*r+in[i_1].second*(1.0f-r))); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | } |
|---|
| 83 | else if (distance[i_1]>0.0f) |
|---|
| 84 | { |
|---|
| 85 | unsigned int mask = (in[i].first & in[i_1].first) | planeMask; |
|---|
| 86 | float r = distance[i_1]/(distance[i_1]-distance[i]); |
|---|
| 87 | out.push_back(Point(mask,in[i].second*r+in[i_1].second*(1.0f-r))); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | return out.size(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | unsigned int clip(const Polytope::PlaneList& planeList,const VertexList& vin,PointList& out) |
|---|
| 97 | { |
|---|
| 98 | PointList in; |
|---|
| 99 | copyVertexListToPointList(vin,in); |
|---|
| 100 | |
|---|
| 101 | unsigned int planeMask = 0x1; |
|---|
| 102 | for(Polytope::PlaneList::const_iterator itr=planeList.begin(); |
|---|
| 103 | itr!=planeList.end(); |
|---|
| 104 | ++itr) |
|---|
| 105 | { |
|---|
| 106 | if (!clip(*itr,in,out,planeMask)) return false; |
|---|
| 107 | in.swap(out); |
|---|
| 108 | planeMask <<= 1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | in.swap(out); |
|---|
| 112 | |
|---|
| 113 | return out.size(); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void transform(PointList& points,const osg::Matrix& matrix) |
|---|
| 117 | { |
|---|
| 118 | for(PointList::iterator itr=points.begin(); |
|---|
| 119 | itr!=points.end(); |
|---|
| 120 | ++itr) |
|---|
| 121 | { |
|---|
| 122 | itr->second = itr->second*matrix; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | void transform(const PointList& in,PointList& out,const osg::Matrix& matrix) |
|---|
| 127 | { |
|---|
| 128 | for(PointList::const_iterator itr=in.begin(); |
|---|
| 129 | itr!=in.end(); |
|---|
| 130 | ++itr) |
|---|
| 131 | { |
|---|
| 132 | out.push_back(Point(itr->first,itr->second * matrix)); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | void pushToFarPlane(PointList& points) |
|---|
| 137 | { |
|---|
| 138 | for(PointList::iterator itr=points.begin(); |
|---|
| 139 | itr!=points.end(); |
|---|
| 140 | ++itr) |
|---|
| 141 | { |
|---|
| 142 | itr->second.z() = 1.0f; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void computePlanes(const PointList& front, const PointList& back, Polytope::PlaneList& planeList) |
|---|
| 147 | { |
|---|
| 148 | for(unsigned int i=0;i<front.size();++i) |
|---|
| 149 | { |
|---|
| 150 | unsigned int i_1 = (i+1)%front.size(); |
|---|
| 151 | if (!(front[i].first & front[i_1].first)) |
|---|
| 152 | { |
|---|
| 153 | planeList.push_back(Plane(front[i].second,front[i_1].second,back[i].second)); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | Plane computeFrontPlane(const PointList& front) |
|---|
| 159 | { |
|---|
| 160 | return Plane(front[2].second,front[1].second,front[0].second); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | float computePolytopeVolume(const PointList& front, const PointList& back) |
|---|
| 165 | { |
|---|
| 166 | float volume = 0.0f; |
|---|
| 167 | Vec3 frontStart = front[0].second; |
|---|
| 168 | Vec3 backStart = back[0].second; |
|---|
| 169 | for(unsigned int i=1;i<front.size()-1;++i) |
|---|
| 170 | { |
|---|
| 171 | volume += computeVolume(frontStart, front[i].second, front[i+1].second, |
|---|
| 172 | backStart, back[i].second, back[i+1].second); |
|---|
| 173 | } |
|---|
| 174 | return volume; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | bool ShadowVolumeOccluder::computeOccluder(const NodePath& nodePath,const ConvexPlanarOccluder& occluder,CullStack& cullStack,bool ) |
|---|
| 178 | { |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | CullingSet& cullingset = cullStack.getCurrentCullingSet(); |
|---|
| 184 | |
|---|
| 185 | const RefMatrix& MV = *cullStack.getModelViewMatrix(); |
|---|
| 186 | const RefMatrix& P = *cullStack.getProjectionMatrix(); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | _nodePath = nodePath; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | _projectionMatrix = &P; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | _volume = 0.0f; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | Matrix invP; |
|---|
| 200 | invP.invert(P); |
|---|
| 201 | |
|---|
| 202 | float volumeview = cullStack.getFrustumVolume(); |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | Matrix MVP(MV*P); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | const VertexList& vertices_in = occluder.getOccluder().getVertexList(); |
|---|
| 217 | |
|---|
| 218 | PointList points; |
|---|
| 219 | |
|---|
| 220 | if (clip(cullingset.getFrustum().getPlaneList(),vertices_in,points)>=3) |
|---|
| 221 | { |
|---|
| 222 | |
|---|
| 223 | PointList farPoints; |
|---|
| 224 | farPoints.reserve(points.size()); |
|---|
| 225 | transform(points,farPoints,MVP); |
|---|
| 226 | pushToFarPlane(farPoints); |
|---|
| 227 | transform(farPoints,invP); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | transform(points,MV); |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | copyPointListToVertexList(points,_occluderVolume.getReferenceVertexList()); |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | Plane occludePlane = computeFrontPlane(points); |
|---|
| 238 | _occluderVolume.add(occludePlane); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | computePlanes(points,farPoints,_occluderVolume.getPlaneList()); |
|---|
| 242 | |
|---|
| 243 | _occluderVolume.setupMask(); |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | if (occludePlane[3]>0.0f) |
|---|
| 247 | { |
|---|
| 248 | _occluderVolume.flip(); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | _volume = computePolytopeVolume(points,farPoints)/volumeview; |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | for(ConvexPlanarOccluder::HoleList::const_iterator hitr=occluder.getHoleList().begin(); |
|---|
| 255 | hitr!=occluder.getHoleList().end(); |
|---|
| 256 | ++hitr) |
|---|
| 257 | { |
|---|
| 258 | PointList points; |
|---|
| 259 | if (clip(cullingset.getFrustum().getPlaneList(),hitr->getVertexList(),points)>=3) |
|---|
| 260 | { |
|---|
| 261 | _holeList.push_back(Polytope()); |
|---|
| 262 | Polytope& polytope = _holeList.back(); |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | PointList farPoints; |
|---|
| 266 | farPoints.reserve(points.size()); |
|---|
| 267 | transform(points,farPoints,MVP); |
|---|
| 268 | pushToFarPlane(farPoints); |
|---|
| 269 | transform(farPoints,invP); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | transform(points,MV); |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | copyPointListToVertexList(points,polytope.getReferenceVertexList()); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | Plane occludePlane = computeFrontPlane(points); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | computePlanes(points,farPoints,polytope.getPlaneList()); |
|---|
| 283 | |
|---|
| 284 | polytope.setupMask(); |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | if (occludePlane[3]>0.0f) |
|---|
| 288 | { |
|---|
| 289 | polytope.flip(); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | _volume -= computePolytopeVolume(points,farPoints)/volumeview; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | return true; |
|---|
| 301 | } |
|---|
| 302 | return false; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | bool ShadowVolumeOccluder::contains(const std::vector<Vec3>& vertices) |
|---|
| 306 | { |
|---|
| 307 | if (_occluderVolume.containsAllOf(vertices)) |
|---|
| 308 | { |
|---|
| 309 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 310 | itr!=_holeList.end(); |
|---|
| 311 | ++itr) |
|---|
| 312 | { |
|---|
| 313 | PointList points; |
|---|
| 314 | if (clip(itr->getPlaneList(),vertices,points)>=3) return false; |
|---|
| 315 | } |
|---|
| 316 | return true; |
|---|
| 317 | } |
|---|
| 318 | return false; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | bool ShadowVolumeOccluder::contains(const BoundingSphere& bound) |
|---|
| 322 | { |
|---|
| 323 | |
|---|
| 324 | if (_occluderVolume.containsAllOf(bound)) |
|---|
| 325 | { |
|---|
| 326 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 327 | itr!=_holeList.end(); |
|---|
| 328 | ++itr) |
|---|
| 329 | { |
|---|
| 330 | if (itr->contains(bound)) |
|---|
| 331 | { |
|---|
| 332 | |
|---|
| 333 | return false; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | return true; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | return false; |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | bool ShadowVolumeOccluder::contains(const BoundingBox& bound) |
|---|
| 344 | { |
|---|
| 345 | |
|---|
| 346 | if (_occluderVolume.containsAllOf(bound)) |
|---|
| 347 | { |
|---|
| 348 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 349 | itr!=_holeList.end(); |
|---|
| 350 | ++itr) |
|---|
| 351 | { |
|---|
| 352 | if (itr->contains(bound)) |
|---|
| 353 | { |
|---|
| 354 | |
|---|
| 355 | return false; |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | return true; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | return false; |
|---|
| 363 | } |
|---|