File: LuaObjectMaterial.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (258 lines) | stat: -rw-r--r-- 6,628 bytes parent folder | download
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef LUA_OBJECT_MATERIAL_H
#define LUA_OBJECT_MATERIAL_H

// NOTE: some implementation is done in LuaMaterial.cpp

typedef            int   GLint;
typedef unsigned   int  GLuint;
typedef          float GLfloat;
typedef unsigned   int  GLenum;

#include <algorithm>
#include <cassert>
#include <vector>

class CSolidObject;
class LuaMatBin;


/******************************************************************************/
/******************************************************************************/

enum LuaObjType {
	LUAOBJ_UNIT    = 0,
	LUAOBJ_FEATURE = 1,
	LUAOBJ_LAST    = 2,
};

enum LuaMatType { //FIXME move deferred here
	LUAMAT_ALPHA          = 0,
	LUAMAT_OPAQUE         = 1,
	LUAMAT_ALPHA_REFLECT  = 2,
	LUAMAT_OPAQUE_REFLECT = 3,
	LUAMAT_SHADOW         = 4,
	LUAMAT_TYPE_COUNT     = 5,
};


/******************************************************************************/

class LuaMatRef {

	friend class LuaMatHandler;

	public:
		LuaMatRef() : bin(nullptr) {}
		LuaMatRef(const LuaMatRef&);
		LuaMatRef& operator=(const LuaMatRef&);
		~LuaMatRef();

		void Reset();

		void AddUnit(CSolidObject*);
		void AddFeature(CSolidObject*);

		inline bool IsActive() const { return (bin != nullptr); }

		inline const LuaMatBin* GetBin() const { return bin; }

	private:
		LuaMatRef(LuaMatBin* _bin);
		
	private:
		LuaMatBin* bin; // can be NULL
};


/******************************************************************************/

class LuaObjectLODMaterial {
	public:
		LuaObjectLODMaterial()
		: preDisplayList(0),
		  postDisplayList(0)
		{}

		inline bool IsActive() const { return matref.IsActive(); }

		inline void AddUnit(CSolidObject* o) { matref.AddUnit(o); }
		inline void AddFeature(CSolidObject* o) { matref.AddFeature(o); }

	public:
		GLuint preDisplayList;
		GLuint postDisplayList;

		LuaMatRef matref;
};


/******************************************************************************/

class LuaObjectMaterial {
	public:
		LuaObjectMaterial() : lodCount(0), lastLOD(0) {}

		bool SetLODCount(unsigned int count);
		bool SetLastLOD(unsigned int count);

		inline const unsigned int GetLODCount() const { return lodCount; }
		inline const unsigned int GetLastLOD() const { return lastLOD; }

		inline bool IsActive(unsigned int lod) const {
			if (lod >= lodCount)
				return false;

			return lodMats[lod].IsActive();
		}

		inline LuaObjectLODMaterial* GetMaterial(unsigned int lod) {
			if (lod >= lodCount)
				return nullptr;

			return &lodMats[lod];
		}

		inline const LuaObjectLODMaterial* GetMaterial(unsigned int lod) const {
			if (lod >= lodCount)
				return nullptr;

			return &lodMats[lod];
		}

	private:
		unsigned int lodCount;
		unsigned int lastLOD;
		std::vector<LuaObjectLODMaterial> lodMats;
};


/******************************************************************************/

struct LuaObjectMaterialData {
public:
	LuaObjectMaterialData() {
		lodCount = 0;
		currentLOD = 0;
	}

	bool Enabled() const { return (lodCount > 0); }
	bool ValidLOD(unsigned int lod) const { return (lod < lodCount); }

	const LuaObjectMaterial* GetLuaMaterials() const { return &luaMats[0]; }

	const LuaObjectMaterial* GetLuaMaterial(LuaMatType type) const { return &luaMats[type]; }
	      LuaObjectMaterial* GetLuaMaterial(LuaMatType type)       { return &luaMats[type]; }

