我目前正在try 创建一个可以渲染体素和平面的最小3D引擎,但我遇到了一点小问题.现在,我正在try 开发一种算法,根据物体在相机上的位置对其进行分类.后面更远的东西总是先绘制的,如果它们的Z轴(相机正前方的距离)相同,它会绘制离屏幕中心更远的那个.
我有一个版本,我知道它可以在我的PC上运行,既可以使用gcc
,也可以使用clang
,但当我使用我正在开发的设备的工具链编译它时,它开始出现故障,并不完全符合我的要求.输出每隔一次更改一次.我可以做些什么来修复它,或者实现一个独立于我正在使用的Toochain的qsort
函数,我能有什么建议吗?
// includes
int8_t player_x = 0;
int8_t player_y = 0;
int8_t player_z = 0;
int compareCoordinates(const void *a, const void *b) {
int8_t *coord1 = (int8_t *)a;
int8_t *coord2 = (int8_t *)b;
if ((coord2[2] - coord1[2]) != 0) {
return (coord2[2] - coord1[2]);
}
int value = (abs(coord2[0] - player_x) + abs(coord2[1] - player_y)) - (abs(coord1[0] - player_y) + abs(coord1[1] - player_y));
if (value == 0){
return 1;
}
return value;
}
void sortCoordinateList(int8_t coordinates[][3], int numCoordinates) {
qsort(coordinates, numCoordinates, sizeof(coordinates[0]), compareCoordinates);
}
int8_t coordinates[][3] = {
{1, 2, 5},
{-1, 2, 5},
{-3, 2, 5},
};
int main(void)
{
sortCoordinateList(coordinates, LEN(coordinates));
for (int i = 0; i < LEN(coordinates); i++) {
dbg_printf("(%d, %d, %d)\n", coordinates[i][0], coordinates[i][1], coordinates[i][2]);
}
dbg_printf("\n\n");
}
这是删除移动函数的代码片段,但这是主要内容.我对C编程相当陌生,但我知道qsort
会随机处理具有相同值的项或类似的东西,称为stable sorting
,但我非常确信情况并非如此.
我try 了多种不同的调试和测试方法,但我完全被难住了.通常,当我遇到错误时,我会自己找出来,但我不得不屈服并提出我的第一个StackOverflow问题.顺便说一句,从今天到昨天,我已经为此工作了至少13-15个小时,所以是的,我已经尽我所能了.