Advertisement
DEKTEN

SDF_009

Nov 25th, 2020 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** ************************************************ ***
  2. ***                                                  ***
  3. *** EASY_SOURCE_CODE_LINK:  tinyurl.com/SDF-009      ***
  4. *** DIRECT_LINK_TO_SOURCE:  pastebin.com/yYSKzCHx    ***
  5. ***                                                  ***
  6. *** About: Voxel Rendering For Patent Drawings.      ***
  7. ***        This version is a work in progress.       ***
  8. ***                                                  ***
  9. *** ************************************************ **/
  10. //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
  11. /** M: Macros Section **/
  12.  
  13.     #define V_4  vec4
  14.     #define V_3  vec3
  15.     #define V_2  vec2
  16.     #define F32 float
  17.     #define I32   int
  18.  
  19. /** M: Macros Section **/
  20. //:MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM://
  21. //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
  22. /** D: Data Section **/
  23.  
  24.     //:DEBUGGING:====================================://
  25.  
  26.         /** Nothing here yet **/
  27.  
  28.     //:====================================:DEBUGGING://
  29.     //:CONFIGURATION:================================://
  30.  
  31.         /** SLICE_RENDER_USING_CAMERA_PLANE **/
  32.         int CFG_SLICE_RENDER=( 1 );
  33.  
  34.     //:================================:CONFIGURATION://
  35.     //:RAYMARCHING_AND_VOXEL_CONSTANTS:==============://
  36.  
  37.         //:These defines are for ray marching when using
  38.         //:fragment coordinates as our coordinate space.
  39.         #define MAX_STE 1000   //:Max Step
  40.         #define MIN_DIS 0.02   //:Min Dist (SurfaceDist)
  41.         #define MAX_DIS 1000.0 //:Max Dist
  42.  
  43.         //:Number of tiles on each axis:
  44.         #define NTX  8  //:Num_Tiles (   in_voxel_map )
  45.         #define NTY  4  //:Num_Tiles (   in_voxel_map )
  46.         #define NTZ  3  //:Num_Tiles (   in_voxel_map )
  47.  
  48.         //:Size_Of_A_Voxel_Tile_Measured_In_Pixels:
  49.         #define NPX  16 //:Num_Pixels_X( in_a_tile    )
  50.         #define NPY  16 //:Num_Pixels_Y( in_a_tile    )
  51.         #define NPZ  16 //:Num_Pixels_Z( in_a_tile    )
  52.  
  53.         //:Total__number_of__Pixels__in_entire_voxel_map
  54.         #define TPX  ( NTX * NPX ) //: Total_Pixels_X
  55.         #define TPY  ( NTY * NPY ) //: Total_Pixels_Y
  56.         #define TPZ  ( NTZ * NPZ ) //: Total_Pixels_Z
  57.  
  58.         #define _ 0                                          
  59.         #define X 1                                          
  60.         int VAT[ NTX * NTY * NTZ ]=int[ 8 * 4 * 3 ](  
  61.  
  62.         /** TODO: Eventually use an integer texture  **/
  63.         /**       for this tilemap data.             **/
  64.            
  65.         //:Highest Z Cross Section Is First Tile Map
  66.         //: 1 2 3 4 5 6 7 8        --- -----------------
  67.             X,X,X,X,X,X,X,X, //: 1  |             ^
  68.             X,_,_,_,_,_,_,X, //: 2  | Z == 0      |
  69.             X,_,_,_,_,_,_,X, //: 3  |             |
  70.             X,X,X,X,X,X,X,X, //: 4  |             |
  71.         //:                        ---            |
  72.         //: 1 2 3 4 5 6 7 8        ---   Cross Sections
  73.             X,_,_,_,_,_,_,X, //: 1  |    Can be thought
  74.             _,_,_,_,_,_,_,_, //: 2  |    of as differ-
  75.             _,_,_,_,_,_,_,_, //: 3  |    -ent 2D
  76.             X,_,_,_,_,_,_,X, //: 4  |    tilemaps.
  77.         //:                        ---            |
  78.         //: 1 2 3 4 5 6 7 8        ---            |
  79.             X,_,_,_,_,_,_,X, //: 1  |             |
  80.             X,_,_,_,_,_,_,X, //: 2  | Z == 2      |
  81.             X,_,_,_,_,_,_,X, //: 3  |             |
  82.             X,_,_,_,_,_,_,X  //: 4  |             V
  83.         //:                        --- -----------------
  84.         );                                                
  85.         #undef  _                                            
  86.         #undef  X  
  87.  
  88.     //:==============:RAYMARCHING_AND_VOXEL_CONSTANTS://
  89.     //:STRUCTS:======================================://
  90.  
  91.         //:RWC_AND_RWN:------------------------------://
  92.  
  93.             struct  RWC_AND_RWN{
  94.                 V_3 rwC        ;
  95.                 V_3         rwN;
  96.             };
  97.        
  98.         //:------------------------------:RWC_AND_RWN://
  99.         //:VOX_000:------------------------------://
  100.  
  101.             /** Current Voxel Information **/
  102.             struct VOC{   //:VOC: VOX_CUR (VoxelCurrent)
  103.                uint has;  //: voxel:exists?
  104.                 int val;  //: voxel:tile_value
  105.                 int dex;  //: voxel:1d_index
  106.                 int t_x;  //: voxel:tile_x
  107.                 int t_y;  //: voxel:tile_y
  108.                 int t_z;  //: voxel:tile_z
  109.             };
  110.  
  111.             /** Distance Information **/
  112.             struct VOD{ //:VOD: VOX_DIS (VoxelDistance)
  113.                 F32 nex; //:Distance_To_Next
  114.             //  F32 d_S; //:Distance_To_Surface
  115.             //           //:Within_Current_Voxel
  116.             };
  117.  
  118.             struct VOX_000{
  119.            
  120.                 VOC  vox_cur; //:Voxel_Current_Info
  121.  
  122.                 VOD  vox_dis; //:Voxel_Distance_Info
  123.            
  124.             };
  125.  
  126.         //:------------------------------:VOX_000://
  127.  
  128.     //:======================================:STRUCTS://
  129.  
  130. /** D: Data Section **/
  131. //:DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD://
  132. //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII://
  133. /** I: Inifnite Degree Functions. Always At Top.     **/
  134.  
  135.     //:NO_HALT_USE_ERROR_PIXEL:======================://
  136.     //:PIX_ERR:======================================://
  137.  
  138.         #define ERR_001  1  /** msg_err # 1 **/
  139.         #define ERR_002  2  /** msg_err # 2 **/
  140.  
  141.         V_4     PIX_ERR(
  142.             int msg_err /** EX: MSG_ERR_001 **/
  143.         ,   V_4 pix_err /** Previous Pixel Error **/
  144.         ,   int   o_k
  145.         )
  146.         {
  147.             /** Muddy Pastel Yellow For When You     **/
  148.             /** forget to set the pix_err_out value. **/
  149.             V_4 pix_err_out=V_4(0.5,0.5,0.2,1);
  150.  
  151.             float flux=( mod(iTime,1.0) );
  152.  
  153.             //:COLOR_PICKER_DEBUG_HEX:---------------://
  154.        
  155.             /** Make it easy to find source of error **/
  156.             /** in code by using color picker on     **/
  157.             /** shader output and then doing a       **/
  158.             /** CTRL+F with that hex code to find    **/
  159.             /** the offending source code.           **/
  160.  
  161.                 F32 _ =F32( 0.0    );
  162.                 F32 MAX =F32( 255.0  ); // MaxIntensity
  163.                 F32  A  =F32( 1.0    ); // Alpha_Max
  164.  
  165.                 //:ERROR_CODE_STROBE_COLORS:---------://
  166.  
  167.                 F32 _01_ =( F32(0x01) / MAX );
  168.                 F32 _10_ =( F32(0x10) / MAX );
  169.                 //
  170.                 F32 _02_ =( F32(0x02) / MAX );
  171.                 F32 _20_ =( F32(0x20) / MAX );
  172.                 //
  173.                 F32 _03_ =( F32(0x03) / MAX );
  174.                 F32 _30_ =( F32(0x30) / MAX );
  175.                 //
  176.                 F32 _04_ =( F32(0x04) / MAX );
  177.                 F32 _40_ =( F32(0x40) / MAX );
  178.                 //
  179.                 F32 _05_ =( F32(0x05) / MAX );
  180.                 F32 _50_ =( F32(0x50) / MAX );
  181.                 //
  182.                 F32 _06_ =( F32(0x06) / MAX );
  183.                 F32 _60_ =( F32(0x60) / MAX );
  184.                 //
  185.                 F32 _07_ =( F32(0x07) / MAX );
  186.                 F32 _70_ =( F32(0x70) / MAX );
  187.                 //
  188.                 F32 _08_ =( F32(0x08) / MAX );
  189.                 F32 _80_ =( F32(0x80) / MAX );
  190.                 //
  191.                 F32 _09_ =( F32(0x09) / MAX );
  192.                 F32 _90_ =( F32(0x90) / MAX );
  193.  
  194.                 V_4 _0x010101_ = V_4(_01_,_01_,_01_, A);
  195.                 V_4 _0x101010_ = V_4(_10_,_10_,_10_, A);
  196.  
  197.                 V_4 _0x020202_ = V_4(_02_,_02_,_02_, A);
  198.                 V_4 _0x202020_ = V_4(_20_,_20_,_20_, A);
  199.  
  200.                 V_4 _0x030303_ = V_4(_03_,_03_,_03_, A);
  201.                 V_4 _0x303030_ = V_4(_30_,_30_,_30_, A);
  202.  
  203.                 V_4 _0x040404_ = V_4(_04_,_04_,_04_, A);
  204.                 V_4 _0x404040_ = V_4(_40_,_40_,_40_, A);
  205.  
  206.                 V_4 _0x050505_ = V_4(_05_,_05_,_05_, A);
  207.                 V_4 _0x505050_ = V_4(_50_,_50_,_50_, A);
  208.  
  209.                 V_4 _0x060606_ = V_4(_06_,_06_,_06_, A);
  210.                 V_4 _0x606060_ = V_4(_60_,_60_,_60_, A);
  211.  
  212.                 V_4 _0x070707_ = V_4(_07_,_07_,_07_, A);
  213.                 V_4 _0x707070_ = V_4(_70_,_70_,_70_, A);
  214.  
  215.                 V_4 _0x080808_ = V_4(_08_,_08_,_08_, A);
  216.                 V_4 _0x808080_ = V_4(_80_,_80_,_80_, A);
  217.  
  218.                 V_4 _0x090909_ = V_4(_09_,_09_,_09_, A);
  219.                 V_4 _0x909090_ = V_4(_90_,_90_,_90_, A);
  220.  
  221.                 //:---------:ERROR_CODE_STROBE_COLORS://
  222.                 //:ERROR_ZERO_COLORS:----------------://
  223.                 #define F float
  224.                 #define V vec4
  225.                 /** If you forget to set error code, **/
  226.                 /** you will see flashing red and    **/
  227.                 /** blue. ( _0xFF0666_ & _0x6660FF_ )**/
  228.                  
  229.                 F   _FF_=( F32(0xFF) / MAX );
  230.                 //  _06_=( F32(0x06) / MAX );
  231.                 //  _60_=( F32(0x60) / MAX );
  232.                 F   _66_=( F32(0x66) / MAX );
  233.                 V   _0xFF0666_ =V_4(_FF_,_06_,_66_,A);
  234.                 V   _0x6660FF_ =V_4(_66_,_60_,_FF_,A);
  235.  
  236.                 #undef F
  237.                 #undef V
  238.                 //:----------------:ERROR_ZERO_COLORS://
  239.                 //:BAD_OK_ERROR_COLOR:---------------://
  240.                 #define F float
  241.                 #define V vec4
  242.                 /** You will see this if you init    **/
  243.                 /** o_k to a value other than 1 in   **/
  244.                 /** your source code.                **/
  245.                 /** Strobes between orange and lime. **/
  246.                 /** ( _0xFF7700_ & _0x77FF00_ )      **/
  247.                  
  248.                 //  _FF_=( F32(0xFF) / MAX );
  249.                 F   _77_=( F32(0x77) / MAX );
  250.                 F   _00_=( F32(0x00) / MAX );
  251.                 V   _0xFF7700_ =V_4(_FF_,_77_,_00_,A);
  252.                 V   _0x77FF00_ =V_4(_77_,_FF_,_00_,A);
  253.  
  254.                 #undef F
  255.                 #undef V
  256.                 //:---------------:BAD_OK_ERROR_COLOR://
  257.  
  258.             //:---------------:COLOR_PICKER_DEBUG_HEX://
  259.  
  260.             if( o_k <= 0 ){
  261.                 pix_err_out = pix_err;
  262.             }else
  263.             if( 1 == o_k ){
  264.  
  265.                 /** table of error "messages" #0 **/
  266.                 V_4 tab_err_000[10]=V_4[10](        
  267.                                                
  268.                     // 0: Invalid Error Code    
  269.                     _0xFF0666_  // RED_FLASH
  270.                          
  271.                     // Odd Frame Error Colors:
  272.                 ,   _0x010101_  //  ERR_001 : ODD_FRAME
  273.                 ,   _0x020202_  //  ERR_002 : ODD_FRAME
  274.                 ,   _0x030303_  //  ERR_003 : ODD_FRAME
  275.                 ,   _0x040404_  //  ERR_004 : ODD_FRAME
  276.                 ,   _0x050505_  //  ERR_005 : ODD_FRAME
  277.                 ,   _0x060606_  //  ERR_006 : ODD_FRAME
  278.                 ,   _0x070707_  //  ERR_007 : ODD_FRAME
  279.                 ,   _0x080808_  //  ERR_008 : ODD_FRAME
  280.                 ,   _0x090909_  //  ERR_009 : ODD_FRAME
  281.                 );;        
  282.                 /** table of error "messages" #1 **/
  283.                 V_4 tab_err_001[10]=V_4[10](        
  284.                                                
  285.                     // 0: Invalid Error Code    
  286.                     _0x6660FF_ // BLUE_FLASH    
  287.                          
  288.                     // Even Frame Error Colors:
  289.                 ,   _0x101010_  //  ERR_001 : EVE_FRAME
  290.                 ,   _0x202020_  //  ERR_002 : EVE_FRAME
  291.                 ,   _0x303030_  //  ERR_003 : EVE_FRAME
  292.                 ,   _0x404040_  //  ERR_004 : EVE_FRAME
  293.                 ,   _0x505050_  //  ERR_005 : EVE_FRAME
  294.                 ,   _0x606060_  //  ERR_006 : EVE_FRAME
  295.                 ,   _0x707070_  //  ERR_007 : EVE_FRAME
  296.                 ,   _0x808080_  //  ERR_008 : EVE_FRAME
  297.                 ,   _0x909090_  //  ERR_009 : EVE_FRAME
  298.                 );;                      
  299.  
  300.                 if( mod(iTime*16.0,2.0) < 1.0 ){
  301.                     pix_err_out =( tab_err_000
  302.                                  [ msg_err ] );;
  303.                 }else{
  304.                     pix_err_out =( tab_err_001
  305.                                  [ msg_err ] );;
  306.                 };;
  307.  
  308.             }else{
  309.                 /** Orange strobe for an o_k value   **/
  310.                 /** that is NOT expected. (o_k >= 2) **/
  311.  
  312.                 if( mod(iTime*2.0,2.0) < 1.0 ){
  313.                     pix_err_out = _0xFF7700_; // ORANGE
  314.                 }else{
  315.                     pix_err_out = _0x77FF00_; // LIME
  316.                 };;
  317.  
  318.             };;
  319.                
  320.             return( pix_err_out );
  321.         }
  322.  
  323.     //:======================================:PIX_ERR://
  324.     //:======================:NO_HALT_USE_ERROR_PIXEL://
  325.  
  326. /** I: Inifnite Degree Functions. Always At Top.     **/
  327. //:IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII://
  328. //:22222222222222222222222222222222222222222222222222://
  329.  
  330.     //:INTERPOLATION_3D:=============================://
  331.     //:sfd_i3d:======================================://
  332.  
  333.         V_3 sdf_i3d(
  334.         /**/V_3 _1_
  335.         ,   V_3 _2_
  336.         ,   F32 f_p
  337.         ){
  338.             return( _1_ + ( ( _2_ - _1_ )*f_p ) );
  339.         }
  340.  
  341.     //:=============================:INTERPOLATION_3D://
  342.     //:======================================:sfd_i3d://
  343.     //:FIRST_VOXEL_QUERY_NEEDED:=====================://
  344.  
  345.         #define T_X vox_000.vox_cur.t_x
  346.         #define T_Y vox_000.vox_cur.t_y
  347.         #define T_Z vox_000.vox_cur.t_z
  348.  
  349.         /** rwN used to find distance to NEXT voxel. **/
  350.             VOX_000
  351.         GET_vox_000_USE_xyz_rwN(
  352.                         V_3 xyz
  353.                     ,   V_3     rwN
  354.         )
  355.         {
  356.  
  357.             VOX_000 vox_000;
  358.  
  359.             //:CURRENT_VOXEL_CELL:-------------------://
  360.  
  361.             vox_000.vox_cur.val = int( 0 - 1 );
  362.             T_X = ( int(floor( xyz.x / float( NPX ))));
  363.             T_Y = ( int(floor( xyz.y / float( NPY ))));
  364.             T_Z = ( int(floor( xyz.z / float( NPZ ))));
  365.  
  366.             //:-------------------:CURRENT_VOXEL_CELL://
  367.             //:GET_VOXEL_CELL_VALUE:-----------------://
  368.  
  369.             /** Voxel Volume Is Hard Coded To have   **/
  370.             /** voxel__tile[0,0,0] stuck at world    **/
  371.             /** world_coord[0,0,0]                   **/
  372.             if( T_X >= 0 && T_X < NTX
  373.             &&  T_Y >= 0 && T_Y < NTY
  374.             &&  T_Z >= 0 && T_Z < NTZ
  375.             ){
  376.  
  377.                 //: Index2D -and- Index3D
  378.                 int D2D = T_X + ( NTX    *    T_Y );
  379.                 int D3D = D2D + ( NTX * NTY * T_Z );
  380.  
  381.                 vox_000.vox_cur.dex=(      D3D   );
  382.                 vox_000.vox_cur.val=( VAT[ D3D ] );
  383.  
  384.                 /** NOTE: Voxel value 0 will NOT get **/
  385.                 /**       special treatment. It could**/
  386.                 /**       contain geometry if we     **/
  387.                 /**       really wanted it to.       **/
  388.                 vox_000.vox_cur.has=uint(  1  );
  389.                
  390.             }else{
  391.                 /** Using a "has" flag so we         **/
  392.                 /** can keep more logic UNSIGNED.    **/
  393.                 /** (No need for negative value to ) **/
  394.                 /** (be used for invalid dex or val) **/
  395.                 vox_000.vox_cur.dex= int(  0  );
  396.                 vox_000.vox_cur.val= int(  0  );
  397.                 vox_000.vox_cur.has=uint(  0  );
  398.             };;
  399.             //:-----------------:GET_VOXEL_CELL_VALUE://
  400.  
  401.  
  402.             //:TODO: Figure out distance to the NEXT voxel.
  403.  
  404.          
  405.             return( vox_000 );
  406.         }
  407.         #undef  T_X
  408.         #undef  T_Y
  409.         #undef  T_Z
  410.     //:=====================:FIRST_VOXEL_QUERY_NEEDED://
  411.     //:INTEGER_MODULO:===============================://
  412.  
  413.     #ifndef I32
  414.     #define I32 int
  415.     #endif
  416.  
  417.         /** PRIVATE: Called only by I32_MOD **/
  418.         I32 i32_mod_neg( I32 neg_a , I32 pos_d ){
  419.  
  420.             /** ************************************ ***
  421.             Function Calculates:
  422.            
  423.             FIXED: (d-1)-[ mod(abs(a)+1 , d) ]
  424.  
  425.             GOAL: Negatives keep exact same tiling
  426.                   pattern as the positives.
  427.  
  428.             IN : -6 -5 -4 -3 -2 -1  0 +1 +2 +3 +4 +5 +6
  429.             OUT:  2  3  0  1  2  3  0  1  2  3  0  1  2
  430.  
  431.             0123 -> 0123 -> 0123 -> 0123 -> 0123 -> ect
  432.  
  433.            
  434.  
  435.             *** ************************************ **/
  436.  
  437.                 I32 pos_a = ( 0 - neg_a ) + 1;
  438.                
  439.                 F32 A = F32( pos_a );
  440.                 F32 D = F32( pos_d );
  441.                
  442.                 //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
  443.                 //: WARD:Wholepart,All,Remainder,Divisor
  444.                 int W = int(  trunc( A / (          D  )) );
  445.                 int R = int(         A - ( F32(W) * D  )  );
  446.                
  447.                 return( (pos_d-1) - R );
  448.  
  449.         ///   I32 pos_a = 0 - neg_a; //:make "a" positive.
  450.         ///  
  451.         ///   F32 A = F32( pos_a );
  452.         ///   F32 D = F32( pos_d );
  453.         ///  
  454.         ///   //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
  455.         ///   //: WARD:Wholepart,All,Remainder,Divisor
  456.         ///   int W = int(  trunc( A / (          D  )) );
  457.         ///   int R = int(         A - ( F32(W) * D  )  );
  458.         ///
  459.         ///   return( pos_d - R );
  460.  
  461.         }
  462.  
  463.         I32
  464.         I32_MOD(
  465.         /**/I32 a /** ALL     : CAN BE NEGATIVE **/
  466.         ,   I32 d /** DIVISOR : ALWAYS POSITIVE **/
  467.         ){
  468.  
  469.             /** ************************************ ***
  470.  
  471.             Allow for I32_MOD to be used for wrapping
  472.             even when the input to wrap[ a ] goes
  473.             negative. d should always be positive.
  474.  
  475.             EX: mod( x , 2 ) , where x == -1
  476.             | -1 | 0 [ 1 ] 2 |
  477.             |  1 | 0 [ 1 ] 0 | 1 | 0 |
  478.             d + 1 == 2 + (-1) == 1
  479.             *** ************************************ **/
  480.            //
  481.  
  482.             int R;
  483.  
  484.  
  485.             if( a < 0 ){
  486.  
  487.                 //:This is CLOSE but then new problem
  488.                 //:of lots of green checkers is showing
  489.                 //:up. No clue...
  490.                 R = i32_mod_neg( a , d );
  491.  
  492.             ////    a = d + a;
  493.             ////    
  494.             ////    F32 A = F32( a );
  495.             ////    F32 D = F32( d );
  496.             ////    
  497.             ////    //: WARD:Wholepart,All,Remainder,Divisor
  498.             ////    int W = int(  trunc( A / (          D  )) );
  499.             ////        R = int(         A - ( F32(W) * D  )  );
  500.  
  501.             }else{
  502.  
  503.                 F32 A = F32( a );
  504.                 F32 D = F32( d );
  505.  
  506.                 //: GLSL_MODULUS_WITH_INTEGERS_FORMULA
  507.                 //: WARD:Wholepart,All,Remainder,Divisor
  508.                 int W = int(  trunc( A / (          D  )) );
  509.                     R = int(         A - ( F32(W) * D  )  );
  510.  
  511.             };;
  512.            
  513.             #ifndef F32
  514.             #define F32 float
  515.             #endif
  516.  
  517.  
  518.  
  519.             //:EXAMPLE: I32_MOD( 9,2 )
  520.             //:         W == 4
  521.             //:         R == 1    (9 - ( 4 * 2 ) )
  522.  
  523.  
  524.  
  525.             return( R );
  526.         }
  527.     //:===============================:INTEGER_MODULO://
  528.  
  529. //:22222222222222222222222222222222222222222222222222://
  530. //:11111111111111111111111111111111111111111111111111://
  531.  
  532.     //:FRAGCOORD_TO_FRAGPERCENT:=====================://
  533.     //:f_c_CTO_f_p:==================================://
  534.  
  535.         V_2 f_c_CTO_f_p( V_2 f_c ){
  536.         V_2         f_p;
  537.             f_p = f_c / ( iResolution.xy - 1.0 );
  538.             return(  f_p );
  539.         }
  540.  
  541.     //:==================================:f_c_CTO_f_p://
  542.     //:=====================:FRAGCOORD_TO_FRAGPERCENT://
  543.     //:FRAGPER_TO_CAMERA_RAY:========================://
  544.     //:f_p_CTO_rwC_AND_rwN:==========================://
  545.  
  546.                 RWC_AND_RWN
  547.         f_p_CTO_rwC_AND_rwN(
  548.             V_2 f_p /** 2 dimensional percentage **/
  549.         )
  550.         {
  551.             F32 dbg = ( F32(NPZ) * 4.0 ); //:DEBUG_ONLY
  552.             F32 spd     =( 0.5 /**SPEED FACTOR **/ );
  553.             F32 cos_pos =( cos(iTime*spd)+1.0 ) / 2.0;
  554.             F32 cos_neg =( cos(iTime*spd)-1.0 ) / 2.0;
  555.             dbg=dbg*cos_pos;
  556.             //:dbg = ( (-16.1) * dbg );
  557.             //:dbg = ( -1036.8 );
  558.             //:dbg=( -32.2   );
  559.  
  560.             /** ************************************ ***
  561.             TODO: Fix bug...
  562.                 dbg=( -   0.2 ); <<<<< OK
  563.                 dbg=( -  16.2 ); <<<<< OK
  564.                 dbg=( -  32.2 ); <<<<< ERR
  565.                 dbg=( -  48.2 ); <<<<< OK
  566.                 dbg=( -  64.2 ); <<<<< ERR
  567.                 dbg=( -  80.2 ); <<<<< OK
  568.                 dbg=( -  96.2 ); <<<<< ERR
  569.                 dbg=( - 112.2 ); <<<<< OK
  570.                 dbg=( - 128.2 ); <<<<< ERR
  571.                 dbg=( -1036.8 );
  572.  
  573.             *** ************************************ **/
  574.          
  575.             V_3 rwC; //:ray_word__COORDINATE
  576.             V_3 rwN; //:ray_world_NORMAL____
  577.  
  578.             //:#FIND_POINT_ON_SCREEN_PLANE#
  579.             F32 H = F32( 0 - 9 );; //:Cam Height Pos
  580.             F32 X = iResolution.x - 1.0;
  581.             F32 Y = iResolution.y - 1.0;
  582.             /** ************************************ ***
  583.                             A_B
  584.                         A----|------B
  585.                         |    |      |
  586.                         |   rwC     |
  587.                         |    |      |
  588.                         C----|------D
  589.                             C_D
  590.                                
  591.             *** ************************************ **/
  592.  
  593.             F32  _ =F32( 0.0 /**ALWAYS_ZERO **/ );
  594.             F32 XOS=F32( 0 );
  595.             F32 YOS=F32( 0 );
  596.             F32 ZOS=F32( 0 );
  597.  
  598.             V_3 _A_ = V_3( _+XOS,_+YOS,  H+ZOS );  
  599.             V_3 _B_ = V_3( X+XOS,_+YOS, dbg+ZOS );  
  600.                                      
  601.             V_3 _C_ = V_3( _+XOS,Y+YOS, dbg+ZOS );  
  602.             V_3 _D_ = V_3( X+XOS,Y+YOS, dbg+ZOS );  
  603.                                      
  604.  
  605.             V_3 A_B = sdf_i3d( _A_ , _B_ , f_p.x );
  606.             V_3 C_D = sdf_i3d( _C_ , _D_ , f_p.x );
  607.                 rwC = sdf_i3d( A_B , C_D , f_p.y );
  608.  
  609.                 //:#POSITIVE_Z_DIVES_INTO_SCREEN#://
  610.                 rwN = normalize( V_3( 0 , 0 , 1 ) );
  611.  
  612.                     RWC_AND_RWN
  613.                     rwC_AND_rwN;
  614.                     rwC_AND_rwN.rwC = rwC;
  615.                     rwC_AND_rwN.rwN = rwN;
  616.             return( rwC_AND_rwN );
  617.         }
  618.  
  619.     //:==========================:f_p_CTO_rwC_AND_rwN://
  620.     //:========================:FRAGPER_TO_CAMERA_RAY://
  621.     //:RENDER_SCENE:=================================://
  622.     //:sdf_RenderScene:==============================://
  623.     #define T_X vox_000.vox_cur.t_x
  624.     #define T_Y vox_000.vox_cur.t_y
  625.     #define T_Z vox_000.vox_cur.t_z
  626.     #define HAS vox_000.vox_cur.has
  627.  
  628.         V_4 sdf_RenderScene( RWC_AND_RWN rwC_AND_rwN )
  629.         {
  630.             I32 o_k = I32( 1 );
  631.             V_4 c4d = V_4(1,1,1,1);
  632.  
  633.             V_3 rwN = rwC_AND_rwN.rwN;
  634.             V_3 rwC = rwC_AND_rwN.rwC;
  635.  
  636.             V_3 xyz = rwC;
  637.          
  638.             VOX_000
  639.             vox_000;
  640.  
  641.             //:nex: Distance To NEXT voxel.
  642.             //:d_S: Distance To Surface Geometry inside
  643.             //:     the current voxel. In world coords.
  644.             vox_000.vox_dis.nex =( 0.0 );  
  645.         //: vox_000.vox_dis.d_S =( 0.0 );  
  646.  
  647.             //:Voxels will be thought of as 3D tiles:
  648.             vox_000.vox_cur.has = uint( 0 );
  649.             vox_000.vox_cur.dex =  int( 0 );
  650.             vox_000.vox_cur.t_x =  int( 0 );
  651.             vox_000.vox_cur.t_y =  int( 0 );
  652.             vox_000.vox_cur.t_z =  int( 0 );
  653.  
  654.             //:RAY_MARCH_LOOP:-----------------------://
  655.             /** #ABOUT_RAY_MARCH_LOOP# **/
  656.  
  657.                 //:March to VOXEL:
  658.                 for( int i = 0; i < MAX_STE ; i++ ){
  659.  
  660.                     //:March by distance to next voxel:
  661.                    
  662.                     //:Point_Normal_Form_To_Get:xyz
  663.                     xyz =
  664.                         rwC
  665.                     + ( rwN * vox_000.vox_dis.nex )
  666.                     ;;
  667.                
  668.                         vox_000 =
  669.                     GET_vox_000_USE_xyz_rwN(
  670.                                         xyz,rwN );;
  671.  
  672.                     //:If voxel is not empty, ray march
  673.                     //:inside of the voxel.
  674.                     if( vox_000.vox_cur.val > 0 ){
  675.                     for( int v = 0; v < MAX_STE ; v++ ){
  676.  
  677.  
  678.                     };;};;
  679.  
  680.                     /** Don't move the ray anywhere  **/
  681.                     /** We are going to slice into   **/
  682.                     /** whatever voxels the camera   **/
  683.                     /** plane passes through.        **/
  684.                     if( CFG_SLICE_RENDER >= 1 ){
  685.                         break;
  686.                     };;
  687.  
  688.                 };;
  689.  
  690.             //:-----------------------:RAY_MARCH_LOOP://
  691.  
  692.  
  693.             if( CFG_SLICE_RENDER >= 1 ){
  694.  
  695.                 //:Layer value, 1 or 0
  696.                 int lay = I32_MOD( T_Z, 2 );
  697.                
  698.                 //:#MODULO_CHECKER_PATTERN#://
  699.                 int chk =(  
  700.                     I32_MOD( //:<<<<<<<<<<<<<<:SET_003
  701.                         I32_MOD( T_Y, 2 )   //:SET_YYY
  702.                     +   I32_MOD( T_X, 2 )   //:SET_XXX
  703.                     ,                 2
  704.                     )
  705.                 );;
  706.            
  707.                 if(   0 != 0
  708.                 || ( lay < 0 )
  709.                 || ( lay > 1 )
  710.                 || ( chk < 0 || chk > 1 )
  711.                 || ( HAS > uint(1)      )
  712.                 ){
  713.                     //: _0x010101_ & _0x101010_
  714.                     c4d=PIX_ERR(ERR_001,c4d,o_k--);
  715.                 };;
  716.  
  717.                
  718.                 F32 M =( 255.0); //:Max RGB value.
  719.                 F32 X =( 1.0  ); //:FOR_DEBUGGING_COLORS
  720.              //:F32 ...........;    Replace with "_"
  721.              //:F32 ...........;    when done debugging.
  722.                 F32 a =( 1.0  ); //:ALPHA
  723.                 F32 _ =( 0.0  );
  724.                 F32 g =( F32(0x33)/M ); //:DARK-GREY
  725.                 F32 G =( F32(0x38)/M );
  726.                 F32 w =( F32(0xE5)/M ); //:WHITE-GREY
  727.                 F32 W =( F32(0xF2)/M );
  728.  
  729.                 V_4 _0x333333_ =V_4(g,g,g,a);
  730.                 V_4 _0x383838_ =V_4(G,G,G,a);
  731.                 V_4 _0xE5E5E5_ =V_4(w,w,w,a);
  732.                 V_4 _0xF2F2F2_ =V_4(W,W,W,a);
  733.                 V_4 _0x003300_ =V_4(_,g,_,a);
  734.                 V_4 _0x003800_ =V_4(_,G,_,a);
  735.                 V_4 _0x00E500_ =V_4(_,w,_,a);
  736.                 V_4 _0x00F200_ =V_4(_,W,_,a);
  737.  
  738.                 V_4 tab_000[8]=V_4[8](
  739.                     //:OUT OF BOUNDS:       //:HAS?
  740.                     _0x333333_ , _0x383838_ //:LAY0
  741.                    ,_0xE5E5E5_ , _0xF2F2F2_ //:LAY1
  742.                 //: |   CHK0   |     CHK1   |::::::::://
  743.                                    
  744.                     //:IN BOUNDS:           //:HAS?
  745.                    ,_0x003300_ , _0x003800_ //:LAY0
  746.                    ,_0x00E500_ , _0x00F200_ //:LAY1
  747.                 //: |   CHK0   |     CHK1   |::::::::://
  748.                 );;
  749.                
  750.                 I32 pik = (
  751.                    (4 * ( HAS >= uint(1) ? 1 : 0 ))
  752.                 +  (2 * lay ) //: LAY0 -or- LAY1
  753.                 +  (1 * chk ) //: CHK0 -or- CHK1
  754.                 );;
  755.  
  756.                 if( pik < 0 || pik >= 8 ){
  757.                     c4d=PIX_ERR(ERR_002,c4d,o_k--);
  758.                 };;
  759.                
  760.                 //:TODO: Why is not rendering as
  761.                 //:      checker?
  762.                 if( 1 == o_k ){
  763.                     c4d = tab_000[ pik ];
  764.                 };;
  765.  
  766.             }else{
  767.                 //:DEBUG ONLY.
  768.                 c4d=V_4(1,1,1,1);
  769.             };;
  770.  
  771.             return( c4d );
  772.         }
  773.  
  774.     #undef  T_X
  775.     #undef  T_Y
  776.     #undef  T_Z
  777.     #undef  HAS
  778.     //:==============================:sdf_RenderScene://
  779.     //:=================================:RENDER_SCENE://
  780.  
  781. //:11111111111111111111111111111111111111111111111111://
  782. //:00000000000000000000000000000000000000000000000000://
  783.  
  784.     void mainImage(
  785.         out vec4 fragColor
  786.     ,   in  V_2 fragCoord
  787.     )
  788.     {
  789.            
  790.         #define R_Y iResolution.y
  791.             V_2 f_c = V_2(
  792.             //:( FLIP? )       ( Discrete XY       ) ://
  793.                (  0.0  )   +   ( fragCoord.x - 0.5 )
  794.             ,  (R_Y-1.0)   -   ( fragCoord.y - 0.5 )
  795.             );;
  796.         #undef R_Y  
  797.         V_2 f_p = f_c_CTO_f_p( f_c );
  798.  
  799.  
  800.         //:CAMERA_RAY_FOR_CURRENT_PIXEL:-------------://
  801.  
  802.             /** The coordinate and direction of the  **/
  803.             /** camera ray [rwC,rwN] are closely     **/
  804.             /** related, so return them using the    **/
  805.             /** same function. This will save        **/
  806.             /** processing power when we decide      **/
  807.             /** to create a camera lense that        **/
  808.             /** is a 360 degree cylinder around      **/
  809.             /** an object. (for fun)                 **/
  810.  
  811.             RWC_AND_RWN
  812.             rwC_AND_rwN;
  813.  
  814.             rwC_AND_rwN = f_p_CTO_rwC_AND_rwN( f_p );
  815.  
  816.         //:-------------:CAMERA_RAY_FOR_CURRENT_PIXEL://
  817.  
  818.         fragColor = sdf_RenderScene( rwC_AND_rwN );
  819.  
  820.     }
  821. //:00000000000000000000000000000000000000000000000000://
  822. //:DOCUMENTATION:====================================://
  823. /** ************************************************ ***
  824.     ABBREVIATIONS:
  825.  
  826.         f : fragment
  827.         p : percent (Never Position, use C for coord)
  828.         c : coordinate
  829.         r : ray
  830.         n : normal, for directions.
  831.         d : distance. NEVER DIRECTION.( use: n:normal )
  832.        
  833.     IDENTIFIERS:
  834.  
  835.         CTO: Convert_TO
  836.         rwC: RayWorldCoord
  837.         rwN: RayWorldNormal
  838.         f_p: FragPercent
  839.         f_c: FragCoord (With Discrete Pixel Coords)
  840.                     (instead of pixel centers  )
  841.         dad: Distance_And_inDEX
  842.             Index is 1D index of XYZ voxel tile
  843.             coordinate.
  844.         vat: Voxel_Array__of__Tiles
  845.         c4d: Color_4_Dimensional(RGBA)
  846.  
  847.     FUNCTIONS:
  848.  
  849.         f_c_CTO_f_p : FragmentCoord -CTO- FragPercent
  850.         f_c_CTO_per : USE[ f_c_CTO_f_p ]
  851.         sdf_i3d     : Interpolate_Two_3D_Points
  852.  
  853.     EXTRACTED_BLOCK_COMMENTS:
  854.  
  855.         #ABOUT_RAY_MARCH_LOOP###########################
  856.  
  857.             Ray marching loop should be able to look    
  858.             at the current voxel coordinate returned    
  859.             and decide to STOP marching if it wants.    
  860.             By stopping immediately without marching    
  861.             at all we can make the camera phosphore    
  862.             surface a slice plane that renders          
  863.             cross sectional views of tilemap data.      
  864.    
  865.         ##########################ABOUT_RAY_MARCH_LOOP##
  866.         #POSITIVE_Z_DIVES_INTO_SCREEN###################
  867.  
  868.             Positive Z is further back into screen.
  869.             This way to help normalize voxel grid math.
  870.        
  871.         ###################POSITIVE_Z_DIVES_INTO_SCREEN#
  872.         #FIND_POINT_ON_SCREEN_PLANE#####################
  873.  
  874.             ORIGINAL_COMMENT:
  875.  
  876.                 Polygon in 3d space to serve as the
  877.                 phosphor surface the CRT monitor
  878.                 electrons will be projected onto.
  879.                 Could probably use a mat4 for this.
  880.  
  881.             MORE_INFORMATION_IN_RETROSPECT:
  882.  
  883.                 "Screen Plane" is also known as
  884.                 "CRT Phospore" or "PHO" for short.
  885.                 This is a plane put into 3D space that
  886.                 represents the pixels of the client
  887.                 viewport (physical monitor).
  888.  
  889.                 The "IMAGE" plane from this diagram:
  890.                 https://tinyurl.com/IMAGE-PLANE
  891.  
  892.         #####################FIND_POINT_ON_SCREEN_PLANE#
  893.         #MODULO_CHECKER_PATTERN#########################
  894.  
  895.             // Checker pattern value, 1 or 0.         //
  896.             // Combine Sets so that they create       //
  897.             // a checkerboard of odd/even values,     //
  898.             // and then convert even to 0 and         //
  899.             // odd to 1.                              //
  900.             //                                        //
  901.             // SET_YYY:[ 0 1 0 1 0 1 0 ]              //
  902.             // SET_XXX:[ 0 1 0 1 0 1 0 ]              //
  903.             //                                        //
  904.             // SET_XXX:     [ 0 1 0 1 0 1 0 ]         //
  905.             //                | | | | | | |           //
  906.             // SET_YYY:[ 0 ]- 0 1 0 1 0 1 0           //
  907.             //         [ 1 ]- 1 2 1 2 1 2 1           //
  908.             //         [ 0 ]- 0 1 0 1 0 1 0           //
  909.             //         [ 1 ]- 1 2 1 2 1 2 1           //
  910.             //         [ 0 ]- 0 1 0 1 0 1 0           //
  911.             //         [ 1 ]- 1 2 1 2 1 2 1           //
  912.             //         [ 0 ]- 0 1 0 1 0 1 0           //
  913.             int chk =(  
  914.                 I32_MOD( // <<<<<<<<<<<<<< SET_003
  915.                     I32_MOD( T_Y, 2 )   // SET_YYY
  916.                 +   I32_MOD( T_X, 2 )   // SET_XXX
  917.                 ,                 2
  918.                 )
  919.             );;
  920.  
  921.         #########################MODULO_CHECKER_PATTERN#
  922.  
  923. *** ************************************************ **/
  924. //:====================================:DOCUMENTATION://
  925. //:UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU://
  926. /** U : Undefine Macros Section **/
  927.  
  928.     #undef  V_4
  929.     #undef  V_3
  930.     #undef  V_2
  931.     #undef  F32
  932.     #undef  I32
  933.  
  934. /** U : Undefine Macros Section **/
  935. //:UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU://
  936.  
  937.  
  938.  
  939.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement