@ -22,35 +22,24 @@ static CVECTOR colors[] =
} ;
static const int numColors = sizeof ( colors ) / sizeof ( CVECTOR ) ;
# define LOAD_CHUNK(type, dst, num, src, entry) \
dst = ( type * ) ( ( src ) + ( entry ) . offset ) ; \
num = ( entry ) . size / sizeof ( type ) ;
void world_load ( const u_long * data , world_t * world )
{
const char * bytes = ( const char * ) data ;
ps1bsp_header_t * header = ( ps1bsp_header_t * ) bytes ;
world - > vertices = ( ps1bsp_vertex_t * ) ( bytes + header - > vertices . offset ) ;
world - > numVertices = header - > vertices . size / sizeof ( ps1bsp_vertex_t ) ;
world - > faces = ( ps1bsp_face_t * ) ( bytes + header - > faces . offset ) ;
world - > numFaces = header - > faces . size / sizeof ( ps1bsp_face_t ) ;
world - > faceVertices = ( ps1bsp_facevertex_t * ) ( bytes + header - > faceVertices . offset ) ;
world - > numFaceVertices = header - > faceVertices . size / sizeof ( ps1bsp_facevertex_t ) ;
world - > planes = ( ps1bsp_plane_t * ) ( bytes + header - > planes . offset ) ;
world - > numPlanes = header - > planes . size / sizeof ( ps1bsp_plane_t ) ;
world - > nodes = ( ps1bsp_node_t * ) ( bytes + header - > nodes . offset ) ;
world - > numNodes = header - > nodes . size / sizeof ( ps1bsp_node_t ) ;
world - > leaves = ( ps1bsp_leaf_t * ) ( bytes + header - > leaves . offset ) ;
world - > numLeaves = header - > leaves . size / sizeof ( ps1bsp_leaf_t ) ;
world - > leafFaces = ( u_short * ) ( bytes + header - > leafFaces . offset ) ;
world - > numLeafFaces = header - > leafFaces . size / sizeof ( u_short ) ;
world - > visData = ( u_char * ) ( bytes + header - > visData . offset ) ;
world - > numVisData = header - > visData . size / sizeof ( u_char ) ;
LOAD_CHUNK ( ps1bsp_vertex_t , world - > vertices , world - > numVertices , bytes , header - > vertices ) ;
LOAD_CHUNK ( ps1bsp_face_t , world - > faces , world - > numFaces , bytes , header - > faces ) ;
LOAD_CHUNK ( ps1bsp_facevertex_t , world - > faceVertices , world - > numFaceVertices , bytes , header - > faceVertices ) ;
LOAD_CHUNK ( ps1bsp_plane_t , world - > planes , world - > numPlanes , bytes , header - > planes ) ;
LOAD_CHUNK ( ps1bsp_node_t , world - > nodes , world - > numNodes , bytes , header - > nodes ) ;
LOAD_CHUNK ( ps1bsp_leaf_t , world - > leaves , world - > numLeaves , bytes , header - > leaves ) ;
LOAD_CHUNK ( u_short , world - > leafFaces , world - > numLeafFaces , bytes , header - > leafFaces ) ;
LOAD_CHUNK ( u_char , world - > visData , world - > numVisData , bytes , header - > visData ) ;
}
static INLINE void drawface_triangle_fan ( const ps1bsp_face_t * face , SVECTOR * vecs )
@ -292,7 +281,7 @@ static void world_drawnode(const world_t *world, short nodeIdx, u_char *pvs, u_c
/ / }
const ps1bsp_plane_t * plane = & world - > planes [ node - > planeId ] ;
short dist = m_pointPlaneDist2 ( & cam_pos , & plane - > normal , plane - > dist ) ;
short dist = m_pointPlaneDist2 ( cam_pos , plane - > normal , plane - > dist ) ;
/ / Draw child nodes in front - to - back order ; adding faces to the OT will reverse the drawing order
if ( dist > 0 )
@ -339,6 +328,20 @@ static u_char *world_loadVisData(const world_t *world, u_short leafIdx, u_char *
return head ;
}
static u_char * world_noVisData ( const world_t * world , u_char * * scratchptr )
{
u_char * head = * scratchptr ;
u_char * tail = head ;
for ( int l = 1 ; l < world - > numLeaves ; l + = 8 )
{
* tail + + = 0xFF ;
}
* scratchptr = tail ;
return head ;
}
static u_short world_leafAtPoint ( const world_t * world , const VECTOR * point )
{
short nodeIdx = 0 ;
@ -348,7 +351,7 @@ static u_short world_leafAtPoint(const world_t *world, const VECTOR *point)
const ps1bsp_plane_t * plane = & world - > planes [ node - > planeId ] ;
/ / TODO : can be optimized for axis - aligned planes , no need for a dot product there
short dist = m_pointPlaneDist2 ( point , & plane - > normal , plane - > dist ) ;
short dist = m_pointPlaneDist2 ( * point , plane - > normal , plane - > dist ) ;
nodeIdx = dist > 0 ? node - > front : node - > back ; / / TODO : this can be done branchless with ( dist < 0 ) ^ 1
}
@ -368,6 +371,7 @@ void world_draw(const world_t *world)
u_char * scratchptr = scratchpad_root ;
u_char * pvs = world_loadVisData ( world , cam_leaf , & scratchptr ) ;
/ / u_char * pvs = world_noVisData ( world , & scratchptr ) ;
world_drawnode ( world , 0 , pvs , scratchptr ) ;
}