Skip to content
Myr

added input.packed_compact for storing most of the major 2/3-bit values in one byte

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent 75f69ce · Browse files · 2 files+124 added−30 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+124 added−30 removed

  • tests:+2 added−1 removed
  • other:+122 added−29 removed

2 files+124 added−30 removed

modified:tests/caveview/cave_mesher.c+2 added-1 removed
23 unmodified lines
24//#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_TEX22828//#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, ...);
894 unmodified lines
modified:stb_voxel_render.h+122 added-29 removed
115 unmodified lines
116// so that mesh creation is data-driven the way the uniforms are, and117// then you wouldn't even have to change anything but the mode number.)118//119//120// IMPROVEMENTS FOR SHIP-WORTHY PROGRAMS USING THIS LIBRARY121//122// I currently tolerate a certain level of "bugginess" in this library.123//124// I'm referring to things which look a little wrong (as long as they125// don't cause holes or cracks in the output meshes), or things which126// do not produce as optimal a mesh as possible. Notable examples:127//128// - incorrect lighting on slopes129// - inefficient meshes for vheight blocks130//131// I am willing to do the work to improve these things if someone is132// going to ship a substantial program that would be improved by them.133// (It need not be commercial, nor need it be a game.) I just didn't134// want to do the work up front if it might never be leveraged. So just135// submit a bug report as usual (github is preferred), but add a note136// that this is for a thing that is really going to ship. (That means137// you need to be far enough into the project that it's clear you're138// committed to it; not during early exploratory development.)139//140//141// VOXEL MESH API142//143// Context144//
44 unmodified lines
189// Thomas Frase190//191// VERSION HISTORY192//193// 0.81 (2015-04-12) added input.packed_compact to store rot, vheight & texlerp efficiently194// 0.80 (2015-04-11) fix broken STBVOX_CONFIG_ROTATION_IN_LIGHTING refactoring195// change STBVOX_MAKE_LIGHTING to STBVOX_MAKE_LIGHTING_EXT so196// that header defs don't need to see config vars197// add STBVOX_CONFIG_VHEIGHT_IN_LIGHTING and other vheight fixes
782 unmodified lines
980 unsigned char *vheight; // STBVOX_MAKE_VHEIGHT -- sw:2, se:2, nw:2, ne:2, doesn't rotate981 // Indexed by 3D coordinates, this defines the four982 // vheight values to use if the geometry is STBVOX_GEOM_vheight*.983 // See the vheight discussion.984985 unsigned char *packed_compact;986 // Stores block rotation, vheight, and texlerp values:987 // block rotation: 2 bits988 // vertex vheight: 2 bits989 // use_texlerp : 1 bit990 // vertex texlerp: 3 bits991 // If STBVOX_CONFIG_UP_TEXLERP_PACKED is defined, then 'vertex texlerp' is992 // used for up faces if use_texlerp is 1. If STBVOX_CONFIG_DOWN_TEXLERP_PACKED993 // is defined, then 'vertex texlerp' is used for down faces if use_texlerp is 1.994 // Note if those symbols are defined but packed_compact is NULL, the normal995 // texlerp default will be used.996 // Encode with STBVOX_MAKE_PACKED_COMPACT(rot, vheight, texlerp, use_texlerp)997};998// @OPTIMIZE allow specializing; build a single struct with all of the999// 3D-indexed arrays combined so it's AoS instead of SoA for better1000// cache efficiency
2 unmodified lines
1003//////////////////////////////////////////////////////////////////////////////1004//1005// VHEIGHT DOCUMENTATION1006//1007// "vheight" is the internal name for the special block types1008// with sloped tops or bottoms. "vheight" stands for "vertex height".1009//1010// Note that these blocks are very flexible (there are 256 of them,1011// although at least 17 of them should never be used), but they1012// also have a disadvantage that they generate extra invisible1013// faces; the generator does not currently detect whether adjacent1014// vheight blocks hide each others sides, so those side faces are1015// always generated. For a continuous ground terrain, this means1016// that you may generate 5x as many quads as needed. See notes1017// on "improvements for shipping products" in the introduction.10181019enum1020{1021 STBVOX_VERTEX_HEIGHT_0,
12 unmodified lines
1034// measured in halves of a block; so you can specify heights of 0/2,1035// 1/2, 2/2, or 3/2. 0 is the bottom of the block, 1 is halfway1036// up the block, 2 is the top of the block, and 3 is halfway up the1037// next block (and actually outside of the block). The value 3 is992// actually legal, and allows you to:1038// actually legal for floor vheight (but not ceiling), and allows you to:1039//994// (A) allows smoother terrain by having slopes that cross blocks,1040// (A) have smoother terrain by having slopes that cross blocks,1041// e.g. (1,1,3,3) is a regular-seeming slope halfway between blocks1042// (B) make slopes steeper than 45-degrees, e.g. (0,0,3,3)1043//1044// (Because only z coordinates have half-block precision, and x&y are
103 unmodified lines
1148#define STBVOX_MAKE_SIDE_TEXROT(e,n,w,s) ((e)+(n)*4+(w)*16+(s)*64)1149#define STBVOX_MAKE_COLOR(color,t1,t2) ((color)+(t1)*64+(t2)*128)1150#define STBVOX_MAKE_TEXLERP_VERT3(e,n,w,s,u) ((e)+(n)*8+(w)*64+(s)*512+(u)*4096)1151#define STBVOX_MAKE_TEXLERP_FACE3(e,n,w,s,u,d) ((e)+(n)*8+(w)*64+(s)*512+(u)*4096+(d)*16384)1152#define STBVOX_MAKE_PACKED_COMPACT(rot, vheight, texlerp, def) ((rot)+4*(vheight)+16*(use)+32*(texlerp))11531154#define STBVOX_MAKE_LIGHTING_EXT(lighting, rot) (((lighting)&~3)+(rot))1155#define STBVOX_MAKE_LIGHTING(lighting) (lighting)1156
1145 unmodified lines
2302 { 47,47,47,47 }, // wall-projected diagonal with down face2303 { 31,31,31,31 }, // wall-projected diagonal with up face2304 { 63,63,63,63 }, // crossed-pair has special handling, but avoid early-out2305 { 63,63,63,63 }, // force2259 { 63,63,63,63 },2260 { 63,63,63,63 },2261 { 63,63,63,63 },2262 { 63,63,63,63 },2306 { 63,63,63,63 }, // vheight2307 { 63,63,63,63 }, // vheight2308 { 63,63,63,63 }, // vheight2309 { 63,63,63,63 }, // vheight2310};23112312// this determines which face type above is visible on each side of the geometry2313static unsigned char stbvox_facetype[STBVOX_GEOM_count][6] =
351 unmodified lines
26652666 // first compute texlerp into p12667 stbvox_mesh_vertex p1[4] = { 0 };26682669 #if defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED) && defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)2670 #define STBVOX_USE_PACKED(f) ((f) == STBVOX_FACE_up || (f) == STBVOX_FACE_down)2671 #elif defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)2672 #define STBVOX_USE_PACKED(f) ((f) == STBVOX_FACE_up )2673 #elif defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)2674 #define STBVOX_USE_PACKED(f) ( (f) == STBVOX_FACE_down)2675 #endif26762677 #if defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED) || defined(STBVOX_CONFIG_UP_TEXLERP_PACKED)2678 if (STBVOX_USE_PACKED(face)) {2679 if (!mm->input.packed_compact || 0==(mm->input.packed_compact[v_off]&16))2680 goto set_default;2681 p1[0] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][0]] >> 5);2682 p1[1] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][1]] >> 5);2683 p1[2] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][2]] >> 5);2684 p1[3] = (mm->input.packed_compact[v_off + mm->cube_vertex_offset[face][3]] >> 5);2685 p1[0] = stbvox_vertex_encode(0,0,0,0,p1[0]);2686 p1[1] = stbvox_vertex_encode(0,0,0,0,p1[1]);2687 p1[2] = stbvox_vertex_encode(0,0,0,0,p1[2]);2688 p1[3] = stbvox_vertex_encode(0,0,0,0,p1[3]);2689 goto skip;2690 }2691 #endif26922693 if (mm->input.block_texlerp) {2694 stbvox_block_type bt = mm->input.blocktype[v_off];2695 unsigned char val = mm->input.block_texlerp[bt];2696 p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,val);
45 unmodified lines
2742 } else {2743 p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,stbvox_vert_lerp_for_face_lerp[facelerp]);2744 }2745 } else {2746 #if defined(STBVOX_CONFIG_UP_TEXLERP_PACKED) || defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)2747 set_default:2748 #endif2675 p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,7);2749 p1[0] = p1[1] = p1[2] = p1[3] = stbvox_vertex_encode(0,0,0,0,7); // @TODO make this configurable2750 }27512752 #if defined(STBVOX_CONFIG_UP_TEXLERP_PACKED) || defined(STBVOX_CONFIG_DOWN_TEXLERP_PACKED)2753 skip:2754 #endif27552756 // now compute lighting and store to vertices2757 {2758 stbvox_mesh_vertex *mv[4];2759 stbvox_get_quad_vertex_pointer(mm, mesh, mv, face_data);2760
140 unmodified lines
2901 #ifdef STBVOX_CONFIG_ROTATION_IN_LIGHTING2902 simple_rot = mm->input.lighting[v_off] & 3;2903 #endif29042905 if (mm->input.packed_compact)2906 simple_rot = mm->input.packed_compact[v_off] & 3;29072908 if (blockptr[ 1]==0) {2909 rot.facerot = simple_rot;2910 stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_up , v_off, pos, basevert, vmesh+4*STBVOX_FACE_up, mesh, STBVOX_FACE_up);2911 }
22 unmodified lines
2934 if (blockptr[-ew_off]==0)2935 stbvox_make_mesh_for_face(mm, rot, STBVOX_FACE_west , v_off, pos, basevert, vmesh+4*STBVOX_FACE_west, mesh, STBVOX_FACE_west);2936}293728562857// void stbvox_make_mesh_for_block_with_geo(stbvox_mesh_maker *mm, stbvox_pos pos, int v_off)2858//2938// complex case for mesh generation: we have lots of different2939// block types, and we don't want to generate faces of blocks2940// if they're hidden by neighbors.2941//2942// we use lots of tables to determine this: we have a table2943// which tells us what face type is generated for each type of2944// geometry, and then a table that tells us whether that type2945// is hidden by a neighbor.286728682946static void stbvox_make_mesh_for_block_with_geo(stbvox_mesh_maker *mm, stbvox_pos pos, int v_off)2947{2948 int ns_off = mm->y_stride_in_bytes;2949 int ew_off = mm->x_stride_in_bytes;
22 unmodified lines
2972 ngeo[3] = mm->input.geometry[v_off - ns_off];2973 ngeo[4] = mm->input.geometry[v_off + 1];2974 ngeo[5] = mm->input.geometry[v_off - 1];29752899 #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING2976 rot = (geo >> 4) & 3;2977 geo &= 15;2978 for (i=0; i < 6; ++i) {2979 nrot[i] = (ngeo[i] >> 4) & 3;2980 ngeo[i] &= 15;2981 }2906 #endif2907 STBVOX_NOTUSED(i);2982 } else {2983 int i;2984 assert(mm->input.block_geometry);2985 geo = mm->input.block_geometry[bt];2986 for (i=0; i < 6; ++i)2987 ngeo[i] = mm->input.block_geometry[nbt[i]];2988 if (mm->input.selector) {2989 #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING2990 if (mm->input.packed_compact == NULL) {2916 rot = (mm->input.selector[v_off ] >> 4) & 3;2917 nrot[0] = (mm->input.selector[v_off + ew_off] >> 4) & 3;2918 nrot[1] = (mm->input.selector[v_off + ns_off] >> 4) & 3;2919 nrot[2] = (mm->input.selector[v_off - ew_off] >> 4) & 3;2920 nrot[3] = (mm->input.selector[v_off - ns_off] >> 4) & 3;2921 nrot[4] = (mm->input.selector[v_off + 1] >> 4) & 3;2922 nrot[5] = (mm->input.selector[v_off - 1] >> 4) & 3;2991 rot = (mm->input.selector[v_off ] >> 4) & 3;2992 nrot[0] = (mm->input.selector[v_off + ew_off] >> 4) & 3;2993 nrot[1] = (mm->input.selector[v_off + ns_off] >> 4) & 3;2994 nrot[2] = (mm->input.selector[v_off - ew_off] >> 4) & 3;2995 nrot[3] = (mm->input.selector[v_off - ns_off] >> 4) & 3;2996 nrot[4] = (mm->input.selector[v_off + 1] >> 4) & 3;2997 nrot[5] = (mm->input.selector[v_off - 1] >> 4) & 3;2998 }2999 #endif3000 } else {3001 #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING3002 if (mm->input.packed_compact == NULL) {2926 rot = (geo>>4)&3;2927 geo &= 15;2928 for (i=0; i < 6; ++i) {2929 nrot[i] = (ngeo[i]>>4)&3;2930 ngeo[i] &= 15;3003 rot = (geo>>4)&3;3004 geo &= 15;3005 for (i=0; i < 6; ++i) {3006 nrot[i] = (ngeo[i]>>4)&3;3007 ngeo[i] &= 15;3008 }3009 }3010 #endif3011 }3012 }30132936 #ifdef STBVOX_CONFIG_ROTATION_IN_LIGHTING3014 #ifndef STBVOX_CONFIG_ROTATION_IN_LIGHTING3015 if (mm->input.packed_compact) {3016 rot = mm->input.packed_compact[rot] & 3;3017 nrot[0] = mm->input.packed_compact[v_off + ew_off] & 3;3018 nrot[1] = mm->input.packed_compact[v_off + ns_off] & 3;3019 nrot[2] = mm->input.packed_compact[v_off - ew_off] & 3;3020 nrot[3] = mm->input.packed_compact[v_off - ns_off] & 3;3021 nrot[4] = mm->input.packed_compact[v_off + 1] & 3;3022 nrot[5] = mm->input.packed_compact[v_off - 1] & 3;3023 }3024 #else3025 rot = mm->input.lighting[v_off] & 3;3026 nrot[0] = (mm->input.lighting[v_off + ew_off]) & 3;3027 nrot[1] = (mm->input.lighting[v_off + ns_off]) & 3;3028 nrot[2] = (mm->input.lighting[v_off - ew_off]) & 3;
180 unmodified lines
3209 raw[3] = (v >> 6) & 3;32103211 for (i=0; i < 4; ++i)3212 ht[i] = raw[stbvox_rotate_vertex[i][rot]];3213 } else if (mm->input.packed_compact) {3214 ht[0] = (mm->input.packed_compact[v_off ] >> 2) & 3;3215 ht[1] = (mm->input.packed_compact[v_off+ew_off ] >> 2) & 3;3216 ht[2] = (mm->input.packed_compact[v_off +ns_off] >> 2) & 3;3217 ht[3] = (mm->input.packed_compact[v_off+ew_off+ns_off] >> 2) & 3;3218 } else if (mm->input.geometry) {3219 ht[0] = mm->input.geometry[v_off ] >> 6;3220 ht[1] = mm->input.geometry[v_off+ew_off ] >> 6;3221 ht[2] = mm->input.geometry[v_off +ns_off] >> 6;
456 unmodified lines
3678//3679// - test API for texture rotation on side faces3680// - API for texture rotation on top & bottom3681// - better culling of vheight faces with vheight neighbors3589// - better culling of non-vheight faces with fheight neighbors3682// - better culling of non-vheight faces with vheight neighbors3683// - gather vertex lighting from slopes correctly3684// - better support texture edge_clamp: currently if you fall3685// exactly on 1.0 you get wrapped incorrectly; this is rare, but3686// can avoid: compute texcoords in vertex shader, offset towards
51 unmodified lines