SHARE
TWEET

help

a guest Jan 19th, 2020 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // IMPORTANT VARIABLE DECLARATIONS:
  2.  
  3. #define SPATIAL_PARTITION_DYNAMIC 3
  4.  
  5. typedef struct SurfaceNode SpatialPartitionCell[4];
  6.  
  7. extern SpatialPartitionCell gStaticSurfacePartition[16][16];
  8. extern SpatialPartitionCell gDynamicSurfacePartition[16][16];
  9. extern struct SurfaceNode *sSurfaceNodePool;
  10. extern struct Surface *sSurfacePool;
  11. extern s16 sSurfacePoolSize;
  12.  
  13.  
  14. static void add_surface_to_cell(s16 dynamic, s16 cellX, s16 cellZ, struct Surface *surface) {
  15.     struct SurfaceNode *newNode = alloc_surface_node();
  16.     struct SurfaceNode *newDynNode = alloc_surface_node();
  17.  
  18.     struct SurfaceNode *list;
  19.     struct SurfaceNode *dynList;
  20.  
  21.     s16 surfacePriority;
  22.     s16 priority;
  23.     s16 sortDir;
  24.     s16 listIndex;
  25.  
  26.     s16 floorOrCeiling = 0;
  27.  
  28.     if (surface->normal.y > 0.01) {
  29.         listIndex = SPATIAL_PARTITION_FLOORS;
  30.         sortDir = 1; // highest to lowest, then insertion order
  31.         floorOrCeiling = 1;
  32.     } else if (surface->normal.y < -0.01) {
  33.         listIndex = SPATIAL_PARTITION_CEILS;
  34.         sortDir = -1; // lowest to highest, then insertion order
  35.         floorOrCeiling = 1;
  36.     } else {
  37.         listIndex = SPATIAL_PARTITION_WALLS;
  38.         sortDir = 0; // insertion order
  39.  
  40.         if (surface->normal.x < -0.707 || surface->normal.x > 0.707) {
  41.             surface->flags |= SURFACE_FLAG_X_PROJECTION;
  42.         }
  43.     }
  44.  
  45.     //! (Surface Cucking) Surfaces are sorted by the height of their first
  46.     //  vertex. Since vertices aren't ordered by height, this causes many
  47.     //  lower triangles to be sorted higher. This worsens surface cucking since
  48.     //  many functions only use the first triangle in surface order that fits,
  49.     //  missing higher surfaces.
  50.     //  upperY would be a better sort method.
  51.     surfacePriority = surface->vertex1[1] * sortDir;
  52.  
  53.     newNode->surface = surface;
  54.  
  55.     newDynNode->surface = surface;
  56.    
  57.  
  58.     if (dynamic) {
  59.         list = &gDynamicSurfacePartition[cellZ][cellX][listIndex];
  60.         if (floorOrCeiling)
  61.             dynList = &gDynamicSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_DYNAMIC];
  62.     } else {
  63.         list = &gStaticSurfacePartition[cellZ][cellX][listIndex];
  64.         if (floorOrCeiling)
  65.             dynList = &gStaticSurfacePartition[cellZ][cellX][SPATIAL_PARTITION_DYNAMIC];
  66.     }
  67.  
  68.     // Loop until we find the appropriate place for the surface in the list.
  69.     while (list->next != NULL) {
  70.         priority = list->next->surface->vertex1[1] * sortDir;
  71.  
  72.         if (surfacePriority > priority) {
  73.             break;
  74.         }
  75.  
  76.         list = list->next;
  77.     }
  78.     while (dynList->next != NULL) {
  79.         priority = dynList->next->surface->vertex1[1] * sortDir;
  80.  
  81.         if (surfacePriority > priority) {
  82.             break;
  83.         }
  84.  
  85.         dynList = dynList->next;
  86.     }
  87.  
  88.     newNode->next = list->next;
  89.     list->next = newNode;
  90.  
  91.     newDynNode->next = dynList->next;
  92.     dynList->next = newDynNode;
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top