@ -1,51 +1,64 @@
# include "common.h"
# include "common.h"
# include "lighting.h"
# include "lighting.h"
unsigned char sample_lightmap ( const world_t * world , const face_t * face , const BoundBox & bounds , const Vec3 & point )
bool sample_lightmap ( const world_t * world , const face_t * face , const BoundBox & bounds , const Vec3 & point , unsigned char * outSample )
{
{
if ( face - > lightmap < 0 )
if ( face - > lightmap < 0 )
return 0 ;
return false ;
const unsigned char * lightmap = & world - > lightmap [ face - > lightmap ] ;
const unsigned char * lightmap = & world - > lightmap [ face - > lightmap ] ;
const plane_t * plane = & world - > planes [ face - > plane_id ] ;
const plane_t * plane = & world - > planes [ face - > plane_id ] ;
Vec3 minBounds = ( bounds . min / 16 ) . floor ( ) * 16 ;
Vec3 maxBounds = ( bounds . max / 16 ) . ceil ( ) * 16 ;
int width , height ;
int width , height ;
float u , v ;
in t u , v ;
switch ( plane - > type )
switch ( plane - > type )
{
{
case 0 :
case 0 :
case 3 :
case 3 :
// Towards X
// Towards X
width = ( int ) ( ceil ( bounds . max . y / 16 ) - floor ( b ounds. min . y / 16 ) ) * 16 ;
height = ( int ) ( ceil ( bounds . max . z / 16 ) - floor ( b ounds. min . z / 16 ) ) * 16 ;
u = ( point . y - bounds . min . y ) / ( bounds . max . y - bounds . min . y ) ;
v = ( point . z - bounds . min . z ) / ( bounds . max . z - bounds . min . z ) ;
width = ( int ) ( maxBounds . y - minB ounds. y ) ;
height = ( int ) ( maxBounds . z - minB ounds. z ) ;
u = ( int ) ( point . y - minBounds . y ) ;
v = ( int ) ( point . z - minBounds . z ) ;
break ;
break ;
case 1 :
case 1 :
case 4 :
case 4 :
// Towards Y
// Towards Y
width = ( int ) ( ceil ( bounds . max . x / 16 ) - floor ( b ounds. min . x / 16 ) ) * 16 ;
height = ( int ) ( ceil ( bounds . max . z / 16 ) - floor ( b ounds. min . z / 16 ) ) * 16 ;
u = ( point . x - bounds . min . x ) / ( bounds . max . x - bounds . min . x ) ;
v = ( point . z - bounds . min . z ) / ( bounds . max . z - bounds . min . z ) ;
width = ( int ) ( maxBounds . x - minB ounds. x ) ;
height = ( int ) ( maxBounds . z - minB ounds. z ) ;
u = ( int ) ( point . x - minBounds . x ) ;
v = ( int ) ( point . z - minBounds . z ) ;
break ;
break ;
case 2 :
case 2 :
case 5 :
case 5 :
// Towards Z
// Towards Z
width = ( int ) ( ceil ( bounds . max . x / 16 ) - floor ( b ounds. min . x / 16 ) ) * 16 ;
height = ( int ) ( ceil ( bounds . max . y / 16 ) - floor ( b ounds. min . y / 16 ) ) * 16 ;
u = ( point . x - bounds . min . x ) / ( bounds . max . x - bounds . min . x ) ;
v = ( point . y - bounds . min . y ) / ( bounds . max . y - bounds . min . y ) ;
width = ( int ) ( maxBounds . x - minB ounds. x ) ;
height = ( int ) ( maxBounds . y - minB ounds. y ) ;
u = ( int ) ( point . x - minBounds . x ) ;
v = ( int ) ( point . y - minBounds . y ) ;
break ;
break ;
default :
default :
printf ( " Error: unknown plane type %d \n " , plane - > type ) ;
printf ( " Error: unknown plane type %d \n " , plane - > type ) ;
return 0 ;
return 0 ;
}
}
height > > = 4 ;
width > > = 4 ;
if ( u < 0 | | v < 0 | | u > width | | v > height )
return false ;
return lightmap [ ( int ) ( v * ( width + 1 ) + u ) ] ;
* outSample = lightmap [ ( v > > 4 ) * ( width > > 4 ) + ( u > > 4 ) ] ;
return true ;
}
unsigned char sample_lightmap ( const world_t * world , const face_t * face , const BoundBox & bounds , const Vec3 & point )
{
unsigned char sample ;
if ( ! sample_lightmap ( world , face , bounds , point , & sample ) )
return 0 ;
return sample ;
}
}
void export_lightmap ( const world_t * world , const face_t * face , const BoundBox & bounds , int faceIdx )
void export_lightmap ( const world_t * world , const face_t * face , const BoundBox & bounds , int faceIdx )
@ -327,6 +340,9 @@ SurfaceList group_surfaces(const world_t* world, const VertexFaces& vertexFaces)
unsigned char compute_faceVertex_light4 ( const world_t * world , const face_t * refFace , const FaceBounds & faceBounds , Vec3 point )
unsigned char compute_faceVertex_light4 ( const world_t * world , const face_t * refFace , const FaceBounds & faceBounds , Vec3 point )
{
{
if ( refFace - > lightmap < 0 )
return 0 ;
auto faces = world - > facesWithPoint ( point ) ;
auto faces = world - > facesWithPoint ( point ) ;
if ( faces . empty ( ) )
if ( faces . empty ( ) )
return 0 ;
return 0 ;
@ -346,7 +362,11 @@ unsigned char compute_faceVertex_light4(const world_t* world, const face_t* refF
if ( dot < 0.5f )
if ( dot < 0.5f )
continue ;
continue ;
light + = sample_lightmap ( world , face , faceBounds . find ( face ) - > second , point ) + ( 0xFF - face - > baselight ) ;
unsigned char sample ;
if ( ! sample_lightmap ( world , face , faceBounds . find ( face ) - > second , point , & sample ) )
continue ;
light + = sample + ( 0xFF - face - > baselight ) ;
numSamples + + ;
numSamples + + ;
}
}