MeshLib C++ Docs
Loading...
Searching...
No Matches
MRRenderGLHelpers.h
Go to the documentation of this file.
1#pragma once
3#include "MRGladGlfw.h"
4#include "exports.h"
5#include "MRMesh/MRColor.h"
6#include "MRMesh/MRVector2.h"
7#include "MRGLTexture.h"
8#include "MRGLMacro.h"
9#include <cassert>
10
11namespace MR
12{
15
16
19{
20 constexpr static GLuint NO_BUF = 0;
21public:
22 GlBuffer() = default;
23 GlBuffer( const GlBuffer & ) = delete;
24 GlBuffer( GlBuffer && r ) : bufferID_( r.bufferID_ ), size_( r.size_ ) { r.detach_(); }
25 ~GlBuffer() { del(); }
26
27 GlBuffer& operator =( const GlBuffer & ) = delete;
28 GlBuffer& operator =( GlBuffer && r ) { del(); bufferID_ = r.bufferID_; size_ = r.size_; r.detach_(); return * this; }
29
30 auto getId() const { return bufferID_; }
31 bool valid() const { return bufferID_ != NO_BUF; }
32 size_t size() const { return size_; }
33
35 MRVIEWER_API void gen();
36
38 MRVIEWER_API void del();
39
41 MRVIEWER_API void bind( GLenum target );
42
44 MRVIEWER_API void loadData( GLenum target, const char * arr, size_t arrSize );
45 template<typename T>
46 void loadData( GLenum target, const T * arr, size_t arrSize ) { loadData( target, (const char *)arr, sizeof( T ) * arrSize ); }
47 template<typename C>
48 void loadData( GLenum target, const C & cont ) { loadData( target, cont.data(), cont.size() ); }
49
51 MRVIEWER_API void loadDataOpt( GLenum target, bool refresh, const char * arr, size_t arrSize );
52 template<typename T>
53 void loadDataOpt( GLenum target, bool refresh, const T * arr, size_t arrSize ) { loadDataOpt( target, refresh, (const char *)arr, sizeof( T ) * arrSize ); }
54 template<typename C>
55 void loadDataOpt( GLenum target, bool refresh, const C & cont ) { loadDataOpt( target, refresh, cont.data(), cont.size() ); }
56
57private:
59 void detach_() { bufferID_ = NO_BUF; size_ = 0; }
60
61private:
62 GLuint bufferID_ = NO_BUF;
63 size_t size_ = 0;
64};
65
67class GlTexture2 : public GlTexture
68{
69public:
70 GlTexture2() : GlTexture( GL_TEXTURE_2D ){}
71
72 static Vector3i ToResolution( const Vector2i& value )
73 {
74 return Vector3i( value.x, value.y, 1 );
75 }
76private:
77 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
78};
79
81class GlTexture3 : public GlTexture
82{
83public:
84 GlTexture3() : GlTexture( GL_TEXTURE_3D ){}
85private:
86 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
87};
88
91{
92public:
93 GlTexture2DArray() : GlTexture( GL_TEXTURE_2D_ARRAY ){}
94private:
95 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
96};
97
99{
100 GLuint program_shader = 0;
101 const char * name = nullptr;
103 const char * arr = nullptr;
104 size_t arrSize = 0;
106 bool refresh = false;
107 bool forceUse = false;
108 bool isColor = false;
109};
110
111MRVIEWER_API GLint bindVertexAttribArray( const BindVertexAttribArraySettings & settings );
112
113template<typename T, template<typename, typename...> class C, typename... args>
115 const GLuint program_shader,
116 const char * name,
117 GlBuffer & buf,
118 const C<T, args...>& V,
119 int baseTypeElementsNumber,
120 bool refresh,
121 bool forceUse = false )
122{
124 {
125 .program_shader = program_shader,
126 .name = name,
127 .buf = buf,
128 .arr = (const char*)V.data(),
129 .arrSize = sizeof(T) * V.size(),
130 .baseTypeElementsNumber = baseTypeElementsNumber,
131 .refresh = refresh,
132 .forceUse = forceUse,
133 .isColor = std::is_same_v<Color, T>
134 };
135 return bindVertexAttribArray( settings );
136}
137
138template <typename T, std::size_t N>
140 const GLuint program_shader,
141 const char * name,
142 GlBuffer & buf,
143 const std::array<T, N>& V,
144 int baseTypeElementsNumber,
145 bool refresh,
146 bool forceUse = false )
147{
149 {
150 .program_shader = program_shader,
151 .name = name,
152 .buf = buf,
153 .arr = (const char*)V.data(),
154 .arrSize = sizeof(T) * N,
155 .baseTypeElementsNumber = baseTypeElementsNumber,
156 .refresh = refresh,
157 .forceUse = forceUse,
158 .isColor = std::is_same_v<Color, T>
159 };
160 return bindVertexAttribArray( settings );
161}
162
166{
167 switch ( funcType )
168 {
171 return GL_LESS;
173 return GL_NEVER;
175 return GL_LEQUAL;
177 return GL_EQUAL;
179 return GL_GEQUAL;
181 return GL_GREATER;
183 return GL_ALWAYS;
185 return GL_NOTEQUAL;
186 default:
187 return 0;
188 }
189}
190
194{
195 if ( funcType == DepthFunction::Default )
196 return GL_LEQUAL;
197 return getDepthFunctionLess( funcType );
198}
199
201class MRVIEWER_CLASS QuadTextureVertexObject
202{
203public:
205 MRVIEWER_API void gen();
207 MRVIEWER_API void bind();
209 MRVIEWER_API void del();
210private:
211 unsigned vao_{ 0 };
212 unsigned vbo_{ 0 };
213};
214
216class MRVIEWER_CLASS FramebufferData
217{
218public:
222 MRVIEWER_API void gen( const Vector2i& size, bool copyDepth, int msaaPow, bool highPrecisionDepth = false );
225 MRVIEWER_API void bind( bool clear = true, float clearDepth = 1.0f );
228 MRVIEWER_API void bindDefault();
230 MRVIEWER_API void bindTexture( bool color = true, bool depth = true );
234 MRVIEWER_API void copyTextureBindDef();
236 MRVIEWER_API void del();
238 unsigned getColorTexture() const { return resColorTexture_.getId(); }
239 unsigned getDepthTexture() const { return resDepthTexture_.getId(); }
240
241 const Vector2i& getSize() const { return size_; }
243 bool isBound() const { return isBound_; }
244
253
254 MRVIEWER_API void draw( QuadTextureVertexObject& quadObject, const DrawParams& params ) const;
255private:
256 void resize_( const Vector2i& size, int msaaPow );
257
258 bool isBound_{ false };
259 bool highPrecisionDepth_{ false };
260 unsigned mainFramebuffer_{ 0 };
261 unsigned colorRenderbuffer_{ 0 };
262 unsigned depthRenderbuffer_{ 0 };
263 unsigned copyFramebuffer_{ 0 };
264 GlTexture2 resColorTexture_;
265 GlTexture2 resDepthTexture_;
266 Vector2i size_;
267};
268
270MRVIEWER_API void bindDepthPeelingTextures( GLuint shaderId, const TransparencyMode& tMode, GLenum startGLTextureIndex );
271
274MRVIEWER_API void objectPreRenderSetup( const TransparencyMode& tMode, RenderModelPassMask desiredPass, bool deptTesting );
275MRVIEWER_API void objectPostRenderSetup( const TransparencyMode& tMode, RenderModelPassMask desiredPass, bool deptTesting );
276
277}
class for easier rendering in framebuffer texture
Definition MRRenderGLHelpers.h:217
represents OpenGL buffer owner, and allows uploading data in it remembering buffer size
Definition MRRenderGLHelpers.h:19
class for rendering simple texture
Definition MRRenderGLHelpers.h:202
auto depth(const Box< V > &box)
returns size along z axis
Definition MRBox.h:368
void bind(GLenum target)
binds current buffer to OpenGL context
WrapType
Definition MREnums.h:18
void gen(const Vector2i &size, bool copyDepth, int msaaPow, bool highPrecisionDepth=false)
void del()
removes this object
void loadData(GLenum target, const char *arr, size_t arrSize)
creates GL data buffer using given data and binds it
void bind(bool clear=true, float clearDepth=1.0f)
const char * arr
Definition MRRenderGLHelpers.h:103
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
GLint bindVertexAttribArray(const BindVertexAttribArraySettings &settings)
void loadData(GLenum target, const T *arr, size_t arrSize)
Definition MRRenderGLHelpers.h:46
void loadData(GLenum target, const C &cont)
Definition MRRenderGLHelpers.h:48
void objectPostRenderSetup(const TransparencyMode &tMode, RenderModelPassMask desiredPass, bool deptTesting)
float simpleDepth
force using simpleDepth for all fragments even if depth texture is present
Definition MRRenderGLHelpers.h:251
GlBuffer & buf
Definition MRRenderGLHelpers.h:102
void loadDataOpt(GLenum target, bool refresh, const char *arr, size_t arrSize)
binds current buffer to OpenGL context, optionally refreshing its data
unsigned getColorTexture() const
gets texture id for binding in other shaders
Definition MRRenderGLHelpers.h:238
const char * name
Definition MRRenderGLHelpers.h:101
WrapType wrap
size of the viewport that is used in draw function
Definition MRRenderGLHelpers.h:248
size_t arrSize
Definition MRRenderGLHelpers.h:104
GlBuffer(GlBuffer &&r)
Definition MRRenderGLHelpers.h:24
int getDepthFunctionLEqual(DepthFunction funcType)
Definition MRRenderGLHelpers.h:193
GlTexture2()
Definition MRRenderGLHelpers.h:70
bool isBound() const
return true if texture is bound
Definition MRRenderGLHelpers.h:243
FilterType
Definition MREnums.h:12
RenderModelPassMask
Various passes of the 3D rendering.
Definition MRRenderModelParameters.h:40
auto getId() const
Definition MRRenderGLHelpers.h:30
GlBuffer()=default
void loadDataOpt(GLenum target, bool refresh, const C &cont)
Definition MRRenderGLHelpers.h:55
GlTexture2DArray()
Definition MRRenderGLHelpers.h:93
unsigned getDepthTexture() const
Definition MRRenderGLHelpers.h:239
void gen()
generates simple quad for rendering
void draw(QuadTextureVertexObject &quadObject, const DrawParams &params) const
draws this framebuffer using quadObject
const Vector2i & getSize() const
Definition MRRenderGLHelpers.h:241
void bindDepthPeelingTextures(GLuint shaderId, const TransparencyMode &tMode, GLenum startGLTextureIndex)
helper function to bind depth and color buffers to given shader program
static Vector3i ToResolution(const Vector2i &value)
Definition MRRenderGLHelpers.h:72
void del()
removes this framebuffer
void bind()
binds simple quad vertex data
int baseTypeElementsNumber
Definition MRRenderGLHelpers.h:105
GlTexture3()
Definition MRRenderGLHelpers.h:84
DepthFunction
Definition MRIRenderObject.h:21
Vector2i size
Definition MRRenderGLHelpers.h:247
GLuint program_shader
Definition MRRenderGLHelpers.h:100
GlBuffer & operator=(const GlBuffer &)=delete
bool forceSimpleDepthDraw
filter type of underlaying textures
Definition MRRenderGLHelpers.h:250
int getDepthFunctionLess(DepthFunction funcType)
Definition MRRenderGLHelpers.h:165
~GlBuffer()
Definition MRRenderGLHelpers.h:25
bool refresh
Definition MRRenderGLHelpers.h:106
void del()
deletes the buffer
void gen()
generates new buffer
GlTexture(GLenum val)
GlBuffer(const GlBuffer &)=delete
void loadDataOpt(GLenum target, bool refresh, const T *arr, size_t arrSize)
Definition MRRenderGLHelpers.h:53
bool isColor
Definition MRRenderGLHelpers.h:108
bool forceUse
Definition MRRenderGLHelpers.h:107
void objectPreRenderSetup(const TransparencyMode &tMode, RenderModelPassMask desiredPass, bool deptTesting)
size_t size() const
Definition MRRenderGLHelpers.h:32
void bindTexture(bool color=true, bool depth=true)
marks the texture to reading
FilterType filter
wrap type of underlaying textures
Definition MRRenderGLHelpers.h:249
bool valid() const
Definition MRRenderGLHelpers.h:31
@ Clamp
Definition MREnums.h:21
@ Linear
Definition MREnums.h:13
@ NotEqual
Definition MRIRenderObject.h:28
@ Less
Definition MRIRenderObject.h:23
@ Always
Definition MRIRenderObject.h:29
@ Never
Definition MRIRenderObject.h:22
@ GreaterOrEqual
Definition MRIRenderObject.h:27
@ Default
Definition MRIRenderObject.h:30
@ Greater
Definition MRIRenderObject.h:25
@ LessOrEqual
Definition MRIRenderObject.h:26
@ Equal
Definition MRIRenderObject.h:24
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRRenderGLHelpers.h:99
Definition MRRenderGLHelpers.h:246
struct to determine transparent rendering mode
Definition MRRenderModelParameters.h:13