| 333 | |
| 334 | //save these values for convenience |
| 335 | u16 index1 = u16(((j+0)*vsize+(i+0))+base); |
| 336 | u16 index2 = u16(((j+0)*vsize+(i+1))+base); |
| 337 | u16 index3 = u16(((j+1)*vsize+(i+1))+base); |
| 338 | u16 index4 = u16(((j+1)*vsize+(i+0))+base); |
| 339 | |
| 340 | //calculate triangulation |
| 341 | //reminder: incrementing i shifts x coordinates, j shifts z |
| 342 | //first, get all the positions of the vertices in world space |
| 343 | CVector3D v1 = m_Vertices[(j+0)*vsize+(i+0)].m_Position; |
| 344 | CVector3D v2 = m_Vertices[(j+0)*vsize+(i+1)].m_Position; |
| 345 | CVector3D v3 = m_Vertices[(j+1)*vsize+(i+1)].m_Position; |
| 346 | CVector3D v4 = m_Vertices[(j+1)*vsize+(i+0)].m_Position; |
| 347 | |
| 348 | /* |
| 349 | debug_printf(L"Vertex 1 is at position: %f, %f, %f\n", v1.X, v1.Y, v1.Z); |
| 350 | debug_printf(L"Vertex 2 is at position: %f, %f, %f\n", v2.X, v2.Y, v2.Z); |
| 351 | debug_printf(L"Vertex 3 is at position: %f, %f, %f\n", v3.X, v3.Y, v3.Z); |
| 352 | debug_printf(L"Vertex 4 is at position: %f, %f, %f\n", v4.X, v4.Y, v4.Z); |
| 353 | */ |
| 354 | |
| 355 | |
| 356 | //we have two possible triangulations to check: (123, 143) and (234, 214) |
| 357 | //our goal is to pick the one that minimizes the angle between the planes formed by the triangles |
| 358 | CVector3D normal1 = (v2-v1).Cross(v3-v1); |
| 359 | CVector3D normal2 = (v3-v1).Cross(v4-v1); |
| 360 | CVector3D normal3 = (v4-v2).Cross(v3-v2); |
| 361 | CVector3D normal4 = (v1-v2).Cross(v4-v2); |
| 362 | normal1.Normalize(); |
| 363 | normal2.Normalize(); |
| 364 | normal3.Normalize(); |
| 365 | normal4.Normalize(); |
| 366 | float angle1 = normal1.Dot(normal2); //since we don't need the exact angle, we can just use the value of the dot product |
| 367 | float angle2 = normal3.Dot(normal4); |
| 368 | |
| 369 | //cos gets smaller as the angle increases so we take the triangulation with the larger value |
| 370 | if (angle1 < angle2) |
| 371 | { |
| 372 | |
| 373 | |
| 374 | m_Indices.push_back(index3); |
| 375 | m_Indices.push_back(index4); |
| 376 | m_Indices.push_back(index2); |
| 377 | m_Indices.push_back(index2); |
| 378 | m_Indices.push_back(index4); |
| 379 | m_Indices.push_back(index1); |
| 380 | |
| 381 | /* |
| 382 | //for triangle strip |
| 383 | m_Indices.push_back(index3); |
| 384 | m_Indices.push_back(index4); |
| 385 | m_Indices.push_back(index2); |
| 386 | m_Indices.push_back(index1); |
| 387 | */ |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | |
| 392 | m_Indices.push_back(index2); |
| 393 | m_Indices.push_back(index3); |
| 394 | m_Indices.push_back(index1); |
| 395 | m_Indices.push_back(index1); |
| 396 | m_Indices.push_back(index3); |
| 397 | m_Indices.push_back(index4); |
| 398 | |
| 399 | /* |
| 400 | //for triangle strip |
| 401 | m_Indices.push_back(index2); |
| 402 | m_Indices.push_back(index3); |
| 403 | m_Indices.push_back(index1); |
| 404 | m_Indices.push_back(index4); |
| 405 | */ |
| 406 | } |
| 407 | |
| 408 | /* |