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

#include "Scenes/Headers/Scene.h"
#include "Graphs/Headers/SceneNode.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/SceneManager.h"
#include "Geometry/Material/Headers/Material.h"
#include "Geometry/Shapes/Headers/Predefined/Box3D.h"
#include "Geometry/Shapes/Headers/Predefined/Quad3D.h"

using namespace physx;

namespace Divide {

enum PhysXSceneInterfaceState {
    STATE_LOADING_ACTORS
};

bool PhysXSceneInterface::init(){
    physx::PxPhysics* gPhysicsSDK = PhysX::getInstance().getSDK();
    //Create the scene
    if(!gPhysicsSDK){
        ERROR_FN(Locale::get("ERROR_PHYSX_SDK"));
        return false;
    }

    PxSceneDesc sceneDesc(gPhysicsSDK->getTolerancesScale());
    sceneDesc.gravity = PxVec3(DEFAULT_GRAVITY.x, DEFAULT_GRAVITY.y, DEFAULT_GRAVITY.z);
    if(!sceneDesc.cpuDispatcher) {
        _cpuDispatcher = PxDefaultCpuDispatcherCreate(3);
        if(!_cpuDispatcher){
            ERROR_FN(Locale::get("ERROR_PHYSX_INTERFACE_CPU_DISPATCH"));
        }
         sceneDesc.cpuDispatcher = _cpuDispatcher;
    }

    if(!sceneDesc.filterShader)
        sceneDesc.filterShader	= PxDefaultSimulationFilterShader;

    sceneDesc.flags |= PxSceneFlag::eENABLE_ACTIVETRANSFORMS;

    _gScene = gPhysicsSDK->createScene(sceneDesc);
    if (!_gScene){
        ERROR_FN(Locale::get("ERROR_PHYSX_INTERFACE_CREATE_SCENE"));
        return false;
    }

    _gScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0);
    _gScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);
    PxMaterial* planeMaterial = gPhysicsSDK->createMaterial(0.9f, 0.1f, 1.0f);
    _materials.push_back(planeMaterial);
    PxMaterial* cubeMaterial = gPhysicsSDK->createMaterial(0.1f, 0.4f, 1.0f);
    _materials.push_back(cubeMaterial);
    PxMaterial* sphereMaterial = gPhysicsSDK->createMaterial(0.6f, 0.1f, 0.6f);
    _materials.push_back(sphereMaterial);
    _rigidCount = 0;
    return true;
}

bool PhysXSceneInterface::exit(){
    D_PRINT_FN(Locale::get("STOP_PHYSX_SCENE_INTERFACE"));
    return true;
}

void PhysXSceneInterface::idle(){
    if(_rigidCount == 0)
        return;

    U32 tempCounter = _rigidCount;

    WriteLock w_lock(_queueLock);
    for(U32 i = 0; i < tempCounter; ++i){
        _sceneRigidActors.push_back(_sceneRigidQueue[i]);
    }
    _sceneRigidQueue.clear();
    w_lock.unlock();

    _rigidCount = 0;
}

void PhysXSceneInterface::release(){
    if(!_gScene){
        ERROR_FN(Locale::get("ERROR_PHYSX_CLOSE_INVALID_INTERFACE"));
        return;
    }

    for(RigidMap::iterator it = _sceneRigidActors.begin(); it != _sceneRigidActors.end(); ++it){
        (*it)->_actor->release();
        SAFE_DELETE(*it);
     }

    _sceneRigidActors.clear();

    //if(_cpuDispatcher)
    //   _cpuDispatcher->release();

    _gScene->release();
}

void PhysXSceneInterface::update(const U64 deltaTime){
    if (!_gScene) {
        return;
    }
    SceneGraphNode* newNode = nullptr;
    for(RigidMap::iterator it = _sceneRigidActors.begin(); it != _sceneRigidActors.end(); ++it){
        PhysXActor& crtActor = *(*it);

        if (!crtActor._isInScene) {
            addToScene(crtActor, newNode);
        }
        updateActor(crtActor);
    }
}

namespace {
    PxShape* g_shapes[2048];// = New PxShape*[nShapes];
}
void PhysXSceneInterface::updateActor(PhysXActor& actor){
    STUBBED("ToDo: Add a better synchronization method between SGN's transform and PhysXActor's pose!! -Ionut")
    if(actor.resetTransforms()){

        const vec3<F32>& position = actor.getComponent()->getConstTransform()->getPosition();
        const vec4<F32>& orientation = actor.getComponent()->getConstTransform()->getOrientation().asVec4();

        physx::PxTransform posePxTransform(PxVec3(position.x, position.y, position.z),
                                           PxQuat(orientation.x,orientation.y,orientation.z,orientation.w).getConjugate());
        actor._actor->setGlobalPose(posePxTransform);
        actor.resetTransforms(false);

    }else{

        PxU32 nShapes = actor._actor->getNbShapes();
        assert(nShapes < 2048);
        actor._actor->getShapes(g_shapes, nShapes);

        while (nShapes--) {
            updateShape(g_shapes[nShapes], actor);
        }
    }
}

