RigsofRods
Soft-body Physics Simulation
Water.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #pragma once
23 
24 #include "IWater.h"
25 #include "Application.h"
26 
27 #include <OgreHardwareVertexBuffer.h> // Ogre::HardwareVertexBufferSharedPtr
28 #include <OgreMesh.h>
29 #include <OgrePlane.h>
30 #include <OgreRenderTargetListener.h>
31 #include <OgreTexture.h>
32 #include <OgreVector3.h>
33 #include <vector>
34 
35 namespace RoR {
36 
39 
40 class Water : public IWater
41 {
42 public:
43 
44  Water(Ogre::Vector3 terrn_size);
45  ~Water();
46 
47  // Interface `IWater`
48  float GetStaticWaterHeight() override;
49  void SetStaticWaterHeight(float value) override;
50  void SetWaterBottomHeight(float value) override;
51  void SetWavesHeight(float value) override;
52  float CalcWavesHeight(Ogre::Vector3 pos) override;
53  Ogre::Vector3 CalcWavesVelocity(Ogre::Vector3 pos) override;
54  void SetWaterVisible(bool value) override;
55  bool IsUnderWater(Ogre::Vector3 pos) override;
56  void SetReflectionPlaneHeight(float centerheight) override;
57  void UpdateReflectionPlane(float h) override;
58  void WaterPrepareShutdown() override;
59  void UpdateWater() override;
60  void FrameStepWater(float dt) override;
61  void SetForcedCameraTransform(Ogre::Radian fovy, Ogre::Vector3 pos, Ogre::Quaternion rot) override;
62  void ClearForcedCameraTransform() override;
63 
64 private:
65 
66  struct WaveTrain
67  {
68  float amplitude;
69  float maxheight;
70  float wavelength;
71  float wavespeed;
72  float direction;
73  float dir_sin;
74  float dir_cos;
75  };
76 
77  struct ReflectionListener: Ogre::RenderTargetListener
78  {
80 
81  void preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) override;
82  void postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) override;
83 
84  Ogre::SceneManager* scene_mgr;
85  Ogre::Entity* waterplane_entity;
86  };
87 
88  struct RefractionListener: Ogre::RenderTargetListener
89  {
91 
92  void preRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) override;
93  void postRenderTargetUpdate(const Ogre::RenderTargetEvent& evt) override;
94 
95  Ogre::SceneManager* scene_mgr;
96  Ogre::Entity* waterplane_entity;
97  };
98 
99  float GetWaveHeight(Ogre::Vector3 pos);
100  void ShowWave(Ogre::Vector3 refpos);
101  bool IsCameraUnderWater();
102  void PrepareWater();
103 
108  float m_max_ampl;
111  Ogre::Vector3 m_map_size;
112  Ogre::Plane m_water_plane;
113  Ogre::MeshPtr m_waterplane_mesh;
114  Ogre::Entity* m_waterplane_entity;
115  Ogre::SceneNode* m_waterplane_node;
116  Ogre::HardwareVertexBufferSharedPtr m_waterplane_vert_buf;
119  Ogre::Plane m_reflect_plane;
120  Ogre::Plane m_refract_plane;
123  Ogre::Camera* m_reflect_cam;
124  Ogre::Camera* m_refract_cam;
125  Ogre::RenderTexture* m_refract_rtt_target;
126  Ogre::RenderTexture* m_reflect_rtt_target;
127  Ogre::TexturePtr m_refract_rtt_texture;
128  Ogre::TexturePtr m_reflect_rtt_texture;
129  Ogre::Viewport* m_refract_rtt_viewport;
130  Ogre::Viewport* m_reflect_rtt_viewport;
131  Ogre::SceneNode* m_bottomplane_node;
132  Ogre::Plane m_bottom_plane;
133  std::vector<WaveTrain> m_wavetrain_defs;
134 
135  // Forced camera transforms, used by UpdateWater()
137  Ogre::Radian m_cam_forced_fovy;
138  Ogre::Vector3 m_cam_forced_position;
139  Ogre::Quaternion m_cam_forced_orientation;
140 };
141 
143 
144 } // namespace RoR
RoR::Water::WaveTrain::direction
float direction
Definition: Water.h:72
RoR::Water::WaveTrain::dir_cos
float dir_cos
Definition: Water.h:74
RoR::Water::m_reflect_listener
ReflectionListener m_reflect_listener
Definition: Water.h:121
RoR::Water::RefractionListener::scene_mgr
Ogre::SceneManager * scene_mgr
Definition: Water.h:95
RoR::Water::IsCameraUnderWater
bool IsCameraUnderWater()
Definition: Water.cpp:406
RoR::Water::m_waterplane_entity
Ogre::Entity * m_waterplane_entity
Definition: Water.h:114
RoR::Water::IsUnderWater
bool IsUnderWater(Ogre::Vector3 pos) override
Definition: Water.cpp:551
RoR::Water::m_bottomplane_node
Ogre::SceneNode * m_bottomplane_node
Definition: Water.h:131
RoR::Water::m_reflect_rtt_target
Ogre::RenderTexture * m_reflect_rtt_target
Definition: Water.h:126
RoR::Water::m_waterplane_node
Ogre::SceneNode * m_waterplane_node
Definition: Water.h:115
RoR::Water::m_waterplane_vert_buf
Ogre::HardwareVertexBufferSharedPtr m_waterplane_vert_buf
Definition: Water.h:116
RoR::Water::SetWavesHeight
void SetWavesHeight(float value) override
Definition: Water.cpp:509
RoR::Water::CalcWavesVelocity
Ogre::Vector3 CalcWavesVelocity(Ogre::Vector3 pos) override
Definition: Water.cpp:568
RoR::Water::m_waterplane_vert_buf_local
float * m_waterplane_vert_buf_local
Definition: Water.h:117
RoR::Water::SetReflectionPlaneHeight
void SetReflectionPlaneHeight(float centerheight) override
Definition: Water.cpp:359
RoR::Water::m_reflect_cam
Ogre::Camera * m_reflect_cam
Definition: Water.h:123
RoR::Water::~Water
~Water()
Definition: Water.cpp:98
RoR::IWater
< TODO: Mixed gfx+physics (waves) - must be separated ~ only_a_ptr, 02/2018
Definition: IWater.h:32
RoR::Water::ClearForcedCameraTransform
void ClearForcedCameraTransform() override
Definition: Water.cpp:641
RoR::Water::m_water_visible
bool m_water_visible
Definition: Water.h:104
RoR::Water::m_reflect_rtt_texture
Ogre::TexturePtr m_reflect_rtt_texture
Definition: Water.h:128
RoR::Water::m_max_ampl
float m_max_ampl
Definition: Water.h:108
RoR::Water::WaveTrain
Definition: Water.h:66
RoR::Water::m_frame_counter
int m_frame_counter
Definition: Water.h:110
RoR::Water
Definition: Water.h:40
RoR::Water::RefractionListener::waterplane_entity
Ogre::Entity * waterplane_entity
Definition: Water.h:96
RoR::Water::GetWaveHeight
float GetWaveHeight(Ogre::Vector3 pos)
Definition: Water.cpp:649
RoR::Water::m_bottom_plane
Ogre::Plane m_bottom_plane
Definition: Water.h:132
RoR::Water::ReflectionListener::waterplane_entity
Ogre::Entity * waterplane_entity
Definition: Water.h:85
RoR::Water::m_water_height
float m_water_height
Definition: Water.h:105
RoR::Water::m_reflect_rtt_viewport
Ogre::Viewport * m_reflect_rtt_viewport
Definition: Water.h:130
RoR::Water::CalcWavesHeight
float CalcWavesHeight(Ogre::Vector3 pos) override
Definition: Water.cpp:519
RoR::Water::PrepareWater
void PrepareWater()
Definition: Water.cpp:176
RoR::Water::m_reflect_plane
Ogre::Plane m_reflect_plane
Definition: Water.h:119
RoR::Water::WaveTrain::wavelength
float wavelength
Definition: Water.h:70
RoR::Water::FrameStepWater
void FrameStepWater(float dt) override
Definition: Water.cpp:625
RoR::Water::UpdateReflectionPlane
void UpdateReflectionPlane(float h) override
Definition: Water.cpp:594
RoR::Water::ReflectionListener::ReflectionListener
ReflectionListener()
Definition: Water.h:79
RoR::Water::WaveTrain::dir_sin
float dir_sin
Definition: Water.h:73
RoR::Water::SetForcedCameraTransform
void SetForcedCameraTransform(Ogre::Radian fovy, Ogre::Vector3 pos, Ogre::Quaternion rot) override
Definition: Water.cpp:633
RoR::Water::RefractionListener::preRenderTargetUpdate
void preRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override
Definition: Water.cpp:665
RoR::Water::RefractionListener::postRenderTargetUpdate
void postRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override
Definition: Water.cpp:672
RoR::Water::ShowWave
void ShowWave(Ogre::Vector3 refpos)
Definition: Water.cpp:368
RoR::Water::GetStaticWaterHeight
float GetStaticWaterHeight() override
Returns static water level configured in 'terrn2'.
Definition: Water.cpp:498
RoR::Water::SetStaticWaterHeight
void SetStaticWaterHeight(float value) override
Definition: Water.cpp:503
RoR::Water::m_wavetrain_defs
std::vector< WaveTrain > m_wavetrain_defs
Definition: Water.h:133
RoR::Water::WaterPrepareShutdown
void WaterPrepareShutdown() override
Definition: Water.cpp:490
RoR::Water::WaveTrain::amplitude
float amplitude
Definition: Water.h:68
Application.h
Central state/object manager and communications hub.
RoR::Water::m_refract_cam
Ogre::Camera * m_refract_cam
Definition: Water.h:124
RoR::Water::m_water_plane
Ogre::Plane m_water_plane
Definition: Water.h:112
RoR::Water::m_refract_rtt_texture
Ogre::TexturePtr m_refract_rtt_texture
Definition: Water.h:127
RoR::Water::m_cam_forced_fovy
Ogre::Radian m_cam_forced_fovy
Definition: Water.h:137
RoR::Water::m_waterplane_force_update_pos
bool m_waterplane_force_update_pos
Definition: Water.h:118
RoR::Water::m_refract_plane
Ogre::Plane m_refract_plane
Definition: Water.h:120
RoR::Water::ReflectionListener::postRenderTargetUpdate
void postRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override
Definition: Water.cpp:685
RoR::Water::RefractionListener::RefractionListener
RefractionListener()
Definition: Water.h:90
RoR::Water::WaveTrain::maxheight
float maxheight
Definition: Water.h:69
IWater.h
RoR::Water::ReflectionListener::preRenderTargetUpdate
void preRenderTargetUpdate(const Ogre::RenderTargetEvent &evt) override
Definition: Water.cpp:679
RoR::Water::Water
Water(Ogre::Vector3 terrn_size)
Definition: Water.cpp:38
RoR::Water::RefractionListener
Definition: Water.h:88
RoR::Water::m_map_size
Ogre::Vector3 m_map_size
Definition: Water.h:111
RoR::Water::m_waves_height
float m_waves_height
Definition: Water.h:106
RoR::Water::m_cam_forced
bool m_cam_forced
Definition: Water.h:136
RoR::Water::ReflectionListener
Definition: Water.h:77
RoR::Water::UpdateWater
void UpdateWater() override
Definition: Water.cpp:411
RoR::Water::m_waterplane_mesh
Ogre::MeshPtr m_waterplane_mesh
Definition: Water.h:113
RoR::Water::WaveTrain::wavespeed
float wavespeed
Definition: Water.h:71
RoR::Water::m_cam_forced_orientation
Ogre::Quaternion m_cam_forced_orientation
Definition: Water.h:139
RoR::Water::SetWaterBottomHeight
void SetWaterBottomHeight(float value) override
Definition: Water.cpp:514
RoR::Water::m_refract_rtt_viewport
Ogre::Viewport * m_refract_rtt_viewport
Definition: Water.h:129
RoR::Water::SetWaterVisible
void SetWaterVisible(bool value) override
Definition: Water.cpp:348
RoR::Water::ReflectionListener::scene_mgr
Ogre::SceneManager * scene_mgr
Definition: Water.h:84
RoR::Water::m_refract_rtt_target
Ogre::RenderTexture * m_refract_rtt_target
Definition: Water.h:125
RoR
Definition: AppContext.h:36
RoR::Water::m_waterplane_mesh_scale
float m_waterplane_mesh_scale
Definition: Water.h:109
RoR::Water::m_bottom_height
float m_bottom_height
Definition: Water.h:107
RoR::Water::m_refract_listener
RefractionListener m_refract_listener
Definition: Water.h:122
RoR::Water::m_cam_forced_position
Ogre::Vector3 m_cam_forced_position
Definition: Water.h:138