[167] | 1 | #include "Ray.h"
|
---|
[162] | 2 | #include "Mesh.h"
|
---|
[170] | 3 | #include "MeshKdTree.h"
|
---|
[191] | 4 | #include "Triangle3.h"
|
---|
[1001] | 5 | #include "ResourceManager.h"
|
---|
[162] | 6 |
|
---|
[863] | 7 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 8 |
|
---|
[878] | 9 | bool MeshDebug = false;
|
---|
[860] | 10 |
|
---|
[339] | 11 | int Intersectable::sMailId = 21843194198;
|
---|
[382] | 12 | int Intersectable::sReservedMailboxes = 1;
|
---|
[162] | 13 |
|
---|
[752] | 14 | struct SortableVertex {
|
---|
| 15 |
|
---|
| 16 | Vector3 vertex;
|
---|
| 17 |
|
---|
| 18 | int originalId;
|
---|
| 19 | int newId;
|
---|
| 20 | int finalPos;
|
---|
| 21 |
|
---|
| 22 | SortableVertex() {}
|
---|
| 23 |
|
---|
| 24 | SortableVertex(const Vector3 &v,
|
---|
| 25 | const int id):
|
---|
| 26 | vertex(v),
|
---|
| 27 | originalId(id),
|
---|
| 28 | newId(id)
|
---|
| 29 | {}
|
---|
| 30 |
|
---|
| 31 | friend bool operator<(const SortableVertex &a,
|
---|
| 32 | const SortableVertex &b)
|
---|
| 33 | {
|
---|
| 34 | if (a.vertex.x < b.vertex.x)
|
---|
| 35 | return true;
|
---|
| 36 | else
|
---|
| 37 | if (a.vertex.x > b.vertex.x)
|
---|
| 38 | return false;
|
---|
| 39 |
|
---|
| 40 | if (a.vertex.y < b.vertex.y)
|
---|
| 41 | return true;
|
---|
| 42 | else
|
---|
| 43 | if (a.vertex.y > b.vertex.y)
|
---|
| 44 | return false;
|
---|
| 45 |
|
---|
| 46 | if (a.vertex.z < b.vertex.z)
|
---|
| 47 | return true;
|
---|
| 48 | else
|
---|
| 49 | // if (a.z > b.z)
|
---|
| 50 | return false;
|
---|
| 51 |
|
---|
| 52 | // return false;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | };
|
---|
| 56 |
|
---|
[863] | 57 |
|
---|
[162] | 58 | void
|
---|
[859] | 59 | Mesh::ComputeBoundingBox()
|
---|
[863] | 60 | {
|
---|
| 61 |
|
---|
[752] | 62 | mBox.Initialize();
|
---|
[162] | 63 | VertexContainer::const_iterator vi = mVertices.begin();
|
---|
| 64 | for (; vi != mVertices.end(); vi++) {
|
---|
[176] | 65 | mBox.Include(*vi);
|
---|
[162] | 66 | }
|
---|
[873] | 67 | //mBox.Enlarge(1e-4f);
|
---|
[863] | 68 | }
|
---|
| 69 |
|
---|
[859] | 70 | void
|
---|
| 71 | Mesh::Preprocess()
|
---|
| 72 | {
|
---|
[870] | 73 | Cleanup();
|
---|
[1076] | 74 |
|
---|
[870] | 75 | ComputeBoundingBox();
|
---|
[1076] | 76 |
|
---|
[870] | 77 | /** true if it is a watertight convex mesh
|
---|
| 78 | */
|
---|
| 79 | mIsConvex = false;
|
---|
| 80 |
|
---|
| 81 | if (mFaces.size() > MeshKdTree::mTermMinCost)
|
---|
| 82 | {
|
---|
| 83 | mKdTree = new MeshKdTree(this);
|
---|
| 84 | MeshKdLeaf *root = (MeshKdLeaf *)mKdTree->GetRoot();
|
---|
| 85 |
|
---|
| 86 | for (int i = 0; i < mFaces.size(); i++)
|
---|
| 87 | root->mFaces.push_back(i);
|
---|
| 88 |
|
---|
| 89 | cout<<"KD";
|
---|
[1076] | 90 |
|
---|
[870] | 91 | mKdTree->Construct();
|
---|
| 92 |
|
---|
| 93 | if (mKdTree->GetRoot()->IsLeaf())
|
---|
| 94 | {
|
---|
| 95 | cout<<"d";
|
---|
| 96 | delete mKdTree;
|
---|
| 97 | mKdTree = NULL;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[162] | 100 | }
|
---|
| 101 |
|
---|
[752] | 102 |
|
---|
| 103 | void
|
---|
| 104 | Mesh::IndexVertices()
|
---|
| 105 | {
|
---|
| 106 | int i;
|
---|
| 107 | // check whether the vertices can be simplfied and reindexed
|
---|
| 108 | vector<SortableVertex> svertices(mVertices.size());
|
---|
| 109 |
|
---|
| 110 | for (i=0; i < mVertices.size(); i++)
|
---|
| 111 | svertices[i] = SortableVertex(mVertices[i], i);
|
---|
| 112 |
|
---|
| 113 | sort(svertices.begin(), svertices.end());
|
---|
| 114 |
|
---|
| 115 | for (i=0; i < svertices.size() - 1; i++)
|
---|
| 116 | if (svertices[i].vertex == svertices[i+1].vertex)
|
---|
| 117 | svertices[i+1].newId = svertices[i].newId;
|
---|
| 118 |
|
---|
| 119 | // remove the same vertices
|
---|
| 120 | int k = 0;
|
---|
| 121 | mVertices[0] = svertices[0].vertex;
|
---|
| 122 | svertices[0].finalPos = 0;
|
---|
| 123 |
|
---|
| 124 | for (i=1; i < svertices.size(); i++) {
|
---|
| 125 | if (svertices[i].newId != svertices[i-1].newId)
|
---|
| 126 | k++;
|
---|
| 127 |
|
---|
| 128 | mVertices[k] = svertices[i].vertex;
|
---|
| 129 | svertices[i].finalPos = k;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | mVertices.resize(k + 1);
|
---|
| 133 |
|
---|
| 134 | vector<int> remapBuffer(svertices.size());
|
---|
| 135 | for (i = 0; i < svertices.size(); i++)
|
---|
| 136 | remapBuffer[svertices[i].originalId] = svertices[i].finalPos;
|
---|
| 137 |
|
---|
| 138 | // remap all faces
|
---|
| 139 |
|
---|
| 140 | for (int faceIndex = 0; faceIndex < mFaces.size(); faceIndex++) {
|
---|
| 141 | Face *face = mFaces[faceIndex];
|
---|
| 142 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 143 | face->mVertexIndices[i] = remapBuffer[face->mVertexIndices[i]];
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[170] | 148 | AxisAlignedBox3
|
---|
| 149 | Mesh::GetFaceBox(const int faceIndex)
|
---|
| 150 | {
|
---|
| 151 | Face *face = mFaces[faceIndex];
|
---|
| 152 | AxisAlignedBox3 box;
|
---|
| 153 | box.SetMin( mVertices[face->mVertexIndices[0]] );
|
---|
| 154 | box.SetMax(box.Min());
|
---|
| 155 | for (int i = 1; i < face->mVertexIndices.size(); i++) {
|
---|
| 156 | box.Include(mVertices[face->mVertexIndices[i]]);
|
---|
| 157 | }
|
---|
| 158 | return box;
|
---|
| 159 | }
|
---|
[162] | 160 |
|
---|
| 161 | int
|
---|
[170] | 162 | Mesh::CastRayToFace(
|
---|
[878] | 163 | const int faceIndex,
|
---|
| 164 | Ray &ray,
|
---|
| 165 | float &nearestT,
|
---|
| 166 | int &nearestFace,
|
---|
| 167 | Intersectable *instance
|
---|
| 168 | )
|
---|
[170] | 169 | {
|
---|
| 170 | float t;
|
---|
| 171 | int hit = 0;
|
---|
| 172 | if (RayFaceIntersection(faceIndex, ray, t, nearestT) == Ray::INTERSECTION) {
|
---|
| 173 | switch (ray.GetType()) {
|
---|
| 174 | case Ray::GLOBAL_RAY:
|
---|
[191] | 175 | ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
|
---|
[170] | 176 | hit++;
|
---|
| 177 | break;
|
---|
| 178 | case Ray::LOCAL_RAY:
|
---|
| 179 | nearestT = t;
|
---|
| 180 | nearestFace = faceIndex;
|
---|
| 181 | hit++;
|
---|
| 182 | break;
|
---|
[245] | 183 | case Ray::LINE_SEGMENT:
|
---|
| 184 | if (t <= 1.0f) {
|
---|
[878] | 185 | ray.intersections.push_back(Ray::Intersection(t, instance, faceIndex));
|
---|
| 186 | hit++;
|
---|
[245] | 187 | }
|
---|
| 188 | break;
|
---|
[170] | 189 | }
|
---|
| 190 | }
|
---|
| 191 | return hit;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | int
|
---|
[162] | 195 | Mesh::CastRay(
|
---|
[444] | 196 | Ray &ray,
|
---|
| 197 | MeshInstance *instance
|
---|
| 198 | )
|
---|
[162] | 199 | {
|
---|
[170] | 200 | if (mKdTree) {
|
---|
| 201 | return mKdTree->CastRay(ray, instance);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[162] | 204 | int faceIndex = 0;
|
---|
| 205 | int hits = 0;
|
---|
| 206 | float nearestT = MAX_FLOAT;
|
---|
[170] | 207 | int nearestFace = -1;
|
---|
| 208 |
|
---|
[162] | 209 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 210 | nearestT = ray.intersections[0].mT;
|
---|
| 211 |
|
---|
[376] | 212 |
|
---|
[162] | 213 | for ( ;
|
---|
[492] | 214 | faceIndex < mFaces.size();
|
---|
| 215 | faceIndex++) {
|
---|
[170] | 216 | hits += CastRayToFace(faceIndex, ray, nearestT, nearestFace, instance);
|
---|
| 217 | if (mIsConvex && nearestFace != -1)
|
---|
| 218 | break;
|
---|
[162] | 219 | }
|
---|
| 220 |
|
---|
| 221 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 222 | if (ray.intersections.size())
|
---|
[191] | 223 | ray.intersections[0] = Ray::Intersection(nearestT, instance, nearestFace);
|
---|
[162] | 224 | else
|
---|
[191] | 225 | ray.intersections.push_back(Ray::Intersection(nearestT, instance, nearestFace));
|
---|
[162] | 226 | }
|
---|
| 227 |
|
---|
| 228 | return hits;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[170] | 231 | int
|
---|
| 232 | Mesh::CastRayToSelectedFaces(
|
---|
[492] | 233 | Ray &ray,
|
---|
| 234 | const vector<int> &faces,
|
---|
| 235 | Intersectable *instance
|
---|
| 236 | )
|
---|
[170] | 237 | {
|
---|
| 238 | vector<int>::const_iterator fi;
|
---|
| 239 | int faceIndex = 0;
|
---|
| 240 | int hits = 0;
|
---|
| 241 | float nearestT = MAX_FLOAT;
|
---|
| 242 | int nearestFace = -1;
|
---|
[162] | 243 |
|
---|
[170] | 244 | if (ray.GetType() == Ray::LOCAL_RAY && ray.intersections.size())
|
---|
| 245 | nearestT = ray.intersections[0].mT;
|
---|
| 246 |
|
---|
| 247 | for ( fi = faces.begin();
|
---|
[878] | 248 | fi != faces.end();
|
---|
| 249 | fi++) {
|
---|
[170] | 250 | hits += CastRayToFace(*fi, ray, nearestT, nearestFace, instance);
|
---|
| 251 | if (mIsConvex && nearestFace != -1)
|
---|
| 252 | break;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if ( hits && ray.GetType() == Ray::LOCAL_RAY ) {
|
---|
| 256 | if (ray.intersections.size())
|
---|
[191] | 257 | ray.intersections[0] = Ray::Intersection(nearestT, instance, nearestFace);
|
---|
[170] | 258 | else
|
---|
[191] | 259 | ray.intersections.push_back(Ray::Intersection(nearestT, instance, nearestFace));
|
---|
[170] | 260 | }
|
---|
| 261 |
|
---|
| 262 | return hits;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
[162] | 266 | // int_lineseg returns 1 if the given line segment intersects a 2D
|
---|
| 267 | // ray travelling in the positive X direction. This is used in the
|
---|
| 268 | // Jordan curve computation for polygon intersection.
|
---|
| 269 | inline int
|
---|
| 270 | int_lineseg(float px,
|
---|
[492] | 271 | float py,
|
---|
| 272 | float u1,
|
---|
| 273 | float v1,
|
---|
| 274 | float u2,
|
---|
| 275 | float v2)
|
---|
[162] | 276 | {
|
---|
| 277 | float ydiff;
|
---|
| 278 |
|
---|
| 279 | u1 -= px; u2 -= px; // translate line
|
---|
| 280 | v1 -= py; v2 -= py;
|
---|
| 281 |
|
---|
| 282 | if ((v1 > 0 && v2 > 0) ||
|
---|
| 283 | (v1 < 0 && v2 < 0) ||
|
---|
| 284 | (u1 < 0 && u2 < 0))
|
---|
| 285 | return 0;
|
---|
| 286 |
|
---|
| 287 | if (u1 > 0 && u2 > 0)
|
---|
| 288 | return 1;
|
---|
| 289 |
|
---|
| 290 | ydiff = v2 - v1;
|
---|
| 291 | if (fabs(ydiff) < Limits::Small) { // denominator near 0
|
---|
| 292 | if (((fabs(v1) > Limits::Small) ||
|
---|
[492] | 293 | (u1 > 0) || (u2 > 0)))
|
---|
[162] | 294 | return 0;
|
---|
| 295 | return 1;
|
---|
| 296 | }
|
---|
[492] | 297 |
|
---|
| 298 | double t = -v1 / ydiff; // Compute parameter
|
---|
[162] | 299 |
|
---|
[492] | 300 | double thresh;
|
---|
[878] | 301 |
|
---|
[492] | 302 | if (ydiff < 0.0f)
|
---|
[878] | 303 | thresh = -1e-20;
|
---|
[492] | 304 | else
|
---|
[878] | 305 | thresh = 1e-20;
|
---|
[162] | 306 |
|
---|
[492] | 307 |
|
---|
| 308 | return (u1 + t * (u2 - u1)) > thresh; //-Limits::Small;
|
---|
[162] | 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | // intersection with the polygonal face of the mesh
|
---|
| 314 | int
|
---|
| 315 | Mesh::RayFaceIntersection(const int faceIndex,
|
---|
[492] | 316 | const Ray &ray,
|
---|
| 317 | float &t,
|
---|
| 318 | const float nearestT
|
---|
| 319 | )
|
---|
[162] | 320 | {
|
---|
| 321 | Face *face = mFaces[faceIndex];
|
---|
| 322 |
|
---|
| 323 | Plane3 plane = GetFacePlane(faceIndex);
|
---|
| 324 | float dot = DotProd(plane.mNormal, ray.GetDir());
|
---|
[878] | 325 |
|
---|
| 326 | if (MeshDebug) {
|
---|
| 327 | cout<<endl<<endl;
|
---|
| 328 | cout<<"normal="<<plane.mNormal<<endl;
|
---|
| 329 | cout<<"dot="<<dot<<endl;
|
---|
| 330 | }
|
---|
[162] | 331 |
|
---|
[878] | 332 |
|
---|
[162] | 333 | // Watch for near-zero denominator
|
---|
| 334 | // ONLY single sided polygons!!!!!
|
---|
[534] | 335 | if (ray.mFlags & Ray::CULL_BACKFACES) {
|
---|
| 336 | if (dot > -Limits::Small)
|
---|
| 337 | // if (fabs(dot) < Limits::Small)
|
---|
| 338 | return Ray::NO_INTERSECTION;
|
---|
| 339 | } else {
|
---|
| 340 | if (fabs(dot) < Limits::Small)
|
---|
| 341 | return Ray::NO_INTERSECTION;
|
---|
| 342 | }
|
---|
[162] | 343 |
|
---|
| 344 | t = (-plane.mD - DotProd(plane.mNormal, ray.GetLoc())) / dot;
|
---|
[878] | 345 |
|
---|
| 346 | if (MeshDebug)
|
---|
| 347 | cout<<"t="<<t<<endl;
|
---|
| 348 |
|
---|
[162] | 349 | if (t <= Limits::Small)
|
---|
| 350 | return Ray::INTERSECTION_OUT_OF_LIMITS;
|
---|
| 351 |
|
---|
| 352 | if (t >= nearestT) {
|
---|
| 353 | return Ray::INTERSECTION_OUT_OF_LIMITS; // no intersection was found
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | int count = 0;
|
---|
| 357 | float u, v, u1, v1, u2, v2;
|
---|
| 358 | int i;
|
---|
| 359 |
|
---|
| 360 | int paxis = plane.mNormal.DrivingAxis();
|
---|
| 361 |
|
---|
[878] | 362 |
|
---|
[162] | 363 | // Project the intersection point onto the coordinate plane
|
---|
| 364 | // specified by which.
|
---|
| 365 | ray.Extrap(t).ExtractVerts(&u, &v, paxis);
|
---|
| 366 |
|
---|
| 367 |
|
---|
[469] | 368 | int size = (int)face->mVertexIndices.size();
|
---|
[170] | 369 |
|
---|
[878] | 370 | if (MeshDebug)
|
---|
| 371 | cout<<"size="<<size<<endl;
|
---|
| 372 |
|
---|
[170] | 373 | mVertices[face->mVertexIndices[size - 1]].
|
---|
[162] | 374 | ExtractVerts(&u1, &v1, paxis );
|
---|
[170] | 375 |
|
---|
[752] | 376 | //$$JB changed 12.4.2006 from 0 ^^
|
---|
[170] | 377 | if (0 && size <= 4) {
|
---|
[162] | 378 | // assume a convex face
|
---|
[170] | 379 | for (i = 0; i < size; i++) {
|
---|
[162] | 380 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 381 | // line u1, v1, u2, v2
|
---|
[878] | 382 | if ((v1 - v2)*(u - u1) + (u2 - u1)*(v - v1) > 0) {
|
---|
| 383 | if (MeshDebug)
|
---|
| 384 | cout<<"exit on "<<i<<endl;
|
---|
[492] | 385 | return Ray::NO_INTERSECTION;
|
---|
[878] | 386 | }
|
---|
[162] | 387 | u1 = u2;
|
---|
| 388 | v1 = v2;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | return Ray::INTERSECTION;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | // We're stuck with the Jordan curve computation. Count number
|
---|
| 395 | // of intersections between the line segments the polygon comprises
|
---|
| 396 | // with a ray originating at the point of intersection and
|
---|
| 397 | // travelling in the positive X direction.
|
---|
[170] | 398 | for (i = 0; i < size; i++) {
|
---|
[162] | 399 | mVertices[face->mVertexIndices[i]].ExtractVerts(&u2, &v2, paxis);
|
---|
| 400 | count += (int_lineseg(u, v, u1, v1, u2, v2) != 0);
|
---|
| 401 | u1 = u2;
|
---|
| 402 | v1 = v2;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[878] | 405 | if (MeshDebug)
|
---|
| 406 | cout<<"count="<<count<<endl;
|
---|
| 407 |
|
---|
[162] | 408 | // We hit polygon if number of intersections is odd.
|
---|
| 409 | return (count & 1) ? Ray::INTERSECTION : Ray::NO_INTERSECTION;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[349] | 412 | int
|
---|
[176] | 413 | Mesh::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 414 | {
|
---|
[1020] | 415 | const int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[176] | 416 |
|
---|
| 417 | // assume the face is convex and generate a convex combination
|
---|
| 418 | //
|
---|
| 419 | Face *face = mFaces[faceIndex];
|
---|
[1020] | 420 |
|
---|
[176] | 421 | point = Vector3(0,0,0);
|
---|
| 422 | float sum = 0.0f;
|
---|
[1020] | 423 |
|
---|
[176] | 424 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
| 425 | float r = RandomValue(0,1);
|
---|
| 426 | sum += r;
|
---|
| 427 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
| 428 | }
|
---|
| 429 | point *= 1.0f/sum;
|
---|
[340] | 430 |
|
---|
| 431 | normal = GetFacePlane(faceIndex).mNormal;
|
---|
[349] | 432 |
|
---|
| 433 | return faceIndex;
|
---|
[176] | 434 | }
|
---|
| 435 |
|
---|
[162] | 436 | int
|
---|
[354] | 437 | Mesh::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
[1001] | 438 | Vector3 &normal,
|
---|
| 439 | const Vector3 &viewpoint,
|
---|
| 440 | const int maxTries)
|
---|
[354] | 441 | {
|
---|
[359] | 442 | Plane3 plane;
|
---|
[485] | 443 | int faceIndex = (int)RandomValue(0, (Real)((int)mFaces.size()-1));
|
---|
[359] | 444 | int tries;
|
---|
| 445 | for (tries = 0; tries < maxTries; tries++) {
|
---|
| 446 | Face *face = mFaces[faceIndex];
|
---|
| 447 | plane = GetFacePlane(faceIndex);
|
---|
| 448 |
|
---|
| 449 | if (plane.Side(viewpoint) > 0) {
|
---|
| 450 | point = Vector3(0,0,0);
|
---|
| 451 | float sum = 0.0f;
|
---|
| 452 | // pickup a point inside this triangle
|
---|
| 453 | for (int i = 0; i < face->mVertexIndices.size(); i++) {
|
---|
[354] | 454 | float r = RandomValue(0,1);
|
---|
| 455 | sum += r;
|
---|
| 456 | point += mVertices[face->mVertexIndices[i]]*r;
|
---|
[359] | 457 | }
|
---|
| 458 | point *= 1.0f/sum;
|
---|
| 459 | break;
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | normal = plane.mNormal;
|
---|
| 464 | return (tries < maxTries) ? faceIndex + 1 : 0;
|
---|
[354] | 465 | }
|
---|
| 466 |
|
---|
| 467 |
|
---|
[162] | 468 | Plane3
|
---|
| 469 | Mesh::GetFacePlane(const int faceIndex)
|
---|
| 470 | {
|
---|
| 471 | Face *face = mFaces[faceIndex];
|
---|
[340] | 472 | return Plane3(mVertices[face->mVertexIndices[0]],
|
---|
[333] | 473 | mVertices[face->mVertexIndices[1]],
|
---|
| 474 | mVertices[face->mVertexIndices[2]]);
|
---|
[162] | 475 | }
|
---|
| 476 |
|
---|
[340] | 477 | bool
|
---|
| 478 | Mesh::ValidateFace(const int i)
|
---|
| 479 | {
|
---|
| 480 | Face *face = mFaces[i];
|
---|
| 481 |
|
---|
| 482 | Plane3 plane = Plane3(mVertices[face->mVertexIndices[0]],
|
---|
[1076] | 483 | mVertices[face->mVertexIndices[1]],
|
---|
| 484 | mVertices[face->mVertexIndices[2]]);
|
---|
[340] | 485 |
|
---|
| 486 | if (!eq(Magnitude(plane.mNormal), 1.0f))
|
---|
| 487 | return false;
|
---|
[350] | 488 |
|
---|
| 489 | return true;
|
---|
[340] | 490 | }
|
---|
| 491 |
|
---|
| 492 | void
|
---|
| 493 | Mesh::Cleanup()
|
---|
| 494 | {
|
---|
| 495 | int toRemove = 0;
|
---|
| 496 | FaceContainer newFaces;
|
---|
| 497 | for (int i=0; i < mFaces.size(); i++)
|
---|
| 498 | if (ValidateFace(i)) {
|
---|
| 499 | newFaces.push_back(mFaces[i]);
|
---|
| 500 | } else {
|
---|
| 501 | cout<<"d";
|
---|
| 502 | delete mFaces[i];
|
---|
| 503 | toRemove++;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | if (toRemove) {
|
---|
| 507 | mFaces = newFaces;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | // cleanup vertices??
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[170] | 513 |
|
---|
[191] | 514 | void
|
---|
| 515 | Mesh::AddTriangle(const Triangle3 &triangle)
|
---|
| 516 | {
|
---|
[469] | 517 | int index = (int)mVertices.size();
|
---|
[191] | 518 |
|
---|
| 519 | for (int i=0; i < 3; i++) {
|
---|
| 520 | mVertices.push_back(triangle.mVertices[i]);
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | AddFace(new Face(index + 0, index + 1, index + 2) );
|
---|
| 524 | }
|
---|
[209] | 525 |
|
---|
| 526 | void
|
---|
| 527 | Mesh::AddRectangle(const Rectangle3 &rect)
|
---|
| 528 | {
|
---|
[469] | 529 | int index = (int)mVertices.size();
|
---|
[209] | 530 |
|
---|
| 531 | for (int i=0; i < 4; i++) {
|
---|
| 532 | mVertices.push_back(rect.mVertices[i]);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | AddFace(new Face(index + 0, index + 1, index + 2, index + 3) );
|
---|
| 536 | }
|
---|
[750] | 537 |
|
---|
[752] | 538 | void
|
---|
| 539 | Mesh::AssignRandomMaterial()
|
---|
[1001] | 540 | {
|
---|
| 541 | mMaterial = MaterialManager::GetSingleton()->CreateResource();
|
---|
| 542 |
|
---|
| 543 | Material randMat = RandomMaterial();
|
---|
[750] | 544 |
|
---|
[1001] | 545 | mMaterial->mDiffuseColor = randMat.mDiffuseColor;
|
---|
| 546 | mMaterial->mSpecularColor = randMat.mSpecularColor;
|
---|
| 547 | mMaterial->mAmbientColor = randMat.mAmbientColor;
|
---|
[752] | 548 | }
|
---|
[750] | 549 |
|
---|
[752] | 550 |
|
---|
[991] | 551 | Mesh *CreateMeshFromBox(const AxisAlignedBox3 &box)
|
---|
[750] | 552 | {
|
---|
[1001] | 553 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
| 554 |
|
---|
| 555 | // add 8 vertices of the box
|
---|
| 556 | const int index = (int)mesh->mVertices.size();
|
---|
| 557 |
|
---|
| 558 | for (int i=0; i < 8; ++ i)
|
---|
| 559 | {
|
---|
| 560 | Vector3 v;
|
---|
| 561 | box.GetVertex(i, v);
|
---|
| 562 | mesh->mVertices.push_back(v);
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 566 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 567 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
[750] | 568 |
|
---|
[1001] | 569 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 570 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 571 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
[750] | 572 |
|
---|
[1001] | 573 | return mesh;
|
---|
| 574 | }
|
---|
| 575 |
|
---|
| 576 |
|
---|
| 577 | Mesh::Mesh(const int id, const int vertices, const int faces):
|
---|
| 578 | mFaces(),
|
---|
| 579 | mMaterial(NULL),
|
---|
| 580 | mKdTree(NULL),
|
---|
| 581 | mVertices(),
|
---|
| 582 | mIsConvex(false),
|
---|
| 583 | mIsWatertight(false),
|
---|
| 584 | mId(id)
|
---|
| 585 | {
|
---|
| 586 | mVertices.reserve(vertices);
|
---|
| 587 | mFaces.reserve(faces);
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 |
|
---|
| 591 | Mesh::Mesh(const int id):
|
---|
| 592 | mId(id), mVertices(), mFaces(), mMaterial(NULL), mKdTree(NULL)
|
---|
| 593 | {}
|
---|
| 594 |
|
---|
| 595 |
|
---|
| 596 | // apply transformation to each vertex
|
---|
| 597 | void Mesh::ApplyTransformation(const Matrix4x4 &m)
|
---|
| 598 | {
|
---|
| 599 | VertexContainer::iterator it, it_end = mVertices.end();
|
---|
| 600 |
|
---|
| 601 | for (it = mVertices.begin(); it != it_end; ++ it)
|
---|
| 602 | {
|
---|
| 603 | (*it) = m * (*it);
|
---|
| 604 | }
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 |
|
---|
[1020] | 608 | Mesh::Mesh(const Mesh &rhs):
|
---|
| 609 | mKdTree(NULL)
|
---|
[1001] | 610 | {
|
---|
| 611 | mVertices = rhs.mVertices;
|
---|
| 612 | mFaces.reserve(rhs.mFaces.size());
|
---|
| 613 | mId = rhs.mId;
|
---|
[1002] | 614 | mMaterial = rhs.mMaterial;
|
---|
[1020] | 615 |
|
---|
[1001] | 616 | FaceContainer::const_iterator it, it_end = rhs.mFaces.end();
|
---|
| 617 |
|
---|
| 618 | for (it = rhs.mFaces.begin(); it != it_end; ++ it)
|
---|
| 619 | {
|
---|
| 620 | Face *face = *it;
|
---|
| 621 | mFaces.push_back(new Face(*face));
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 |
|
---|
| 626 | Mesh& Mesh::operator=(const Mesh& m)
|
---|
| 627 | {
|
---|
| 628 | if (this == &m)
|
---|
| 629 | return *this;
|
---|
| 630 |
|
---|
| 631 | CLEAR_CONTAINER(mFaces);
|
---|
| 632 |
|
---|
| 633 | mVertices = m.mVertices;
|
---|
| 634 | mFaces.reserve(m.mFaces.size());
|
---|
[1002] | 635 | mMaterial = m.mMaterial;
|
---|
| 636 | // note: we don't copy id on purpose
|
---|
[1001] | 637 | //mId = m.mId;
|
---|
[1002] | 638 |
|
---|
[1001] | 639 | FaceContainer::const_iterator it, it_end = m.mFaces.end();
|
---|
| 640 |
|
---|
| 641 | for (it = m.mFaces.begin(); it != it_end; ++ it)
|
---|
| 642 | {
|
---|
| 643 | Face *face = *it;
|
---|
| 644 | mFaces.push_back(new Face(*face));
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | return *this;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 |
|
---|
[1005] | 651 | Mesh::~Mesh()
|
---|
| 652 | {
|
---|
| 653 | for (int i=0; i < mFaces.size(); ++ i)
|
---|
| 654 | delete mFaces[i];
|
---|
[1001] | 655 |
|
---|
[1005] | 656 | DEL_PTR(mKdTree);
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | void Mesh::Clear()
|
---|
| 661 | {
|
---|
| 662 | mVertices.clear();
|
---|
| 663 | mFaces.clear();
|
---|
| 664 |
|
---|
| 665 | DEL_PTR(mKdTree);
|
---|
| 666 | }
|
---|
| 667 |
|
---|
[1001] | 668 | /********************************************************/
|
---|
| 669 | /* MeshInstance implementation */
|
---|
| 670 | /********************************************************/
|
---|
| 671 |
|
---|
| 672 | int
|
---|
| 673 | MeshInstance::CastRay(
|
---|
| 674 | Ray &ray
|
---|
| 675 | )
|
---|
| 676 | {
|
---|
| 677 | int res = mMesh->CastRay(ray, this);
|
---|
| 678 | return res;
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | int
|
---|
| 682 | MeshInstance::CastRay(
|
---|
| 683 | Ray &ray,
|
---|
| 684 | const vector<int> &faces
|
---|
| 685 | )
|
---|
| 686 | {
|
---|
| 687 | return mMesh->CastRayToSelectedFaces(ray, faces, this);
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 |
|
---|
| 691 |
|
---|
| 692 | int
|
---|
| 693 | MeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 694 | {
|
---|
| 695 | return mMesh->GetRandomSurfacePoint(point, normal);
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | int
|
---|
| 699 | MeshInstance::GetRandomVisibleSurfacePoint(Vector3 &point,
|
---|
| 700 | Vector3 &normal,
|
---|
| 701 | const Vector3 &viewpoint,
|
---|
| 702 | const int maxTries)
|
---|
| 703 | {
|
---|
| 704 | return mMesh->GetRandomVisibleSurfacePoint(point, normal, viewpoint, maxTries);
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 |
|
---|
| 708 | void MeshInstance::SetMaterial(Material *mat)
|
---|
| 709 | {
|
---|
| 710 | mMaterial = mat;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | Material *MeshInstance::GetMaterial() const
|
---|
| 714 | {
|
---|
| 715 | return mMaterial;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 |
|
---|
| 719 | /*************************************************************/
|
---|
[1002] | 720 | /* TransformedMeshInstance implementation */
|
---|
[1001] | 721 | /*************************************************************/
|
---|
| 722 |
|
---|
| 723 | TransformedMeshInstance::TransformedMeshInstance(Mesh *mesh):
|
---|
| 724 | MeshInstance(mesh)
|
---|
| 725 | {
|
---|
| 726 | mWorldTransform = IdentityMatrix();
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 |
|
---|
| 730 | int TransformedMeshInstance::GetRandomSurfacePoint(Vector3 &point, Vector3 &normal)
|
---|
| 731 | {
|
---|
| 732 | int index = mMesh->GetRandomSurfacePoint(point, normal);
|
---|
| 733 | point = mWorldTransform*point;
|
---|
| 734 | normal = TransformNormal(mWorldTransform, normal);
|
---|
| 735 | return index;
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 |
|
---|
| 739 | int TransformedMeshInstance::CastRay(Ray &ray)
|
---|
| 740 | {
|
---|
[1004] | 741 | ray.ApplyTransform(Invert(mWorldTransform));
|
---|
| 742 |
|
---|
| 743 | const int res = mMesh->CastRay(ray, this);
|
---|
| 744 | ray.ApplyTransform(mWorldTransform);
|
---|
| 745 |
|
---|
| 746 | return res;
|
---|
[863] | 747 | }
|
---|
| 748 |
|
---|
[1004] | 749 | int TransformedMeshInstance::CastRay(Ray &ray, const vector<int> &faces)
|
---|
| 750 | {
|
---|
| 751 | ray.ApplyTransform(Invert(mWorldTransform));
|
---|
[1001] | 752 |
|
---|
[1004] | 753 | const int res = mMesh->CastRayToSelectedFaces(ray, faces, this);
|
---|
| 754 | ray.ApplyTransform(mWorldTransform);
|
---|
| 755 |
|
---|
| 756 | return res;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 |
|
---|
[1001] | 760 | void TransformedMeshInstance::ApplyWorldTransform(const Matrix4x4 &m)
|
---|
| 761 | {
|
---|
| 762 | mWorldTransform = m * mWorldTransform;
|
---|
[878] | 763 | }
|
---|
[1001] | 764 |
|
---|
| 765 |
|
---|
| 766 | void TransformedMeshInstance::LoadWorldTransform(const Matrix4x4 &m)
|
---|
| 767 | {
|
---|
| 768 | mWorldTransform = m;
|
---|
| 769 | }
|
---|
| 770 |
|
---|
| 771 |
|
---|
[1020] | 772 | void TransformedMeshInstance::GetWorldTransform(Matrix4x4 &m) const
|
---|
[1001] | 773 | {
|
---|
| 774 | m = mWorldTransform;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 |
|
---|
[1020] | 778 | AxisAlignedBox3 TransformedMeshInstance::GetBox() const
|
---|
[1001] | 779 | {
|
---|
| 780 | return Transform(mMesh->mBox, mWorldTransform);
|
---|
| 781 | }
|
---|
| 782 |
|
---|
[1020] | 783 |
|
---|
| 784 | void TransformedMeshInstance::GetTransformedMesh(Mesh &transformedMesh) const
|
---|
| 785 | {
|
---|
| 786 | // copy mesh
|
---|
[1002] | 787 | transformedMesh = *mMesh;
|
---|
| 788 | transformedMesh.ApplyTransformation(mWorldTransform);
|
---|
[1020] | 789 | }
|
---|
[1001] | 790 |
|
---|
| 791 | }
|
---|