void PhysXSceneInterface::updateShape(PxShape* const shape, PhysXActor& actor){
    Transform* t = actor.getComponent()->getTransform();

    if(!t || !shape)
        return;

    PxTransform pT = PxShapeExt::getGlobalPose(*shape, *actor._actor);
    PxQuat pQ = pT.q.getConjugate();

    if(actor._type == physx::PxGeometryType::ePLANE)
        std::swap(pQ.x,pQ.z);
       
    t->setRotation(Quaternion<F32>(pQ.x,pQ.y,pQ.z,pQ.w));
    t->setPosition(vec3<F32>(pT.p.x,pT.p.y,pT.p.z));
}

void PhysXSceneInterface::process(const U64 deltaTime){
    if(!_gScene) return;

    _gScene->simulate(static_cast<physx::PxReal>(getUsToMs(deltaTime)));

    while(!_gScene->fetchResults()) idle();
}

void PhysXSceneInterface::addRigidActor(PhysXActor* const actor, bool threaded) {
    if(!actor)
        return;
    if(threaded){
        WriteLock w_lock(_queueLock);
        _sceneRigidQueue.push_back(actor);
        w_lock.unlock();
        _rigidCount++;
    }else{
        _sceneRigidActors.push_back(actor);
    }
}

PhysXActor* PhysXSceneInterface::getOrCreateRigidActor(const std::string& actorName) {
    if(!_gScene)
        return nullptr;

    for(RigidMap::iterator it = _sceneRigidActors.begin(); it != _sceneRigidActors.end(); ++it){
        if((*it)->_actorName.compare(actorName) == 0)
            return (*it);
    }

    PhysXActor* newActor = New  PhysXActor();
    newActor->_actorName = actorName;
    return newActor;
}

void PhysXSceneInterface::addToScene(PhysXActor& actor, SceneGraphNode* outNode){
    STUBBED("ToDo: Only 1 shape per actor for now. Fix This! -Ionut")
    SceneNode* sceneNode = nullptr;

    _gScene->addActor(*(actor._actor));
    U32 nbActors = _gScene->getNbActors(PxActorTypeSelectionFlag::eRIGID_DYNAMIC |
                                        PxActorTypeSelectionFlag::eRIGID_STATIC);
    PxShape** shapes=New PxShape*[actor._actor->getNbShapes()];
    actor._actor->getShapes(shapes, actor._actor->getNbShapes());

    std::string sgnName = "";
    bool shadowState = true;
    switch(actor._type){
        case PxGeometryType::eBOX: {
            ResourceDescriptor boxDescriptor("BoxActor");
            sceneNode = CreateResource<Box3D>(boxDescriptor);

            sgnName = "BoxActor_" + Util::toString(nbActors);

            if(sceneNode->getRefCount() == 1){
                Material* boxMaterial = CreateResource<Material>(ResourceDescriptor("BoxActor_material"));
                boxMaterial->setDiffuse(vec4<F32>(0.0f,0.0f,1.0f,1.0f));
                boxMaterial->setAmbient(vec4<F32>(0.0f,0.0f,1.0f,1.0f));
                boxMaterial->setEmissive(vec4<F32>(0.1f,0.1f,0.1f,1.0f));
                boxMaterial->setShininess(2);
                sceneNode->setMaterial(boxMaterial);
            }
        }
        break;

        case PxGeometryType::ePLANE: {
            sgnName = "PlaneActor";
            if(FindResourceImpl<Quad3D>(sgnName)) {
                outNode = GET_ACTIVE_SCENEGRAPH()->findNode(sgnName);
                PhysicsSceneInterface::addToScene(actor, outNode);
                return;
            }
            ResourceDescriptor planeDescriptor(sgnName);
            planeDescriptor.setFlag(true);

            sceneNode = CreateResource<Quad3D>(planeDescriptor);
            Material* planeMaterial = CreateResource<Material>(ResourceDescriptor(sgnName + "_material"));
            planeMaterial->setDiffuse(vec4<F32>(0.4f,0.1f,0.1f,1.0f));
            planeMaterial->setAmbient(vec4<F32>(0.4f,0.1f,0.1f,1.0f));
            planeMaterial->setEmissive(vec4<F32>(0.3f,0.3f,0.3f,1.0f));
            planeMaterial->setShininess(0);
            shadowState = false;
            sceneNode->setMaterial(planeMaterial);
        }break;
    }

    SAFE_DELETE_ARRAY(shapes);

    if(actor._type != PxGeometryType::eTRIANGLEMESH) {
        if(sceneNode){
            sceneNode->getSceneNodeRenderState().setDrawState(true);
            outNode = _parentScene->addGeometry(sceneNode,sgnName);
            outNode->castsShadows(shadowState);
        }
        if(!outNode){
            ERROR_FN(Locale::get("ERROR_ACTOR_CAST"), sgnName.c_str());
        }else{
            actor._actorName = sgnName;
            actor.getComponent()->getTransform()->setScale(actor._userData);
        }
    }

    actor._isInScene = true;

    PhysicsSceneInterface::addToScene(actor, outNode);
}

};

Commits for Divide-Framework/trunk/Source Code/Dynamics/Physics/PhysX/PhysXSceneInterface.cpp

