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

#include "Graphs/Headers/SceneGraph.h"
#include "Core/Headers/ParamHandler.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/SceneManager.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 stringImpl& name, const F32 range, const LightType& type, LightPool& parentPool)
    : SceneNode(name, SceneNodeType::TYPE_LIGHT),
      _parentPool(parentPool),
      _type(type),
      _rangeChanged(true),
      _drawImpostor(false),
      _impostor(nullptr),
      _lightSGN(nullptr),
      _shadowMapInfo(nullptr),
      _shadowCamera(nullptr),
      _castsShadows(false),
      _spotPropertiesChanged(false),
      _spotCosOuterConeAngle(0.0f)
{
    for (U8 i = 0; i < Config::Lighting::MAX_SPLITS_PER_LIGHT; ++i) {
        _shadowProperties._lightVP[i].identity();
        _shadowProperties._floatValues[i].set(std::numeric_limits<F32>::max());
    }
    
    setDiffuseColour(DefaultColours::WHITE());
    setRange(1.0f);

    _renderState.addToDrawExclusionMask(RenderStage::SHADOW);

    _enabled = true;
}

Light::~Light()
{
}

bool Light::load() {
    _shadowCamera =
        Application::instance()
        .kernel()
        .getCameraMgr()
        .createCamera(getName() + "_shadowCamera", Camera::CameraType::FREE_FLY);

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

    if (_parentPool.addLight(*this)) {
        return Resource::load();
    }

    return false;
}

bool Light::unload() {
    _parentPool.removeLight(getGUID(), getLightType());

    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;
    _lightSGN->get<PhysicsComponent>()->setPosition(_positionAndRange.xyz());
    _lightSGN->get<BoundsComponent>()->lockBBTransforms(true);
    SceneNode::postLoad(sgn);
}

void Light::setDiffuseColour(const vec3<U8>& newDiffuseColour) {
    _colour.rgb(newDiffuseColour);
}

void Light::setRange(F32 range) {
    _positionAndRange.w = range;
    _rangeChanged = true;
}

void Light::setSpotAngle(F32 newAngle) {
    _spotProperties.w = newAngle;
    _spotPropertiesChanged = true;
}

void Light::setSpotCosOuterConeAngle(F32 newCosAngle) {
    _spotCosOuterConeAngle = newCosAngle;
}

void Light::sceneUpdate(const U64 deltaTime, SceneGraphNode& sgn, SceneState& sceneState) {
    vec3<F32> dir(_lightSGN->get<PhysicsComponent>()->getOrientation() * WORLD_Z_NEG_AXIS);
    dir.normalize();
    _spotProperties.xyz(dir);
    _positionAndRange.xyz(_lightSGN->get<PhysicsComponent>()->getPosition());
    setFlag(UpdateFlag::BOUNDS_CHANGED);

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

void Light::updateBoundsInternal(SceneGraphNode& sgn) {
    if (_type == LightType::DIRECTIONAL) {
        vec3<F32> directionalLightPosition =
            _positionAndRange.xyz() * 
            (to_const_float(Config::Lighting::DIRECTIONAL_LIGHT_DISTANCE) * -1.0f);
        _boundingBox.set(directionalLightPosition - 10.0f,
                         directionalLightPosition + 10.0f);
    } else {
        _boundingBox.set(vec3<F32>(-getRange()), vec3<F32>(getRange()));
        _boundingBox.multiply(0.5f);
    }

    if (_type == LightType::SPOT) {
        _boundingBox.multiply(0.5f);
    }
    SceneNode::updateBoundsInternal(sgn);
}

bool Light::onRender(SceneGraphNode& sgn, RenderStage currentStage) {
    if (!_drawImpostor) {
        return true;
    }

    if (!_impostor) {
        static const U32 normalMask = to_const_uint(SGNComponent::ComponentType::PHYSICS) |
                                      to_const_uint(SGNComponent::ComponentType::BOUNDS) |
                                      to_const_uint(SGNComponent::ComponentType::RENDERING);

        _impostor = CreateResource<ImpostorSphere>(ResourceDescriptor(_name + "_impostor"));
        _impostor->setRadius(_positionAndRange.w);
        _impostor->renderState().setDrawState(true);
        _impostorSGN = _lightSGN->addNode(_impostor, normalMask, PhysicsGroup::GROUP_IGNORE);
        _impostorSGN.lock()->setActive(true);
    }

    _impostorSGN.lock()->get<RenderingComponent>()->getMaterialInstance()->setDiffuse(getDiffuseColour());

    updateImpostor();

    return true;
}

void Light::updateImpostor() {
    if (_type == LightType::DIRECTIONAL) {
        return;
    }
    // Updating impostor range is expensive, so check if we need to
    if (_rangeChanged) {
        F32 range = getRange();
        if (_type == LightType::SPOT) {
            range *= 0.5f;
            // Spot light's bounding sphere extends from the middle of the light's range outwards,
            // touching the light's position on one end and the cone at the other
            // so we need to offest the impostor's position a bit
            PhysicsComponent* pComp = _impostorSGN.lock()->get<PhysicsComponent>();
            pComp->setPosition(getSpotDirection() * range);
        }
        _impostor->setRadius(range);
        _rangeChanged = false;
    }
}

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

bool Light::removeShadowMapInfo() {
    MemoryManager::DELETE(_shadowMapInfo);
    return true;
}

void Light::validateOrCreateShadowMaps(SceneRenderState& sceneRenderState) {
    _shadowMapInfo->createShadowMap(sceneRenderState, shadowCamera());
}

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

    DIVIDE_ASSERT(sm != nullptr,
                  "Light::generateShadowMaps error: Shadow casting light "
                  "with no shadow map found!");

    _shadowProperties._arrayOffset.set(sm->getArrayOffset());
    sm->render(sceneRenderState);

}

};

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

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

