Compare commits

...

2 Commits

Author SHA1 Message Date
4d0fb8f60f Let physics run for a while to prevent bugs 2025-09-08 11:40:53 +03:00
3f59a384e4 Water: distortions 2025-09-08 09:46:16 +03:00
8 changed files with 55 additions and 30 deletions

View File

@@ -185,6 +185,7 @@ class App : public OgreBites::ApplicationContext {
ECS::Vector2 mouse{ 0, 0 };
float wheel_y;
bool mouse_moved = false, wheel_moved = false;
float mInitDelay;
public:
Ogre::Timer fps_timer;
@@ -195,6 +196,7 @@ class App : public OgreBites::ApplicationContext {
, mApp(app)
, fast(false)
, control(0)
, mInitDelay(1.0)
{
}
bool isGuiEnabled()
@@ -322,6 +324,8 @@ class App : public OgreBites::ApplicationContext {
}
if (!isGuiEnabled()) {
mApp->updateWorld(evt.timeSinceLastFrame);
if (mInitDelay >= 0.0f)
mInitDelay -= evt.timeSinceLastFrame;
}
if (!isGuiEnabled() && ECS::get().has<ECS::Input>()) {
ECS::Input &input =

View File

@@ -24,6 +24,8 @@ CharacterModule::CharacterModule(flecs::world &ecs)
.kind(flecs::OnUpdate)
.each([this](EngineData &eng, CharacterBase &ch) {
ch.mTimer += eng.delta;
if (eng.startupDelay >= 0.0f)
eng.startupDelay -= eng.delta;
});
ecs.system<Input, Camera>("HandleInput")
.kind(flecs::OnUpdate)
@@ -179,11 +181,12 @@ CharacterModule::CharacterModule(flecs::world &ecs)
return;
ch.mBoneMotion = ch.mRootBone->getPosition();
});
ecs.system<CharacterBase, CharacterBody, AnimationControl>(
"HandleRootMotion")
ecs.system<const EngineData, CharacterBase, CharacterBody,
AnimationControl>("HandleRootMotion")
.kind(flecs::OnUpdate)
.each([this](flecs::entity e, CharacterBase &ch,
CharacterBody &body, AnimationControl &anim) {
.each([this](flecs::entity e, const EngineData &eng,
CharacterBase &ch, CharacterBody &body,
AnimationControl &anim) {
float delta = e.world().delta_time();
if (!ch.mBodyNode)
return;
@@ -218,26 +221,29 @@ CharacterModule::CharacterModule(flecs::world &ecs)
body.gvelocity.y, -2.5f, 2.5f);
} else
body.gvelocity += gravity * delta;
body.gvelocity *= 0.99;
velocity += body.gvelocity;
Ogre::Vector3 rotMotion = velocity * delta;
btVector3 currentPosition =
body.mGhostObject->getWorldTransform()
.getOrigin();
is_on_floor = body.mController->isOnFloor();
penetration = body.mController->isPenetrating();
if (is_on_floor)
body.gvelocity =
Ogre::Vector3(0.0f, 0.0f, 0.0f);
if (eng.startupDelay < 0.0f) {
body.gvelocity *= 0.99;
velocity += body.gvelocity;
Ogre::Vector3 rotMotion = velocity * delta;
btVector3 currentPosition =
body.mGhostObject->getWorldTransform()
.getOrigin();
is_on_floor = body.mController->isOnFloor();
penetration = body.mController->isPenetrating();
if (is_on_floor)
body.gvelocity =
Ogre::Vector3(0.0f, 0.0f, 0.0f);
btTransform from(
Ogre::Bullet::convert(
ch.mBodyNode->getOrientation()),
Ogre::Bullet::convert(
ch.mBodyNode->getPosition()));
ch.mBodyNode->setPosition(ch.mBodyNode->getPosition() +
rotMotion);
ch.mBoneMotion = Ogre::Vector3(0, 0, 0);
btTransform from(
Ogre::Bullet::convert(
ch.mBodyNode->getOrientation()),
Ogre::Bullet::convert(
ch.mBodyNode->getPosition()));
ch.mBodyNode->setPosition(
ch.mBodyNode->getPosition() +
rotMotion);
ch.mBoneMotion = Ogre::Vector3(0, 0, 0);
}
});
ecs.system<const Input, AnimationControl>("HandlePlayerAnimations")
.kind(flecs::OnUpdate)

View File

@@ -17,6 +17,7 @@ struct EngineData {
Ogre::SceneManager *mScnMgr;
Ogre::Bullet::DynamicsWorld *mWorld;
float delta;
float startupDelay;
};
struct Vector3 {
float x;

View File

@@ -31,7 +31,7 @@ void setup(Ogre::SceneManager *scnMgr, Ogre::Bullet::DynamicsWorld *world,
.each([](EngineData &eng) {
eng.delta = ECS::get().delta_time();
});
ecs.set<EngineData>({ scnMgr, world, 0.0f });
ecs.set<EngineData>({ scnMgr, world, 0.0f, 0.0f });
ecs.set<Camera>({ cameraNode, camera, false });
ecs.add<GameData>();
ecs.add<Input>();

View File

@@ -233,7 +233,7 @@ WaterModule::WaterModule(flecs::world &ecs)
texture_unit2->setTextureFiltering(
Ogre::FT_MAG, Ogre::FO_LINEAR);
texture_unit2->setTextureFiltering(
Ogre::FT_MIP, Ogre::FO_NONE);
Ogre::FT_MIP, Ogre::FO_LINEAR);
#if 0
bool success =
Ogre::RTShader::ShaderGenerator::getSingletonPtr()
@@ -267,6 +267,7 @@ WaterModule::WaterModule(flecs::world &ecs)
.getByName("Water/Above");
mat->load();
#endif
mat->load();
mat->setReceiveShadows(false);
/*
auto mat2 =

View File

@@ -6,14 +6,21 @@ SAMPLER2D(noiseMap, 1);
#if NEW_WATER_SHADER
MAIN_PARAMETERS
IN(vec4 clipSpace, TEXCOORD0)
IN(vec2 textureCoords, TEXCOORD1)
MAIN_DECLARATION
{
// vec2 ndc = gl_FragCoord.xy / viewportSize.xy;
vec2 ndc = clipSpace.xy / clipSpace.w / 2.0 + vec2(0.5, 0.5);
vec2 reflectionUV = vec2(ndc.x, 1.0 - ndc.y) * 0.5;
vec2 refractionUV = vec2(ndc.x, 1.0 - ndc.y) * 0.5 + vec2(0.5, 0.0);
vec4 reflectionColour = texture2D(reflectMap, reflectionUV);
vec4 refractionColour = texture2D(reflectMap, refractionUV);
vec2 distortion1 = texture2D(noiseMap, textureCoords).rg * 2.0 - vec2(1.0, 1.0);
vec2 distortion2 = texture2D(noiseMap, textureCoords * 5.0 + vec2(10202.0, 221.0)).rg * 2.0 - vec2(1.0, 1.0);
vec2 distortion3 = texture2D(noiseMap, textureCoords * 10.0 + vec2(1302.0, 721.0)).rg * 2.0 - vec2(1.0, 1.0);
vec2 reflectionUV = (vec2(ndc.x, 1.0 - ndc.y) + distortion1 * 0.035 + distortion2 * 0.02 + distortion3 * 0.01);
vec2 refractionUV = (vec2(ndc.x, 1.0 - ndc.y) + distortion1 * 0.035 + distortion2 * 0.02 + distortion3 * 0.01);
reflectionUV.x = clamp(reflectionUV.x, 0.001, 0.999);
reflectionUV.y = clamp(reflectionUV.y, 0.001, 0.999);
refractionUV.x = clamp(refractionUV.x, 0.001, 0.999);
refractionUV.y = clamp(refractionUV.y, 0.001, 0.999);
vec4 reflectionColour = texture2D(reflectMap, reflectionUV * 0.5);
vec4 refractionColour = texture2D(reflectMap, refractionUV * 0.5 + vec2(0.5, 0.0));
gl_FragColor = mix(reflectionColour, refractionColour, 0.5);
}
#else

View File

@@ -91,6 +91,8 @@ fragment_program Water/water_fp glsl glsles glslang hlsl
source water.frag
default_params
{
param_named reflectMap int 0
param_named noiseMap int 1
}
preprocessor_defines NEW_WATER_SHADER
}

View File

@@ -28,13 +28,17 @@ uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
)
const float tiling = 0.012;
MAIN_PARAMETERS
IN(vec4 position, POSITION)
OUT(vec4 clipSpace, TEXCOORD0)
OUT(vec2 textureCoords, TEXCOORD1)
MAIN_DECLARATION
{
clipSpace = projectionMatrix * viewMatrix * modelMatrix * vec4(position.xyz, 1.0);
vec4 worldPos = modelMatrix * vec4(position.xyz, 1.0);
gl_Position = clipSpace;
textureCoords = vec2(worldPos.x / 2.0 + 0.5, worldPos.z / 2.0 + 0.5) * tiling;
}
#else