]> git.tdb.fi Git - libs/game.git/commitdiff
Add an example application for physics
authorMikko Rasa <tdb@tdb.fi>
Sat, 15 Mar 2025 18:26:13 +0000 (20:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 15 Mar 2025 22:29:37 +0000 (00:29 +0200)
22 files changed:
examples/playground/.gitignore [new file with mode: 0644]
examples/playground/Build [new file with mode: 0644]
examples/playground/data/blue_ball.toy.setup [new file with mode: 0644]
examples/playground/data/checker.glsl [new file with mode: 0644]
examples/playground/data/checker.shader [new file with mode: 0644]
examples/playground/data/checker.tech [new file with mode: 0644]
examples/playground/data/checker_blue.tech [new file with mode: 0644]
examples/playground/data/checker_brown.tech [new file with mode: 0644]
examples/playground/data/checker_red.tech [new file with mode: 0644]
examples/playground/data/edge.fixt.setup [new file with mode: 0644]
examples/playground/data/ground.fixt.setup [new file with mode: 0644]
examples/playground/data/playground.camera.setup [new file with mode: 0644]
examples/playground/data/playground.stage [new file with mode: 0644]
examples/playground/data/red_ball.toy.setup [new file with mode: 0644]
examples/playground/data/sun.light.setup [new file with mode: 0644]
examples/playground/source/fixture.cpp [new file with mode: 0644]
examples/playground/source/fixture.h [new file with mode: 0644]
examples/playground/source/playground.cpp [new file with mode: 0644]
examples/playground/source/playground.h [new file with mode: 0644]
examples/playground/source/setups.mgs [new file with mode: 0644]
examples/playground/source/toy.cpp [new file with mode: 0644]
examples/playground/source/toy.h [new file with mode: 0644]

diff --git a/examples/playground/.gitignore b/examples/playground/.gitignore
new file mode 100644 (file)
index 0000000..5bed91c
--- /dev/null
@@ -0,0 +1,2 @@
+/playground
+/playground.*
diff --git a/examples/playground/Build b/examples/playground/Build
new file mode 100644 (file)
index 0000000..7690a8d
--- /dev/null
@@ -0,0 +1,16 @@
+package "playground"
+{
+       require "mspcore";
+       require "mspdatafile";
+       require "mspmath";
+       require "mspgui";
+       require "mspgl";
+       require "mspvr";
+       require "mspgame";
+       require "sigc++-2.0";
+
+       program "playground"
+       {
+               source "source";
+       };
+};
diff --git a/examples/playground/data/blue_ball.toy.setup b/examples/playground/data/blue_ball.toy.setup
new file mode 100644 (file)
index 0000000..711a904
--- /dev/null
@@ -0,0 +1,8 @@
+shape
+{
+       sphere
+       {
+               radius 0.1;
+       };
+       technique_name "checker_blue.tech";
+};
diff --git a/examples/playground/data/checker.glsl b/examples/playground/data/checker.glsl
new file mode 100644 (file)
index 0000000..676fd8b
--- /dev/null
@@ -0,0 +1,13 @@
+import pbr_material;
+
+#pragma MSP stage(fragment)
+vec4 get_base_color() override
+{
+       ivec3   ipos = ivec3(floor(vertex.xyz*1.73205));
+       float intensity = 1.0;
+       if(((ipos.x^ipos.y^ipos.z)&2)!=0)
+               intensity -= 0.5f;
+       if(((ipos.x^ipos.y^ipos.z)&1)==0)
+               intensity -= 0.1f;
+       return intensity*pbr_material.base_color*pbr_material.tint;
+}
diff --git a/examples/playground/data/checker.shader b/examples/playground/data/checker.shader
new file mode 100644 (file)
index 0000000..22a6060
--- /dev/null
@@ -0,0 +1,4 @@
+module "checker.glsl"
+{
+       specialize "use_shadow_map" true;
+};
diff --git a/examples/playground/data/checker.tech b/examples/playground/data/checker.tech
new file mode 100644 (file)
index 0000000..a122606
--- /dev/null
@@ -0,0 +1,15 @@
+method ""
+{
+       material
+       {
+               type pbr;
+               base_color 0.8 0.8 0.8;
+               roughness 0.9;
+       };
+       shader "checker.shader";
+       receive_shadows true;
+};
+method "shadow"
+{
+       shader "occluder.glsl.shader";
+};
diff --git a/examples/playground/data/checker_blue.tech b/examples/playground/data/checker_blue.tech
new file mode 100644 (file)
index 0000000..3970a23
--- /dev/null
@@ -0,0 +1,15 @@
+method ""
+{
+       material
+       {
+               type pbr;
+               base_color 0.0 0.3 1.0;
+               roughness 0.3;
+       };
+       shader "checker.shader";
+       receive_shadows true;
+};
+method "shadow"
+{
+       shader "occluder.glsl.shader";
+};
diff --git a/examples/playground/data/checker_brown.tech b/examples/playground/data/checker_brown.tech
new file mode 100644 (file)
index 0000000..48042e4
--- /dev/null
@@ -0,0 +1,15 @@
+method ""
+{
+       material
+       {
+               type pbr;
+               base_color 0.5 0.3 0.1;
+               roughness 0.9;
+       };
+       shader "checker.shader";
+       receive_shadows true;
+};
+method "shadow"
+{
+       shader "occluder.glsl.shader";
+};
diff --git a/examples/playground/data/checker_red.tech b/examples/playground/data/checker_red.tech
new file mode 100644 (file)
index 0000000..69da1da
--- /dev/null
@@ -0,0 +1,15 @@
+method ""
+{
+       material
+       {
+               type pbr;
+               base_color 0.8 0.0 0.0;
+               roughness 0.3;
+       };
+       shader "checker.shader";
+       receive_shadows true;
+};
+method "shadow"
+{
+       shader "occluder.glsl.shader";
+};
diff --git a/examples/playground/data/edge.fixt.setup b/examples/playground/data/edge.fixt.setup
new file mode 100644 (file)
index 0000000..99f5568
--- /dev/null
@@ -0,0 +1,8 @@
+shape
+{
+       box
+       {
+               extent 0.1 1.5 0.3;
+       };
+       technique_name "checker_brown.tech";
+};
diff --git a/examples/playground/data/ground.fixt.setup b/examples/playground/data/ground.fixt.setup
new file mode 100644 (file)
index 0000000..04c6746
--- /dev/null
@@ -0,0 +1,8 @@
+shape
+{
+       box
+       {
+               extent 10.0 10.0 1.0;
+       };
+       technique_name "checker.tech";
+};
diff --git a/examples/playground/data/playground.camera.setup b/examples/playground/data/playground.camera.setup
new file mode 100644 (file)
index 0000000..9781038
--- /dev/null
@@ -0,0 +1,2 @@
+near_clip 0.05;
+far_clip 20.0;
diff --git a/examples/playground/data/playground.stage b/examples/playground/data/playground.stage
new file mode 100644 (file)
index 0000000..7fa3645
--- /dev/null
@@ -0,0 +1,39 @@
+fixture "ground.fixt.setup"
+{
+       transform
+       {
+               position 0.0 0.0 -0.5;
+       };
+};
+fixture "edge.fixt.setup"
+{
+       transform
+       {
+               position 0.7 0.0 0.14;
+               euler 0.0 15.0 0.0;
+       };
+};
+fixture "edge.fixt.setup"
+{
+       transform
+       {
+               position 0.0 0.7 0.14;
+               euler 0.0 15.0 90.0;
+       };
+};
+fixture "edge.fixt.setup"
+{
+       transform
+       {
+               position -0.7 0.0 0.14;
+               euler 0.0 15.0 180.0;
+       };
+};
+fixture "edge.fixt.setup"
+{
+       transform
+       {
+               position 0.0 -0.7 0.14;
+               euler 0.0 15.0 270.0;
+       };
+};
diff --git a/examples/playground/data/red_ball.toy.setup b/examples/playground/data/red_ball.toy.setup
new file mode 100644 (file)
index 0000000..5e0931c
--- /dev/null
@@ -0,0 +1,8 @@
+shape
+{
+       sphere
+       {
+               radius 0.06;
+       };
+       technique_name "checker_red.tech";
+};
diff --git a/examples/playground/data/sun.light.setup b/examples/playground/data/sun.light.setup
new file mode 100644 (file)
index 0000000..e66efeb
--- /dev/null
@@ -0,0 +1,3 @@
+type DIRECTIONAL;
+intensity 2.0;
+cast_shadows true;
diff --git a/examples/playground/source/fixture.cpp b/examples/playground/source/fixture.cpp
new file mode 100644 (file)
index 0000000..fea4944
--- /dev/null
@@ -0,0 +1,10 @@
+#include "fixture.h"
+
+using namespace Msp;
+
+Fixture::Fixture(Game::Handle<Game::Entity> p, const Setup &s, const Game::TransformValues &t):
+       Entity(p, t),
+       setup(s),
+       rigid_body(this),
+       shape(this, setup.shape)
+{ }
diff --git a/examples/playground/source/fixture.h b/examples/playground/source/fixture.h
new file mode 100644 (file)
index 0000000..8ac0e29
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef FIXTURE_H_
+#define FIXTURE_H_
+
+#include <msp/game/entity.h>
+#include <msp/game/rigidbody.h>
+#include <msp/game/shape.h>
+#include "playground/setups.h"
+
+class Fixture: public Msp::Game::Entity
+{
+public:
+       using Setup = FixtureSetup;
+
+private:
+       const Setup &setup;
+       Msp::Game::Owned<Msp::Game::RigidBody> rigid_body;
+       Msp::Game::Owned<Msp::Game::Shape> shape;
+
+public:
+       Fixture(Msp::Game::Handle<Msp::Game::Entity>, const Setup &, const Msp::Game::TransformValues &);
+};
+
+#endif
diff --git a/examples/playground/source/playground.cpp b/examples/playground/source/playground.cpp
new file mode 100644 (file)
index 0000000..0db8e32
--- /dev/null
@@ -0,0 +1,47 @@
+#include "playground.h"
+#include <random>
+#include <msp/game/physicssystem.h>
+#include <msp/game/root.h>
+#include <msp/game/stageplan.h>
+#include "fixture.h"
+#include "playground/setups.h"
+#include "toy.h"
+
+using namespace std;
+using namespace Msp;
+
+Playground::Playground(int, char **):
+       stage(director.create_stage("playground")),
+       cam_entity(stage.get_root(), Game::TransformValues({ 0.0f, -1.5f, 1.5f }, Geometry::make_quat(45.0f*Geometry::degrees, { 1.0f, 0.0f, 0.0f }))),
+       camera(cam_entity, resources.get<Game::CameraSetup>("playground.camera.setup")),
+       shadow_setup{ 15.0f },
+       shadow_target(cam_entity, shadow_setup),
+       sun_entity(stage.get_root(), Game::TransformValues({}, Geometry::make_quat(30.0f*Geometry::degrees, { 1.0f, -0.5f, 0.0f }))),
+       sun(sun_entity, resources.get<Game::LightSetup>("sun.light.setup"))
+{
+       Game::StagePlan::register_entity_type<Fixture>("fixture");
+
+       stage.add_system<Game::PhysicsSystem>();
+       stage.apply_plan(resources.get<Game::StagePlan>("playground.stage"));
+
+       vector<ToySetup *> toy_setups = resources.get_all<ToySetup>();
+       std::mt19937 random;
+       std::uniform_int_distribution<unsigned> type_dist(0, toy_setups.size()-1);
+       std::uniform_real_distribution<float> hdist(-0.65f, 0.65f);
+       std::uniform_real_distribution<float> vdist(0.3f, 0.6f);
+       for(unsigned i=0; i<15; ++i)
+       {
+               const ToySetup &setup = *toy_setups[type_dist(random)];
+               LinAl::Vector<float, 3> pos(hdist(random), hdist(random), vdist(random));
+               toys.emplace_back(stage.get_root(), setup, Game::TransformValues(pos, Geometry::Quaternion<float>::one()));
+       }
+
+       director.activate_stage(stage);
+}
+
+
+PlaygroundResources::PlaygroundResources()
+{
+       add_type<FixtureSetup>().suffix(".fixt.setup");
+       add_type<ToySetup>().suffix(".toy.setup");
+}
diff --git a/examples/playground/source/playground.h b/examples/playground/source/playground.h
new file mode 100644 (file)
index 0000000..01d48b3
--- /dev/null
@@ -0,0 +1,37 @@
+#ifndef PLAYGROUND_H_
+#define PLAYGROUND_H_
+
+#include <msp/game/camera.h>
+#include <msp/game/light.h>
+#include <msp/game/owned.h>
+#include <msp/game/resources.h>
+#include <msp/game/shadowtarget.h>
+#include <msp/game/stage.h>
+#include <msp/gameview/application.h>
+#include <msp/gameview/resources.h>
+
+class Toy;
+
+class PlaygroundResources: public Msp::Game::ApplicationResources, public Msp::GameView::Resources
+{
+public:
+       PlaygroundResources();
+};
+
+class Playground: public Msp::GameView::Application<Playground, PlaygroundResources>
+{
+private:
+       Msp::Game::Stage &stage;
+       Msp::Game::Owned<Msp::Game::Entity> cam_entity;
+       Msp::Game::Owned<Msp::Game::Camera> camera;
+       Msp::Game::ShadowTargetSetup shadow_setup;
+       Msp::Game::Owned<Msp::Game::ShadowTarget> shadow_target;
+       Msp::Game::Owned<Msp::Game::Entity> sun_entity;
+       Msp::Game::Owned<Msp::Game::Light> sun;
+       std::vector<Msp::Game::Owned<Toy>> toys;
+
+public:
+       Playground(int, char **);
+};
+
+#endif
diff --git a/examples/playground/source/setups.mgs b/examples/playground/source/setups.mgs
new file mode 100644 (file)
index 0000000..24fddad
--- /dev/null
@@ -0,0 +1,11 @@
+import "msp/game/setups";
+
+entity Fixture
+{
+       field shape Shape;
+};
+
+entity Toy
+{
+       field shape Shape;
+};
diff --git a/examples/playground/source/toy.cpp b/examples/playground/source/toy.cpp
new file mode 100644 (file)
index 0000000..6ee3024
--- /dev/null
@@ -0,0 +1,11 @@
+#include "toy.h"
+
+using namespace Msp;
+
+Toy::Toy(Game::Handle<Game::Entity> p, const Setup &s, const Game::TransformValues &t):
+       Entity(p, t),
+       setup(s),
+       rigid_body(this),
+       shape(this, setup.shape),
+       motion(this)
+{ }
diff --git a/examples/playground/source/toy.h b/examples/playground/source/toy.h
new file mode 100644 (file)
index 0000000..2a24e3a
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef TOY_H_
+#define TOY_H_
+
+#include <msp/game/entity.h>
+#include <msp/game/motion.h>
+#include <msp/game/rigidbody.h>
+#include <msp/game/shape.h>
+#include "playground/setups.h"
+
+class Toy: public Msp::Game::Entity
+{
+public:
+       using Setup = ToySetup;
+
+private:
+       const Setup &setup;
+       Msp::Game::Owned<Msp::Game::RigidBody> rigid_body;
+       Msp::Game::Owned<Msp::Game::Shape> shape;
+       Msp::Game::Owned<Msp::Game::Motion> motion;
+
+public:
+       Toy(Msp::Game::Handle<Msp::Game::Entity>, const Setup &, const Msp::Game::TransformValues &);
+};
+
+#endif