| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | #ifndef VOX_LOADER_H |
| 51 | #define VOX_LOADER_H |
| 52 | |
| 53 | |
| 54 | #ifndef VOX_MALLOC |
| 55 | #define VOX_MALLOC(sz) malloc(sz) |
| 56 | #endif |
| 57 | #ifndef VOX_CALLOC |
| 58 | #define VOX_CALLOC(n,sz) calloc(n,sz) |
| 59 | #endif |
| 60 | #ifndef VOX_REALLOC |
| 61 | #define VOX_REALLOC(n,sz) realloc(n,sz) |
| 62 | #endif |
| 63 | #ifndef VOX_FREE |
| 64 | #define VOX_FREE(p) free(p) |
| 65 | #endif |
| 66 | |
| 67 | #define VOX_SUCCESS (0) |
| 68 | #define VOX_ERROR_FILE_NOT_FOUND (-1) |
| 69 | #define VOX_ERROR_INVALID_FORMAT (-2) |
| 70 | #define VOX_ERROR_FILE_VERSION_NOT_MATCH (-3) |
| 71 | |
| 72 | |
| 73 | typedef struct { |
| 74 | unsigned char r, g, b, a; |
| 75 | } VoxColor; |
| 76 | |
| 77 | |
| 78 | typedef struct { |
| 79 | float x, y, z; |
| 80 | } VoxVector3; |
| 81 | |
| 82 | typedef struct { |
| 83 | VoxVector3* array; |
| 84 | int used, size; |
| 85 | } ArrayVector3; |
| 86 | |
| 87 | typedef struct { |
| 88 | VoxColor* array; |
| 89 | int used, size; |
| 90 | } ArrayColor; |
| 91 | |
| 92 | typedef struct { |
| 93 | unsigned short* array; |
| 94 | int used, size; |
| 95 | } ArrayUShort; |
| 96 | |
| 97 | |
| 98 | typedef struct { |
| 99 | unsigned char* m_array; |
| 100 | int arraySize; |
| 101 | } CubeChunk3D; |
| 102 | |
| 103 | |
| 104 | |
| 105 | typedef struct { |
| 106 | |
| 107 | int sizeX; |
| 108 | int sizeY; |
| 109 | int sizeZ; |
| 110 | |
| 111 | |
| 112 | int chunksSizeX; |
| 113 | int chunksSizeY; |
| 114 | int chunksSizeZ; |
| 115 | |
| 116 | |
| 117 | CubeChunk3D* m_arrayChunks; |
| 118 | int arrayChunksSize; |
| 119 | |
| 120 | int ChunkFlattenOffset; |
| 121 | int chunksAllocated; |
| 122 | int chunksTotal; |
| 123 | |
| 124 | |
| 125 | ArrayVector3 vertices; |
| 126 | ArrayVector3 normals; |
| 127 | ArrayUShort indices; |
| 128 | ArrayColor colors; |
| 129 | |
| 130 | |
| 131 | VoxColor palette[256]; |
| 132 | |
| 133 | } VoxArray3D; |
| 134 | |
| 135 | #if defined(__cplusplus) |
| 136 | extern "C" { |
| 137 | #endif |
| 138 | |
| 139 | |
| 140 | int Vox_LoadFromMemory(unsigned char* pvoxData, unsigned int voxDataSize, VoxArray3D* pvoxarray); |
| 141 | void Vox_FreeArrays(VoxArray3D* voxarray); |
| 142 | |
| 143 | #ifdef __cplusplus |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #endif |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | #ifdef VOX_LOADER_IMPLEMENTATION |
| 158 | |
| 159 | #include <string.h> |
| 160 | #include <stdlib.h> |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | static void initArrayUShort(ArrayUShort* a, int initialSize) |
| 167 | { |
| 168 | a->array = (unsigned short *)VOX_MALLOC(initialSize * sizeof(unsigned short)); |
| 169 | a->used = 0; |
| 170 | a->size = initialSize; |
| 171 | } |
| 172 | |
| 173 | static void insertArrayUShort(ArrayUShort* a, unsigned short element) |
| 174 | { |
| 175 | if (a->used == a->size) |
| 176 | { |
| 177 | a->size *= 2; |
| 178 | a->array = (unsigned short *)VOX_REALLOC(a->array, a->size * sizeof(unsigned short)); |
| 179 | } |
| 180 | a->array[a->used++] = element; |
| 181 | } |
| 182 | |
| 183 | static void freeArrayUShort(ArrayUShort* a) |
| 184 | { |
| 185 | VOX_FREE(a->array); |
| 186 | a->array = NULL; |
| 187 | a->used = a->size = 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | static void initArrayVector3(ArrayVector3* a, int initialSize) |
| 196 | { |
| 197 | a->array = (VoxVector3 *)VOX_MALLOC(initialSize * sizeof(VoxVector3)); |
| 198 | a->used = 0; |
| 199 | a->size = initialSize; |
| 200 | } |
| 201 | |
| 202 | static void insertArrayVector3(ArrayVector3* a, VoxVector3 element) |
| 203 | { |
| 204 | if (a->used == a->size) |
| 205 | { |
| 206 | a->size *= 2; |
| 207 | a->array = (VoxVector3 *)VOX_REALLOC(a->array, a->size * sizeof(VoxVector3)); |
| 208 | } |
| 209 | a->array[a->used++] = element; |
| 210 | } |
| 211 | |
| 212 | static void freeArrayVector3(ArrayVector3* a) |
| 213 | { |
| 214 | VOX_FREE(a->array); |
| 215 | a->array = NULL; |
| 216 | a->used = a->size = 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | static void initArrayColor(ArrayColor* a, int initialSize) |
| 224 | { |
| 225 | a->array = (VoxColor *)VOX_MALLOC(initialSize * sizeof(VoxColor)); |
| 226 | a->used = 0; |
| 227 | a->size = initialSize; |
| 228 | } |
| 229 | |
| 230 | static void insertArrayColor(ArrayColor* a, VoxColor element) |
| 231 | { |
| 232 | if (a->used == a->size) |
| 233 | { |
| 234 | a->size *= 2; |
| 235 | a->array = (VoxColor *)VOX_REALLOC(a->array, a->size * sizeof(VoxColor)); |
| 236 | } |
| 237 | a->array[a->used++] = element; |
| 238 | } |
| 239 | |
| 240 | static void freeArrayColor(ArrayColor* a) |
| 241 | { |
| 242 | VOX_FREE(a->array); |
| 243 | a->array = NULL; |
| 244 | a->used = a->size = 0; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | #define CHUNKSIZE 16 |
| 253 | #define CHUNKSIZE_OPSHIFT 4 |
| 254 | #define CHUNK_FLATTENOFFSET_OPSHIFT 8 |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | const int fv[6][4] = { |
| 277 | {0, 2, 6, 4 }, |
| 278 | {5, 7, 3, 1 }, |
| 279 | {0, 4, 5, 1 }, |
| 280 | {6, 2, 3, 7 }, |
| 281 | {1, 3, 2, 0 }, |
| 282 | {4, 6, 7, 5 } |
| 283 | }; |
| 284 | |
| 285 | const VoxVector3 SolidVertex[] = { |
| 286 | {0, 0, 0}, |
| 287 | {1, 0, 0}, |
| 288 | {0, 1, 0}, |
| 289 | {1, 1, 0}, |
| 290 | {0, 0, 1}, |
| 291 | {1, 0, 1}, |
| 292 | {0, 1, 1}, |
| 293 | {1, 1, 1} |
| 294 | }; |
| 295 | |
| 296 | const VoxVector3 FacesPerSideNormal[] = { |
| 297 | { -1, 0, 0 }, |
| 298 | {1, 0, 0 }, |
| 299 | {0,-1, 0}, |
| 300 | {0, 1, 0}, |
| 301 | {0, 0, -1}, |
| 302 | {0, 0, 1}, |
| 303 | }; |
| 304 | |
| 305 | |
| 306 | |
| 307 | static void Vox_AllocArray(VoxArray3D* pvoxarray, int _sx, int _sy, int _sz) |
| 308 | { |
| 309 | int sx = _sx + ((CHUNKSIZE - (_sx % CHUNKSIZE)) % CHUNKSIZE); |
| 310 | int sy = _sy + ((CHUNKSIZE - (_sy % CHUNKSIZE)) % CHUNKSIZE); |
| 311 | int sz = _sz + ((CHUNKSIZE - (_sz % CHUNKSIZE)) % CHUNKSIZE); |
| 312 | |
| 313 | int chx = sx >> CHUNKSIZE_OPSHIFT; |
| 314 | int chy = sy >> CHUNKSIZE_OPSHIFT; |
| 315 | int chz = sz >> CHUNKSIZE_OPSHIFT; |
| 316 | |
| 317 | |
| 318 | pvoxarray->sizeX = sx; |
| 319 | pvoxarray->sizeY = sy; |
| 320 | pvoxarray->sizeZ = sz; |
| 321 | |
| 322 | pvoxarray->chunksSizeX = chx; |
| 323 | pvoxarray->chunksSizeY = chy; |
| 324 | pvoxarray->chunksSizeZ = chz; |
| 325 | |
| 326 | pvoxarray->ChunkFlattenOffset = (chy * chz); |
| 327 | |
| 328 | |
| 329 | int size = sizeof(CubeChunk3D) * chx * chy * chz; |
| 330 | pvoxarray->m_arrayChunks = (CubeChunk3D *)VOX_MALLOC(size); |
| 331 | pvoxarray->arrayChunksSize = size; |
| 332 | |
| 333 | |
| 334 | size = chx * chy * chz; |
| 335 | pvoxarray->chunksTotal = size; |
| 336 | pvoxarray->chunksAllocated = 0; |
| 337 | |
| 338 | for (int i = 0; i < size; i++) |
| 339 | { |
| 340 | pvoxarray->m_arrayChunks[i].m_array = 0; |
| 341 | pvoxarray->m_arrayChunks[i].arraySize = 0; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static void Vox_SetVoxel(VoxArray3D* pvoxarray, int x, int y, int z, unsigned char id) |
| 347 | { |
| 348 | |
| 349 | int chX = x >> CHUNKSIZE_OPSHIFT; |
| 350 | int chY = y >> CHUNKSIZE_OPSHIFT; |
| 351 | int chZ = z >> CHUNKSIZE_OPSHIFT; |
| 352 | int offset = (chX * pvoxarray->ChunkFlattenOffset) + (chZ * pvoxarray->chunksSizeY) + chY; |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | CubeChunk3D* chunk = &pvoxarray->m_arrayChunks[offset]; |
| 360 | |
| 361 | |
| 362 | chX = x - (chX << CHUNKSIZE_OPSHIFT); |
| 363 | chY = y - (chY << CHUNKSIZE_OPSHIFT); |
| 364 | chZ = z - (chZ << CHUNKSIZE_OPSHIFT); |
| 365 | |
| 366 | if (chunk->m_array == 0) |
| 367 | { |
| 368 | int size = CHUNKSIZE * CHUNKSIZE * CHUNKSIZE; |
| 369 | chunk->m_array = (unsigned char *)VOX_MALLOC(size); |
| 370 | chunk->arraySize = size; |
| 371 | memset(chunk->m_array, 0, size); |
| 372 | |
| 373 | pvoxarray->chunksAllocated++; |
| 374 | } |
| 375 | |
| 376 | offset = (chX << CHUNK_FLATTENOFFSET_OPSHIFT) + (chZ << CHUNKSIZE_OPSHIFT) + chY; |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | chunk->m_array[offset] = id; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | static unsigned char Vox_GetVoxel(VoxArray3D* pvoxarray, int x, int y, int z) |
| 388 | { |
| 389 | if (x < 0 || y < 0 || z < 0) return 0; |
| 390 | |
| 391 | if (x >= pvoxarray->sizeX || y >= pvoxarray->sizeY || z >= pvoxarray->sizeZ) return 0; |
| 392 | |
| 393 | |
| 394 | int chX = x >> CHUNKSIZE_OPSHIFT; |
| 395 | int chY = y >> CHUNKSIZE_OPSHIFT; |
| 396 | int chZ = z >> CHUNKSIZE_OPSHIFT; |
| 397 | int offset = (chX * pvoxarray->ChunkFlattenOffset) + (chZ * pvoxarray->chunksSizeY) + chY; |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | CubeChunk3D* chunk = &pvoxarray->m_arrayChunks[offset]; |
| 405 | |
| 406 | |
| 407 | chX = x - (chX << CHUNKSIZE_OPSHIFT); |
| 408 | chY = y - (chY << CHUNKSIZE_OPSHIFT); |
| 409 | chZ = z - (chZ << CHUNKSIZE_OPSHIFT); |
| 410 | |
| 411 | if (chunk->m_array == 0) |
| 412 | { |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | offset = (chX << CHUNK_FLATTENOFFSET_OPSHIFT) + (chZ << CHUNKSIZE_OPSHIFT) + chY; |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | return chunk->m_array[offset]; |
| 423 | |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static unsigned char Vox_CalcFacesVisible(VoxArray3D* pvoxArray, int cx, int cy, int cz) |
| 428 | { |
| 429 | unsigned char idXm1 = Vox_GetVoxel(pvoxArray, cx - 1, cy, cz); |
| 430 | unsigned char idXp1 = Vox_GetVoxel(pvoxArray, cx + 1, cy, cz); |
| 431 | |
| 432 | unsigned char idYm1 = Vox_GetVoxel(pvoxArray, cx, cy - 1, cz); |
| 433 | unsigned char idYp1 = Vox_GetVoxel(pvoxArray, cx, cy + 1, cz); |
| 434 | |
| 435 | unsigned char idZm1 = Vox_GetVoxel(pvoxArray, cx, cy, cz - 1); |
| 436 | unsigned char idZp1 = Vox_GetVoxel(pvoxArray, cx, cy, cz + 1); |
| 437 | |
| 438 | unsigned char byVFMask = 0; |
| 439 | |
| 440 | |
| 441 | if (idXm1 == 0) byVFMask |= (1 << 0); |
| 442 | |
| 443 | |
| 444 | if (idXp1 == 0) byVFMask |= (1 << 1); |
| 445 | |
| 446 | |
| 447 | if (idYm1 == 0) byVFMask |= (1 << 2); |
| 448 | |
| 449 | |
| 450 | if (idYp1 == 0) byVFMask |= (1 << 3); |
| 451 | |
| 452 | |
| 453 | if (idZm1 == 0) byVFMask |= (1 << 4); |
| 454 | |
| 455 | |
| 456 | if (idZp1 == 0) byVFMask |= (1 << 5); |
| 457 | |
| 458 | return byVFMask; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static VoxVector3 Vox_GetVertexPosition(int _wcx, int _wcy, int _wcz, int _nNumVertex) |
| 463 | { |
| 464 | float scale = 0.25; |
| 465 | |
| 466 | VoxVector3 vtx = SolidVertex[_nNumVertex]; |
| 467 | vtx.x = (vtx.x + _wcx) * scale; |
| 468 | vtx.y = (vtx.y + _wcy) * scale; |
| 469 | vtx.z = (vtx.z + _wcz) * scale; |
| 470 | |
| 471 | return vtx; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static void Vox_Build_Voxel(VoxArray3D* pvoxArray, int x, int y, int z, int matID) |
| 476 | { |
| 477 | unsigned char byVFMask = Vox_CalcFacesVisible(pvoxArray, x, y, z); |
| 478 | |
| 479 | if (byVFMask == 0) return; |
| 480 | |
| 481 | int i, j; |
| 482 | VoxVector3 vertComputed[8]; |
| 483 | int bVertexComputed[8]; |
| 484 | memset(vertComputed, 0, sizeof(vertComputed)); |
| 485 | memset(bVertexComputed, 0, sizeof(bVertexComputed)); |
| 486 | |
| 487 | |
| 488 | for (i = 0; i < 6; i++) |
| 489 | { |
| 490 | if ((byVFMask & (1 << i)) != 0) |
| 491 | { |
| 492 | for (j = 0; j < 4; j++) |
| 493 | { |
| 494 | int nNumVertex = fv[i][j]; |
| 495 | if (bVertexComputed[nNumVertex] == 0) |
| 496 | { |
| 497 | bVertexComputed[nNumVertex] = 1; |
| 498 | vertComputed[nNumVertex] = Vox_GetVertexPosition(x, y, z, nNumVertex); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | for (i = 0; i < 6; i++) |
| 506 | { |
| 507 | if ((byVFMask & (1 << i)) == 0) |
| 508 | continue; |
| 509 | |
| 510 | int v0 = fv[i][0]; |
| 511 | int v1 = fv[i][1]; |
| 512 | int v2 = fv[i][2]; |
| 513 | int v3 = fv[i][3]; |
| 514 | |
| 515 | |
| 516 | int idx = pvoxArray->vertices.used; |
| 517 | insertArrayVector3(&pvoxArray->vertices, vertComputed[v0]); |
| 518 | insertArrayVector3(&pvoxArray->vertices, vertComputed[v1]); |
| 519 | insertArrayVector3(&pvoxArray->vertices, vertComputed[v2]); |
| 520 | insertArrayVector3(&pvoxArray->vertices, vertComputed[v3]); |
| 521 | |
| 522 | insertArrayVector3(&pvoxArray->normals, FacesPerSideNormal[i]); |
| 523 | insertArrayVector3(&pvoxArray->normals, FacesPerSideNormal[i]); |
| 524 | insertArrayVector3(&pvoxArray->normals, FacesPerSideNormal[i]); |
| 525 | insertArrayVector3(&pvoxArray->normals, FacesPerSideNormal[i]); |
| 526 | |
| 527 | VoxColor col = pvoxArray->palette[matID]; |
| 528 | |
| 529 | insertArrayColor(&pvoxArray->colors, col); |
| 530 | insertArrayColor(&pvoxArray->colors, col); |
| 531 | insertArrayColor(&pvoxArray->colors, col); |
| 532 | insertArrayColor(&pvoxArray->colors, col); |
| 533 | |
| 534 | |
| 535 | |
| 536 | insertArrayUShort(&pvoxArray->indices, idx + 0); |
| 537 | insertArrayUShort(&pvoxArray->indices, idx + 2); |
| 538 | insertArrayUShort(&pvoxArray->indices, idx + 1); |
| 539 | |
| 540 | insertArrayUShort(&pvoxArray->indices, idx + 0); |
| 541 | insertArrayUShort(&pvoxArray->indices, idx + 3); |
| 542 | insertArrayUShort(&pvoxArray->indices, idx + 2); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | |
| 547 | int Vox_LoadFromMemory(unsigned char* pvoxData, unsigned int voxDataSize, VoxArray3D* pvoxarray) |
| 548 | { |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | unsigned char* fileData = pvoxData; |
| 559 | unsigned char* fileDataPtr = fileData; |
| 560 | unsigned char* endfileDataPtr = fileData + voxDataSize; |
| 561 | |
| 562 | if (strncmp((char*)fileDataPtr, "VOX ", 4) != 0) |
| 563 | { |
| 564 | return VOX_ERROR_INVALID_FORMAT; |
| 565 | } |
| 566 | |
| 567 | fileDataPtr += 4; |
| 568 | |
| 569 | |
| 570 | unsigned int version = 0; |
| 571 | version = ((unsigned int*)fileDataPtr)[0]; |
| 572 | fileDataPtr += 4; |
| 573 | |
| 574 | if (version != 150 && version != 200) |
| 575 | { |
| 576 | return VOX_ERROR_FILE_VERSION_NOT_MATCH; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | unsigned int sizeX, sizeY, sizeZ; |
| 592 | sizeX = sizeY = sizeZ = 0; |
| 593 | unsigned int numVoxels = 0; |
| 594 | |
| 595 | while (fileDataPtr < endfileDataPtr) |
| 596 | { |
| 597 | char szChunkName[5]; |
| 598 | memcpy(szChunkName, fileDataPtr, 4); |
| 599 | szChunkName[4] = 0; |
| 600 | fileDataPtr += 4; |
| 601 | |
| 602 | unsigned int chunkSize = *((unsigned int*)fileDataPtr); |
| 603 | fileDataPtr += sizeof(unsigned int); |
| 604 | |
| 605 | |
| 606 | fileDataPtr += sizeof(unsigned int); |
| 607 | |
| 608 | if (strcmp(szChunkName, "SIZE") == 0) |
| 609 | { |
| 610 | |
| 611 | sizeX = *((unsigned int*)fileDataPtr); |
| 612 | fileDataPtr += sizeof(unsigned int); |
| 613 | |
| 614 | sizeY = *((unsigned int*)fileDataPtr); |
| 615 | fileDataPtr += sizeof(unsigned int); |
| 616 | |
| 617 | sizeZ = *((unsigned int*)fileDataPtr); |
| 618 | fileDataPtr += sizeof(unsigned int); |
| 619 | |
| 620 | |
| 621 | Vox_AllocArray(pvoxarray, sizeX, sizeZ, sizeY); |
| 622 | } |
| 623 | else if (strcmp(szChunkName, "XYZI") == 0) |
| 624 | { |
| 625 | unsigned char vx, vy, vz, vi; |
| 626 | |
| 627 | |
| 628 | |
| 629 | numVoxels = *((unsigned int*)fileDataPtr); |
| 630 | fileDataPtr += sizeof(unsigned int); |
| 631 | |
| 632 | while (numVoxels > 0) |
| 633 | { |
| 634 | vx = *((unsigned char*)fileDataPtr++); |
| 635 | vy = *((unsigned char*)fileDataPtr++); |
| 636 | vz = *((unsigned char*)fileDataPtr++); |
| 637 | vi = *((unsigned char*)fileDataPtr++); |
| 638 | |
| 639 | Vox_SetVoxel(pvoxarray, vx, vz, pvoxarray->sizeZ-vy-1, vi); |
| 640 | |
| 641 | numVoxels--; |
| 642 | } |
| 643 | } |
| 644 | else if (strcmp(szChunkName, "RGBA") == 0) |
| 645 | { |
| 646 | VoxColor col; |
| 647 | |
| 648 | |
| 649 | for (int i = 0; i < 256 - 1; i++) |
| 650 | { |
| 651 | col.r = *((unsigned char*)fileDataPtr++); |
| 652 | col.g = *((unsigned char*)fileDataPtr++); |
| 653 | col.b = *((unsigned char*)fileDataPtr++); |
| 654 | col.a = *((unsigned char*)fileDataPtr++); |
| 655 | |
| 656 | pvoxarray->palette[i + 1] = col; |
| 657 | } |
| 658 | |
| 659 | } |
| 660 | else |
| 661 | { |
| 662 | fileDataPtr += chunkSize; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | initArrayVector3(&pvoxarray->vertices, 3 * 1024); |
| 672 | initArrayVector3(&pvoxarray->normals, 3 * 1024); |
| 673 | initArrayUShort(&pvoxarray->indices, 3 * 1024); |
| 674 | initArrayColor(&pvoxarray->colors, 3 * 1024); |
| 675 | |
| 676 | |
| 677 | int x, y, z; |
| 678 | |
| 679 | for (x = 0; x <= pvoxarray->sizeX; x++) |
| 680 | { |
| 681 | for (z = 0; z <= pvoxarray->sizeZ; z++) |
| 682 | { |
| 683 | for (y = 0; y <= pvoxarray->sizeY; y++) |
| 684 | { |
| 685 | unsigned char matID = Vox_GetVoxel(pvoxarray, x, y, z); |
| 686 | if (matID != 0) |
| 687 | Vox_Build_Voxel(pvoxarray, x, y, z, matID); |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return VOX_SUCCESS; |
| 693 | } |
| 694 | |
| 695 | void Vox_FreeArrays(VoxArray3D* voxarray) |
| 696 | { |
| 697 | |
| 698 | if (voxarray->m_arrayChunks != 0) |
| 699 | { |
| 700 | for (int i = 0; i < voxarray->chunksTotal; i++) |
| 701 | { |
| 702 | CubeChunk3D* chunk = &voxarray->m_arrayChunks[i]; |
| 703 | if (chunk->m_array != 0) |
| 704 | { |
| 705 | chunk->arraySize = 0; |
| 706 | VOX_FREE(chunk->m_array); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | VOX_FREE(voxarray->m_arrayChunks); |
| 711 | voxarray->m_arrayChunks = 0; |
| 712 | voxarray->arrayChunksSize = 0; |
| 713 | |
| 714 | voxarray->chunksSizeX = voxarray->chunksSizeY = voxarray->chunksSizeZ = 0; |
| 715 | voxarray->chunksTotal = 0; |
| 716 | voxarray->chunksAllocated = 0; |
| 717 | voxarray->ChunkFlattenOffset = 0; |
| 718 | voxarray->sizeX = voxarray->sizeY = voxarray->sizeZ = 0; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | freeArrayVector3(&voxarray->vertices); |
| 723 | freeArrayUShort(&voxarray->indices); |
| 724 | freeArrayColor(&voxarray->colors); |
| 725 | } |
| 726 | |
| 727 | #endif |