-
Notifications
You must be signed in to change notification settings - Fork 765
/
Lighting.cpp
316 lines (273 loc) · 11.7 KB
/
Lighting.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2020 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <osgEarth/Lighting>
#include <osgUtil/CullVisitor>
#include <osgDB/ObjectWrapper>
#include <osgEarth/StringUtils>
#include <osgEarth/GLUtils>
using namespace osgEarth;
using namespace osgEarth::Util;
#define LC "[Lighting] "
// prefix to use for uniforms.
#define UPREFIX "osg_"
//............................................................................
void
Lighting::set(osg::StateSet* stateSet, osg::StateAttribute::OverrideValue value)
{
GLUtils::setLighting(stateSet, value);
}
void
Lighting::remove(osg::StateSet* stateSet)
{
GLUtils::remove(stateSet, GL_LIGHTING);
}
void
Lighting::installDefaultMaterial(osg::StateSet* stateSet)
{
osg::Material* m = new osgEarth::MaterialGL3();
m->setDiffuse(m->FRONT, osg::Vec4(1,1,1,1));
m->setAmbient(m->FRONT, osg::Vec4(1,1,1,1));
stateSet->setAttributeAndModes(m, 1);
osgEarth::MaterialCallback().operator()(m, 0L);
}
//............................................................................
GenerateGL3LightingUniforms::GenerateGL3LightingUniforms() :
osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
{
setNodeMaskOverride(~0);
}
void
GenerateGL3LightingUniforms::apply(osg::Node& node)
{
osg::StateSet* stateset = node.getStateSet();
if (stateset)
{
if (_statesets.find(stateset) == _statesets.end())
{
const osg::StateSet::RefAttributePair* rap = stateset->getAttributePair(osg::StateAttribute::MATERIAL);
if (rap)
{
osg::Material* material = dynamic_cast<osg::Material*>(rap->first.get());
if (material)
{
osg::Material* mat = material;
#if !defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
// If there's no FFP, we need to replace the Material with a GL3 Material to prevent
// error messages on the console.
if (dynamic_cast<MaterialGL3*>(material) == 0L)
{
mat = new MaterialGL3(*material);
stateset->setAttributeAndModes(mat, rap->second);
}
#endif
// Install the MaterialCallback so uniforms are updated.
if (!mat->getUpdateCallback())
{
if (stateset->getDataVariance() == osg::Object::DYNAMIC)
mat->setUpdateCallback(new MaterialCallback());
else
{
MaterialCallback mc;
mc.operator()(mat, NULL);
}
}
}
// mark this stateset as visited.
_statesets.insert(stateset);
}
}
}
traverse(node);
}
void
GenerateGL3LightingUniforms::apply(osg::LightSource& lightSource)
{
if (lightSource.getLight())
{
if (!alreadyInstalled<LightSourceGL3UniformGenerator>(lightSource.getCullCallback()))
{
lightSource.addCullCallback(new LightSourceGL3UniformGenerator());
}
//#if !defined(OSG_GL_FIXED_FUNCTION_AVAILABLE)
// If there's no FFP, we need to replace the Light with a LightGL3 to prevent
// error messages on the console.
if (dynamic_cast<LightGL3*>(lightSource.getLight()) == 0L)
{
lightSource.setLight(new LightGL3(*lightSource.getLight()));
}
//#endif
}
apply(static_cast<osg::Node&>(lightSource));
}
//............................................................................
LightSourceGL3UniformGenerator::LightSourceGL3UniformGenerator() :
_statesetsMutex()
{
//nop
}
bool
LightSourceGL3UniformGenerator::run(osg::Object* obj, osg::Object* data)
{
osg::LightSource* lightSource = dynamic_cast<osg::LightSource*>(obj);
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(data);
if (cv && lightSource && lightSource->getLight())
{
osg::Light* light = lightSource->getLight();
// replace the index with the light number:
//std::string prefix = Stringify() << UPREFIX << "LightSource[" << light->getLightNum() << "].";
std::string prefix;
if (light->getLightNum() < 10)
{
prefix = UPREFIX "LightSource[#].";
prefix[prefix.length() - 3] = (char)('0' + light->getLightNum());
}
else
{
prefix = UPREFIX "LightSource[##].";
int lightNumTens = light->getLightNum()/10;
prefix[prefix.length() - 4] = (char)('0'+lightNumTens);
prefix[prefix.length() - 3] = (char)('0' +(light->getLightNum()-(10*lightNumTens)));
}
// Lights are positional state so their location in the scene graph is only important
// in terms of model transformation, and not in terms of what gets lit.
// Place these uniforms at the root stateset so they affect the entire graph:
osg::StateSet* ss = cv->getCurrentRenderStage()->getStateSet();
if (ss == 0L)
{
cv->getCurrentRenderStage()->setStateSet(ss = new osg::StateSet());
std::lock_guard<std::mutex> lock(_statesetsMutex);
_statesets.push_back(ss);
}
ss->getOrCreateUniform(prefix + "ambient", osg::Uniform::FLOAT_VEC4)->set(light->getAmbient());
ss->getOrCreateUniform(prefix + "diffuse", osg::Uniform::FLOAT_VEC4)->set(light->getDiffuse());
ss->getOrCreateUniform(prefix + "specular", osg::Uniform::FLOAT_VEC4)->set(light->getSpecular());
// add the positional elements:
const osg::Matrix& mvm = *cv->getModelViewMatrix();
ss->getOrCreateUniform(prefix + "position", osg::Uniform::FLOAT_VEC4)->set(light->getPosition() * mvm);
osg::Vec3 directionLocal = osg::Matrix::transform3x3(light->getDirection(), mvm);
directionLocal.normalize();
ss->getOrCreateUniform(prefix + "spotDirection", osg::Uniform::FLOAT_VEC3)->set(directionLocal);
ss->getOrCreateUniform(prefix + "spotExponent", osg::Uniform::FLOAT)->set(light->getSpotExponent());
ss->getOrCreateUniform(prefix + "spotCutoff", osg::Uniform::FLOAT)->set(light->getSpotCutoff());
ss->getOrCreateUniform(prefix + "spotCosCutoff", osg::Uniform::FLOAT)->set(cosf(light->getSpotCutoff()));
ss->getOrCreateUniform(prefix + "constantAttenuation", osg::Uniform::FLOAT)->set(light->getConstantAttenuation());
ss->getOrCreateUniform(prefix + "linearAttenuation", osg::Uniform::FLOAT)->set(light->getLinearAttenuation());
ss->getOrCreateUniform(prefix + "quadraticAttenuation", osg::Uniform::FLOAT)->set(light->getQuadraticAttenuation());
LightGL3* lightGL3 = dynamic_cast<LightGL3*>(light);
bool enabled = lightGL3 ? lightGL3->getEnabled() : true;
ss->getOrCreateUniform(prefix + "enabled", osg::Uniform::BOOL)->set(enabled);
osg::Uniform* fsu = ss->getOrCreateUniform("oe_lighting_framestamp", osg::Uniform::UNSIGNED_INT);
unsigned fs;
fsu->get(fs);
osg::StateSet::DefinePair* numLights = ss->getDefinePair("OE_NUM_LIGHTS");
if (fs != cv->getFrameStamp()->getFrameNumber())
{
ss->setDefine("OE_NUM_LIGHTS", "1", osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
fsu->set(cv->getFrameStamp()->getFrameNumber());
}
else
{
int value = 1;
if (numLights) {
value = ::atoi(numLights->first.c_str()) + 1;
}
ss->setDefine("OE_NUM_LIGHTS", Stringify() << value, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
}
}
return traverse(obj, data);
}
void
LightSourceGL3UniformGenerator::resizeGLBufferObjects(unsigned maxSize)
{
std::lock_guard<std::mutex> lock(_statesetsMutex);
for(unsigned i=0; i<_statesets.size(); ++i)
_statesets[i]->resizeGLObjectBuffers(maxSize);
}
void
LightSourceGL3UniformGenerator::releaseGLObjects(osg::State* state) const
{
std::lock_guard<std::mutex> lock(_statesetsMutex);
for(unsigned i=0; i<_statesets.size(); ++i)
_statesets[i]->releaseGLObjects(state);
_statesets.clear();
}
//............................................................................
void MaterialCallback::operator() (osg::StateAttribute* attr, osg::NodeVisitor* nv)
{
static const std::string AMBIENT = UPREFIX "FrontMaterial.ambient";
static const std::string DIFFUSE = UPREFIX "FrontMaterial.diffuse";
static const std::string SPECULAR = UPREFIX "FrontMaterial.specular";
static const std::string EMISSION = UPREFIX "FrontMaterial.emission";
static const std::string SHININESS = UPREFIX "FrontMaterial.shininess";
osg::Material* material = static_cast<osg::Material*>(attr);
for (unsigned int i = 0; i < attr->getNumParents(); i++)
{
osg::StateSet* stateSet = attr->getParent(i);
stateSet->getOrCreateUniform(AMBIENT, osg::Uniform::FLOAT_VEC4)->set(material->getAmbient(osg::Material::FRONT));
stateSet->getOrCreateUniform(DIFFUSE, osg::Uniform::FLOAT_VEC4)->set(material->getDiffuse(osg::Material::FRONT));
stateSet->getOrCreateUniform(SPECULAR, osg::Uniform::FLOAT_VEC4)->set(material->getSpecular(osg::Material::FRONT));
stateSet->getOrCreateUniform(EMISSION, osg::Uniform::FLOAT_VEC4)->set(material->getEmission(osg::Material::FRONT));
stateSet->getOrCreateUniform(SHININESS, osg::Uniform::FLOAT)->set(material->getShininess(osg::Material::FRONT));
//TODO: back-face materials
}
}
//............................................................................
void
LightGL3::apply(osg::State& state) const
{
#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
osg::Light::apply(state);
#endif
}
// Serializer for LightGL3.
// The odd namespace name is here because this macro cannot be used more than once
// in the same namespace in the same cpp file.
namespace osgEarth { namespace Serializers { namespace LightGL3
{
REGISTER_OBJECT_WRAPPER(
LightGL3,
new osgEarth::LightGL3,
osgEarth::LightGL3,
"osg::Object osg::StateAttribute osg::Light osgEarth::LightGL3")
{
ADD_BOOL_SERIALIZER(Enabled, true);
}
} } }
//............................................................................
void
MaterialGL3::apply(osg::State& state) const
{
#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
osg::Material::apply(state);
#endif
}
// Serializer for MaterialGL3.
// The odd namespace name is here because this macro cannot be used more than once
// in the same namespace in the same cpp file.
namespace osgEarth { namespace Serializers { namespace MaterialGL3
{
REGISTER_OBJECT_WRAPPER(
MaterialGL3,
new osgEarth::MaterialGL3,
osgEarth::MaterialGL3,
"osg::Object osg::StateAttribute osg::Material osgEarth::MaterialGL3") { }
} } }