Skip to content

Commit

Permalink
Add the physics playground
Browse files Browse the repository at this point in the history
  • Loading branch information
godlikepanos committed Jun 17, 2018
1 parent a670013 commit 0b2523b
Show file tree
Hide file tree
Showing 39 changed files with 387 additions and 108 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -11,5 +11,7 @@
!*.md
!*.txt
!*.glslp
!*.anki*
!*.blend
!CMakeLists.txt
build*/*
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
@@ -1,3 +1,4 @@
add_definitions(-UANKI_BUILD)
add_subdirectory(simple_scene)
add_subdirectory(sponza)
add_subdirectory(physics_playground)
10 changes: 3 additions & 7 deletions samples/common/Framework.cpp
Expand Up @@ -89,10 +89,6 @@ Error SampleApp::userMainLoop(Bool& quit)
{
// renderer.getDbg().flipFlags(DbgFlag::SPATIAL_COMPONENT);
}
if(in.getKey(KeyCode::F6) == 1)
{
renderer.getDbg().switchDepthTestEnabled();
}

if(in.getKey(KeyCode::UP))
{
Expand Down Expand Up @@ -124,14 +120,14 @@ Error SampleApp::userMainLoop(Bool& quit)
mover->moveLocalX(MOVE_DISTANCE);
}

if(in.getKey(KeyCode::Z))
if(in.getKey(KeyCode::C))
{
mover->moveLocalY(MOVE_DISTANCE);
mover->moveLocalY(-MOVE_DISTANCE);
}

if(in.getKey(KeyCode::SPACE))
{
mover->moveLocalY(-MOVE_DISTANCE);
mover->moveLocalY(MOVE_DISTANCE);
}

if(in.getKey(KeyCode::W))
Expand Down
4 changes: 4 additions & 0 deletions samples/physics_playground/CMakeLists.txt
@@ -0,0 +1,4 @@
add_executable(physics_playground Main.cpp ../common/Framework.cpp)
target_link_libraries(physics_playground anki)

installExecutable(physics_playground)
77 changes: 77 additions & 0 deletions samples/physics_playground/Main.cpp
@@ -0,0 +1,77 @@
// Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
// All rights reserved.
// Code licensed under the BSD License.
// http://www.anki3d.org/LICENSE

#include <cstdio>
#include "../common/Framework.h"

using namespace anki;

class MyApp : public SampleApp
{
public:
Error sampleExtraInit() override
{
ScriptResourcePtr script;
ANKI_CHECK(getResourceManager().loadResource("assets/scene.lua", script));
ANKI_CHECK(getScriptManager().evalString(script->getSource()));
return Error::NONE;
}

Error userMainLoop(Bool& quit) override;
};

Error MyApp::userMainLoop(Bool& quit)
{
ANKI_CHECK(SampleApp::userMainLoop(quit));

if(getInput().getMouseButton(MouseButton::LEFT) == 1)
{
ANKI_LOGI("Firing a monkey");

static U instance = 0;

Transform camTrf = getSceneGraph().getActiveCameraNode().getComponent<MoveComponent>().getWorldTransform();

ModelNode* monkey;
ANKI_CHECK(getSceneGraph().newSceneNode<ModelNode>(
StringAuto(getAllocator()).sprintf("monkey%u", instance++).toCString(),
monkey,
"assets/SuzanneMaterial-material.ankimdl"));
// monkey->getComponent<MoveComponent>().setLocalTransform(camTrf);

BodyNode* body;
ANKI_CHECK(getSceneGraph().newSceneNode<BodyNode>(
StringAuto(getAllocator()).sprintf("bmonkey%u", instance++).toCString(), body, "assets/Suzanne.ankicl"));
body->getComponent<BodyComponent>().setTransform(camTrf);

body->addChild(monkey);
}

return Error::NONE;
}

int main(int argc, char* argv[])
{
Error err = Error::NONE;

MyApp* app = new MyApp;
err = app->init(argc, argv, argv[0]);
if(!err)
{
err = app->mainLoop();
}

if(err)
{
ANKI_LOGE("Error reported. Bye!");
}
else
{
delete app;
ANKI_LOGI("Bye!!");
}

return 0;
}
29 changes: 29 additions & 0 deletions samples/physics_playground/assets/Material-material.ankimtl
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This file is auto generated by ExporterMaterial.cpp -->
<material shaderProgram="shaders/GBufferGeneric.glslp">

<mutators>
<mutator name="DIFFUSE_TEX" value="0"/>
<mutator name="SPECULAR_TEX" value="0"/>
<mutator name="ROUGHNESS_TEX" value="0"/>
<mutator name="METAL_TEX" value="0"/>
<mutator name="NORMAL_TEX" value="0"/>
<mutator name="PARALLAX" value="0"/>
<mutator name="EMISSIVE_TEX" value="0"/>
</mutators>

<inputs>
<input shaderInput="mvp" builtin="MODEL_VIEW_PROJECTION_MATRIX"/>
<input shaderInput="rotationMat" builtin="ROTATION_MATRIX"/>


<input shaderInput="diffColor" value="0.548460 0.640000 0.063144"/>
<input shaderInput="specColor" value="0.500000 0.500000 0.500000"/>
<input shaderInput="roughness" value="0.097847" />
<input shaderInput="metallic" value="0.000000"/>

<input shaderInput="emission" value="0.000000 0.000000 0.000000"/>
<input shaderInput="subsurface" value="0.000000"/>

</inputs>
</material>
29 changes: 29 additions & 0 deletions samples/physics_playground/assets/Material_001-material.ankimtl
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This file is auto generated by ExporterMaterial.cpp -->
<material shaderProgram="shaders/GBufferGeneric.glslp">

<mutators>
<mutator name="DIFFUSE_TEX" value="0"/>
<mutator name="SPECULAR_TEX" value="0"/>
<mutator name="ROUGHNESS_TEX" value="0"/>
<mutator name="METAL_TEX" value="0"/>
<mutator name="NORMAL_TEX" value="0"/>
<mutator name="PARALLAX" value="0"/>
<mutator name="EMISSIVE_TEX" value="0"/>
</mutators>

<inputs>
<input shaderInput="mvp" builtin="MODEL_VIEW_PROJECTION_MATRIX"/>
<input shaderInput="rotationMat" builtin="ROTATION_MATRIX"/>


<input shaderInput="diffColor" value="0.012172 0.367335 0.640000"/>
<input shaderInput="specColor" value="0.500000 0.500000 0.500000"/>
<input shaderInput="roughness" value="0.097847" />
<input shaderInput="metallic" value="0.000000"/>

<input shaderInput="emission" value="0.000000 0.000000 0.000000"/>
<input shaderInput="subsurface" value="0.000000"/>

</inputs>
</material>
5 changes: 5 additions & 0 deletions samples/physics_playground/assets/Suzanne.ankicl
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<collisionShape>
<type>staticMesh</type>
<value>assets/Suzanne.ankimesh</value>
</collisionShape>
Binary file not shown.
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<model>
<modelPatches>
<modelPatch>
<mesh>assets/Suzanne.ankimesh</mesh>
<material>assets/Material-material.ankimtl</material>
</modelPatch>
</modelPatches>
</model>
5 changes: 5 additions & 0 deletions samples/physics_playground/assets/floor.ankicl
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<collisionShape>
<type>staticMesh</type>
<value>assets/floor.ankimesh</value>
</collisionShape>
Binary file added samples/physics_playground/assets/floor.ankimesh
Binary file not shown.
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<model>
<modelPatches>
<modelPatch>
<mesh>assets/floor.ankimesh</mesh>
<material>assets/Material-material.ankimtl</material>
</modelPatch>
</modelPatches>
</model>
Binary file not shown.
5 changes: 5 additions & 0 deletions samples/physics_playground/assets/walls.ankicl
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<collisionShape>
<type>staticMesh</type>
<value>assets/walls.ankimesh</value>
</collisionShape>
Binary file added samples/physics_playground/assets/walls.ankimesh
Binary file not shown.
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<model>
<modelPatches>
<modelPatch>
<mesh>assets/walls.ankimesh</mesh>
<material>assets/Material_001-material.ankimtl</material>
</modelPatch>
</modelPatches>
</model>
1 change: 1 addition & 0 deletions src/anki/Scene.h
Expand Up @@ -20,6 +20,7 @@
#include <anki/scene/OccluderNode.h>
#include <anki/scene/DecalNode.h>
#include <anki/scene/Octree.h>
#include <anki/scene/PhysicsDebugNode.h>

#include <anki/scene/components/MoveComponent.h>
#include <anki/scene/components/RenderComponent.h>
Expand Down
9 changes: 3 additions & 6 deletions src/anki/physics/PhysicsBody.cpp
Expand Up @@ -30,19 +30,16 @@ class PhysicsBody::MotionState : public btMotionState
PhysicsBody::PhysicsBody(PhysicsWorld* world, const PhysicsBodyInitInfo& init)
: PhysicsObject(PhysicsObjectType::BODY, world)
{
if(init.m_static)
{
ANKI_ASSERT(init.m_mass > 0.0f);
}
const Bool dynamic = init.m_mass > 0.0f;

// Create motion state
m_motionState = getAllocator().newInstance<MotionState>();
m_motionState->m_body = this;

// Compute inertia
btCollisionShape* shape = init.m_shape->getBtShape(!init.m_static);
btCollisionShape* shape = init.m_shape->getBtShape(dynamic);
btVector3 localInertia(0, 0, 0);
if(!init.m_static)
if(dynamic)
{
shape->calculateLocalInertia(init.m_mass, localInertia);
}
Expand Down
3 changes: 1 addition & 2 deletions src/anki/physics/PhysicsBody.h
Expand Up @@ -18,11 +18,10 @@ class PhysicsBodyInitInfo
{
public:
PhysicsCollisionShapePtr m_shape;
F32 m_mass = 0.0;
F32 m_mass = 0.0f;
Transform m_startTrf = Transform::getIdentity();
Bool m_kinematic = false;
Bool m_gravity = true;
Bool m_static = false;
};

/// Rigid body.
Expand Down
1 change: 1 addition & 0 deletions src/anki/physics/PhysicsCollisionShape.cpp
Expand Up @@ -54,6 +54,7 @@ PhysicsTriangleSoup::~PhysicsTriangleSoup()
{
getAllocator().deleteInstance(m_staticShape);
getAllocator().deleteInstance(m_shape);
m_shape = nullptr;
getAllocator().deleteInstance(m_mesh);
}

Expand Down
1 change: 1 addition & 0 deletions src/anki/scene/Forward.h
Expand Up @@ -14,6 +14,7 @@ class FrustumComponent;
class InstanceComponent;
class MoveComponent;
class RenderComponent;
class MaterialRenderComponent;
class SpatialComponent;
class DecalComponent;
class PortalComponent;
Expand Down
30 changes: 0 additions & 30 deletions src/anki/scene/Misc.h

This file was deleted.

29 changes: 15 additions & 14 deletions src/anki/scene/ModelNode.cpp
Expand Up @@ -7,7 +7,7 @@
#include <anki/scene/SceneGraph.h>
#include <anki/scene/components/BodyComponent.h>
#include <anki/scene/components/SkinComponent.h>
#include <anki/scene/Misc.h>
#include <anki/scene/components/RenderComponent.h>
#include <anki/resource/ModelResource.h>
#include <anki/resource/ResourceManager.h>
#include <anki/resource/SkeletonResource.h>
Expand All @@ -17,7 +17,7 @@ namespace anki
{

/// Render component implementation.
class ModelPatchNode::MRenderComponent : public RenderComponent
class ModelPatchNode::MRenderComponent : public MaterialRenderComponent
{
public:
const ModelPatchNode& getNode() const
Expand All @@ -26,7 +26,7 @@ class ModelPatchNode::MRenderComponent : public RenderComponent
}

MRenderComponent(ModelPatchNode* node)
: RenderComponent(node, node->m_modelPatch->getMaterial())
: MaterialRenderComponent(node, node->m_modelPatch->getMaterial())
{
}

Expand Down Expand Up @@ -111,11 +111,11 @@ void ModelPatchNode::drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<vo
trfs[i] = Mat4(self2.getParent()->getComponentAt<MoveComponent>(0).getWorldTransform());
}

self.getComponentAt<RenderComponent>(1).allocateAndSetupUniforms(
self.m_modelPatch->getMaterial()->getDescriptorSetIndex(),
ctx,
ConstWeakArray<Mat4>(&trfs[0], userData.getSize()),
*ctx.m_stagingGpuAllocator);
static_cast<const MaterialRenderComponent&>(self.getComponentAt<RenderComponent>(1))
.allocateAndSetupUniforms(self.m_modelPatch->getMaterial()->getDescriptorSetIndex(),
ctx,
ConstWeakArray<Mat4>(&trfs[0], userData.getSize()),
*ctx.m_stagingGpuAllocator);

// Draw
cmdb->drawElements(PrimitiveTopology::TRIANGLES,
Expand Down Expand Up @@ -150,11 +150,11 @@ class ModelNode::MoveFeedbackComponent : public SceneComponent
}
};

class ModelNode::MRenderComponent : public RenderComponent
class ModelNode::MRenderComponent : public MaterialRenderComponent
{
public:
MRenderComponent(ModelNode* node)
: RenderComponent(node, node->m_model->getModelPatches()[0]->getMaterial())
: MaterialRenderComponent(node, node->m_model->getModelPatches()[0]->getMaterial())
{
}

Expand Down Expand Up @@ -300,10 +300,11 @@ void ModelNode::drawCallback(RenderQueueDrawContext& ctx, ConstWeakArray<void*>
trfs[i] = Mat4(self2.getComponent<MoveComponent>().getWorldTransform());
}

self.getComponent<RenderComponent>().allocateAndSetupUniforms(patch->getMaterial()->getDescriptorSetIndex(),
ctx,
ConstWeakArray<Mat4>(&trfs[0], userData.getSize()),
*ctx.m_stagingGpuAllocator);
static_cast<const MaterialRenderComponent&>(self.getComponent<RenderComponent>())
.allocateAndSetupUniforms(patch->getMaterial()->getDescriptorSetIndex(),
ctx,
ConstWeakArray<Mat4>(&trfs[0], userData.getSize()),
*ctx.m_stagingGpuAllocator);

// Bones storage
if(self.m_model->getSkeleton())
Expand Down

0 comments on commit 0b2523b

Please sign in to comment.