MeshLib
 
Loading...
Searching...
No Matches
MRIRenderObject.h
Go to the documentation of this file.
1#pragma once
4#include "MRMeshFwd.h"
5#include "MRViewportId.h"
6#include "MRVector2.h"
7#include "MRVector4.h"
8#include "MRAffineXf3.h"
9#include <functional>
10#include <typeindex>
11#include <memory>
12
13namespace MR
14{
15
16enum class DepthFunction
17{
18 Never = 0,
19 Less = 1,
20 Equal = 2,
21 Greater = 4,
26 Default = 8 // usually "Less" but may differ for different object types
27};
29
30
32{
33 const Matrix4f& viewMatrix;
34 const Matrix4f& projMatrix;
35 ViewportId viewportId; // id of the viewport
36 Vector4i viewport; // viewport x0, y0, width, height
37};
38
42{
43 const Matrix4f& modelMatrix;
44 const Plane3f& clipPlane; // viewport clip plane (it is not applied while object does not have clipping flag set)
46};
47
50{
51 const Matrix4f* normMatrixPtr{ nullptr }; // normal matrix, only necessary for triangles rendering
52 Vector3f lightPos; // position of light source
53 bool allowAlphaSort{ false }; // if true, the object can use the alpha sorting shader if it wants to
54
55 RenderModelPassMask passMask = RenderModelPassMask::All; // Only perform rendering if `bool( passMask & desiredPass )` is true.
56};
57
60{
61 virtual ~BasicUiRenderTask() = default;
62
63 BasicUiRenderTask() = default;
68
70 float renderTaskDepth = 0;
71
72 enum class InteractionMask
73 {
74 mouseHover = 1 << 0,
75 mouseScroll = 1 << 1,
76 };
78
80 {
81 // Which interactions should be blocked by this object.
82 // This is passed along between all `renderUi()` calls in a single frame, and then the end result is used.
83 mutable InteractionMask consumedInteractions{};
84
85 // If nothing else is hovered, this returns true and writes `mouseHover` to `consumedInteractions`.
86 [[nodiscard]] bool tryConsumeMouseHover() const
87 {
88 if ( !bool( consumedInteractions & InteractionMask::mouseHover ) )
89 {
90 consumedInteractions |= InteractionMask::mouseHover;
91 return true;
92 }
93 return false;
94 }
95 };
96
99 virtual void earlyBackwardPass( const BackwardPassParams& params ) { (void)params; }
100
102 virtual void renderPass() = 0;
103};
104
106{
108 float scale = 1;
109
110 using UiTaskList = std::vector<std::shared_ptr<BasicUiRenderTask>>;
111
112 // Those are Z-sorted and then executed.
113 UiTaskList* tasks = nullptr;
114};
115
117{
118 virtual ~UiRenderManager() = default;
119
120 // This is called before doing `IRenderObject::renderUi()` on even object in a viewport. Each viewport is rendered separately.
121 virtual void preRenderViewport( ViewportId viewport ) { (void)viewport; }
122 // This is called after doing `IRenderObject::renderUi()` on even object in a viewport. Each viewport is rendered separately.
123 virtual void postRenderViewport( ViewportId viewport ) { (void)viewport; }
124
125 // Returns the parameters for the `IRenderObject::earlyBackwardPass()`.
126 // This will be called exactly once per viewport, each time the UI in it is rendered.
127 virtual BasicUiRenderTask::BackwardPassParams beginBackwardPass( ViewportId viewport, UiRenderParams::UiTaskList& tasks ) { (void)viewport; (void)tasks; return {}; }
128 // After the backward pass is performed, the parameters should be passed back into this function.
129 virtual void finishBackwardPass( const BasicUiRenderTask::BackwardPassParams& params ) { (void)params; }
130};
131
133{
134public:
135 virtual ~IRenderObject() = default;
136
137 // These functions do:
138 // 1) bind data
139 // 2) pass shaders arguments
140 // 3) draw data
141
142 // Returns true if something was rendered, or false if nothing to render.
143 virtual bool render( const ModelRenderParams& params ) = 0;
144 virtual void renderPicker( const ModelBaseRenderParams& params, unsigned geomId ) = 0;
146 virtual size_t heapBytes() const = 0;
148 virtual size_t glBytes() const = 0;
150 virtual void forceBindAll() {}
151
156 virtual void renderUi( const UiRenderParams& params ) { (void)params; }
157};
158// Those dummy definitions remove undefined references in `RenderObjectCombinator` when it calls non-overridden pure virtual methods.
159// We could check in `RenderObjectCombinator` if they're overridden or not, but it's easier to just define them.
160inline bool IRenderObject::render( const ModelRenderParams& ) { return false; }
161inline void IRenderObject::renderPicker( const ModelBaseRenderParams&, unsigned ) {}
162inline size_t IRenderObject::heapBytes() const { return 0; }
163inline size_t IRenderObject::glBytes() const { return 0; }
164
165// Combines several different `IRenderObject`s into one in a meaningful way.
166template <typename ...Bases>
167requires ( ( std::derived_from<Bases, IRenderObject> && !std::same_as<Bases, IRenderObject> ) && ... )
168class RenderObjectCombinator : public Bases...
169{
170public:
172 : Bases( object )...
173 {}
174
175 bool render( const ModelRenderParams& params ) override
176 {
177 bool ret = false;
178 // Clang 11 chokes on this if I fold from the right instead of from the left. But why?
179 (void)( ..., ( ret = Bases::render( params ) || ret ) );
180 return ret;
181 }
182 void renderPicker( const ModelBaseRenderParams& params, unsigned geomId ) override { ( Bases::renderPicker( params, geomId ), ... ); }
183 size_t heapBytes() const override { return ( std::size_t{} + ... + Bases::heapBytes() ); }
184 size_t glBytes() const override { return ( std::size_t{} + ... + Bases::glBytes() ); }
185 void forceBindAll() override { ( Bases::forceBindAll(), ... ); }
186 void renderUi( const UiRenderParams& params ) override { ( Bases::renderUi( params ), ... ); }
187};
188
189MRMESH_API std::unique_ptr<IRenderObject> createRenderObject( const VisualObject& visObj, const std::type_index& type );
190
191template<typename ObjectType>
192std::unique_ptr<IRenderObject> createRenderObject( const VisualObject& visObj )
193{
194 static_assert( std::is_base_of_v<VisualObject, std::remove_reference_t<ObjectType>>, "MR::VisualObject is not base of ObjectType" );
195 return createRenderObject( visObj, typeid( ObjectType ) );
196}
197
198using IRenderObjectConstructorLambda = std::function<std::unique_ptr<IRenderObject>( const VisualObject& )>;
199
200template<typename RenderObjType>
202{
203 return [] ( const VisualObject& visObj ) { return std::make_unique<RenderObjType>( visObj ); };
204}
205
211
212#define MR_REGISTER_RENDER_OBJECT_IMPL(objectType, .../*rendObjectType*/)\
213 static MR::RegisterRenderObjectConstructor __objectRegistrator##objectType{typeid(objectType),makeRenderObjectConstructor<__VA_ARGS__>()};
214
215}
#define MR_MAKE_FLAG_OPERATORS_IN_CLASS(T)
Definition MRFlagOperators.h:9
#define MR_MAKE_FLAG_OPERATORS(T)
Definition MRFlagOperators.h:6
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRIRenderObject.h:133
virtual bool render(const ModelRenderParams &params)=0
Definition MRIRenderObject.h:160
virtual void forceBindAll()
binds all data for this render object, not to bind ever again (until object becomes dirty)
Definition MRIRenderObject.h:150
virtual size_t heapBytes() const =0
returns the amount of memory this object occupies on heap
Definition MRIRenderObject.h:162
virtual void renderUi(const UiRenderParams &params)
Definition MRIRenderObject.h:156
virtual void renderPicker(const ModelBaseRenderParams &params, unsigned geomId)=0
Definition MRIRenderObject.h:161
virtual ~IRenderObject()=default
virtual size_t glBytes() const =0
returns the amount of memory this object allocated in OpenGL
Definition MRIRenderObject.h:163
Definition MRIRenderObject.h:207
MRMESH_API RegisterRenderObjectConstructor(const std::type_index &type, IRenderObjectConstructorLambda lambda)
Definition MRIRenderObject.h:169
size_t glBytes() const override
Definition MRIRenderObject.h:184
void renderUi(const UiRenderParams &params) override
Definition MRIRenderObject.h:186
size_t heapBytes() const override
Definition MRIRenderObject.h:183
bool render(const ModelRenderParams &params) override
Definition MRIRenderObject.h:175
void renderPicker(const ModelBaseRenderParams &params, unsigned geomId) override
Definition MRIRenderObject.h:182
RenderObjectCombinator(const VisualObject &object)
Definition MRIRenderObject.h:171
void forceBindAll() override
Definition MRIRenderObject.h:185
Definition MRViewportId.h:16
Visual Object.
Definition MRVisualObject.h:131
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
Definition MRCameraOrientationPlugin.h:7
IRenderObjectConstructorLambda makeRenderObjectConstructor()
Definition MRIRenderObject.h:201
RenderModelPassMask
Various passes of the 3D rendering.
Definition MRRenderModelParameters.h:10
std::function< std::unique_ptr< IRenderObject >(const VisualObject &)> IRenderObjectConstructorLambda
Definition MRIRenderObject.h:198
DepthFunction
Definition MRIRenderObject.h:17
MRMESH_API std::unique_ptr< IRenderObject > createRenderObject(const VisualObject &visObj, const std::type_index &type)
Common rendering parameters for meshes and UI.
Definition MRIRenderObject.h:32
const Matrix4f & viewMatrix
Definition MRIRenderObject.h:33
Vector4i viewport
Definition MRIRenderObject.h:36
const Matrix4f & projMatrix
Definition MRIRenderObject.h:34
ViewportId viewportId
Definition MRIRenderObject.h:35
Definition MRIRenderObject.h:80
bool tryConsumeMouseHover() const
Definition MRIRenderObject.h:86
IRenderObject::renderUi() can emit zero or more or more of those tasks. They are sorted by depth ever...
Definition MRIRenderObject.h:60
BasicUiRenderTask()=default
virtual void renderPass()=0
This is the main rendering pass.
BasicUiRenderTask & operator=(const BasicUiRenderTask &)=default
BasicUiRenderTask(const BasicUiRenderTask &)=default
virtual void earlyBackwardPass(const BackwardPassParams &params)
Definition MRIRenderObject.h:99
float renderTaskDepth
The tasks are sorted by this depth, descending (larger depth = further away).
Definition MRIRenderObject.h:70
BasicUiRenderTask(BasicUiRenderTask &&)=default
virtual ~BasicUiRenderTask()=default
InteractionMask
Definition MRIRenderObject.h:73
BasicUiRenderTask & operator=(BasicUiRenderTask &&)=default
Definition MRIRenderObject.h:42
const Matrix4f & modelMatrix
Definition MRIRenderObject.h:43
DepthFunction depthFunction
Definition MRIRenderObject.h:45
const Plane3f & clipPlane
Definition MRIRenderObject.h:44
Mesh rendering parameters for primary rendering (as opposed to the picker).
Definition MRIRenderObject.h:50
Vector3f lightPos
Definition MRIRenderObject.h:52
RenderModelPassMask passMask
Definition MRIRenderObject.h:55
const Matrix4f * normMatrixPtr
Definition MRIRenderObject.h:51
bool allowAlphaSort
Definition MRIRenderObject.h:53
Definition MRIRenderObject.h:117
virtual ~UiRenderManager()=default
virtual BasicUiRenderTask::BackwardPassParams beginBackwardPass(ViewportId viewport, UiRenderParams::UiTaskList &tasks)
Definition MRIRenderObject.h:127
virtual void finishBackwardPass(const BasicUiRenderTask::BackwardPassParams &params)
Definition MRIRenderObject.h:129
virtual void preRenderViewport(ViewportId viewport)
Definition MRIRenderObject.h:121
virtual void postRenderViewport(ViewportId viewport)
Definition MRIRenderObject.h:123
Definition MRIRenderObject.h:106
UiTaskList * tasks
Definition MRIRenderObject.h:113
std::vector< std::shared_ptr< BasicUiRenderTask > > UiTaskList
Definition MRIRenderObject.h:110
float scale
Multiply all your hardcoded sizes by this amount.
Definition MRIRenderObject.h:108