	const LuaObjectLODMaterial* GetLuaLODMaterial(LuaMatType type) const {
		const LuaObjectMaterial* mat = GetLuaMaterial(type);
		const LuaObjectLODMaterial* lodMat = mat->GetMaterial(currentLOD);
		return lodMat;
	}

	LuaObjectLODMaterial* GetLuaLODMaterial(LuaMatType type) {
		LuaObjectMaterial* mat = GetLuaMaterial(type);
		LuaObjectLODMaterial* lodMat = mat->GetMaterial(currentLOD);
		return lodMat;
	}

	void UpdateCurrentLOD(LuaObjType objType, float lodDist, LuaMatType matType) {
		SetCurrentLOD(CalcCurrentLOD(objType, lodDist, luaMats[matType].GetLastLOD()));
	}

	unsigned int CalcCurrentLOD(LuaObjType objType, float lodDist, unsigned int lastLOD) const {
		if (lastLOD == 0)
			return 0;

		// positive values only!
		const float lpp = std::max(0.0f, lodDist * GLOBAL_LOD_FACTORS[objType]);

		for (/* no-op */; lastLOD != 0; lastLOD--) {
			if (lpp > lodLengths[lastLOD]) {
				break;
			}
		}

		return lastLOD;
	}

	unsigned int SetCurrentLOD(unsigned int lod) { return (currentLOD = lod); }

	unsigned int GetLODCount() const { return lodCount; }
	unsigned int GetCurrentLOD() const { return currentLOD; }

	float GetLODLength(unsigned int lod) const { return lodLengths[lod]; }


	void PushLODCount(unsigned int tmpCount) { lodStack.push_back(lodCount); lodCount = tmpCount; }
	void PopLODCount() { lodCount = lodStack.back(); lodStack.pop_back(); }

	void SetLODCount(unsigned int count) {
		const unsigned int oldCount = lodCount;

		lodLengths.resize(lodCount = count);

		for (unsigned int i = oldCount; i < count; i++) {
			lodLengths[i] = -1.0f;
		}

		for (int m = 0; m < LUAMAT_TYPE_COUNT; m++) {
			luaMats[m].SetLODCount(count);
		}
	}

	void SetLODLength(unsigned int lod, float length) {
		if (lod >= lodCount)
			return;

		lodLengths[lod] = length;
	}


	bool AddObjectForLOD(CSolidObject* o, LuaObjType objType, LuaMatType matType, float lodDist) {
		if (!Enabled())
			return false;

		LuaObjectMaterial* objMat = GetLuaMaterial(matType);
		LuaObjectLODMaterial* lodMat = objMat->GetMaterial(SetCurrentLOD(CalcCurrentLOD(objType, lodDist, objMat->GetLastLOD())));

		if ((lodMat != nullptr) && lodMat->IsActive()) {
			switch (objType) {
				case LUAOBJ_UNIT   : { lodMat->AddUnit   (o); } break;
				case LUAOBJ_FEATURE: { lodMat->AddFeature(o); } break;
				default            : {         assert(false); } break; // gcc needs always a default case, else it's not able to optimize the switch
			}

			return true;
		}

		return false;
	}

	static void SetGlobalLODFactor(LuaObjType objType, float lodFactor) {
		GLOBAL_LOD_FACTORS[objType] = lodFactor;
	}

private:
	static float GLOBAL_LOD_FACTORS[LUAOBJ_LAST];

	// equal to lodLengths.size(); if non-zero, then at least
	// one LOD-level has been assigned a custom Lua material
	unsigned int lodCount;
	// which LuaObjectLODMaterial should be used
	unsigned int currentLOD;

	// length-per-pixel; see CalcCurrentLOD
	std::vector<float> lodLengths;
	std::vector<unsigned int> lodStack;

	LuaObjectMaterial luaMats[LUAMAT_TYPE_COUNT];
};


/******************************************************************************/
/******************************************************************************/


#endif /* LUA_OBJECT_MATERIAL_H */