Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
#include "Headers/Light.h"

#include "Graphs/Headers/SceneGraph.h"
#include "Core/Headers/ParamHandler.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/LightManager.h"
#include "Managers/Headers/SceneManager.h"
#include "ShadowMapping/Headers/ShadowMap.h"
#include "Dynamics/Entities/Headers/Impostor.h"
#include "Geometry/Material/Headers/Material.h"
#include "Rendering/Camera/Headers/FreeFlyCamera.h"
#include "Geometry/Shapes/Headers/Predefined/Sphere3D.h"

namespace Divide {

Light::Light(const F32 range, const LightType& type)
    : SceneNode(SceneNodeType::TYPE_LIGHT),
      _type(type),
      _drawImpostor(false),
      _updateLightBB(false),
      _lightSGN(nullptr),
      _impostor(nullptr),
      _resolutionFactor(1),
      _impostorSGN(nullptr),
      _par(ParamHandler::getInstance()),
      _shadowMapInfo(nullptr),
      _score(0.0f) {
    // All lights default to fully dynamic for now.
    setLightMode(LIGHT_MODE_MOVABLE);
    for (U8 i = 0; i < Config::Lighting::MAX_SPLITS_PER_LIGHT; ++i) {
        _shadowProperties._lightVP[i].identity();
    }
    _shadowProperties._floatValues.set(-1.0f);

    _shadowCamera = MemoryManager_NEW FreeFlyCamera();
    _shadowCamera->setMoveSpeedFactor(0.0f);
    _shadowCamera->setTurnSpeedFactor(0.0f);
    _shadowCamera->setFixedYawAxis(true);

    _properties._diffuse.set(DefaultColors::WHITE());
    _properties._position.w = 35.0f;
    _properties._options.x = getLightTypeValue();
    _properties._options.y = 0;
    _properties._direction.w = 0.0f;
    _properties._attenuation =
        vec4<F32>(1.0f, 0.07f, 0.017f, 1.0f);  // constAtt, linearAtt, quadAtt

    memset(_dirty, true, PropertyType_PLACEHOLDER * sizeof(bool));
    _enabled = true;
    _renderState.addToDrawExclusionMask(RenderStage::DEPTH_STAGE);
}

Light::~Light() { unload(); }

bool Light::load(const stringImpl& name) {
    setName(name);
    setShadowMappingCallback(DELEGATE_BIND(&SceneManager::renderVisibleNodes,
                                           &SceneManager::getInstance()));
    assert(LightManager::hasInstance());
    return LightManager::getInstance().addLight(this);
}

bool Light::unload() {
    if (getState() != RES_LOADED && getState() != RES_LOADING) {
        return true;
    }

    // DELETE(_shadowCamera); <-- deleted by the camera manager
    LightManager::getInstance().removeLight(getGUID());

    removeShadowMapInfo();

    return SceneNode::unload();
}

void Light::postLoad(SceneGraphNode& sgn) {
    // Hold a pointer to the light's location in the SceneGraph
    DIVIDE_ASSERT(
        _lightSGN == nullptr,
        "Light error: Lights can only be bound to a single SGN node!");

    _lightSGN = &sgn;
    _updateLightBB = true;

    SceneNode::postLoad(sgn);
}

void Light::onCameraChange() {
    assert(_lightSGN != nullptr);
    if (!getEnabled()) {
        return;
    }

    _dirty[PROPERTY_TYPE_PHYSICAL] = true;

    if (_type != LIGHT_TYPE_DIRECTIONAL) {
        if (_mode == LIGHT_MODE_MOVABLE) {
            const vec3<F32>& newPosition =
                _lightSGN->getComponent<PhysicsComponent>()->getPosition();
            if (_properties._position.xyz() != newPosition) {
                _properties._position.xyz(newPosition);
            }
        } else {
            _lightSGN->getComponent<PhysicsComponent>()->setPosition(
                _properties._position.xyz());
        }

        if (_updateLightBB) {
            _lightSGN->updateBoundingBoxTransform(
                _lightSGN->getComponent<PhysicsComponent>()->getWorldMatrix());
        }
    }

    _updateLightBB = false;
}

void Light::setPosition(const vec3<F32>& newPosition) {
    // Togglable lights can't be moved.
    if (_mode == LIGHT_MODE_TOGGLABLE) {
        Console::errorfn(Locale::get("WARNING_ILLEGAL_PROPERTY"), getGUID(),
                         "Light_Togglable", "LIGHT_POSITION");
        return;
    }

    _properties._position.xyz(newPosition);

    if (_mode == LIGHT_MODE_MOVABLE) {
        _lightSGN->getComponent<PhysicsComponent>()->setPosition(newPosition);
    }
    _updateLightBB = true;

    _dirty[PROPERTY_TYPE_PHYSICAL] = true;
}

void Light::setDiffuseColor(const vec3<F32>& newDiffuseColor) {
    _properties._diffuse.set(newDiffuseColor, _properties._diffuse.a);
    _dirty[PROPERTY_TYPE_VISUAL] = true;
}

void Light::setRange(F32 range) {
    _properties._attenuation.w = range;
    _dirty[PROPERTY_TYPE_PHYSICAL] = true;
}

void Light::setDirection(const vec3<F32>& newDirection) {
    // Togglable lights can't be moved.
    if (_mode == LIGHT_MODE_TOGGLABLE) {
        Console::errorfn(Locale::get("WARNING_ILLEGAL_PROPERTY"), getGUID(),
                         "Light_Togglable", "LIGHT_DIRECTION");
        return;
    }
    vec3<F32> newDirectionNormalized(newDirection);
    newDirectionNormalized.normalize();

    if (_type == LIGHT_TYPE_SPOT) {
        _properties._direction.xyz(newDirectionNormalized);
    } else {
        _properties._position.xyz(newDirectionNormalized);
    }

    _updateLightBB = true;
    _dirty[PROPERTY_TYPE_PHYSICAL] = true;
}

void Light::sceneUpdate(const U64 deltaTime, SceneGraphNode& sgn,
                        SceneState& sceneState) {
    if (_type == LIGHT_TYPE_DIRECTIONAL) {
        return;
    }

    // Check if range changed
    if (FLOAT_COMPARE(getRange(), sgn.getBoundingBoxConst().getMax().x)) {
        return;
    }

    sgn.getBoundingBox().setComputed(false);

    _updateLightBB = true;

    SceneNode::sceneUpdate(deltaTime, sgn, sceneState);
}

bool Light::computeBoundingBox(SceneGraphNode& sgn) {
    if (_type == LIGHT_TYPE_DIRECTIONAL) {
        vec3<F32> directionalLightPosition =
            _properties._position.xyz() *
            Config::Lighting::DIRECTIONAL_LIGHT_DISTANCE * -1.0f;
        sgn.getBoundingBox().set(directionalLightPosition - vec3<F32>(10),
                                 directionalLightPosition + vec3<F32>(10));
    } else {
        sgn.getBoundingBox().set(vec3<F32>(-getRange()),
                                 vec3<F32>(getRange()));
    }

    _updateLightBB = true;

    return SceneNode::computeBoundingBox(sgn);
}

bool Light::isInView(const SceneRenderState& sceneRenderState,
                     SceneGraphNode& sgn, const bool distanceCheck) {
    return ((_impostorSGN != nullptr) && _drawImpostor);
}

void Light::render(SceneGraphNode& sgn,
                   const SceneRenderState& sceneRenderState,
                   const RenderStage& currentRenderStage) {
    // The isInView call should stop impostor rendering if needed
    if (!_impostor) {
        _impostor =
            CreateResource<Impostor>(ResourceDescriptor(_name + "_impostor"));
        _impostor->setRadius(_properties._attenuation.w);
        _impostor->renderState().setDrawState(true);
        _lightSGN->addNode(_impostor).setActive(true);
    }
    SceneGraphNode* impostorSGN = std::begin(_lightSGN->getChildren())->second;
    Material* const impostorMaterialInst =
        impostorSGN->getComponent<RenderingComponent>()->getMaterialInstance();
    impostorMaterialInst->setDiffuse(getDiffuseColor());
    impostorMaterialInst->setAmbient(getDiffuseColor());

    // Updating impostor range is expensive, so check if we need to
    if (!FLOAT_COMPARE(getRange(), _impostor->getRadius())) {
        _impostor->setRadius(getRange());
    }
}

void Light::addShadowMapInfo(ShadowMapInfo* shadowMapInfo) {
    MemoryManager::SAFE_UPDATE(_shadowMapInfo, shadowMapInfo);
}

bool Light::removeShadowMapInfo() {
    MemoryManager::DELETE(_shadowMapInfo);
    return true;
}
void Light::updateResolution(I32 newWidth, I32 newHeight) {
    ShadowMap* sm = _shadowMapInfo->getShadowMap();

    if (!sm) {
        return;
    }

    sm->updateResolution(newWidth, newHeight);
}

void Light::generateShadowMaps(SceneRenderState& sceneRenderState) {
    ShadowMap* sm =
        _shadowMapInfo->getOrCreateShadowMap(sceneRenderState, _shadowCamera);

    if (!sm) {
        return;
    }

    sm->render(sceneRenderState, _callback);
    sm->postRender();

    _dirty[PROPERTY_TYPE_SHADOW] = true;
}

void Light::setLightMode(const LightMode& mode) { _mode = mode; }
};

