File: LightHandler.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 (153 lines) | stat: -rwxr-xr-x 5,371 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "myGL.h"
#include "LightHandler.h"
#include "Rendering/Shaders/Shader.h"
#include "Sim/Misc/GlobalSynced.h"

static const float4 ZeroVector4 = float4(0.0f, 0.0f, 0.0f, 0.0f);

void GL::LightHandler::Init(unsigned int cfgBaseLight, unsigned int cfgMaxLights) {
	glGetIntegerv(GL_MAX_LIGHTS, reinterpret_cast<int*>(&maxLights));

	baseLight = cfgBaseLight;
	maxLights -= baseLight;
	maxLights = std::min(maxLights, cfgMaxLights);

	for (unsigned int i = 0; i < maxLights; i++) {
		const unsigned int lightID = GL_LIGHT0 + baseLight + i;

		glEnable(lightID);
		glLightfv(lightID, GL_POSITION, &ZeroVector4.x);
		glLightfv(lightID, GL_AMBIENT,  &ZeroVector4.x);
		glLightfv(lightID, GL_DIFFUSE,  &ZeroVector4.x);
		glLightfv(lightID, GL_SPECULAR, &ZeroVector4.x);
		glLightfv(lightID, GL_SPOT_DIRECTION, &ZeroVector4.x);
		glLightf(lightID, GL_SPOT_CUTOFF, 180.0f);
		glLightf(lightID, GL_CONSTANT_ATTENUATION,  1.0f);
		glLightf(lightID, GL_LINEAR_ATTENUATION,    0.0f);
		glLightf(lightID, GL_QUADRATIC_ATTENUATION, 0.0f);
		glDisable(lightID);

		// reserve free light ID's
		lightIDs.push_back(lightID);
	}
}


unsigned int GL::LightHandler::AddLight(const GL::Light& light) {
	if (light.GetTTL() == 0 || light.GetRadius() <= 0.0f) { return -1U; }
	if (light.GetIntensityWeight().SqLength() <= 0.01f) { return -1U; }

	if (lights.size() >= maxLights || lightIDs.empty()) {
		unsigned int minPriorityValue = light.GetPriority();
		unsigned int minPriorityHandle = -1U;

		for (std::map<unsigned int, GL::Light>::const_iterator it = lights.begin(); it != lights.end(); ++it) {
			const GL::Light& lgt = it->second;

			if (lgt.GetPriority() < minPriorityValue) {
				minPriorityValue = lgt.GetPriority();
				minPriorityHandle = it->first;
			}
		}

		if (minPriorityHandle != -1U) {
			lightIntensityWeight -= lights[minPriorityHandle].GetIntensityWeight();
			lightIDs.push_back(lights[minPriorityHandle].GetID());
			lights.erase(minPriorityHandle);
		} else {
			// no available light to replace
			return -1U;
		}
	}

	lights[lightHandle] = light;
	lights[lightHandle].SetID(lightIDs.front());
	lights[lightHandle].SetRelativeTime(0);
	lights[lightHandle].SetAbsoluteTime(gs->frameNum);

	lightIntensityWeight += light.GetIntensityWeight();
	lightIDs.pop_front();

	return (lightHandle++);
}

GL::Light* GL::LightHandler::GetLight(unsigned int _lightHandle) {
	const std::map<unsigned int, GL::Light>::iterator it = lights.find(_lightHandle);

	if (it != lights.end()) {
		return &(it->second);
	}

	return NULL;
}


void GL::LightHandler::Update(Shader::IProgramObject* shader) {
	if (lights.size() != numLights) {
		numLights = lights.size();

		// update the active light-count (note: unused, number of lights
		// to iterate over needs to be known at shader compilation-time)
		// shader->SetUniform1i(uniformIndex, numLights);
	}

	if (numLights == 0) {
		return;
	}

	for (std::map<unsigned int, GL::Light>::iterator it = lights.begin(); it != lights.end(); ) {
		GL::Light& light = it->second;

		if (light.GetAbsoluteTime() != gs->frameNum) {
			light.SetRelativeTime(light.GetRelativeTime() + 1);
			light.SetAbsoluteTime(gs->frameNum);
			light.DecayColors();
			light.ClampColors();
		}

		const unsigned int lightID = light.GetID();
		const unsigned int lightHndle = it->first;

		const float4  weightedAmbientCol  = (light.GetAmbientColor()  * light.GetIntensityWeight().x) / lightIntensityWeight.x;
		const float4  weightedDiffuseCol  = (light.GetDiffuseColor()  * light.GetIntensityWeight().y) / lightIntensityWeight.y;
		const float4  weightedSpecularCol = (light.GetSpecularColor() * light.GetIntensityWeight().z) / lightIntensityWeight.z;
		const float3* lightTrackPos       = light.GetTrackPosition();
		const float3* lightTrackDir       = light.GetTrackDirection();
		const float4& lightPos            = (lightTrackPos != NULL)? float4(*lightTrackPos, 1.0f): light.GetPosition();
		const float3& lightDir            = (lightTrackDir != NULL)? float3(*lightTrackDir      ): light.GetDirection();

		++it;

		if (light.GetRelativeTime() > light.GetTTL()) {
			lightIntensityWeight -= light.GetIntensityWeight();
			lightIDs.push_back(lightID);
			lights.erase(lightHndle);

			// kill the contribution from this light
			glEnable(lightID);
			glLightfv(lightID, GL_AMBIENT,  &ZeroVector4.x);
			glLightfv(lightID, GL_DIFFUSE,  &ZeroVector4.x);
			glLightfv(lightID, GL_SPECULAR, &ZeroVector4.x);
			glDisable(lightID);
		} else {
			// communicate properties via the FFP to save uniforms
			// note: we want MV to be identity here
			glEnable(lightID);
			glLightfv(lightID, GL_POSITION, &lightPos.x);
			glLightfv(lightID, GL_AMBIENT,  &weightedAmbientCol.x);
			glLightfv(lightID, GL_DIFFUSE,  &weightedDiffuseCol.x);
			glLightfv(lightID, GL_SPECULAR, &weightedSpecularCol.x);
			glLightfv(lightID, GL_SPOT_DIRECTION, &lightDir.x);
			glLightf(lightID, GL_SPOT_CUTOFF, light.GetFOV());
			glLightf(lightID, GL_CONSTANT_ATTENUATION, light.GetRadius()); //!
			#if (OGL_SPEC_ATTENUATION == 1)
			glLightf(lightID, GL_CONSTANT_ATTENUATION,  light.GetAttenuation().x);
			glLightf(lightID, GL_LINEAR_ATTENUATION,    light.GetAttenuation().y);
			glLightf(lightID, GL_QUADRATIC_ATTENUATION, light.GetAttenuation().z);
			#endif
			glDisable(lightID);
		}
	}
}