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
#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(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(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* const 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 ) {
        ERROR_FN( 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 ) {
        ERROR_FN( 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* const 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* const 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* const sgn, const bool distanceCheck ) {
    return ((_impostorSGN != nullptr) && _drawImpostor);
}

void Light::render(SceneGraphNode* const 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 = _lightSGN->getChildren().begin()->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
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 Diff Diff 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

327 Diff Diff IonutCava picture IonutCava Thu 02 Oct, 2014 12:32:15 +0000

[Ionut]
- Removed specular color from lights (specular color should be identical to light’s diffuse color anyway)
- Doublesided materials use their own shader defines now
- Fixed a bug in the particle shader related to screen dimensions never being passed
- Disabled material loading from XML for Primitive type geometry

326 Diff Diff IonutCava picture IonutCava Tue 30 Sep, 2014 21:11:32 +0000

[Ionut]
- Fixed more memory leaks
- Simplified Task interface and timing System
- Improved compatibility between Boost, STL and EASTL with random combinations of vectors, strings and maps
- Simplified Light class by removing the “slot” member
- Random optimizations

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

308 IonutCava picture IonutCava Sat 16 Aug, 2014 19:49:31 +0000

[Ionut]
- Moved Transform from SceneGraphNode to PhysicsComponent and modified BoundingBox update system based on transform update
- Added PhysicsAsset to PhysicsComponent for interfacing with PshysicsAPIs (e.g. PhysXActor inherits PhysicsAsset)
- PhysicsAsset keeps track of the parent PhysicsComponent to have direct access to a node’s transform
- Rewrote GUIConsole to only print items when visible, and use a single circular buffer for entries.
- Updated GUI input system to match the interface that both Kernel and SceneManager share (should probably abstract all the input methods in a class and have Kernel, SceneManager, Scene and GUI inherit from that. In next commit.)
- Updated CEGUI to probably match the OIS states(mouse, keyboard, etc) and properly inject time inputs.