| 296 | */ |
| 297 | |
| 298 | //save these for convenience |
| 299 | CVector3D v1 = m_BlendVertices[splats[k].m_Indices[0]].m_Position; |
| 300 | CVector3D v2 = m_BlendVertices[splats[k].m_Indices[1]].m_Position; |
| 301 | CVector3D v3 = m_BlendVertices[splats[k].m_Indices[2]].m_Position; |
| 302 | CVector3D v4 = m_BlendVertices[splats[k].m_Indices[3]].m_Position; |
| 303 | |
| 304 | /* |
| 305 | debug_printf(L"Trying to add vertex1 (%f,%f,%f)\n", v1.X, v1.Y, v1.Z); |
| 306 | debug_printf(L"Trying to add vertex2 (%f,%f,%f)\n", v2.X, v2.Y, v2.Z); |
| 307 | debug_printf(L"Trying to add vertex3 (%f,%f,%f)\n", v3.X, v3.Y, v3.Z); |
| 308 | debug_printf(L"Trying to add vertex4 (%f,%f,%f)\n", v4.X, v4.Y, v4.Z); |
| 309 | */ |
| 310 | |
| 311 | //this is terrible, but: find the set of indices in m_Indices that correspond to the vertices |
| 312 | //being used by the current blend texture so that we can match up the triangulation used |
| 313 | std::list<unsigned short> IndexList; //store the order indices should be added to m_BlendIndices |
| 314 | for (int p = 0; p < m_Indices.size()-5; p++) |
| 315 | { |
| 316 | if ( |
| 317 | m_Vertices[m_Indices[p]-indexBase].m_Position == v1 || |
| 318 | m_Vertices[m_Indices[p]-indexBase].m_Position == v2 || |
| 319 | m_Vertices[m_Indices[p]-indexBase].m_Position == v3 || |
| 320 | m_Vertices[m_Indices[p]-indexBase].m_Position == v4 |
| 321 | ) |
| 322 | { |
| 323 | //debug_printf(L"Matched\n"); |
| 324 | //we've found a match to one of the vertices, but that does not necessarily mean we're at the right set of |
| 325 | //indices so we check the block of 6 vertices to see if they contain all the vertices we're using |
| 326 | for (int q = p; q < p+6; q++) |
| 327 | { |
| 328 | CVector3D f = m_Vertices[m_Indices[q]-indexBase].m_Position; |
| 329 | //debug_printf(L"Looking at (%f,%f,%f)\n", f.X, f.Y,f.Z); |
| 330 | if (m_Vertices[m_Indices[q]-indexBase].m_Position == v1) |
| 331 | { |
| 332 | IndexList.push_back(0); |
| 333 | } |
| 334 | else if (m_Vertices[m_Indices[q]-indexBase].m_Position == v2) |
| 335 | { |
| 336 | IndexList.push_back(1); |
| 337 | } |
| 338 | else if (m_Vertices[m_Indices[q]-indexBase].m_Position == v3) |
| 339 | { |
| 340 | IndexList.push_back(2); |
| 341 | } |
| 342 | else if (m_Vertices[m_Indices[q]-indexBase].m_Position == v4) |
| 343 | { |
| 344 | IndexList.push_back(3); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | //this means we've encountered a vertex that's not one of the ones we're looking for so exit out |
| 349 | CVector3D z = m_Vertices[m_Indices[q]-indexBase].m_Position; |
| 350 | CVector3D z2 = m_BlendVertices[splats[k].m_Indices[3]].m_Position; |
| 351 | //debug_printf(L"Failed to match (%f,%f,%f)\n", z.X,z.Y,z.Z); |
| 352 | IndexList.clear(); |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | if (IndexList.size() == 6) |
| 358 | { |
| 359 | //found our triangulation so early out for the search |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (IndexList.size() != 6) |
| 365 | { |
| 366 | //tried to use debug_assert(IndexList.size() == 6), but that started throwing memory access violations for some reason |
| 367 | debug_printf(L"WARNING: Blend Indices building failed. IndexList does not contain 6 elements.\n"); |
| 368 | debug_break(); |
| 369 | } |
| 370 | std::list<unsigned short>::iterator it; |
| 371 | //debug_printf(L"About to start adding indices\n"); |
| 372 | for (it = IndexList.begin(); it != IndexList.end(); it++) |
| 373 | { |
| 374 | //add the indices in the order we found them in |
| 375 | //debug_printf(L"%i",*it); |
| 376 | //debug_printf(L"\n"); |
| 377 | m_BlendIndices.push_back(splats[k].m_Indices[*it]+base); |
| 378 | } |
| 425 | |
| 426 | //save these values for convenience |
| 427 | u16 index1 = u16(((j+0)*vsize+(i+0))+base); |
| 428 | u16 index2 = u16(((j+0)*vsize+(i+1))+base); |
| 429 | u16 index3 = u16(((j+1)*vsize+(i+1))+base); |
| 430 | u16 index4 = u16(((j+1)*vsize+(i+0))+base); |
| 431 | |
| 432 | //calculate triangulation |
| 433 | //reminder: incrementing i shifts x coordinates, j shifts z |
| 434 | //first, get all the positions of the vertices in world space |
| 435 | CVector3D v1 = m_Vertices[(j+0)*vsize+(i+0)].m_Position; |
| 436 | CVector3D v2 = m_Vertices[(j+0)*vsize+(i+1)].m_Position; |
| 437 | CVector3D v3 = m_Vertices[(j+1)*vsize+(i+1)].m_Position; |
| 438 | CVector3D v4 = m_Vertices[(j+1)*vsize+(i+0)].m_Position; |
| 439 | |
| 440 | /* |
| 441 | debug_printf(L"Vertex 1 is at position: %f, %f, %f\n", v1.X, v1.Y, v1.Z); |
| 442 | debug_printf(L"Vertex 2 is at position: %f, %f, %f\n", v2.X, v2.Y, v2.Z); |
| 443 | debug_printf(L"Vertex 3 is at position: %f, %f, %f\n", v3.X, v3.Y, v3.Z); |
| 444 | debug_printf(L"Vertex 4 is at position: %f, %f, %f\n", v4.X, v4.Y, v4.Z); |
| 445 | */ |
| 446 | |
| 447 | |
| 448 | //we have two possible triangulations to check: (123, 143) and (234, 214) |
| 449 | //our goal is to pick the one that minimizes the angle between the planes formed by the triangles |
| 450 | //the normals are constructed in such a way as to avoid having to normalize the vectors for the dot product |
| 451 | CVector3D normal1 = (v1-v2).Cross(v3-v2); |
| 452 | CVector3D normal2 = (v1-v4).Cross(v3-v4); |
| 453 | CVector3D normal3 = (v4-v3).Cross(v2-v3); |
| 454 | CVector3D normal4 = (v4-v1).Cross(v2-v1); |
| 455 | float angle1 = normal1.Dot(normal2); //since we don't need the exact angle, we can just use the value of the dot product |
| 456 | float angle2 = normal3.Dot(normal4); |
| 457 | |
| 458 | //cos gets smaller as the angle increases so we take the triangulation with the larger value |
| 459 | if (angle1 < angle2) |
| 460 | { |
| 461 | |
| 462 | |
| 463 | m_Indices.push_back(index3); |
| 464 | m_Indices.push_back(index4); |
| 465 | m_Indices.push_back(index2); |
| 466 | m_Indices.push_back(index2); |
| 467 | m_Indices.push_back(index4); |
| 468 | m_Indices.push_back(index1); |
| 469 | |
| 470 | /* |
| 471 | //for triangle strip |
| 472 | m_Indices.push_back(index3); |
| 473 | m_Indices.push_back(index4); |
| 474 | m_Indices.push_back(index2); |
| 475 | m_Indices.push_back(index1); |
| 476 | */ |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | |
| 481 | m_Indices.push_back(index2); |
| 482 | m_Indices.push_back(index3); |
| 483 | m_Indices.push_back(index1); |
| 484 | m_Indices.push_back(index1); |
| 485 | m_Indices.push_back(index3); |
| 486 | m_Indices.push_back(index4); |
| 487 | |
| 488 | /* |
| 489 | //for triangle strip |
| 490 | m_Indices.push_back(index2); |
| 491 | m_Indices.push_back(index3); |
| 492 | m_Indices.push_back(index1); |
| 493 | m_Indices.push_back(index4); |
| 494 | */ |
| 495 | } |
| 496 | |
| 497 | /* |