Diff revisions: vs.
Revision Author Commited Message
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 Diff Diff 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.

307 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 19:45:12 +0000

[Ionut]
- More editor work:
— Per object skeleton rendering, bounding box rendering and shadow map toggling
— Full screen depth preview toggle
— Text entry based transforms

306 Diff Diff IonutCava picture IonutCava Tue 12 Aug, 2014 12:47:31 +0000

[Ionut]
- More World Editor work:
— Move and Scale selected objects
— Disabling object specific controls when nothing is selected
— Toggle PostFX and Fog controls working
— Do not pass input to scene when clicking on any editor controls
— Granularity fields working
— Input fields only allow floating point inputs (via regex check)
- Cleaned up Transform class

259 Diff Diff k1ngp1n picture k1ngp1n Sun 27 Apr, 2014 18:24:52 +0000

[Ionut][[BR]]
- Implemented Exponential Stable Cascaded Shadow Maps for directional lights (still buggy, but basically working) [[BR]]
— CSM settings (resolution, number of splits, etc) are part of the scene render state [[BR]]
- SGN can skip rendering for the current frame if they are not ready (e.g. OnDraw can now return false or materials can fail to bind) [[BR]]
- Frustum class is now using the Plane class [[BR]]
- Removed Math SIMD implementation as the compiler generated code will always be faster and safer [[BR]]
- Added a “STUBBED” macro for outputing to the error stream of important messages (instead of using #pragma message) [[BR]]
- Added “freeze time” logic to pause/resume updates with or without freezing the GUI too [[BR]]
- Added “processGUI” method the every Scene class to separate time-sensitive GUI updates from game logic loop [[BR]]
- GLSL <-> Vertex Buffer bind locations are automatically set for every vertex shader [[BR]]
- Added basic support for compute shaders [[BR]]
- Removed most OS define checks to make porting to a new platform easier [[BR]]
- Texture transparency check per-pixel is now using OpenMP [[BR]]
- Texture samplers can now set a border color for CLAMP_TO_BORDER wrap mode [[BR]]
- Removed “Object” suffix from GFX Classes (pixel buffer, vertex buffer, frame buffer, etc) [[BR]]
- Stop processing scene input when application looses focus [[BR]]
- SceneGraphNode class is now responsible for updating SGNComponents on draw and calling its _node’s OnDraw method [[BR]]
- UBOs can now list all of the active uniforms inside a block with info such as type and offset [[BR]]
- Removed deprecated ‘GLCheck’ macro [[BR]]
- A lot of bug fixes and performance improvements in various places [[BR]]

253 Diff Diff k1ngp1n picture k1ngp1n Wed 09 Apr, 2014 11:30:54 +0000

[Ionut] [[BR]]
- Split SceneAnimator in global and per-instance data (SceneAnimator and AnimatorComponent) respectively [[BR]]
— Each SceneNode, sharing the same skinned mesh can have different animations with different timings now [[BR]]
- Fixed particle systems' billboarding issues [[BR]]
- More cleaning up of the Material class [[BR]]

251 Diff Diff k1ngp1n picture k1ngp1n Wed 02 Apr, 2014 20:38:18 +0000

[Ionut] [[BR]]

  • Divide is now a 64bit statically linked application * [[BR]]

- Updated to a Visual Studio 2013 solution for better C++11 support [[BR]]
— Recompiled all projects with platform toolset v120 [[BR]]
- Recompiled all projects as x64 – MT / MTd (from x86 – MD / MDd) [[BR]]
- Updated PhysX to 3.3 [[BR]]
- Updated GLFW to 3.0.4 [[BR]]

249 Diff Diff k1ngp1n picture k1ngp1n Mon 31 Mar, 2014 20:42:31 +0000

[Ionut] [[BR]]
- Migrated to platform toolset v100 [[BR]]
- Updated boost to 1.55 [[BR]]
- Moved from “NULL” to “nullptr” [[BR]]
- DevIL is now linked dynamically [[BR]]

248 Diff Diff k1ngp1n picture k1ngp1n Mon 24 Feb, 2014 22:35:21 +0000

[Ionut][[BR]]
- Added a GenericVertexData class that can be used to fill VBOs with generic data and upload it to the GPU (and render it, either direct or instanced) [[BR]]
- Added a basic (soft) particle system [[BR]]
- Reworked memory allocation logging [[BR]]
- Improved transparency detection for textures on load [[BR]]

247 k1ngp1n picture k1ngp1n Sun 16 Feb, 2014 16:36:06 +0000

[Ionut] [[BR]]
- Fixed VSM rendering [[BR]]
- Added Z-prePass as a separate render stage [[BR]]
- Added GL error checking and asserts to “Profile” builds [[BR]]
- Fixed bad XML material dump (invalid file name bug) [[BR]]
- Fixed random crashes [[BR]]
- Shadow cast/receive is now a node property, and not a material property [[BR]]
- Fixed an issue with previous RenderPassManager lock mechanism (clearing the queue on frame end to prevent infinite lock) [[BR]]
- Added depth buffer preview (F10 key) [[BR]]