void bp_add(float4 *data) { int doff = 0; int3 boff; int itmp = brick_count % B_COUNT; boff.x = itmp; itmp = (brick_count-boff.x)/B_COUNT; boff.y = itmp % B_COUNT; boff.z = (itmp-boff.y) / B_COUNT; F3MULS(boff, boff, B_SIZE); for(int z=0; z < B_SIZE; z++) for(int y=0; y < B_SIZE; y++) { memcpy(&bPool[boff.z+z][boff.y+y][boff.x], data+doff, sizeof(float4)*B_SIZE); doff += B_SIZE; } brick_count++; }
void gen_oct(MESH *m, char *filename) { // place each point into the oct-tree OctTree *tree = oct_new(100000, 500000); int depth = 6; float total = (float)(B_EDGE-1) / (float)B_EDGE; for(int i=0; i<m->nv; i++) { F3MULS(m->v[i], m->v[i], total); int leaf = oct_leaf(tree, &m->v[i], depth); ListInt2 *tmp = malloc(sizeof(ListInt2)); tmp->x = i; tmp->y = tree->brick[leaf].nv; tmp->next = tree->brick[leaf].v; tree->brick[leaf].v = tmp; tree->brick[leaf].nv++; } depth++; printf("TreeBlock/Brick = (%d %d)\n", tree->blockcount, tree->brickcount); // tree->brickcount ++; unsigned int buf_size = 16*B_CUBE; float4 (*bricktex)[B_EDGE][B_EDGE] = malloc(buf_size); memset(bricktex, 0, buf_size); int3 index; int3 itmp; int id, idt; ListInt2 *tmp; printf("Generating Voxel bricks.\n"); int lastbrickcount = tree->brickcount; for(int i=0; i<lastbrickcount; i++) { // printf("Brick #%d. %d\n", i, tree->brick[i].nv); tmp = tree->brick[i].v; if(tmp) for(int j=0; j<tree->brick[i].nv && tmp; j++) //while(tmp) { index.x = ((int)(m->v[tmp->x].x * (7<<depth)) % (B_SIZE-1))+1; index.y = ((int)(m->v[tmp->x].y * (7<<depth)) % (B_SIZE-1))+1; index.z = ((int)(m->v[tmp->x].z * (7<<depth)) % (B_SIZE-1))+1; id = i; itmp.x = id % B_COUNT; idt = (id-itmp.x) / B_COUNT; itmp.y = idt % B_COUNT; itmp.z = (idt-itmp.y) / B_COUNT; F3MULS(itmp, itmp, (B_SIZE)); F3ADD(index, index, itmp); F3COPY(bricktex[index.z][index.y][index.x], m->n[tmp->x]); bricktex[index.z][index.y][index.x].w = 1.0f; tmp = tmp->next; } } printf("Copy neighbour voxels.\n"); for(int i=1; i<tree->brickcount; i++) { float size = tree->location[i].w; int3 d,s; d.x = i % B_COUNT; idt = (i-d.x) / B_COUNT; d.y = idt % B_COUNT; d.z = (idt-d.y) / B_COUNT; F3MULS(d, d, B_SIZE); int src; float3 pos; F3COPY(pos, tree->location[i]); pos.x += size*1.5; pos.y += size * 0.5; pos.z += size * 0.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=0; x<B_SIZE; x++) for(int y=0; y<B_SIZE; y++) { bricktex[s.z+x][s.y+y][s.x] = bricktex[d.z+x][d.y+y][d.x+7]; } } F3COPY(pos, tree->location[i]); pos.x += size*0.5; pos.y += size*1.5; pos.z += size*0.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=1; x<B_SIZE; x++) for(int y=1; y<B_SIZE; y++) { bricktex[s.z+x][s.y][s.x+y] = bricktex[d.z+x][d.y+7][d.x+y]; } } F3COPY(pos, tree->location[i]); pos.x += size*0.5; pos.y += size*0.5; pos.z += size*1.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=1; x<B_SIZE; x++) for(int y=1; y<B_SIZE; y++) { bricktex[s.z][s.y+x][s.x+y] = bricktex[d.z+7][d.y+x][d.x+y]; } } F3COPY(pos, tree->location[i]); pos.x += size*1.5; pos.y += size*1.5; pos.z += size*0.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=1; x<B_SIZE; x++) { bricktex[s.z+x][s.y][s.x] = bricktex[d.z+x][d.y+7][d.x+7]; } } F3COPY(pos, tree->location[i]); pos.x += size*1.5; pos.y += size*0.5; pos.z += size*1.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=1; x<B_SIZE; x++) { bricktex[s.z][s.y+x][s.x] = bricktex[d.z+7][d.y+x][d.x+7]; } } F3COPY(pos, tree->location[i]); pos.x += size*0.5; pos.y += size*1.5; pos.z += size*1.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); for(int x=1; x<B_SIZE; x++) { bricktex[s.z][s.y][s.x+x] = bricktex[d.z+7][d.y+7][d.x+x]; } } F3COPY(pos, tree->location[i]); pos.x += size*1.5; pos.y += size*1.5; pos.z += size*1.5; src = oct_read(tree, &pos); if(src) { s.x = src % B_COUNT; idt = (src-s.x) / B_COUNT; s.y = idt % B_COUNT; s.z = (idt-s.y) / B_COUNT; F3MULS(s, s, B_SIZE); bricktex[s.z][s.y][s.x] = bricktex[d.z+7][d.y+7][d.x+7]; } } printf("TreeBlock/Brick = (%d %d)\n", tree->blockcount, tree->brickcount); printf("Walking the normals.\n"); for(int i=1; i<tree->brickcount; i++) { } printf("Writing \"%s\".\n", filename); FILE *fptr = fopen(filename, "wb"); int zero = 0; fwrite("VOCT", 4, 1, fptr); fwrite(&tree->blockcount, 4, 1, fptr); fwrite(&tree->brickcount, 4, 1, fptr); fwrite(&zero, 4, 1, fptr); fwrite(tree->block, tree->blockcount, sizeof(OctBlock), fptr); z_stream strm = { .zalloc=Z_NULL, .zfree=Z_NULL, .opaque=Z_NULL }; int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION); if(ret != Z_OK) { printf("deflateInit() failed.\n"); return; } #define CHUNK (256 * 1024) unsigned char in[CHUNK]; unsigned char out[CHUNK]; int row = sizeof(float4)*B_SIZE; int have, inoff = 0; for(int i=0; i<tree->brickcount; i++) { int3 boff; int itmp = i % B_COUNT; boff.x = itmp; itmp = (i-boff.x) / B_COUNT; boff.y = itmp % B_COUNT; boff.z = (itmp-boff.y) / B_COUNT; F3MULS(boff, boff, B_SIZE); for(int z=0; z < B_SIZE; z++) for(int y=0; y < B_SIZE; y++) { memcpy(in+inoff, &bricktex[boff.z+z][boff.y+y][boff.x], row); inoff += row; if(inoff+row > CHUNK) { strm.next_in = in; strm.avail_in = inoff; do{ strm.avail_out = CHUNK; strm.next_out = out; ret = deflate(&strm, Z_NO_FLUSH); // assert(ret != Z_STREAM_ERROR); have = CHUNK - strm.avail_out; fwrite(out, have, 1, fptr); } while (strm.avail_out == 0); // assert(strm.avail_in == 0); inoff = 0; } // fwrite(&bricktex[boff.z+z][boff.y+y][boff.x], // sizeof(float4)*B_SIZE, 1, fptr); } } strm.next_in = in; strm.avail_in = inoff; do{ strm.avail_out = CHUNK; strm.next_out = out; ret = deflate(&strm, Z_FINISH); // assert(ret != Z_STREAM_ERROR); have = CHUNK - strm.avail_out; fwrite(out, have, 1, fptr); } while (strm.avail_out == 0); // assert(strm.avail_in == 0); deflateEnd(&strm); // fwrite(bricktex, 16*B_CUBE, 1, fptr); // fwrite(bricktex[B_EDGE], 16*B_CUBE, 1, fptr); fclose(fptr); }