18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 15:17:08 +01:00
Files
maszyna/rendering/common/windscreenwater.h
T
lcddisplay ba3129275c Snow windscreen: the snow of the glass simulated on a map that keeps it
A glass of mat_snow_windscreen (the material of the "snow:" block, with a
mask of the wipers) gets a slot of the windscreen water storage, as the
glasses of the water could, and two maps of its snow (a step reads one,
writes the other): the snow, the water, how packed the snow is and the shape
of its flakes for each of 256 x 256 texels of its uv. water_snow.comp runs a
step a frame, both APIs:
- the flakes land as many as come to the glass facing the snow against the
  cab (and a few from the eddies), the dry ones bouncing off at speed;
- the blades take the snow they go over (a little stays, a wet trace) and
  push it ahead of themselves, leaving it as ridges at the ends of their
  sweeps; piled snow slips onto its neighbours into a wedge;
- the glass is warmer than the air (the cab is heated, less so at speed):
  above zero the snow melts into water, slush slides down, dry powder is
  blown along by the air at speed, the water runs and dries.
mat_snow_windscreen.frag draws the map: flakes from the atlas where the snow
is thin (water spots where it melted), a layer with a torn edge, as opaque as
it is thick, grey when wet, its relief from the slopes of the map; the wet
glass bends the picture behind through the post-processing. Without the
simulation the glass is drawn as before.

The glasses are found again when a material of the cab changes its kind
(the weather switched its shader). The drop passes run only for glasses of
the water. No assets change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BqfRrTiBDaAyQ2gT6ZrnZD
2026-11-29 11:39:10 +01:00

118 lines
5.1 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <array>
#include <cstdint>
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include <glm/glm.hpp>
#include "gl/ubo.h"
#include "interfaces/ITexture.h"
#include "utilities/Classes.h"
struct opengl_material;
class TModel3d;
class TSubModel;
class TDynamicObject;
namespace gfx
{
// the water on the windscreens of the cab of the train, simulated on the gpu, whichever api runs it.
//
// a glass is a material of mat_rain_windscreen (one with a mask of the wipers) in the model of the cab of the train, up to
// gl::WATER_SLOTS of them; the material is told its slot in its param water_slot (1 + the slot, the drawing of the glass reads
// the water of the slot where it's there, mat_rain_windscreen.frag). the water lies in the coordinates of the texture of the glass
// (uv), as the mask does. the map of a glass is made here from the triangles of the model with its material: for the texels of
// the uv on the glass, the step along the glass (metres, the space of the model of the cab) for a step of u and of v, and which
// way is out of the cab; the passes of the gpu bake the mask of the wipers into it. once a frame the step of the simulation is
// worked out here (the time, the rain or snow, gravity and the air against the cab in the space of its model, the blades) and
// the passes run it (water_*.comp: the landing drops, the drops moving and carried by the blades, the grids, the drops flowing
// together); what they keep and the glasses read is the storage of the water (water.glsl, gl::WATER_*_OFFSET)
class windscreen_water
{
public:
// what the renderer gives: the materials of its manager, which get their slots
struct host
{
std::function<opengl_material *(material_handle)> material;
};
windscreen_water() = default;
virtual ~windscreen_water() = default;
windscreen_water(windscreen_water const &) = delete;
windscreen_water &operator=(windscreen_water const &) = delete;
void set_host(host Host)
{
m_host = std::move(Host);
}
// whether the passes could be made
bool available();
// once a frame, before the colour passes: the cab of the train followed, the step of the simulation run. Seconds: of the
// simulation since the frame before (0 when it's paused)
void update(double Seconds);
// the materials let go of their slots, the glasses forgotten (the storage stays as it is, unused)
void release();
std::string info() const;
protected:
// the glasses forgotten and the programs taken as not made yet (the renderer let go of them), made again when next needed
void reset();
// the programs and the storage; false: the simulation can't be used, the glasses make their water up
virtual bool gpu_init() = 0;
// the slot set up for a glass: its drops, its grid and its paths cleared, its header saying it isn't simulated (until the
// first step), the axes of its map (two vec4 a texel, gl::WATER_MAP x WATER_MAP texels, see water.glsl) put in the storage
virtual void gpu_setup(std::size_t Slot, std::vector<glm::vec4> const &Axes) = 0;
// the mask of the wipers of a glass (a texture of the renderer) baked into its map; false when the texture isn't ready yet
// (tried again with the next frame)
virtual bool gpu_bake(std::size_t Slot, texture_handle Mask, gl::water_ubs const &Block) = 0;
// the step of the simulation of the glasses Block says are simulated; Tries: the most landings tried on a glass
virtual void gpu_step(gl::water_ubs const &Block, std::uint32_t Tries) = 0;
private:
struct glass
{
material_handle material{null_handle};
texture_handle mask{null_handle};
float densest{0.f}; // the most metres squared a unit of the uv holds
float area{0.f}; // metres squared of the glass
double landings{0.0}; // the share of a landing left over from the steps before
bool baked{false};
bool snow{false}; // a glass of the snow (mat_snow_windscreen), else of the water
};
// the glasses of the model of a cab found and set up
void build(TModel3d &Cab);
// the triangles of a glass laid into its map; Inside: a point inside the cab
void rasterize(TSubModel *Submodel, material_handle Material, glm::vec3 const &Inside, std::vector<glm::vec4> &Axes) const;
// the slots given to the materials of the glasses (every frame: a material made anew takes its default params)
void assign(bool Assigned);
host m_host;
int m_state{0}; // 0: not set up yet, 1: ready, -1: can't be used
TDynamicObject const *m_vehicle{nullptr};
TModel3d const *m_cab{nullptr};
std::vector<std::pair<material_handle, int>> m_kinds; // the materials of the cab with a mask of the wipers, what kind of glass they were
std::array<glass, gl::WATER_SLOTS> m_glasses{};
std::size_t m_glasscount{0};
glm::vec4 m_bladesbefore{-1.f};
double m_time{0.0}; // seconds the simulation runs
std::uint32_t m_step{0};
std::uint32_t m_tries{0}; // landings tried in the last step, all glasses
int m_snowmap{0}; // the map of the snow of the glasses the next step reads (0, 1)
};
} // namespace gfx