@ -179,7 +179,7 @@ static SVECTOR convertNormal(vec3_t normal)
return outNormal ;
return outNormal ;
}
}
static SVECTOR convertPoint ( vec3_t point )
static SVECTOR convertWorld Posit io n ( vec3_t point )
{
{
SVECTOR outPoint ;
SVECTOR outPoint ;
outPoint . vx = ( short ) ( point . x * 4 ) ;
outPoint . vx = ( short ) ( point . x * 4 ) ;
@ -189,6 +189,42 @@ static SVECTOR convertPoint(vec3_t point)
return outPoint ;
return outPoint ;
}
}
static float computeFaceArea ( const world_t * world , const face_t * face )
{
const plane_t * plane = & world - > planes [ face - > plane_id ] ;
// Construct a tangent and bitangent for the plane using the face's first vertex
int edgeIdx = world - > edgeList [ face - > ledge_id ] ;
unsigned short vertIndex = edgeIdx > 0 ?
world - > edges [ edgeIdx ] . vertex0 :
world - > edges [ - edgeIdx ] . vertex1 ;
const vertex_t * vertex = & world - > vertices [ vertIndex ] ;
Vec3 refPoint = plane - > normal * plane - > dist ;
Vec3 tangent = ( vertex - > toVec ( ) - refPoint ) . normalized ( ) ;
Vec3 bitangent = plane - > normal . crossProduct ( tangent ) ;
// Project all face vertices onto the face's plane
BoundBox bounds ;
for ( int edgeListIdx = 0 ; edgeListIdx < face - > ledge_num ; + + edgeListIdx )
{
edgeIdx = world - > edgeList [ face - > ledge_id + edgeListIdx ] ;
vertIndex = edgeIdx > 0 ?
world - > edges [ edgeIdx ] . vertex0 :
world - > edges [ - edgeIdx ] . vertex1 ;
vertex_t * vertex = & world - > vertices [ vertIndex ] ;
Vec3 vec = vertex - > toVec ( ) ;
float x = tangent . dotProduct ( vec ) ;
float y = bitangent . dotProduct ( vec ) ;
bounds . includePoint ( Vec3 ( x , y , 0 ) ) ;
}
Vec3 extents = bounds . max - bounds . min ;
return extents . x * extents . y ;
}
int process_faces ( const world_t * world )
int process_faces ( const world_t * world )
{
{
// Write some data to a file
// Write some data to a file
@ -241,9 +277,8 @@ int process_faces(const world_t* world)
outFace . side = face - > side ;
outFace . side = face - > side ;
outFace . firstFaceVertex = ( unsigned short ) outFaceVertices . size ( ) ;
outFace . firstFaceVertex = ( unsigned short ) outFaceVertices . size ( ) ;
Vec3 vertexSum ;
// Traverse the list of face edges to collect all of the face's vertices
// Traverse the list of face edges to collect all of the face's vertices
Vec3 vertexSum ;
BoundBox bounds ;
BoundBox bounds ;
for ( int edgeListIdx = 0 ; edgeListIdx < face - > ledge_num ; + + edgeListIdx )
for ( int edgeListIdx = 0 ; edgeListIdx < face - > ledge_num ; + + edgeListIdx )
{
{
@ -283,7 +318,9 @@ int process_faces(const world_t* world)
// export_lightmap(world, face, bounds, faceIdx);
// export_lightmap(world, face, bounds, faceIdx);
outFace . numFaceVertices = ( unsigned short ) ( outFaceVertices . size ( ) - outFace . firstFaceVertex ) ;
outFace . numFaceVertices = ( unsigned short ) ( outFaceVertices . size ( ) - outFace . firstFaceVertex ) ;
outFace . centerPoint = convertPoint ( vertexSum / outFace . numFaceVertices ) ;
outFace . centerPoint = convertWorldPosition ( vertexSum / outFace . numFaceVertices ) ;
float area = computeFaceArea ( world , face ) ;
outFace . centerPoint . pad = ( short ) ( sqrt ( area ) ) ;
outFaces . push_back ( outFace ) ;
outFaces . push_back ( outFace ) ;
}
}
@ -363,6 +400,8 @@ int process_faces(const world_t* world)
outLeaf . firstLeafFace = leaf - > lface_id ;
outLeaf . firstLeafFace = leaf - > lface_id ;
outLeaf . numLeafFaces = leaf - > lface_num ;
outLeaf . numLeafFaces = leaf - > lface_num ;
//outLeaf.center = convertWorldPosition(leaf->bound.getCenter());
outLeaves . push_back ( outLeaf ) ;
outLeaves . push_back ( outLeaf ) ;
}
}