based on: https://blackpawn.com/texts/lightmaps/default.html
| 717 | |
| 718 | // based on: https://blackpawn.com/texts/lightmaps/default.html |
| 719 | uint32_t Tr2LightManager::InsertAtlasNode( std::vector<AtlasNode>& atlasNodes, uint32_t nodeId, uint32_t lightIndex, int32_t width, int32_t height ) |
| 720 | { |
| 721 | if( atlasNodes[nodeId].children[0] != -1 || atlasNodes[nodeId].children[1] != -1 ) |
| 722 | { |
| 723 | uint32_t newNode = InsertAtlasNode( atlasNodes, atlasNodes[nodeId].children[0], lightIndex, width, height ); |
| 724 | if( newNode != -1 ) |
| 725 | { |
| 726 | return newNode; |
| 727 | } |
| 728 | return InsertAtlasNode( atlasNodes, atlasNodes[nodeId].children[1], lightIndex, width, height ); |
| 729 | } |
| 730 | else |
| 731 | { |
| 732 | if( atlasNodes[nodeId].lightIndex != -1 ) |
| 733 | { |
| 734 | return -1; |
| 735 | } |
| 736 | if( atlasNodes[nodeId].width < width || atlasNodes[nodeId].height < height ) |
| 737 | { |
| 738 | return -1; |
| 739 | } |
| 740 | if( atlasNodes[nodeId].width == width && atlasNodes[nodeId].height == height ) |
| 741 | { |
| 742 | atlasNodes[nodeId].lightIndex = lightIndex; |
| 743 | return nodeId; |
| 744 | } |
| 745 | |
| 746 | atlasNodes.insert( atlasNodes.end(), 2, AtlasNode() ); |
| 747 | |
| 748 | atlasNodes[nodeId].children[0] = uint32_t( atlasNodes.size() - 2 ); |
| 749 | atlasNodes[nodeId].children[1] = uint32_t( atlasNodes.size() - 1 ); |
| 750 | |
| 751 | AtlasNode& child0 = atlasNodes[atlasNodes[nodeId].children[0]]; |
| 752 | AtlasNode& child1 = atlasNodes[atlasNodes[nodeId].children[1]]; |
| 753 | |
| 754 | int32_t deltaWidth = atlasNodes[nodeId].width - width; |
| 755 | int32_t deltaHeight = atlasNodes[nodeId].height - height; |
| 756 | |
| 757 | if( deltaWidth > deltaHeight ) |
| 758 | { |
| 759 | child0.x = atlasNodes[nodeId].x; |
| 760 | child0.y = atlasNodes[nodeId].y; |
| 761 | child0.width = width; |
| 762 | child0.height = atlasNodes[nodeId].height; |
| 763 | child1.x = atlasNodes[nodeId].x + width; |
| 764 | child1.y = atlasNodes[nodeId].y; |
| 765 | child1.width = atlasNodes[nodeId].width - width; |
| 766 | child1.height = atlasNodes[nodeId].height; |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | child0.x = atlasNodes[nodeId].x; |
| 771 | child0.y = atlasNodes[nodeId].y; |
| 772 | child0.width = atlasNodes[nodeId].width; |
| 773 | child0.height = height; |
| 774 | child1.x = atlasNodes[nodeId].x; |
| 775 | child1.y = atlasNodes[nodeId].y + height; |
| 776 | child1.width = atlasNodes[nodeId].width; |