text
stringlengths
0
2.2M
int minx = (int)floorf(cx - maxr*ics);
int maxx = (int)floorf(cx + maxr*ics);
int minz = (int)floorf(cz - maxr*ics);
int maxz = (int)floorf(cz + maxr*ics);
int miny = (int)floorf((center[1]-halfExtents[1]-orig[1])*ich);
int maxy = (int)floorf((center[1]+halfExtents[1]-orig[1])*ich);
if (maxx < 0) return DT_SUCCESS;
if (minx >= w) return DT_SUCCESS;
if (maxz < 0) return DT_SUCCESS;
if (minz >= h) return DT_SUCCESS;
if (minx < 0) minx = 0;
if (maxx >= w) maxx = w-1;
if (minz < 0) minz = 0;
if (maxz >= h) maxz = h-1;
float xhalf = halfExtents[0]*ics + 0.5f;
float zhalf = halfExtents[2]*ics + 0.5f;
for (int z = minz; z <= maxz; ++z)
{
for (int x = minx; x <= maxx; ++x)
{
float x2 = 2.0f*(float(x) - cx);
float z2 = 2.0f*(float(z) - cz);
float xrot = rotAux[1]*x2 + rotAux[0]*z2;
if (xrot > xhalf || xrot < -xhalf)
continue;
float zrot = rotAux[1]*z2 - rotAux[0]*x2;
if (zrot > zhalf || zrot < -zhalf)
continue;
const int y = layer.heights[x+z*w];
if (y < miny || y > maxy)
continue;
layer.areas[x+z*w] = areaId;
}
}
return DT_SUCCESS;
}
dtStatus dtBuildTileCacheLayer(dtTileCacheCompressor* comp,
dtTileCacheLayerHeader* header,
const unsigned char* heights,
const unsigned char* areas,
const unsigned char* cons,
unsigned char** outData, int* outDataSize)
{
const int headerSize = dtAlign4(sizeof(dtTileCacheLayerHeader));
const int gridSize = (int)header->width * (int)header->height;
const int maxDataSize = headerSize + comp->maxCompressedSize(gridSize*3);
unsigned char* data = (unsigned char*)dtAlloc(maxDataSize, DT_ALLOC_PERM);
if (!data)
return DT_FAILURE | DT_OUT_OF_MEMORY;
memset(data, 0, maxDataSize);
// Store header
memcpy(data, header, sizeof(dtTileCacheLayerHeader));
// Concatenate grid data for compression.
const int bufferSize = gridSize*3;
unsigned char* buffer = (unsigned char*)dtAlloc(bufferSize, DT_ALLOC_TEMP);
if (!buffer)
{
dtFree(data);
return DT_FAILURE | DT_OUT_OF_MEMORY;
}
memcpy(buffer, heights, gridSize);
memcpy(buffer+gridSize, areas, gridSize);
memcpy(buffer+gridSize*2, cons, gridSize);
// Compress
unsigned char* compressed = data + headerSize;
const int maxCompressedSize = maxDataSize - headerSize;
int compressedSize = 0;
dtStatus status = comp->compress(buffer, bufferSize, compressed, maxCompressedSize, &compressedSize);
if (dtStatusFailed(status))
{
dtFree(buffer);
dtFree(data);
return status;
}
*outData = data;
*outDataSize = headerSize + compressedSize;
dtFree(buffer);
return DT_SUCCESS;
}
void dtFreeTileCacheLayer(dtTileCacheAlloc* alloc, dtTileCacheLayer* layer)
{
dtAssert(alloc);
// The layer is allocated as one conitguous blob of data.
alloc->free(layer);
}