File: SkyLight.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (152 lines) | stat: -rwxr-xr-x 4,489 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include <cfloat>

#include "SkyLight.h"
#include "Map/BaseGroundDrawer.h"
#include "Map/MapInfo.h"
#include "Map/ReadMap.h"
#include "Rendering/GroundDecalHandler.h"
#include "Rendering/UnitDrawer.h"
#include "Rendering/Env/ISky.h"
#include "Sim/Misc/GlobalSynced.h"
#include "System/EventHandler.h"
#include "System/myMath.h"
#include "System/Config/ConfigHandler.h"

CONFIG(float, DynamicSunMinElevation).defaultValue(0.1f);


StaticSkyLight::StaticSkyLight() {
	const CMapInfo::light_t& light = mapInfo->light;

	lightIntensity = 1.0f;
	groundShadowDensity = light.groundShadowDensity;
	unitShadowDensity = light.unitShadowDensity;

	lightDir = light.sunDir;
	lightDir.w = 0.0f;
}



DynamicSkyLight::DynamicSkyLight()
	: luaControl(false)
	, updateNeeded(true)
{
	const CMapInfo::light_t& light = mapInfo->light;

	lightIntensity = 1.0f;
	groundShadowDensity = light.groundShadowDensity;
	unitShadowDensity = light.unitShadowDensity;
	orbitMinSunHeight = configHandler->GetFloat("DynamicSunMinElevation"); //FIXME mapinfo option???
	orbitMinSunHeight = Clamp(orbitMinSunHeight, -1.0f, 1.0f);

	// light.sunDir has already been normalized (in 3D) in MapInfo
	SetLightParams(light.sunDir, light.sunStartAngle, light.sunOrbitTime);
}

void DynamicSkyLight::Update() {
	if (!luaControl) {
		SetLightDir(CalculateSunPos(sunStartAngle).ANormalize());

		lightIntensity = math::sqrt(Clamp(lightDir.y, 0.0f, 1.0f));

		const float shadowDensity = std::min(1.0f, lightIntensity * shadowDensityFactor);
		const CMapInfo::light_t& light = mapInfo->light;

		groundShadowDensity = shadowDensity * light.groundShadowDensity;
		unitShadowDensity   = shadowDensity * light.unitShadowDensity;
	}

	if (updateNeeded) {
		updateNeeded = false;

		sky->UpdateSunDir();
		readmap->GetGroundDrawer()->UpdateSunDir();
		eventHandler.SunChanged(lightDir);
	}
}



bool DynamicSkyLight::SetLightDir(const float4& newLightDir) {
	if (newLightDir != lightDir) {
		static float4 lastUpdate = ZeroVector;
		static const float minCosAngle = cos(1.5f * (PI/180.f));

		if (lastUpdate.dot(newLightDir) < minCosAngle) {
			lastUpdate   = newLightDir;
			updateNeeded = true;
		}

		lightDir = newLightDir;
		lightDir.w = 0.0f;
		return true;
	}

	return false;
}

float4 DynamicSkyLight::CalculateSunPos(const float startAngle) const {
	const float gameSeconds = gs->frameNum / (float)GAME_SPEED;
	const float angularVelocity = 2.0f * PI / sunOrbitTime;

	const float sunAng  = startAngle - initialSunAngle - angularVelocity * gameSeconds;
	const float4 sunPos = sunRotation.Mul(float3(sunOrbitRad * cos(sunAng), sunOrbitHeight, sunOrbitRad * sin(sunAng)));

	return sunPos;
}

void DynamicSkyLight::SetLightParams(float4 newLightDir, float startAngle, float orbitTime) {
	newLightDir.ANormalize();

	sunStartAngle = PI + startAngle; //FIXME WHY +PI?
	sunOrbitTime = orbitTime;
	initialSunAngle = GetRadFromXY(newLightDir.x, newLightDir.z);

	//FIXME This function really really needs comments about what it does!
	if (newLightDir.w == FLT_MAX) {
		// old: newLightDir is position where sun reaches highest altitude
		const float sunLen = newLightDir.Length2D();
		const float sunAzimuth = (sunLen <= 0.001f) ? PI / 2.0f : atan(newLightDir.y / sunLen);
		const float sunHeight = tan(sunAzimuth - 0.001f);

		float3 v1(cos(initialSunAngle), sunHeight, sin(initialSunAngle));
		v1.ANormalize();

		if (v1.y <= orbitMinSunHeight) {
			newLightDir = UpVector;
			sunOrbitHeight = v1.y;
			sunOrbitRad = sqrt(1.0f - sunOrbitHeight * sunOrbitHeight);
		} else {
			float3 v2(cos(initialSunAngle + PI), orbitMinSunHeight, sin(initialSunAngle + PI));
			v2.ANormalize();
			float3 v3 = v2 - v1;
			sunOrbitRad = v3.Length() / 2.0f;
			v3.ANormalize();

			float3 v4 = (v3.cross(UpVector)).ANormalize();
			float3 v5 = (v3.cross(v4)).ANormalize();

			if (v5.y < 0.0f)
				v5 = -v5;

			newLightDir = v5;
			sunOrbitHeight = v5.dot(v1);
		}
	} else {
		// new: newLightDir is center position of orbit, and newLightDir.w is orbit height
		sunOrbitHeight = std::max(-1.0f, std::min(newLightDir.w, 1.0f));
		sunOrbitRad = sqrt(1.0f - sunOrbitHeight * sunOrbitHeight);
	}

	sunRotation.LoadIdentity();
	sunRotation.SetUpVector(newLightDir);

	const float4& peakDir  = CalculateSunPos(0.0f);
	const float peakElev   = std::max(0.01f, peakDir.y);

	shadowDensityFactor = 1.0f / peakElev;

	SetLightDir(CalculateSunPos(sunStartAngle).ANormalize());
}