Commits for Divide-Framework/trunk/Source Code/Rendering/Lighting/Light.cpp

Diff revisions: vs.
Revision Author Commited Message
365 Diff Diff IonutCava picture IonutCava Sun 08 Mar, 2015 21:26:10 +0000

[Ionut]
- More enum-related type safety changes

364 Diff Diff IonutCava picture IonutCava Fri 06 Mar, 2015 16:55:21 +0000

[Ionut]
- Part 1 of type safe enum replacement (does not compile)

354 Diff Diff IonutCava picture IonutCava Fri 20 Feb, 2015 17:17:37 +0000

[Ionut]
- Replaced GLEW with glbinding (requires a CEGUI recompile. It currently crashes)
- Replaced most “SceneGraphNode* const sgn” expressions with "SceneGraphNode& sgn"
- Replaced some pointers to kernel to references to kernel
- Fixed compilation issues with EASTL
- Removed NPOT -> POT texture conversion code. Modern GPUs handle NPOT textures perfectly fine

350 Diff Diff IonutCava picture IonutCava Tue 10 Feb, 2015 16:25:39 +0000

[Ionut]
- Simplified some Util-namespace level functions by using STL algorithms where possible
- Formatted the entire codebase using clang-format with the following style: "{BasedOnStyle: Google, IndentWidth: 4}"
- Attempted to make ProfileTimer thread-safe (temp fix until std::chrono works properly in Visual Studio)

347 Diff Diff IonutCava picture IonutCava Thu 05 Feb, 2015 13:39:09 +0000

[Ionut]
- Changed all begin/end calls on containers to non-member versions
- Replaced all useless “auto” keywords from code. (“auto” only makes sense for return values for delegate functions)
- Replaced useless code in Util::Mat4::Multiply with something that makes sense

343 Diff Diff IonutCava picture IonutCava Thu 15 Jan, 2015 17:30:12 +0000

[Ionut]
- Refactored 2 singletons into static classes (Console and TerrainLoader)
- Changed some C-casts to static_casts
- Updated copyright notices

337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials