Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

614 lines
19 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //-----------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #ifndef VRAD_H
  14. #define VRAD_H
  15. #pragma once
  16. #include "commonmacros.h"
  17. #include "worldsize.h"
  18. #include "cmdlib.h"
  19. #include "mathlib/mathlib.h"
  20. #include "bsplib.h"
  21. #include "polylib.h"
  22. #include "threads.h"
  23. #include "builddisp.h"
  24. #include "VRAD_DispColl.h"
  25. #include "UtlMemory.h"
  26. #include "UtlHash.h"
  27. #include "utlvector.h"
  28. #include "iincremental.h"
  29. #include "raytrace.h"
  30. #ifdef _WIN32
  31. #include <windows.h>
  32. #endif
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #pragma warning(disable: 4142 4028)
  36. #include <io.h>
  37. #pragma warning(default: 4142 4028)
  38. #include <fcntl.h>
  39. #include <direct.h>
  40. #include <ctype.h>
  41. // Can remove these options if they don't generate problems.
  42. //#define SAMPLEHASH_USE_AREA_PATCHES // Add patches to sample hash based on their AABB instead of as a single point.
  43. #define SAMPLEHASH_QUERY_ONCE // Big optimization - causes way less sample hash queries.
  44. extern float dispchop; // "-dispchop" tightest number of luxel widths for a patch, used on edges
  45. extern float g_MaxDispPatchRadius;
  46. //-----------------------------------------------------------------------------
  47. // forward declarations
  48. //-----------------------------------------------------------------------------
  49. struct Ray_t;
  50. #define TRANSFER_EPSILON 0.0000001
  51. struct directlight_t
  52. {
  53. int index;
  54. directlight_t *next;
  55. dworldlight_t light;
  56. byte *pvs; // accumulated domain of the light
  57. int facenum; // domain of attached lights
  58. int texdata; // texture source of traced lights
  59. Vector snormal;
  60. Vector tnormal;
  61. float sscale;
  62. float tscale;
  63. float soffset;
  64. float toffset;
  65. int dorecalc; // position, vector, spot angle, etc.
  66. IncrementalLightID m_IncrementalID;
  67. // hard-falloff lights (lights that fade to an actual zero). between m_flStartFadeDistance and
  68. // m_flEndFadeDistance, a smoothstep to zero will be done, so that the light goes to zero at
  69. // the end.
  70. float m_flStartFadeDistance;
  71. float m_flEndFadeDistance;
  72. float m_flCapDist; // max distance to feed in
  73. directlight_t(void)
  74. {
  75. m_flEndFadeDistance = -1.0; // end<start indicates not set
  76. m_flStartFadeDistance= 0.0;
  77. m_flCapDist = 1.0e22;
  78. }
  79. };
  80. struct bumplights_t
  81. {
  82. Vector light[NUM_BUMP_VECTS+1];
  83. };
  84. struct transfer_t
  85. {
  86. int patch;
  87. float transfer;
  88. };
  89. struct LightingValue_t
  90. {
  91. Vector m_vecLighting;
  92. float m_flDirectSunAmount;
  93. FORCEINLINE bool IsValid( void ) const
  94. {
  95. return ( m_vecLighting.x >= 0 &&
  96. m_vecLighting.y >= 0 &&
  97. m_vecLighting.z >= 0 &&
  98. m_vecLighting.x < 1e10 &&
  99. m_vecLighting.y < 1e10 &&
  100. m_vecLighting.z < 1e10 );
  101. }
  102. FORCEINLINE void Zero( void )
  103. {
  104. m_vecLighting.Init( 0, 0, 0 );
  105. m_flDirectSunAmount = 0.0;
  106. }
  107. FORCEINLINE void Scale( float m_flScale )
  108. {
  109. m_vecLighting *= m_flScale;
  110. m_flDirectSunAmount *= m_flScale;
  111. }
  112. FORCEINLINE void AddWeighted( LightingValue_t const &src, float flWeight )
  113. {
  114. m_vecLighting += flWeight * src.m_vecLighting;
  115. m_flDirectSunAmount += flWeight * src.m_flDirectSunAmount;
  116. }
  117. FORCEINLINE void AddWeighted( Vector const &src, float flWeight )
  118. {
  119. m_vecLighting += flWeight * src;
  120. }
  121. FORCEINLINE float Intensity( void ) const
  122. {
  123. return m_vecLighting.x + m_vecLighting.y + m_vecLighting.z;
  124. }
  125. FORCEINLINE void AddLight( float flAmount, Vector const &vecColor, float flSunAmount = 0.0 )
  126. {
  127. VectorMA( m_vecLighting, flAmount, vecColor, m_vecLighting );
  128. m_flDirectSunAmount += flSunAmount;
  129. Assert( this->IsValid() );
  130. }
  131. FORCEINLINE void AddLight( LightingValue_t const &src )
  132. {
  133. m_vecLighting += src.m_vecLighting;
  134. m_flDirectSunAmount += src.m_flDirectSunAmount;
  135. Assert( this->IsValid() );
  136. }
  137. FORCEINLINE void Init( float x, float y, float z )
  138. {
  139. m_vecLighting.Init( x, y, z );
  140. m_flDirectSunAmount = 0.0;
  141. }
  142. };
  143. #define MAX_PATCHES (4*65536)
  144. struct CPatch
  145. {
  146. winding_t *winding;
  147. Vector mins, maxs, face_mins, face_maxs;
  148. Vector origin; // adjusted off face by face normal
  149. dplane_t *plane; // plane (corrected for facing)
  150. unsigned short m_IterationKey; // Used to prevent touching the same patch multiple times in the same query.
  151. // See IncrementPatchIterationKey().
  152. // these are packed into one dword
  153. unsigned int normalMajorAxis : 2; // the major axis of base face normal
  154. unsigned int sky : 1;
  155. unsigned int needsBumpmap : 1;
  156. unsigned int pad : 28;
  157. Vector normal; // adjusted for phong shading
  158. float planeDist; // Fixes up patch planes for brush models with an origin brush
  159. float chop; // smallest acceptable width of patch face
  160. float luxscale; // average luxels per world coord
  161. float scale[2]; // Scaling of texture in s & t
  162. bumplights_t totallight; // accumulated by radiosity
  163. // does NOT include light
  164. // accounted for by direct lighting
  165. Vector baselight; // emissivity only
  166. float basearea; // surface per area per baselight instance
  167. Vector directlight; // direct light value
  168. float area;
  169. Vector reflectivity; // Average RGB of texture, modified by material type.
  170. Vector samplelight;
  171. float samplearea; // for averaging direct light
  172. int faceNumber;
  173. int clusterNumber;
  174. int parent; // patch index of parent
  175. int child1; // patch index for children
  176. int child2;
  177. int ndxNext; // next patch index in face
  178. int ndxNextParent; // next parent patch index in face
  179. int ndxNextClusterChild; // next terminal child index in cluster
  180. // struct patch_s *next; // next in face
  181. // struct patch_s *nextparent; // next in face
  182. // struct patch_s *nextclusterchild; // next terminal child in cluster
  183. int numtransfers;
  184. transfer_t *transfers;
  185. short indices[3]; // displacement use these for subdivision
  186. };
  187. extern CUtlVector<CPatch> g_Patches;
  188. extern CUtlVector<int> g_FacePatches; // constains all patches, children first
  189. extern CUtlVector<int> faceParents; // contains only root patches, use next parent to iterate
  190. extern CUtlVector<int> clusterChildren;
  191. struct sky_camera_t
  192. {
  193. Vector origin;
  194. float world_to_sky;
  195. float sky_to_world;
  196. int area;
  197. };
  198. extern int num_sky_cameras;
  199. extern sky_camera_t sky_cameras[MAX_MAP_AREAS];
  200. extern int area_sky_cameras[MAX_MAP_AREAS];
  201. void ProcessSkyCameras();
  202. extern entity_t *face_entity[MAX_MAP_FACES];
  203. extern Vector face_offset[MAX_MAP_FACES]; // for rotating bmodels
  204. extern Vector face_centroids[MAX_MAP_EDGES];
  205. extern int leafparents[MAX_MAP_LEAFS];
  206. extern int nodeparents[MAX_MAP_NODES];
  207. extern float lightscale;
  208. extern float dlight_threshold;
  209. extern float coring;
  210. extern qboolean g_bDumpPatches;
  211. extern bool bRed2Black;
  212. extern bool g_bNoSkyRecurse;
  213. extern bool bDumpNormals;
  214. extern bool g_bFastAmbient;
  215. extern float maxchop;
  216. extern FileHandle_t pFileSamples[4][4];
  217. extern qboolean g_bLowPriority;
  218. extern qboolean do_fast;
  219. extern bool g_bInterrupt; // Was used with background lighting in WC. Tells VRAD to stop lighting.
  220. extern IIncremental *g_pIncremental; // null if not doing incremental lighting
  221. extern bool g_bDumpPropLightmaps;
  222. extern float g_flSkySampleScale; // extra sampling factor for indirect light
  223. extern bool g_bLargeDispSampleRadius;
  224. extern bool g_bStaticPropPolys;
  225. extern bool g_bTextureShadows;
  226. extern bool g_bShowStaticPropNormals;
  227. extern bool g_bDisablePropSelfShadowing;
  228. extern CUtlVector<char const *> g_NonShadowCastingMaterialStrings;
  229. extern void ForceTextureShadowsOnModel( const char *pModelName );
  230. extern bool IsModelTextureShadowsForced( const char *pModelName );
  231. // Raytracing
  232. #define TRACE_ID_SKY 0x01000000 // sky face ray blocker
  233. #define TRACE_ID_OPAQUE 0x02000000 // everyday light blocking face
  234. #define TRACE_ID_STATICPROP 0x04000000 // static prop - lower bits are prop ID
  235. extern RayTracingEnvironment g_RtEnv;
  236. #include "mpivrad.h"
  237. void MakeShadowSplits (void);
  238. //==============================================
  239. void BuildVisMatrix (void);
  240. void BuildClusterTable( void );
  241. void AddDispsToClusterTable( void );
  242. void FreeVisMatrix (void);
  243. // qboolean CheckVisBit (unsigned int p1, unsigned int p2);
  244. void TouchVMFFile (void);
  245. //==============================================
  246. extern qboolean do_extra;
  247. extern qboolean do_fast;
  248. extern qboolean do_centersamples;
  249. extern int extrapasses;
  250. extern Vector ambient;
  251. extern float maxlight;
  252. extern unsigned numbounce;
  253. extern qboolean g_bLogHashData;
  254. extern bool debug_extra;
  255. extern directlight_t *activelights;
  256. extern directlight_t *freelights;
  257. // because of hdr having two face lumps (light styles can cause them to be different, among other
  258. // things), we need to always access (r/w) face data though this pointer
  259. extern dface_t *g_pFaces;
  260. extern bool g_bMPIProps;
  261. extern byte nodehit[MAX_MAP_NODES];
  262. extern float gamma;
  263. extern float indirect_sun;
  264. extern float smoothing_threshold;
  265. extern int dlight_map;
  266. extern float g_flMaxDispSampleSize;
  267. extern float g_SunAngularExtent;
  268. extern char source[MAX_PATH];
  269. // Used by incremental lighting to trivial-reject faces.
  270. // There is a bit in here for each face telling whether or not any of the
  271. // active lights can see the face.
  272. extern CUtlVector<byte> g_FacesVisibleToLights;
  273. void MakeTnodes (dmodel_t *bm);
  274. void PairEdges (void);
  275. void SaveVertexNormals( void );
  276. qboolean IsIncremental(char *filename);
  277. int SaveIncremental(char *filename);
  278. int PartialHead (void);
  279. void BuildFacelights (int facenum, int threadnum);
  280. void PrecompLightmapOffsets();
  281. void FinalLightFace (int threadnum, int facenum);
  282. void PvsForOrigin (Vector& org, byte *pvs);
  283. void ConvertRGBExp32ToRGBA8888( const ColorRGBExp32 *pSrc, unsigned char *pDst, Vector* _optOutLinear = NULL );
  284. void ConvertRGBExp32ToLinear(const ColorRGBExp32 *pSrc, Vector* pDst);
  285. void ConvertLinearToRGBA8888( const Vector *pSrc, unsigned char *pDst );
  286. inline byte PVSCheck( const byte *pvs, int iCluster )
  287. {
  288. if ( iCluster >= 0 )
  289. {
  290. return pvs[iCluster >> 3] & ( 1 << ( iCluster & 7 ) );
  291. }
  292. else
  293. {
  294. // PointInLeaf still returns -1 for valid points sometimes and rather than
  295. // have black samples, we assume the sample is in the PVS.
  296. return 1;
  297. }
  298. }
  299. // outputs 1 in fractionVisible if no occlusion, 0 if full occlusion, and in-between values
  300. void TestLine( FourVectors const& start, FourVectors const& stop, fltx4 *pFractionVisible, int static_prop_index_to_ignore=-1);
  301. // returns 1 if the ray sees the sky, 0 if it doesn't, and in-between values for partial coverage
  302. void TestLine_DoesHitSky( FourVectors const& start, FourVectors const& stop,
  303. fltx4 *pFractionVisible, bool canRecurse = true, int static_prop_to_skip=-1, bool bDoDebug = false );
  304. // converts any marked brush entities to triangles for shadow casting
  305. void ExtractBrushEntityShadowCasters ( void );
  306. void AddBrushesForRayTrace ( void );
  307. void BaseLightForFace( dface_t *f, Vector& light, float *parea, Vector& reflectivity );
  308. void CreateDirectLights (void);
  309. void GetPhongNormal( int facenum, Vector const& spot, Vector& phongnormal );
  310. int LightForString( char *pLight, Vector& intensity );
  311. void MakeTransfer( int ndxPatch1, int ndxPatch2, transfer_t *all_transfers );
  312. void MakeScales( int ndxPatch, transfer_t *all_transfers );
  313. // Run startup code like initialize mathlib.
  314. void VRAD_Init();
  315. // Load the BSP file and prepare to do the lighting.
  316. // This is called after any command-line parameters have been set.
  317. void VRAD_LoadBSP( char const *pFilename );
  318. int VRAD_Main(int argc, char **argv);
  319. // This performs an actual lighting pass.
  320. // Returns true if the process was interrupted (with g_bInterrupt).
  321. bool RadWorld_Go();
  322. dleaf_t *PointInLeaf (Vector const& point);
  323. int ClusterFromPoint( Vector const& point );
  324. winding_t *WindingFromFace (dface_t *f, Vector& origin );
  325. void WriteWinding (FileHandle_t out, winding_t *w, Vector& color );
  326. void WriteNormal( FileHandle_t out, Vector const &nPos, Vector const &nDir,
  327. float length, Vector const &color );
  328. void WriteLine( FileHandle_t out, const Vector &vecPos1, const Vector &vecPos2, const Vector &color );
  329. void WriteTrace( const char *pFileName, const FourRays &rays, const RayTracingResult& result );
  330. #ifdef STATIC_FOG
  331. qboolean IsFog( dface_t * f );
  332. #endif
  333. #define CONTENTS_EMPTY 0
  334. #define TEX_SPECIAL (SURF_SKY|SURF_NOLIGHT)
  335. //=============================================================================
  336. // trace.cpp
  337. bool AddDispCollTreesToWorld( void );
  338. int PointLeafnum( Vector const &point );
  339. float TraceLeafBrushes( int leafIndex, const Vector &start, const Vector &end, CBaseTrace &traceOut );
  340. //=============================================================================
  341. // dispinfo.cpp
  342. struct SSE_sampleLightOutput_t
  343. {
  344. fltx4 m_flDot[NUM_BUMP_VECTS+1];
  345. fltx4 m_flFalloff;
  346. fltx4 m_flSunAmount;
  347. };
  348. #define GATHERLFLAGS_FORCE_FAST 1
  349. #define GATHERLFLAGS_IGNORE_NORMALS 2
  350. // SSE Gather light stuff
  351. void GatherSampleLightSSE( SSE_sampleLightOutput_t &out, directlight_t *dl, int facenum,
  352. FourVectors const& pos, FourVectors *pNormals, int normalCount, int iThread,
  353. int nLFlags = 0, // GATHERLFLAGS_xxx
  354. int static_prop_to_skip=-1,
  355. float flEpsilon = 0.0 );
  356. //void GatherSampleSkyLightSSE( SSE_sampleLightOutput_t &out, directlight_t *dl, int facenum,
  357. // FourVectors const& pos, FourVectors *pNormals, int normalCount, int iThread,
  358. // int nLFlags = 0,
  359. // int static_prop_to_skip=-1,
  360. // float flEpsilon = 0.0 );
  361. //void GatherSampleAmbientSkySSE( SSE_sampleLightOutput_t &out, directlight_t *dl, int facenum,
  362. // FourVectors const& pos, FourVectors *pNormals, int normalCount, int iThread,
  363. // int nLFlags = 0, // GATHERLFLAGS_xxx
  364. // int static_prop_to_skip=-1,
  365. // float flEpsilon = 0.0 );
  366. //void GatherSampleStandardLightSSE( SSE_sampleLightOutput_t &out, directlight_t *dl, int facenum,
  367. // FourVectors const& pos, FourVectors *pNormals, int normalCount, int iThread,
  368. // int nLFlags = 0, // GATHERLFLAGS_xxx
  369. // int static_prop_to_skip=-1,
  370. // float flEpsilon = 0.0 );
  371. //-----------------------------------------------------------------------------
  372. // VRad Displacements
  373. //-----------------------------------------------------------------------------
  374. struct facelight_t;
  375. typedef struct radial_s radial_t;
  376. struct lightinfo_t;
  377. // NOTE: should probably come up with a bsptreetested_t struct or something,
  378. // see below (PropTested_t)
  379. struct DispTested_t
  380. {
  381. int m_Enum;
  382. int *m_pTested;
  383. };
  384. class IVRadDispMgr
  385. {
  386. public:
  387. // creation/destruction
  388. virtual void Init( void ) = 0;
  389. virtual void Shutdown( void ) = 0;
  390. // "CalcPoints"
  391. virtual bool BuildDispSamples( lightinfo_t *pLightInfo, facelight_t *pFaceLight, int ndxFace ) = 0;
  392. virtual bool BuildDispLuxels( lightinfo_t *pLightInfo, facelight_t *pFaceLight, int ndxFace ) = 0;
  393. virtual bool BuildDispSamplesAndLuxels_DoFast( lightinfo_t *pLightInfo, facelight_t *pFaceLight, int ndxFace ) = 0;
  394. // patching functions
  395. virtual void MakePatches( void ) = 0;
  396. virtual void SubdividePatch( int iPatch ) = 0;
  397. // pre "FinalLightFace"
  398. virtual void InsertSamplesDataIntoHashTable( void ) = 0;
  399. virtual void InsertPatchSampleDataIntoHashTable( void ) = 0;
  400. // "FinalLightFace"
  401. virtual radial_t *BuildLuxelRadial( int ndxFace, int ndxStyle, bool bBump ) = 0;
  402. virtual bool SampleRadial( int ndxFace, radial_t *pRadial, Vector const &vPos, int ndxLxl, LightingValue_t *pLightSample, int sampleCount, bool bPatch ) = 0;
  403. virtual radial_t *BuildPatchRadial( int ndxFace, bool bBump ) = 0;
  404. // utility
  405. virtual void GetDispSurfNormal( int ndxFace, Vector &pt, Vector &ptNormal, bool bInside ) = 0;
  406. virtual void GetDispSurf( int ndxFace, CVRADDispColl **ppDispTree ) = 0;
  407. // bsp tree functions
  408. virtual bool ClipRayToDisp( DispTested_t &dispTested, Ray_t const &ray ) = 0;
  409. virtual bool ClipRayToDispInLeaf( DispTested_t &dispTested, Ray_t const &ray, int ndxLeaf ) = 0;
  410. virtual void ClipRayToDispInLeaf( DispTested_t &dispTested, Ray_t const &ray,
  411. int ndxLeaf, float& dist, dface_t*& pFace, Vector2D& luxelCoord ) = 0;
  412. virtual void ClipRayToDispInLeaf( DispTested_t &dispTested, Ray_t const &ray,
  413. int ndxLeaf, float& dist, Vector *pNormal ) = 0;
  414. virtual void StartRayTest( DispTested_t &dispTested ) = 0;
  415. virtual void AddPolysForRayTrace() = 0;
  416. // general timing -- should be moved!!
  417. virtual void StartTimer( const char *name ) = 0;
  418. virtual void EndTimer( void ) = 0;
  419. };
  420. IVRadDispMgr *StaticDispMgr( void );
  421. //-----------------------------------------------------------------------------
  422. //-----------------------------------------------------------------------------
  423. inline bool ValidDispFace( dface_t *pFace )
  424. {
  425. if( !pFace ) { return false; }
  426. if( pFace->dispinfo == -1 ) { return false; }
  427. if( pFace->numedges != 4 ) { return false; }
  428. return true;
  429. }
  430. #define SAMPLEHASH_VOXEL_SIZE 64.0f
  431. typedef unsigned int SampleHandle_t; // the upper 16 bits = facelight index (works because max face are 65536)
  432. // the lower 16 bits = sample index inside of facelight
  433. struct sample_t;
  434. struct SampleData_t
  435. {
  436. unsigned short x, y, z;
  437. CUtlVector<SampleHandle_t> m_Samples;
  438. };
  439. struct PatchSampleData_t
  440. {
  441. unsigned short x, y, z;
  442. CUtlVector<int> m_ndxPatches;
  443. };
  444. UtlHashHandle_t SampleData_AddSample( sample_t *pSample, SampleHandle_t sampleHandle );
  445. void PatchSampleData_AddSample( CPatch *pPatch, int ndxPatch );
  446. unsigned short IncrementPatchIterationKey();
  447. void SampleData_Log( void );
  448. extern CUtlHash<SampleData_t> g_SampleHashTable;
  449. extern CUtlHash<PatchSampleData_t> g_PatchSampleHashTable;
  450. extern int samplesAdded;
  451. extern int patchSamplesAdded;
  452. //-----------------------------------------------------------------------------
  453. // Computes lighting for the detail props
  454. //-----------------------------------------------------------------------------
  455. void ComputeDetailPropLighting( int iThread );
  456. void ComputeIndirectLightingAtPoint( Vector &position, Vector &normal, Vector &outColor,
  457. int iThread, bool force_fast = false, bool bIgnoreNormals = false );
  458. //-----------------------------------------------------------------------------
  459. // VRad static props
  460. //-----------------------------------------------------------------------------
  461. class IPhysicsCollision;
  462. struct PropTested_t
  463. {
  464. int m_Enum;
  465. int* m_pTested;
  466. IPhysicsCollision *pThreadedCollision;
  467. };
  468. class IVradStaticPropMgr
  469. {
  470. public:
  471. // methods of IStaticPropMgr
  472. virtual void Init() = 0;
  473. virtual void Shutdown() = 0;
  474. virtual void ComputeLighting( int iThread ) = 0;
  475. virtual void AddPolysForRayTrace() = 0;
  476. };
  477. //extern PropTested_t s_PropTested[MAX_TOOL_THREADS+1];
  478. extern DispTested_t s_DispTested[MAX_TOOL_THREADS+1];
  479. IVradStaticPropMgr* StaticPropMgr();
  480. extern float ComputeCoverageFromTexture( float b0, float b1, float b2, int32 hitID );
  481. #endif // VRAD_H