721 Diff Diff IonutCava picture IonutCava Wed 08 Jun, 2016 15:47:49 +0000

[IonutCava]
- Spelling: change all references from our code of “color” to the PROPER British version of “colour” because it sounds 100x better

709 Diff Diff IonutCava picture IonutCava Thu 19 May, 2016 16:21:46 +0000

[IonutCava]
- Massive rewrite of the resource system:
— ResourceCache now uses a map of weak pointers and passes shared pointers to the call point of CreateResource
— RemoveResource is no longer needed, but resource based smart pointers use a custom deleter that calls unload()
— Keep a shared_ptr of the resource in the entity that needs it and pass shared_ptr references from getter methods
-— All of this removed the need for calls to RemoveResource, REGISTER_TRACKED_DEPENDENCY and UNREGISTER_TRACKED_DEPENDENCY reducing the number of mutex locks and atomic exchanges
- Singleton cleanup:
— Removed ShaderManager singleton and merged it’s data and responsibilities in static methods in both ShaderProgram and Shader classes.
Explanation: Due to the complex interdependency chains in the system, copying, updating, tracking the cache in a thread safe way became a lot more slower than a straight forward smart pointer based implementation. (e.g. scene graph nodes have 3d objects and material instances, 3d objects have materials, materials have textures and shaders, etc)

706 Diff Diff IonutCava picture IonutCava Fri 13 May, 2016 16:29:40 +0000

[IonutCava]
- Renamed LightManager to LightPool and changed it from a Singleton to a scene specific pointer
- More scene load / unload updates (changing scenes now works properly!)
- Allowed GUI interface to hold per-scene elements and only render relevant ones
- Reduced dependencies between nodes, scenemanager, scene, loaders and kernel
- Removed Reflector base class as it was useless and integrate Water class with the Reflection system already in place
- Extended RenderingComponent’s reflection system to allow refraction as well

705 Diff Diff IonutCava picture IonutCava Thu 12 May, 2016 20:27:20 +0000

[IonutCava]
- Decouple asset loading from the active scene
— completely remove GET_ACTIVE_SCENE() method

696 Diff Diff IonutCava picture IonutCava Mon 09 May, 2016 16:14:09 +0000

[IonutCava]
- More physics system preparation:
— Define specific px groups for nodes: static, kinematic, dynamic, ragdoll, etc

692 Diff Diff IonutCava picture IonutCava Wed 27 Apr, 2016 16:24:26 +0000

[IonutCava]
- Resource system cleanup:
— Resource name and resource file location (if any) is now passed only via explicit constructors! (ResourceDescriptors already contain this data, so why use setters?)
- Fix a bug with ortho projection matrix calculation (set m[3][3] to 1. this was skipped when the identity() call was changed to zero() in the ortho() function)
— This also fixed text rendering

670 Diff Diff IonutCava picture IonutCava Wed 06 Apr, 2016 16:21:35 +0000

[IonutCava]
- getInstance() renamed to instance() for simplicity (should really go away altogether, but that’s a different issue)
- some particle emitter threading updates

669 Diff Diff IonutCava picture IonutCava Mon 04 Apr, 2016 16:15:35 +0000

[IonutCava]
- Move Task creation calls to TaskPool.h
- Allow tasks to use any pool, but default behaviour is to use the Kernel’s main task pool
- Change image transparency check to use the task pool instead of OpenMP
— Perfect place for future parallel_for implementation
- Warning fixes
- Some method renaming for improved readability

651 IonutCava picture IonutCava Wed 24 Feb, 2016 16:54:46 +0000

[IonutCava]
- Improved component access speed
- Added update flags to SceneNodes
— Currently used to flag bounding box update
- SceneNode bounds are only updated in the sceneUpdate call. BOUNDS_CHANGED flag can be set at any time.
- Changed SceneNode->SceneGraphNode BB update system to use the new SceneNode flag system
- Added initial code for compressed texture support (formats, containers, etc)
- Added STB libraries for future use