Important - vertnum is the index into the face_verts[] array in the face structure, not an index into the verts[] array of the room structure
| 637 | // Important - vertnum is the index into the face_verts[] array in the face structure, |
| 638 | // not an index into the verts[] array of the room structure |
| 639 | void GetUVLForRoomPoint(int roomnum, int facenum, int vertnum, roomUVL *uvl) { |
| 640 | matrix face_matrix, trans_matrix; |
| 641 | vector fvec; |
| 642 | vector avg_vert; |
| 643 | vector verts[MAX_VERTS_PER_FACE]; |
| 644 | vector rot_vert; |
| 645 | |
| 646 | ASSERT(Rooms[roomnum].used); |
| 647 | ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3); |
| 648 | |
| 649 | // find the center point of this face |
| 650 | vm_MakeZero(&avg_vert); |
| 651 | for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) |
| 652 | avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]]; |
| 653 | |
| 654 | avg_vert /= i; |
| 655 | |
| 656 | // Make the orientation matrix |
| 657 | // Reverse the normal because we're looking "at" the face, not from it |
| 658 | fvec = -Rooms[roomnum].faces[facenum].normal; |
| 659 | |
| 660 | vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL); |
| 661 | // Make the transformation matrix |
| 662 | |
| 663 | angvec avec; |
| 664 | vm_ExtractAnglesFromMatrix(&avec, &face_matrix); |
| 665 | vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b); |
| 666 | |
| 667 | // Rotate all the points |
| 668 | for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) { |
| 669 | vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]]; |
| 670 | |
| 671 | vert -= avg_vert; |
| 672 | vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix); |
| 673 | |
| 674 | verts[i] = rot_vert; |
| 675 | } |
| 676 | |
| 677 | // Find left most point |
| 678 | int leftmost_point = -1; |
| 679 | float leftmost_x = 900000.00f; // a big number |
| 680 | |
| 681 | for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) { |
| 682 | if (verts[i].x < leftmost_x) { |
| 683 | leftmost_point = i; |
| 684 | leftmost_x = verts[i].x; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | ASSERT(leftmost_point != -1); |
| 689 | |
| 690 | // Find top most point |
| 691 | int topmost_point = -1; |
| 692 | float topmost_y = -900000.0f; // a big number |
| 693 | |
| 694 | for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) { |
| 695 | if (verts[i].y > topmost_y) { |
| 696 | topmost_point = i; |
no test coverage detected