Skip to content
Myr

cave_mesher.c

This file

Path

tests/caveview/cave_mesher.c

933 lines · 28.4 KiB · C

Last changed by

Not known.

History of this file

Take it

1// This file takes minecraft chunks (decoded by cave_parse) and2// uses stb_voxel_render to turn them into vertex buffers.34#define STB_GLEXT_DECLARE "glext_list.h"5#include "stb_gl.h"6#include "stb_image.h"7#include "stb_glprog.h"89#include "caveview.h"10#include "cave_parse.h"11#include "stb.h"12#include "sdl.h"13#include "sdl_thread.h"14#include <math.h>1516//#define VHEIGHT_TEST17//#define STBVOX_OPTIMIZED_VHEIGHT1819#define STBVOX_CONFIG_MODE  120#define STBVOX_CONFIG_OPENGL_MODELVIEW21#define STBVOX_CONFIG_PREFER_TEXBUFFER22//#define STBVOX_CONFIG_LIGHTING_SIMPLE23#define STBVOX_CONFIG_FOG_SMOOTHSTEP24//#define STBVOX_CONFIG_PREMULTIPLIED_ALPHA  // this doesn't work properly alpha test without next #define25//#define STBVOX_CONFIG_UNPREMULTIPLY  // slower, fixes alpha test makes windows & fancy leaves look better26//#define STBVOX_CONFIG_TEX1_EDGE_CLAMP27#define STBVOX_CONFIG_DISABLE_TEX228//#define STBVOX_CONFIG_DOWN_TEXLERP_PACKED29#define STBVOX_CONFIG_ROTATION_IN_LIGHTING3031#define STB_VOXEL_RENDER_IMPLEMENTATION32#include "stb_voxel_render.h"3334extern void ods(char *fmt, ...);3536//#define FANCY_LEAVES  // nearly 2x the triangles when enabled (if underground is filled)37#define FAST_CHUNK38#define IN_PLACE3940#define SKIP_TERRAIN   041//#define SKIP_TERRAIN   48 // use to avoid building underground stuff42                          // allows you to see what perf would be like if underground was efficiently culled,43                          // or if you were making a game without underground4445enum46{47   C_empty,48   C_solid,49   C_trans,50   C_cross,51   C_water,52   C_slab,53   C_stair,54   C_force,55};5657unsigned char geom_map[] =58{59   STBVOX_GEOM_empty,60   STBVOX_GEOM_solid,61   STBVOX_GEOM_transp,62   STBVOX_GEOM_crossed_pair,63   STBVOX_GEOM_solid,64   STBVOX_GEOM_slab_lower,65   STBVOX_GEOM_floor_slope_north_is_top,66   STBVOX_GEOM_force,67};6869unsigned char minecraft_info[256][7] =70{71   { C_empty, 0,0,0,0,0,0 },72   { C_solid, 1,1,1,1,1,1 },73   { C_solid, 3,3,3,3,40,2 },74   { C_solid, 2,2,2,2,2,2 },75   { C_solid, 16,16,16,16,16,16 },76   { C_solid, 4,4,4,4,4,4 },77   { C_cross, 15,15,15,15 },78   { C_solid, 17,17,17,17,17,17 },7980   // 881   { C_water, 223,223,223,223,223,223 },82   { C_water, 223,223,223,223,223,223 },83   { C_solid, 255,255,255,255,255,255 },84   { C_solid, 255,255,255,255,255,255 },85   { C_solid, 18,18,18,18,18,18 },86   { C_solid, 19,19,19,19,19,19 },87   { C_solid, 32,32,32,32,32,32 },88   { C_solid, 33,33,33,33,33,33 },8990   // 1691   { C_solid, 34,34,34,34,34,34 },92   { C_solid, 20,20,20,20,21,21 },93#ifdef FANCY_LEAVES94   { C_force, 52,52,52,52,52,52 }, // leaves95#else96   { C_solid, 53,53,53,53,53,53 }, // leaves97#endif98   { C_solid, 24,24,24,24,24,24 },99   { C_trans, 49,49,49,49,49,49 }, // glass100   { C_solid, 160,160,160,160,160,160 },101   { C_solid, 144,144,144,144,144,144 },102   { C_solid, 46,45,45,45,62,62 },103104   // 24105   { C_solid, 192,192,192,192, 176,176 },106   { C_solid, 74,74,74,74,74,74 },107   { C_empty }, // bed108   { C_empty }, // powered rail109   { C_empty }, // detector rail110   { C_solid, 106,108,109,108,108,108 },111   { C_empty }, // cobweb=11112   { C_cross, 39,39,39,39 },113114   // 32115   { C_cross, 55,55,55,55,0,0 },116   { C_solid, 107,108,109,108,108,108 },117   { C_empty }, // piston head118   { C_solid, 64,64,64,64,64,64 }, // various colors119   { C_empty }, // unused120   { C_cross, 13,13,13,13,0,0 },121   { C_cross, 12,12,12,12,0,0 },122   { C_cross, 29,29,29,29,0,0 },123124   // 40125   { C_cross, 28,28,28,28,0,0 },126   { C_solid, 23,23,23,23,23,23 },127   { C_solid, 22,22,22,22,22,22 },128   { C_solid, 5,5,5,5,6,6, },129   { C_slab , 5,5,5,5,6,6, },130   { C_solid, 7,7,7,7,7,7, },131   { C_solid, 8,8,8,8,9,10 },132   { C_solid, 35,35,35,35,4,4, },133134   // 48135   //{ C_solid, 36,36,36,36,36,36 },136   { C_force, 36,36,36,36,36,36 },137   { C_solid, 37,37,37,37,37,37 },138   { C_cross, 80,80,80,80,80,80 }, // torch139   { C_empty }, // fire140   { C_trans, 65,65,65,65,65,65 },141   { C_stair, 4,4,4,4,4,4 },142   { C_solid, 26,26,26,27,25,25 },143   { C_empty }, // redstone144145   // 56146   { C_solid, 50,50,50,50,50,50 },147   //{ C_force, 50,50,50,50,50,50 },148   { C_solid, 26,26,26,26,26,26 },149   { C_solid, 60,59,59,59,43,43 },150   { C_cross, 95,95,95,95 },151   { C_solid, 2,2,2,2,86,2 },152   { C_solid, 44,45,45,45,62,62 },153   { C_solid, 61,45,45,45,62,62 },154   { C_empty }, // sign155156   // 64157   { C_empty }, // door158   { C_empty }, // ladder159   { C_empty }, // rail160   { C_stair, 16,16,16,16,16,16 }, // cobblestone stairs161   { C_empty }, // sign162   { C_empty }, // lever163   { C_empty }, // stone pressure plate164   { C_empty }, // iron door165166   // 72167   { C_empty }, // wooden pressure168   { C_solid, 51,51,51,51,51,51 },169   { C_solid, 51,51,51,51,51,51 },170   { C_empty },171   { C_empty },172   { C_empty },173   { C_empty }, // snow on block below, do as half slab?174   { C_solid, 67,67,67,67,67,67 },175176   // 80177   { C_solid, 66,66,66,66,66,66 },178   { C_solid, 70,70,70,70,69,71 },179   { C_solid, 72,72,72,72,72,72 },180   { C_cross, 73,73,73,73,73,73 },181   { C_solid, 74,74,74,74,75,74 },182   { C_empty }, // fence183   { C_solid,119,118,118,118,102,102 },184   { C_solid,103,103,103,103,103,103 },185186   // 88187   { C_solid, 104,104,104,104,104,104 },188   { C_solid, 105,105,105,105,105,105 },189   { C_solid, 167,167,167,167,167,167 },190   { C_solid, 120,118,118,118,102,102 },191   { C_empty }, // cake192   { C_empty }, // repeater193   { C_empty }, // repeater194   { C_solid, 49,49,49,49,49,49 }, // colored glass195196   // 96197   { C_empty },198   { C_empty },199   { C_solid, 54,54,54,54,54,54 },200   { C_solid, 125,125,125,125,125,125 },201   { C_solid, 126,126,126,126,126,126 },202   { C_empty }, // bars203   { C_trans, 49,49,49,49,49,49 }, // glass pane204   { C_solid, 136,136,136,136,137,137 }, // melon205206   // 104207   { C_empty }, // pumpkin stem208   { C_empty }, // melon stem209   { C_empty }, // vines210   { C_empty }, // gate211   { C_stair, 7,7,7,7,7,7, }, // brick stairs212   { C_stair, 54,54,54,54,54,54 }, // stone brick stairs213   { C_empty }, // mycelium214   { C_empty }, // lily pad215216   // 112217   { C_solid, 224,224,224,224,224,224 },218   { C_empty }, // nether brick fence219   { C_stair, 224,224,224,224,224,224 }, // nether brick stairs220   { C_empty }, // nether wart221   { C_solid, 182,182,182,182,166,183 },222   { C_empty }, // brewing stand223   { C_empty }, // cauldron224   { C_empty }, // end portal225226   // 120227   { C_solid, 159,159,159,159,158,158 },228   { C_solid, 175,175,175,175,175,175 },229   { C_empty }, // dragon egg230   { C_solid, 211,211,211,211,211,211 },231   { C_solid, 212,212,212,212,212,212 },232   { C_solid, 4,4,4,4,4,4, }, // wood double-slab233   { C_slab , 4,4,4,4,4,4, }, // wood slab234   { C_empty }, // cocoa235236   // 128237   { C_solid, 192,192,192,192,176,176 }, // sandstone stairs238   { C_solid, 32,32,32,32,32,32 }, // emerald ore239   { C_solid, 26,26,26,27,25,25 }, // ender chest240   { C_empty },241   { C_empty },242   { C_solid, 23,23,23,23,23,23 }, // emerald block243   { C_solid, 198,198,198,198,198,198 }, // spruce stairs244   { C_solid, 214,214,214,214,214,214 }, // birch stairs245246   // 136247   { C_stair, 199,199,199,199,199,199 }, // jungle stairs248   { C_empty }, // command block249   { C_empty }, // beacon250   { C_slab, 16,16,16,16,16,16 }, // cobblestone wall251   { C_empty }, // flower pot252   { C_empty }, // carrot253   { C_empty }, // potatoes254   { C_empty }, // wooden button255256   // 144257   { C_empty }, // mob head258   { C_empty }, // anvil259   { C_solid, 26,26,26,27,25,25 }, // trapped chest260   { C_empty }, // weighted pressure plate light261   { C_empty }, // weighted pressure plat eheavy262   { C_empty }, // comparator inactive263   { C_empty }, // comparator active264   { C_empty }, // daylight sensor265266   // 152267   { C_solid, 135,135,135,135,135,135 }, // redstone block268   { C_solid, 0,0,0,0,0,0, }, // nether quartz ore269   { C_empty }, // hopper270   { C_solid, 22,22,22,22,22,22 }, // quartz block271   { C_stair, 22,22,22,22,22,22 }, // quartz stairs272   { C_empty }, // activator rail273   { C_solid, 46,45,45,45,62,62 }, // dropper274   { C_solid, 72,72,72,72,72,72 }, // stained clay275276   // 160277   { C_trans, 49,49,49,49,49,49 }, // stained glass pane278   #ifdef FANCY_LEAVES279   { C_force, 52,52,52,52,52,52 }, // leaves280   #else281   { C_solid, 53,53,53,53,53,53 }, // acacia leaves282   #endif283   { C_solid, 20,20,20,20,21,21 }, // acacia tree284   { C_solid, 199,199,199,199,199,199 }, // acacia wood stairs285   { C_solid, 198,198,198,198,198,198 }, // dark oak stairs286   { C_solid, 146,146,146,146,146,146 }, // slime block287288   { C_solid, 176,176,176,176,176,176 }, // red sandstone289   { C_solid, 176,176,176,176,176,176 }, // red sandstone290291   // 168292   { C_empty },293   { C_empty },294   { C_empty },295   { C_empty },296   { C_solid, 72,72,72,72,72,72 }, // hardened clay297   { C_empty },298   { C_empty },299   { C_empty },300301   // 176302   { C_empty },303   { C_empty },304   { C_solid, 176,176,176,176,176,176 }, // red sandstone305};306307unsigned char minecraft_tex1_for_blocktype[256][6];308unsigned char effective_blocktype[256];309unsigned char minecraft_color_for_blocktype[256][6];310unsigned char minecraft_geom_for_blocktype[256];311312uint8 build_buffer[BUILD_BUFFER_SIZE];313uint8 face_buffer[FACE_BUFFER_SIZE];314315//GLuint vbuf, fbuf, fbuf_tex;316317//unsigned char tex1_for_blocktype[256][6];318319//unsigned char blocktype[34][34][257];320//unsigned char lighting[34][34][257];321322// a superchunk is 64x64x256, with the border blocks computed as well,323// which means we need 4x4 chunks plus 16 border chunks plus 4 corner chunks324325#define SUPERCHUNK_X   4326#define SUPERCHUNK_Y   4327328unsigned char remap_data[16][16];329unsigned char remap[256];330unsigned char rotate_data[4] = { 1,3,2,0 };331332void convert_fastchunk_inplace(fast_chunk *fc)333{334   int i;335   int num_blocks=0, step=0;336   unsigned char rot[4096];337   #ifndef IN_PLACE338   unsigned char *storage;339   #endif340341   memset(rot, 0, 4096);342343   for (i=0; i < 16; ++i)344      num_blocks += fc->blockdata[i] != NULL;345346   #ifndef IN_PLACE347   storage = malloc(16*16*16*2 * num_blocks);348   #endif349350   for (i=0; i < 16; ++i) {351      if (fc->blockdata[i]) {352         int o=0;353         unsigned char *bd,*dd,*lt,*sky;354         unsigned char *out, *outb;355356         // this ordering allows us to determine which data we can safely overwrite for in-place processing357         bd = fc->blockdata[i];358         dd = fc->data[i];359         lt = fc->light[i];360         sky = fc->skylight[i];361362         #ifdef IN_PLACE363         out = bd;364         #else365         out = storage + 16*16*16*2*step;366         #endif367368         // bd is written in place, but also reads from dd369         for (o=0; o < 16*16*16/2; o += 1) {370            unsigned char v1,v2;371            unsigned char d = dd[o];372            v1 = bd[o*2+0];373            v2 = bd[o*2+1];374375            if (remap[v1])376            {377               //unsigned char d = bd[o] & 15;378               v1 = remap_data[remap[v1]][d&15];379               rot[o*2+0] = rotate_data[d&3];380            } else381               v1 = effective_blocktype[v1];382383            if (remap[v2])384            {385               //unsigned char d = bd[o] >> 4;386               v2 = remap_data[remap[v2]][d>>4];387               rot[o*2+1] = rotate_data[(d>>4)&3];388            } else389               v2 = effective_blocktype[v2];390391            out[o*2+0] = v1;392            out[o*2+1] = v2;393         }394395         // this reads from lt & sky396         #ifndef IN_PLACE397         outb = out + 16*16*16;398         ++step;399         #endif400401         // MC used to write in this order and it makes it possible to compute in-place402         if (dd < sky && sky < lt) {403            // @TODO go this path always if !IN_PLACE404            #ifdef IN_PLACE405            outb = dd;406            #endif407408            for (o=0; o < 16*16*16/2; ++o) {409               int bright;410               bright = (lt[o]&15)*12 + 15 + (sky[o]&15)*16;411               if (bright > 255) bright = 255;412               if (bright <  32) bright =  32;413               outb[o*2+0] = STBVOX_MAKE_LIGHTING_EXT((unsigned char) bright, (rot[o*2+0]&3));414415               bright = (lt[o]>>4)*12 + 15 + (sky[o]>>4)*16;416               if (bright > 255) bright = 255;417               if (bright <  32) bright =  32;418               outb[o*2+1] = STBVOX_MAKE_LIGHTING_EXT((unsigned char) bright, (rot[o*2+1]&3));419            }420         } else {421            // @TODO: if blocktype is in between others, this breaks; need to find which side has two pointers, and use that422            // overwrite rot[] array, then copy out423            #ifdef IN_PLACE424            outb = (dd < sky) ? dd : sky;425            if (lt < outb) lt = outb;426            #endif427428            for (o=0; o < 16*16*16/2; ++o) {429               int bright;430               bright = (lt[o]&15)*12 + 15 + (sky[o]&15)*16;431               if (bright > 255) bright = 255;432               if (bright <  32) bright =  32;433               rot[o*2+0] = STBVOX_MAKE_LIGHTING_EXT((unsigned char) bright, (rot[o*2+0]&3));434435               bright = (lt[o]>>4)*12 + 15 + (sky[o]>>4)*16;436               if (bright > 255) bright = 255;437               if (bright <  32) bright =  32;438               rot[o*2+1] = STBVOX_MAKE_LIGHTING_EXT((unsigned char) bright, (rot[o*2+1]&3));439            }440441            memcpy(outb, rot, 4096);442            fc->data[i] = outb;443         }444445         #ifndef IN_PLACE446         fc->blockdata[i] = out;447         fc->data[i] = outb;448         #endif449      }450   }451452   #ifndef IN_PLACE453   free(fc->pointer_to_free);454   fc->pointer_to_free = storage;455   #endif456}457458void make_converted_fastchunk(fast_chunk *fc, int x, int y, int segment, uint8 *sv_blocktype, uint8 *sv_lighting)459{460   int z;461   assert(fc == NULL || (fc->refcount > 0 && fc->refcount < 64));462   if (fc == NULL || fc->blockdata[segment] == NULL) {463      for (z=0; z < 16; ++z) {464         sv_blocktype[z] = C_empty;465         sv_lighting[z] = 255;466      }467   } else {468      unsigned char *block = fc->blockdata[segment];469      unsigned char *data  = fc->data[segment];470      y = 15-y;471      for (z=0; z < 16; ++z) {472         sv_blocktype[z] = block[z*256 + y*16 + x];473         sv_lighting [z] = data [z*256 + y*16 + x];474      }475   }476}477478479#define CHUNK_CACHE   64480typedef struct481{482   int valid;483   int chunk_x, chunk_y;484   fast_chunk *fc;485} cached_converted_chunk;486487cached_converted_chunk chunk_cache[CHUNK_CACHE][CHUNK_CACHE];488int cache_size = CHUNK_CACHE;489490void reset_cache_size(int size)491{492   int i,j;493   for (j=size; j < cache_size; ++j) {494      for (i=size; i < cache_size; ++i) {495         cached_converted_chunk *ccc = &chunk_cache[j][i];496         if (ccc->valid) {497            if (ccc->fc) {498               free(ccc->fc->pointer_to_free);499               free(ccc->fc);500               ccc->fc = NULL;501            }502            ccc->valid = 0;503         }504      }505   }506   cache_size = size;507}508509// this must be called inside mutex510void deref_fastchunk(fast_chunk *fc)511{512   if (fc) {513      assert(fc->refcount > 0);514      --fc->refcount;515      if (fc->refcount == 0) {516         free(fc->pointer_to_free);517         free(fc);518      }519   }520}521522SDL_mutex * chunk_cache_mutex;523SDL_mutex * chunk_get_mutex;524525void lock_chunk_get_mutex(void)526{527   SDL_LockMutex(chunk_get_mutex);528}529void unlock_chunk_get_mutex(void)530{531   SDL_UnlockMutex(chunk_get_mutex);532}533534fast_chunk *get_converted_fastchunk(int chunk_x, int chunk_y)535{536   int slot_x = (chunk_x & (cache_size-1));537   int slot_y = (chunk_y & (cache_size-1));538   fast_chunk *fc;539   cached_converted_chunk *ccc;540   SDL_LockMutex(chunk_cache_mutex);541   ccc = &chunk_cache[slot_y][slot_x];542   if (ccc->valid) {543      if (ccc->chunk_x == chunk_x && ccc->chunk_y == chunk_y) {544         fast_chunk *fc = ccc->fc;545         if (fc)546            ++fc->refcount;547         SDL_UnlockMutex(chunk_cache_mutex);548         return fc;549      }550      if (ccc->fc) {551         deref_fastchunk(ccc->fc);552         ccc->fc = NULL;553         ccc->valid = 0;554      }555   }556   SDL_UnlockMutex(chunk_cache_mutex);557558   fc = get_decoded_fastchunk_uncached(chunk_x, -chunk_y);559   if (fc)560      convert_fastchunk_inplace(fc);561562   SDL_LockMutex(chunk_cache_mutex);563   // another thread might have updated it, so before we overwrite it...564   if (ccc->fc) {565      deref_fastchunk(ccc->fc);566      ccc->fc = NULL;567   }568569   if (fc)570      fc->refcount = 1; // 1 in the cache571572   ccc->chunk_x = chunk_x;573   ccc->chunk_y = chunk_y;574   ccc->valid = 1;575   if (fc)576      ++fc->refcount;577   ccc->fc = fc;578   SDL_UnlockMutex(chunk_cache_mutex);579   return fc;580}581582void make_map_segment_for_superchunk_preconvert(int chunk_x, int chunk_y, int segment, fast_chunk *fc_table[4][4], uint8 sv_blocktype[34][34][18], uint8 sv_lighting[34][34][18])583{584   int a,b;585   assert((chunk_x & 1) == 0);586   assert((chunk_y & 1) == 0);587   for (b=-1; b < 3; ++b) {588      for (a=-1; a < 3; ++a) {589         int xo = a*16+1;590         int yo = b*16+1;591         int x,y;592         fast_chunk *fc = fc_table[b+1][a+1];593         for (y=0; y < 16; ++y)594            for (x=0; x < 16; ++x)595               if (xo+x >= 0 && xo+x < 34 && yo+y >= 0 && yo+y < 34)596                  make_converted_fastchunk(fc,x,y, segment, sv_blocktype[xo+x][yo+y], sv_lighting[xo+x][yo+y]);597      }598   }599}600601// build 1 mesh covering 2x2 chunks602void build_chunk(int chunk_x, int chunk_y, fast_chunk *fc_table[4][4], raw_mesh *rm)603{604   int a,b,z;605   stbvox_input_description *map;606607   #ifdef VHEIGHT_TEST608   unsigned char vheight[34][34][18];609   #endif610611   #ifndef STBVOX_CONFIG_DISABLE_TEX2612   unsigned char tex2_choice[34][34][18];613   #endif614615   assert((chunk_x & 1) == 0);616   assert((chunk_y & 1) == 0);617618   rm->cx = chunk_x;619   rm->cy = chunk_y;620621   stbvox_set_input_stride(&rm->mm, 34*18, 18);622623   assert(rm->mm.input.geometry == NULL);624625   map = stbvox_get_input_description(&rm->mm);626   map->block_tex1_face = minecraft_tex1_for_blocktype;627   map->block_color_face = minecraft_color_for_blocktype;628   map->block_geometry = minecraft_geom_for_blocktype;629630   stbvox_reset_buffers(&rm->mm);631   stbvox_set_buffer(&rm->mm, 0, 0, rm->build_buffer, BUILD_BUFFER_SIZE);632   stbvox_set_buffer(&rm->mm, 0, 1, rm->face_buffer , FACE_BUFFER_SIZE);633634   map->blocktype = &rm->sv_blocktype[1][1][1]; // this is (0,0,0), but we need to be able to query off the edges635   map->lighting = &rm->sv_lighting[1][1][1];636637   // fill in the top two rows of the buffer638   for (a=0; a < 34; ++a) {639      for (b=0; b < 34; ++b) {640         rm->sv_blocktype[a][b][16] = 0;641         rm->sv_lighting [a][b][16] = 255;642         rm->sv_blocktype[a][b][17] = 0;643         rm->sv_lighting [a][b][17] = 255;644      }645   }646647   #ifndef STBVOX_CONFIG_DISABLE_TEX2648   for (a=0; a < 34; ++a) {649      for (b=0; b < 34; ++b) {650         int px = chunk_x*16 + a - 1;651         int py = chunk_y*16 + b - 1;652         float dist = (float) sqrt(px*px + py*py);653         float s1 = (float) sin(dist / 16), s2, s3;654         dist = (float) sqrt((px-80)*(px-80) + (py-50)*(py-50));655         s2 = (float) sin(dist / 11);656         for (z=0; z < 18; ++z) {657            s3 = (float) sin(z * 3.141592 / 8);658659            s3 = s1*s2*s3;660            tex2_choice[a][b][z] = 63 & (int) stb_linear_remap(s3,-1,1, -20,83);661         }662      }663   }664   #endif665666   for (z=256-16; z >= SKIP_TERRAIN; z -= 16)667   {668      int z0 = z;669      int z1 = z+16;670      if (z1 == 256) z1 = 255;671672      make_map_segment_for_superchunk_preconvert(chunk_x, chunk_y, z >> 4, fc_table, rm->sv_blocktype, rm->sv_lighting);673674      map->blocktype = &rm->sv_blocktype[1][1][1-z]; // specify location of 0,0,0 so that accessing z0..z1 gets right data675      map->lighting = &rm->sv_lighting[1][1][1-z];676      #ifndef STBVOX_CONFIG_DISABLE_TEX2677      map->tex2 = &tex2_choice[1][1][1-z];678      #endif679680      #ifdef VHEIGHT_TEST681      // hacky test of vheight682      for (a=0; a < 34; ++a) {683         for (b=0; b < 34; ++b) {684            int c;685            for (c=0; c < 17; ++c) {686               if (rm->sv_blocktype[a][b][c] != 0 && rm->sv_blocktype[a][b][c+1] == 0) {687                  // topmost block688                  vheight[a][b][c] = rand() & 255;689                  rm->sv_blocktype[a][b][c] = 168;690               } else if (c > 0 && rm->sv_blocktype[a][b][c] != 0 && rm->sv_blocktype[a][b][c-1] == 0) {691                  // bottommost block692                  vheight[a][b][c] = ((rand() % 3) << 6) + ((rand() % 3) << 4) + ((rand() % 3) << 2) + (rand() % 3);693                  rm->sv_blocktype[a][b][c] = 169;694               }695            }696            vheight[a][b][c] = STBVOX_MAKE_VHEIGHT(2,2,2,2); // flat top697         }698      }699      map->vheight = &vheight[1][1][1-z];700      #endif701702      {703         stbvox_set_input_range(&rm->mm, 0,0,z0, 32,32,z1);704         stbvox_set_default_mesh(&rm->mm, 0);705         stbvox_make_mesh(&rm->mm);706      }707708      // copy the bottom two rows of data up to the top709      for (a=0; a < 34; ++a) {710         for (b=0; b < 34; ++b) {711            rm->sv_blocktype[a][b][16] = rm->sv_blocktype[a][b][0];712            rm->sv_blocktype[a][b][17] = rm->sv_blocktype[a][b][1];713            rm->sv_lighting [a][b][16] = rm->sv_lighting [a][b][0];714            rm->sv_lighting [a][b][17] = rm->sv_lighting [a][b][1];715         }716      }717   }718719   stbvox_set_mesh_coordinates(&rm->mm, chunk_x*16, chunk_y*16, 0);720   stbvox_get_transform(&rm->mm, rm->transform);721722   stbvox_set_input_range(&rm->mm, 0,0,0, 32,32,255);723   stbvox_get_bounds(&rm->mm, rm->bounds);724725   rm->num_quads = stbvox_get_quad_count(&rm->mm, 0);726}727728int next_blocktype = 255;729730unsigned char mc_rot[4] = { 1,3,2,0 };731732// create blocktypes with rotation baked into type...733// @TODO we no longer need this now that we store rotations734// in lighting735void build_stair_rotations(int blocktype, unsigned char *map)736{737   int i;738739   // use the existing block type for floor stairs; allocate a new type for ceil stairs740   for (i=0; i < 6; ++i) {741      minecraft_color_for_blocktype[next_blocktype][i] = minecraft_color_for_blocktype[blocktype][i];742      minecraft_tex1_for_blocktype [next_blocktype][i] = minecraft_tex1_for_blocktype [blocktype][i];743   }744   minecraft_geom_for_blocktype[next_blocktype] = (unsigned char) STBVOX_MAKE_GEOMETRY(STBVOX_GEOM_ceil_slope_north_is_bottom, 0, 0);745   minecraft_geom_for_blocktype[     blocktype] = (unsigned char) STBVOX_MAKE_GEOMETRY(STBVOX_GEOM_floor_slope_north_is_top, 0, 0);746747   for (i=0; i < 4; ++i) {748      map[0+i+8] = map[0+i] =      blocktype;749      map[4+i+8] = map[4+i] = next_blocktype;750   }751   --next_blocktype;752}753754void build_wool_variations(int bt, unsigned char *map)755{756   int i,k;757   unsigned char tex[16] = { 64, 210, 194, 178,  162, 146, 130, 114,  225, 209, 193, 177,  161, 145, 129, 113 };758   for (i=0; i < 16; ++i) {759      if (i == 0)760         map[i] = bt;761      else {762         map[i] = next_blocktype;763         for (k=0; k < 6; ++k) {764            minecraft_tex1_for_blocktype[next_blocktype][k] = tex[i];765         }766         minecraft_geom_for_blocktype[next_blocktype] = minecraft_geom_for_blocktype[bt];767         --next_blocktype;768      }769   }770}771772void build_wood_variations(int bt, unsigned char *map)773{774   int i,k;775   unsigned char tex[4] = { 5, 198, 214, 199 };776   for (i=0; i < 4; ++i) {777      if (i == 0)778         map[i] = bt;779      else {780         map[i] = next_blocktype;781         for (k=0; k < 6; ++k) {782            minecraft_tex1_for_blocktype[next_blocktype][k] = tex[i];783         }784         minecraft_geom_for_blocktype[next_blocktype] = minecraft_geom_for_blocktype[bt];785         --next_blocktype;786      }787   }788   map[i] = map[i-1];789   ++i;790   for (; i < 16; ++i)791      map[i] = bt;792}793794void remap_in_place(int bt, int rm)795{796   int i;797   remap[bt] = rm;798   for (i=0; i < 16; ++i)799      remap_data[rm][i] = bt;800}801802803void mesh_init(void)804{805   int i;806807   chunk_cache_mutex = SDL_CreateMutex();808   chunk_get_mutex   = SDL_CreateMutex();809810   for (i=0; i < 256; ++i) {811      memcpy(minecraft_tex1_for_blocktype[i], minecraft_info[i]+1, 6);812      effective_blocktype[i] = (minecraft_info[i][0] == C_empty ? 0 : i);813      minecraft_geom_for_blocktype[i] = geom_map[minecraft_info[i][0]];814   }815   //effective_blocktype[50] = 0; // delete torches816817   for (i=0; i < 6*256; ++i) {818      if (minecraft_tex1_for_blocktype[0][i] == 40)819         minecraft_color_for_blocktype[0][i] = 38 | 64; // apply to tex1820      if (minecraft_tex1_for_blocktype[0][i] == 39)821         minecraft_color_for_blocktype[0][i] = 39 | 64; // apply to tex1822      if (minecraft_tex1_for_blocktype[0][i] == 105)823         minecraft_color_for_blocktype[0][i] = 63; // emissive824      if (minecraft_tex1_for_blocktype[0][i] == 212)825         minecraft_color_for_blocktype[0][i] = 63; // emissive826      if (minecraft_tex1_for_blocktype[0][i] == 80)827         minecraft_color_for_blocktype[0][i] = 63; // emissive828   }829830   for (i=0; i < 6; ++i) {831      minecraft_color_for_blocktype[172][i] = 47 | 64; // apply to tex1832      minecraft_color_for_blocktype[178][i] = 47 | 64; // apply to tex1833      minecraft_color_for_blocktype[18][i] = 39 | 64; // green834      minecraft_color_for_blocktype[161][i] = 37 | 64; // green835      minecraft_color_for_blocktype[10][i] = 63; // emissive lava836      minecraft_color_for_blocktype[11][i] = 63; // emissive837      //minecraft_color_for_blocktype[56][i] = 63; // emissive diamond838      minecraft_color_for_blocktype[48][i] = 63; // emissive dungeon839   }840841   #ifdef VHEIGHT_TEST842   effective_blocktype[168] = 168;843   minecraft_tex1_for_blocktype[168][0] = 1;844   minecraft_tex1_for_blocktype[168][1] = 1;845   minecraft_tex1_for_blocktype[168][2] = 1;846   minecraft_tex1_for_blocktype[168][3] = 1;847   minecraft_tex1_for_blocktype[168][4] = 1;848   minecraft_tex1_for_blocktype[168][5] = 1;849   minecraft_geom_for_blocktype[168] = STBVOX_GEOM_floor_vheight_12;850   effective_blocktype[169] = 169;851   minecraft_tex1_for_blocktype[169][0] = 1;852   minecraft_tex1_for_blocktype[169][1] = 1;853   minecraft_tex1_for_blocktype[169][2] = 1;854   minecraft_tex1_for_blocktype[169][3] = 1;855   minecraft_tex1_for_blocktype[169][4] = 1;856   minecraft_tex1_for_blocktype[169][5] = 1;857   minecraft_geom_for_blocktype[169] = STBVOX_GEOM_ceil_vheight_03;858   #endif859860   remap[53] = 1;861   remap[67] = 2;862   remap[108] = 3;863   remap[109] = 4;864   remap[114] = 5;865   remap[136] = 6;866   remap[156] = 7;867   for (i=0; i < 256; ++i)868      if (remap[i])869         build_stair_rotations(i, remap_data[remap[i]]);870   remap[35]  = 8;871   build_wool_variations(35, remap_data[remap[35]]);872   remap[5] = 11;873   build_wood_variations(5, remap_data[remap[5]]);874875   // set the remap flags for these so they write the rotation values876   remap_in_place(54, 9);877   remap_in_place(146, 10);878}879880// Timing stats while optimizing the single-threaded builder881882// 32..-32, 32..-32, SKIP_TERRAIN=0, !FANCY_LEAVES on 'mcrealm' data set883884// 6.27s  - reblocked to do 16 z at a time instead of 256 (still using 66x66x258), 4 meshes in parallel885// 5.96s  - reblocked to use FAST_CHUNK (no intermediate data structure)886// 5.45s  - unknown change, or previous measurement was wrong887888// 6.12s  - use preconverted data, not in-place889// 5.91s  - use preconverted, in-place890// 5.34s  - preconvert, in-place, avoid dependency chain (suggested by ryg)891// 5.34s  - preconvert, in-place, avoid dependency chain, use bit-table instead of byte-table892// 5.50s  - preconvert, in-place, branchless893894// 6.42s  - non-preconvert, avoid dependency chain (not an error)895// 5.40s  - non-preconvert, w/dependency chain (same as earlier)896897// 5.50s  - non-FAST_CHUNK, reblocked outer loop for better cache reuse898// 4.73s  - FAST_CHUNK non-preconvert, reblocked outer loop899// 4.25s  - preconvert, in-place, reblocked outer loop900// 4.18s  - preconvert, in-place, unrolled again901// 4.10s  - 34x34 1 mesh instead of 66x66 and 4 meshes (will make it easier to do multiple threads)902903// 4.83s  - building bitmasks but not using them (2 bits per block, one if empty, one if solid)904905// 5.16s  - using empty bitmasks to early out906// 5.01s  - using solid & empty bitmasks to early out - "foo"907// 4.64s  - empty bitmask only, test 8 at a time, then test geom908// 4.72s  - empty bitmask only, 8 at a time, then test bits909// 4.46s  - split bitmask building into three loops (each byte is separate)910// 4.42s  - further optimize computing bitmask911912// 4.58s  - using solid & empty bitmasks to early out, same as "foo" but faster bitmask building913// 4.12s  - using solid & empty bitmasks to efficiently test neighbors914// 4.04s  - using 16-bit fetches (not endian-independent)915//        - note this is first place that beats previous best '4.10s - 34x34 1 mesh'916917// 4.30s  - current time with bitmasks disabled again (note was 4.10s earlier)918// 3.95s  - bitmasks enabled again, no other changes919// 4.00s  - current time with bitmasks disabled again, no other changes -- wide variation that is time dependent?920//          (note that most of the numbers listed here are median of 3 values already)921// 3.98s  - bitmasks enabled922923// Bitmasks removed from the code as not worth the complexity increase924925926927// Raw data for Q&A:928//929//   26% parsing & loading minecraft files (4/5ths of which is zlib decode)930//   39% building mesh from stb input format931//   18% converting from minecraft blocks to stb blocks932//    9% reordering from minecraft axis order to stb axis order933//    7% uploading vertex buffer to OpenGL