我有一个我想用C渲染的实体列表,我的渲染缓冲区使用每个帧的堆插入来确保实体深度按顺序渲染。但是,由于某种原因,堆始终未排序。我已经查看了几十次代码,有朋友查看代码,我似乎无法找到为什么实体总是乱序。我希望也许一副新鲜的眼睛可以帮助我以我的方式看到错误。这是我的评论代码:
(请注意,x,y,z和深度(此处未使用)都存储为实体结构中的int
。
void AEC_RenderCatalogToBuffer(AEC_EntityCatalog* entityCatalog,
AEC_SpriteBuffer* spriteBuffer)
{
//The values we'll be using
unsigned int heap_size = 0;
unsigned int heap_at = 1;
int frame = spriteBuffer->frame + 1;
int x, y, z;
int temp_depth;
//Loop through all the entities
for (int search = 0; search < AEC_ENTITY_COUNT; search++)
{
// If an entity is drawable and it has an x, y, z,
// insert it into the buffer for rendering
if ( entityCatalog
->entity_components[search].component_mask[AEC_DRAWABLE]
&& entityCatalog
->entity_components[search].component_mask[AEC_DISPLACEMENT])
{
//Prepare data for heap insertion
temp_depth = AEC_GetIsoDepth(entityCatalog, search);
x = entityCatalog->displacement[search].x;
y = entityCatalog->displacement[search].y;
z = entityCatalog->displacement[search].z;
//Increase the heap size by 1, save the size as the end node
heap_size++;
heap_at = heap_size;
spriteBuffer->is_filled[heap_at] = frame;
// If the parent node is greater than 0,
// has a larger or equal y (depth)
// and is being drawn in the current frame
while ( (heap_at - 1)/2 > 0
&& spriteBuffer->y[(heap_at - 1) / 2] >= y
&& spriteBuffer->is_filled[(heap_at - 1) / 2] == frame
)
{
spriteBuffer->entity[heap_at]
= spriteBuffer->entity[(heap_at - 1)/2];
spriteBuffer->depth[heap_at]
= spriteBuffer->depth[(heap_at - 1)/2];
spriteBuffer->x[heap_at] = spriteBuffer->x[(heap_at - 1)/2];
spriteBuffer->y[heap_at] = spriteBuffer->y[(heap_at - 1)/2];
spriteBuffer->z[heap_at] = spriteBuffer->z[(heap_at - 1)/2];
heap_at = (heap_at - 1)/2;
}
// Place the new entity's information into
// the correct place in the array
spriteBuffer->is_filled[heap_at] = frame;
spriteBuffer->x[heap_at] = x;
spriteBuffer->y[heap_at] = y;
spriteBuffer->z[heap_at]= z;
spriteBuffer->entity[heap_at] = search;
spriteBuffer->depth[heap_at] = temp_depth;
}
}
// Once all the entities have submitted their draw depth
// and have been sorted by y-index,
// save the heap size and the current frame
spriteBuffer->size = heap_size;
spriteBuffer->frame = frame;
printf("Checking: ");
for (int q=0;q<heap_size+1;q++)
{
if (spriteBuffer->is_filled[q] == frame)
{
printf("%d ", spriteBuffer->y[q]);
}
}
printf("\n");
}
如何修复堆插入???谢谢!
答案 0 :(得分:0)
您实现的二进制最小堆仅保证两个子节点的父节点小于两个子节点(如@chtz所评论的那样)。但它并不能保证左孩子比正确孩子小。
例如,您的堆可能看起来像这样(index:value):
0:3
1:5 2:13
按索引顺序打印会给你3,5,13。这恰好是排序的。 但堆也可能看起来像这样,仍然是正确的:
0:3
1:15 2:13
您可能打算做的是实现二叉搜索树 在这样的树中(假设唯一值)
例如,当插入(5,1,2,3,20,10,24,12)时,你会得到这样一棵树:
5
1 20
_ 2 10 24
_ _ _ 3 _ 12 _ _
请注意,这样的树在数组中不紧凑,它通常是空的“_”
如果要执行此类树,则需要使用规则找到添加新成员的正确位置。向上交换,就像在堆中一样不需要
但是为了打印二叉树,必须遍历它。递归打印,首先是左边的孩子,然后是父母,然后是右边的孩子
遍历示例树将给出:
_,_,_,1,_,2,3,5,_,10,12,_,24,_
显然,有必要跟踪实际使用的节点或空(“_”);与堆相比。
在选择适当大小的树结构时,请考虑最坏的情况,即插入已排序的数据 例如,如果插入1,2,3,5,10,则得到:
1
_ 2
_ _ _ 3
_ _ _ _ _ _ _ 5
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 10
因此,为了处理N值,您需要在树中(2<<N)-1
个位置才能安全
或者使用由动态分配的内存和指针构建的树
如果你讨厌最坏的情况(很多人都这样做),那么看看自平衡树
例如。 Wikipedia