aboutsummaryrefslogtreecommitdiff
path: root/mp/src/utils/vrad/incremental.h
blob: 9e98054b352a4bfeeaccb55926b9c923ec21c89b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef INCREMENTAL_H
#define INCREMENTAL_H
#ifdef _WIN32
#pragma once
#endif



#include "iincremental.h"
#include "utllinkedlist.h"
#include "utlvector.h"
#include "utlbuffer.h"
#include "vrad.h"


#define INCREMENTALFILE_VERSION	31241


class CIncLight;


class CLightValue
{
public:
	float m_Dot;
};


class CLightFace
{
public:
	unsigned short				m_FaceIndex;		// global face index
	unsigned short				m_LightFacesIndex;	// index into CIncLight::m_LightFaces.

	// The lightmap grid for this face. Only used while building lighting data for a face.
	// Compressed into m_CompressedData immediately afterwards.
	CUtlVector<CLightValue>		m_LightValues;

	CUtlBuffer					m_CompressedData;
	CIncLight					*m_pLight;
};


class CIncLight
{
public:
					CIncLight();
					~CIncLight();

	CLightFace*		FindOrCreateLightFace( int iFace, int lmSize, bool *bNew=NULL );


public:

	CRITICAL_SECTION	m_CS;

	// This is the light for which m_LightFaces was built.
	dworldlight_t	m_Light;

	CLightFace		*m_pCachedFaces[MAX_TOOL_THREADS+1];

	// The list of faces that this light contributes to.
	CUtlLinkedList<CLightFace*, unsigned short>	m_LightFaces;

	// Largest value in intensity of light. Used to scale dot products up into a
	// range where their values make sense.
	float			m_flMaxIntensity;
};


class CIncrementalHeader
{
public:
	class CLMSize
	{
	public:
		unsigned char m_Width;
		unsigned char m_Height;
	};

	CUtlVector<CLMSize>	m_FaceLightmapSizes;
};


class CIncremental : public IIncremental
{
public:

						CIncremental();
						~CIncremental();



// IIncremental overrides.
public:

	virtual bool		Init( char const *pBSPFilename, char const *pIncrementalFilename );

	// Load the light definitions out of the incremental file.
	// Figure out which lights have changed.
	// Change 'activelights' to only consist of new or changed lights.
	virtual bool		PrepareForLighting();

	virtual void		AddLightToFace( 
		IncrementalLightID lightID, 
		int iFace, 
		int iSample,
		int lmSize,
		float dot,
		int iThread );

	virtual void		FinishFace(
		IncrementalLightID lightID,
		int iFace,
		int iThread );

	// For each face that was changed during the lighting process, save out
	// new data for it in the incremental file.
	virtual bool		Finalize();

	virtual void		GetFacesTouched( CUtlVector<unsigned char> &touched );

	virtual bool		Serialize();


private:

	// Read/write the header from the file.
	bool				ReadIncrementalHeader( long fp, CIncrementalHeader *pHeader );
	bool				WriteIncrementalHeader( long fp );

	// Returns true if the incremental file is valid and we can use InitUpdate.
	bool				IsIncrementalFileValid();
	
	void				Term();

	// For each light in 'activelights', add a light to m_Lights and link them together.
	void				AddLightsForActiveLights();

	// Load and save the state.
	bool				LoadIncrementalFile();
	bool				SaveIncrementalFile();

	typedef CUtlVector<CLightFace*> CFaceLightList;
	void				LinkLightsToFaces( CUtlVector<CFaceLightList> &faceLights );


private:

	char const		*m_pIncrementalFilename;
	char const		*m_pBSPFilename;
	
	CUtlLinkedList<CIncLight*, IncrementalLightID>	
					m_Lights;

	// The face index is set to 1 if a face has new lighting data applied to it.
	// This is used to optimize the set of lightmaps we recomposite.
	CUtlVector<unsigned char>	m_FacesTouched;
	
	int				m_TotalMemory;

	// Set to true when one or more runs were completed successfully.
	bool			m_bSuccessfulRun;
};



#endif // INCREMENTAL_H