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