void main_loop(void) { #ifndef __APPLE__ gpuinfo_tick(); #endif for(int i=0; i<4; i++) { joy[i].fflarge = joy[i].lt; joy[i].ffsmall = joy[i].rt; } pos.w = 0.5 / (float)vid_width; float3 req = {0,0,0}; float nice = 0.007; if(keys[KEY_ESCAPE]) { killme=1; } if(keys[KEY_W]) { req.z -= nice; } if(keys[KEY_S]) { req.z += nice; } if(keys[KEY_A]) { req.x -= nice; } if(keys[KEY_D]) { req.x += nice; } if(keys[KEY_LCONTROL]) { req.y -= nice; } if(keys[KEY_SPACE]) { req.y += nice; } if(mouse[2]) /// right mouse { angle.x += mickey_y * 0.003; angle.y += mickey_x * 0.003; } if(keys[KEY_O]) { p_swim = !p_swim; keys[KEY_S] = 0; printf("Swimming is %s.\n", p_swim ? "engaged" : "off"); } if(keys[KEY_P]) { printf("float4 pos = {%f, %f, %f, 0.0};\n", pos.x, pos.y, pos.z); printf("float4 angle = {%f, %f, %f, M_PI*0.5};\n", angle.x, angle.y, angle.z); } if(p_swim) { float cx = cos(angle.x), sx = sin(angle.x), ty = req.y; req.y = req.y * cx - req.z * sx; // around x req.z = ty * sx + req.z * cx; } float cy = cos(angle.y), sy = sin(angle.y), tx = req.x; req.x = req.x * cy + req.z * sy; // around y req.z = tx * sy - req.z * cy; F3ADD(pos, pos, req); time = (float)(sys_time() - time_start)/(float)sys_ticksecond; glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); gui_input(); // voxel_loop(); ocl_loop(); gui_draw(); http_loop(); }
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); }