File: SunLighting.cpp

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 (150 lines) | stat: -rw-r--r-- 3,915 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <cassert>

#include "SunLighting.h"

#include "Map/MapInfo.h"
#include "System/EventHandler.h"
#include "System/Sync/HsiehHash.h"

/**
 * @brief sunLightingInst
 *
 * Global instance of CSunLighting
 */
CSunLighting sunLightingInst;

CSunLighting::CSunLighting() {
	colors[0] = &groundAmbientColor;
	colors[1] = &groundDiffuseColor;
	colors[2] = &groundSpecularColor;

	colors[3] = &modelAmbientColor;
	colors[4] = &modelDiffuseColor;
	colors[5] = &modelSpecularColor;

	specularExponent    = 0.0f;
	groundShadowDensity = 0.0f;
	modelShadowDensity  = 0.0f;
}

// need an explicit copy-ctor because of colors[]
CSunLighting::CSunLighting(const CSunLighting& sl) {
	colors[0] = &groundAmbientColor;
	colors[1] = &groundDiffuseColor;
	colors[2] = &groundSpecularColor;

	colors[3] = &modelAmbientColor;
	colors[4] = &modelDiffuseColor;
	colors[5] = &modelSpecularColor;

	Copy(sl);
}

CSunLighting& CSunLighting::operator = (const CSunLighting& sl) {
	Copy(sl);
	return (*this);
}


void CSunLighting::Init() {
	assert(mapInfo != nullptr);
	assert(IsGlobalInstance());

	const CMapInfo::light_t& light = mapInfo->light;

	groundAmbientColor   = light.groundAmbientColor;
	groundDiffuseColor   = light.groundDiffuseColor;
	groundSpecularColor  = light.groundSpecularColor;

	modelAmbientColor    = light.modelAmbientColor;
	modelDiffuseColor    = light.modelDiffuseColor;
	modelSpecularColor   = light.modelSpecularColor;

	specularExponent     = light.specularExponent;
	groundShadowDensity  = light.groundShadowDensity;
	modelShadowDensity   = light.modelShadowDensity;
}

void CSunLighting::Copy(const CSunLighting& sl) {
	assert(   colors[0] == &   groundAmbientColor);
	assert(sl.colors[0] == &sl.groundAmbientColor);

	if (sl == (*this))
		return;

	for (unsigned int n = 0; n < sizeof(colors) / sizeof(colors[0]); n++)
		*colors[n] = *sl.colors[n];

	specularExponent    = sl.specularExponent;
	groundShadowDensity = sl.groundShadowDensity;
	modelShadowDensity  = sl.modelShadowDensity;

	if (!IsGlobalInstance())
		return;

	// send event only if at least one value was changed for the global instance
	eventHandler.SunChanged();
}


bool CSunLighting::SetValue(unsigned int keyHash, const float4 value) {
	static const unsigned int keyHashes[] = {
		HsiehHash("groundAmbientColor",  sizeof("groundAmbientColor" ) - 1, 0),
		HsiehHash("groundDiffuseColor",  sizeof("groundDiffuseColor" ) - 1, 0),
		HsiehHash("groundSpecularColor", sizeof("groundSpecularColor") - 1, 0),

		HsiehHash("unitAmbientColor",  sizeof("unitAmbientColor" ) - 1, 0),
		HsiehHash("unitDiffuseColor",  sizeof("unitDiffuseColor" ) - 1, 0),
		HsiehHash("unitSpecularColor", sizeof("unitSpecularColor") - 1, 0),

		HsiehHash(   "specularExponent", sizeof(   "specularExponent") - 1, 0),
		HsiehHash("groundShadowDensity", sizeof("groundShadowDensity") - 1, 0),
		HsiehHash( "modelShadowDensity", sizeof( "modelShadowDensity") - 1, 0),
	};

	// special casees
	if (keyHash == keyHashes[6]) {
		specularExponent = value.x;
		return true;
	}
	if (keyHash == keyHashes[7]) {
		groundShadowDensity = value.x;
		return true;
	}
	if (keyHash == keyHashes[8]) {
		modelShadowDensity = value.x;
		return true;
	}

	for (unsigned int n = 0; n < sizeof(colors) / sizeof(colors[0]); n++) {
		if (keyHash == keyHashes[n]) {
			*(colors[n]) = value;
			return true;
		}
	}

	return false;
}


bool CSunLighting::operator == (const CSunLighting& sl) const {
	for (unsigned int n = 0; n < sizeof(colors) / sizeof(colors[0]); n++) {
		if (colors[n] != sl.colors[n]) {
			return false;
		}
	}

	if (groundShadowDensity != sl.groundShadowDensity)
		return false;
	if (modelShadowDensity != sl.modelShadowDensity)
		return false;

	return (specularExponent == sl.specularExponent);
}

bool CSunLighting::IsGlobalInstance() const {
	return (this == &sunLightingInst);
}