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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "Headers/MainScene.h"

#include "Core/Headers/ParamHandler.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/SceneManager.h"
#include "Environment/Water/Headers/Water.h"
#include "Geometry/Material/Headers/Material.h"
#include "Environment/Terrain/Headers/Terrain.h"
#include "Rendering/RenderPass/Headers/RenderQueue.h"
#include "Environment/Terrain/Headers/TerrainDescriptor.h"

namespace Divide {

REGISTER_SCENE(MainScene);

void MainScene::updateLights(){
    if (!_updateLights)
        return;

    _sun_cosy = cosf(_sunAngle.y);
    _sunColor = lerp(vec4<F32>(1.0f, 0.5f, 0.0f, 1.0f),
                     vec4<F32>(1.0f, 1.0f, 0.8f, 1.0f),
                     0.25f + _sun_cosy * 0.75f);

    _sun->setDirection(_sunvector);
	_sun->setDiffuseColor(_sunColor);
	_currentSky->getNode<Sky>()->setSunProperties(_sunvector, _sunColor);
    for(SceneGraphNode* const ter : _visibleTerrains){
        ter->getComponent<RenderingComponent>()->getMaterialInstance()->setAmbient(_sunColor);
    }

    _updateLights = false;
    return;
}

void MainScene::processInput(const U64 deltaTime){
    if(state()._cameraUpdated){
        Camera& cam = renderState().getCamera();
        const vec3<F32>& eyePos = cam.getEye();
        const vec3<F32>& euler  = cam.getEuler();
        if(!_freeflyCamera){
            F32 terrainHeight = 0.0f;
            vec3<F32> eyePosition = cam.getEye();
            for(SceneGraphNode* const terrainNode : _visibleTerrains){
                Terrain* ter = terrainNode->getNode<Terrain>();
                assert(ter != nullptr);
                CLAMP<F32>(eyePosition.x, ter->getDimensions().width  * 0.5 * -1.0f, ter->getDimensions().width  * 0.5f);
                CLAMP<F32>(eyePosition.z, ter->getDimensions().height * 0.5 * -1.0f, ter->getDimensions().height * 0.5f);
               
                terrainHeight = ter->getPositionFromGlobal(eyePosition.x, eyePosition.z).y;
                if(!IS_ZERO(terrainHeight)){
                    eyePosition.y = terrainHeight + 1.85f;
                    cam.setEye(eyePosition);
                    break;
                }
            }
            _GUI->modifyText("camPosition","[ X: %5.2f | Y: %5.2f | Z: %5.2f ] [Pitch: %5.2f | Yaw: %5.2f] [TerHght: %5.2f ]",
                              eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw,
                              terrainHeight);
        }else{
            _GUI->modifyText("camPosition","[ X: %5.2f | Y: %5.2f | Z: %5.2f ] [Pitch: %5.2f | Yaw: %5.2f]",
                              eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw);
        }
    }
}

void MainScene::processGUI(const U64 deltaTime){
    D32 FpsDisplay = getSecToMs(0.5);
    D32 TimeDisplay = getSecToMs(1.0);

    if (_guiTimers[0] >= FpsDisplay){
        _GUI->modifyText("fpsDisplay", "FPS: %3.0f. FrameTime: %3.1f", 
                         ApplicationTimer::getInstance().getFps(), ApplicationTimer::getInstance().getFrameTime());
        _GUI->modifyText("underwater", "Underwater [ %s ] | WaterLevel [%f] ]",
                         state()._cameraUnderwater ? "true" : "false", state().getWaterLevel());
        _GUI->modifyText("RenderBinCount", "Number of items in Render Bin: %d", GFX_RENDER_BIN_SIZE);
        _guiTimers[0] = 0.0;
    }

    if (_guiTimers[1] >= TimeDisplay){
        _GUI->modifyText("timeDisplay", "Elapsed time: %5.0f", GETTIME());
        _guiTimers[1] = 0.0;
    }

    Scene::processGUI(deltaTime);
}

void MainScene::processTasks(const U64 deltaTime){
    updateLights();
    D32 SunDisplay = getSecToMs(1.50);
    if (_taskTimers[0] >= SunDisplay){
        _sunAngle.y += 0.0005f;
        _sunvector = vec4<F32>(	-cosf(_sunAngle.x) * sinf(_sunAngle.y),
                                -cosf(_sunAngle.y),
                                -sinf(_sunAngle.x) * sinf(_sunAngle.y),
                                0.0f );
        _taskTimers[0] = 0.0;
        _updateLights = true;
    }

    Scene::processTasks(deltaTime);
}

bool MainScene::load(const stringImpl& name, CameraManager* const cameraMgr, GUI* const gui){
    //Load scene resources
    bool loadState = SCENE_LOAD(name,cameraMgr,gui,true,true);
    renderState().getCamera().setMoveSpeedFactor(10.0f);

	_sun = addLight(LIGHT_TYPE_DIRECTIONAL)->getNode<DirectionalLight>();
    _sun->csmSplitCount(3); // 3 splits
    _sun->csmSplitLogFactor(0.965f);
    _sun->csmNearClipOffset(25.0f);
	_currentSky = addSky(CreateResource<Sky>(ResourceDescriptor("Default Sky")));

    for(U8 i = 0; i < _terrainInfoArray.size(); i++){
        SceneGraphNode* terrainNode = _sceneGraph->findNode(_terrainInfoArray[i]->getVariable("terrainName"));
        if(terrainNode){ //We might have an unloaded terrain in the Array, and thus, not present in the graph
            Terrain* tempTerrain = terrainNode->getNode<Terrain>();
            if(terrainNode->isActive()){
                tempTerrain->toggleBoundingBoxes();
                _visibleTerrains.push_back(terrainNode);
             }
        }else{
            ERROR_FN(Locale::get("ERROR_MISSING_TERRAIN"), _terrainInfoArray[i]->getVariable("terrainName"));
        }
    }

    ResourceDescriptor infiniteWater("waterEntity");
    _water = CreateResource<WaterPlane>(infiniteWater);
    _water->setParams(50.0f, vec2<F32>(10.0f, 10.0f), vec2<F32>(0.1f,0.1f),0.34f);
    _waterGraphNode = _sceneGraph->getRoot()->addNode(_water);
    _waterGraphNode->useDefaultTransform(false);
    _waterGraphNode->usageContext(SceneGraphNode::NODE_STATIC);
    _waterGraphNode->getComponent<NavigationComponent>()->navigationContext(NavigationComponent::NODE_IGNORE);
    //Render the scene for water reflection FB generation
    _water->setReflectionCallback(DELEGATE_BIND(&SceneManager::renderVisibleNodes, &SceneManager::getInstance()));
    _water->setRefractionCallback(DELEGATE_BIND(&SceneManager::renderVisibleNodes, &SceneManager::getInstance()));

    return loadState;
}

bool MainScene::unload(){
    SFX_DEVICE.stopMusic();
    SFX_DEVICE.stopAllSounds();
    RemoveResource(_beep);
    return Scene::unload();
}

void MainScene::test(cdiggins::any a, CallbackParam b){
    static bool switchAB = false;
    vec3<F32> pos;
    SceneGraphNode* boxNode = _sceneGraph->findNode("box");
    Object3D* box = nullptr;
    if(boxNode) box = boxNode->getNode<Object3D>();
    if(box) pos = boxNode->getComponent<PhysicsComponent>()->getPosition();

    if(!switchAB){
        if(pos.x < 300 && pos.z == 0)		   pos.x++;
        if(pos.x == 300)
        {
            if(pos.y < 800 && pos.z == 0)      pos.y++;
            if(pos.y == 800)
            {
                if(pos.z > -500)   pos.z--;
                if(pos.z == -500)  switchAB = true;
            }
        }
    } else {
        if(pos.x > -300 && pos.z ==  -500)      pos.x--;
        if(pos.x == -300)
        {
            if(pos.y > 100 && pos.z == -500)    pos.y--;
            if(pos.y == 100) {
                if(pos.z < 0)    pos.z++;
                if(pos.z == 0)   switchAB = false;
            }
        }
    }
    if(box)	boxNode->getComponent<PhysicsComponent>()->setPosition(pos);
}

bool MainScene::loadResources(bool continueOnErrors){

    _GUI->addText("fpsDisplay",               //Unique ID
                  vec2<I32>(60,60),           //Position
                  Font::DIVIDE_DEFAULT,       //Font
                  vec3<F32>(0.0f,0.2f, 1.0f), //Color
                  "FPS: %s",0);               //Text and arguments

    _GUI->addText("timeDisplay",
                  vec2<I32>(60,80),
                  Font::DIVIDE_DEFAULT,
                  vec3<F32>(0.6f,0.2f,0.2f),
                  "Elapsed time: %5.0f",GETTIME());
    _GUI->addText("underwater",
                  vec2<I32>(60,115),
                  Font::DIVIDE_DEFAULT,
                  vec3<F32>(0.2f,0.8f,0.2f),
                  "Underwater [ %s ] | WaterLevel [%f] ]","false", 0);
    _GUI->addText("RenderBinCount",
                  vec2<I32>(60,135),
                  Font::BATANG,
                  vec3<F32>(0.6f,0.2f,0.2f),
                  "Number of items in Render Bin: %d",0);
    _taskTimers.push_back(0.0); //Sun
    _guiTimers.push_back(0.0); //Fps
    _guiTimers.push_back(0.0); //Time

    _sunAngle = vec2<F32>(0.0f, RADIANS(45.0f));
    _sunvector = vec4<F32>(	-cosf(_sunAngle.x) * sinf(_sunAngle.y),
                            -cosf(_sunAngle.y),
                            -sinf(_sunAngle.x) * sinf(_sunAngle.y),
                            0.0f );

    Kernel* kernel = Application::getInstance().getKernel();

    Task_ptr boxMove(kernel->AddTask(getMsToUs(30), 0, DELEGATE_BIND(&MainScene::test, this, stringImpl("test"), TYPE_STRING)));
	registerTask(boxMove);
	boxMove->startTask();

    ResourceDescriptor backgroundMusic("background music");
	backgroundMusic.setResourceLocation(_paramHandler.getParam<stringImpl>("assetsLocation") + "/music/background_music.ogg");
    backgroundMusic.setFlag(true);

    ResourceDescriptor beepSound("beep sound");
	beepSound.setResourceLocation(_paramHandler.getParam<stringImpl>("assetsLocation") + "/sounds/beep.wav");
    beepSound.setFlag(false);
    state()._backgroundMusic["generalTheme"] = CreateResource<AudioDescriptor>(backgroundMusic);
    _beep = CreateResource<AudioDescriptor>(beepSound);

    const vec3<F32>& eyePos = renderState().getCamera().getEye();
    const vec3<F32>& euler = renderState().getCamera().getEuler();
    _GUI->addText("camPosition",  vec2<I32>(60,100),
                  Font::DIVIDE_DEFAULT,
                  vec3<F32>(0.2f,0.8f,0.2f),
                  "Position [ X: %5.0f | Y: %5.0f | Z: %5.0f ] [Pitch: %5.2f | Yaw: %5.2f]",
                  eyePos.x, eyePos.y, eyePos.z, euler.pitch, euler.yaw);

    return true;
}

bool _playMusic = false;
bool MainScene::onKeyUp(const Input::KeyEvent& key){
    switch(key._key)	{
        default: break;
        case Input::KeyCode::KC_X:	SFX_DEVICE.playSound(_beep); break;
        case Input::KeyCode::KC_M:{
            _playMusic = !_playMusic;
            if(_playMusic){
                SFX_DEVICE.playMusic(state()._backgroundMusic["generalTheme"]);
            }else{
                SFX_DEVICE.stopMusic();
            }
            }break;
        case Input::KeyCode::KC_R:{
            _water->togglePreviewReflection();
            }break;
        case Input::KeyCode::KC_F:{
            _freeflyCamera = !_freeflyCamera;
            renderState().getCamera().setMoveSpeedFactor(_freeflyCamera ? 20.0f : 10.0f);
            }break;
        case Input::KeyCode::KC_T:
            for(SceneGraphNode* const ter : _visibleTerrains){
                ter->getNode<Terrain>()->toggleBoundingBoxes();
            }
            break;
    }
    return Scene::onKeyUp(key);
}

bool MainScene::mouseMoved(const Input::MouseEvent& key){
    return Scene::mouseMoved(key);
}

bool MainScene::mouseButtonReleased(const Input::MouseEvent& key, Input::MouseButton button){
    return Scene::mouseButtonReleased(key,button);
}

};

Commits for Divide-Framework/trunk/Source Code/Scenes/MainScene/MainScene.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)

320 Diff Diff IonutCava picture IonutCava Sun 31 Aug, 2014 21:45:33 +0000

[Ionut]
- More AI work (entity captures flag and returns it to base)

318 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)