MeshLib Documentation
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{
13
14// represents OpenGL buffer owner, and allows uploading data in it remembering buffer size
16{
17 constexpr static GLuint NO_BUF = 0;
18public:
19 GlBuffer() = default;
20 GlBuffer( const GlBuffer & ) = delete;
21 GlBuffer( GlBuffer && r ) : bufferID_( r.bufferID_ ), size_( r.size_ ) { r.detach_(); }
22 ~GlBuffer() { del(); }
23
24 GlBuffer& operator =( const GlBuffer & ) = delete;
25 GlBuffer& operator =( GlBuffer && r ) { del(); bufferID_ = r.bufferID_; size_ = r.size_; r.detach_(); return * this; }
26
27 auto getId() const { return bufferID_; }
28 bool valid() const { return bufferID_ != NO_BUF; }
29 size_t size() const { return size_; }
30
31 // generates new buffer
32 MRVIEWER_API void gen();
33
34 // deletes the buffer
35 MRVIEWER_API void del();
36
37 // binds current buffer to OpenGL context
38 MRVIEWER_API void bind( GLenum target );
39
40 // creates GL data buffer using given data and binds it
41 MRVIEWER_API void loadData( GLenum target, const char * arr, size_t arrSize );
42 template<typename T>
43 void loadData( GLenum target, const T * arr, size_t arrSize ) { loadData( target, (const char *)arr, sizeof( T ) * arrSize ); }
44 template<typename C>
45 void loadData( GLenum target, const C & cont ) { loadData( target, cont.data(), cont.size() ); }
46
47 // binds current buffer to OpenGL context, optionally refreshing its data
48 MRVIEWER_API void loadDataOpt( GLenum target, bool refresh, const char * arr, size_t arrSize );
49 template<typename T>
50 void loadDataOpt( GLenum target, bool refresh, const T * arr, size_t arrSize ) { loadDataOpt( target, refresh, (const char *)arr, sizeof( T ) * arrSize ); }
51 template<typename C>
52 void loadDataOpt( GLenum target, bool refresh, const C & cont ) { loadDataOpt( target, refresh, cont.data(), cont.size() ); }
53
54private:
56 void detach_() { bufferID_ = NO_BUF; size_ = 0; }
57
58private:
59 GLuint bufferID_ = NO_BUF;
60 size_t size_ = 0;
61};
62
63// represents OpenGL 2D texture owner, and allows uploading data in it remembering texture size
64class GlTexture2 : public GlTexture
65{
66public:
67 GlTexture2() : GlTexture( GL_TEXTURE_2D ){}
68
69 static Vector3i ToResolution( const Vector2i& value )
70 {
71 return Vector3i( value.x, value.y, 1 );
72 }
73private:
74 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
75};
76
77// represents OpenGL 3D texture owner, and allows uploading data in it remembering texture size
78class GlTexture3 : public GlTexture
79{
80public:
81 GlTexture3() : GlTexture( GL_TEXTURE_3D ){}
82private:
83 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
84};
85
86// represents OpenGL array texture 2D owner, and allows uploading data in it remembering texture size
88{
89public:
90 GlTexture2DArray() : GlTexture( GL_TEXTURE_2D_ARRAY ){}
91private:
92 MRVIEWER_API virtual void texImage_( const Settings& settings, const char* arr ) override;
93};
94
96{
97 GLuint program_shader = 0;
98 const char * name = nullptr;
100 const char * arr = nullptr;
101 size_t arrSize = 0;
103 bool refresh = false;
104 bool forceUse = false;
105 bool isColor = false;
106};
107
108MRVIEWER_API GLint bindVertexAttribArray( const BindVertexAttribArraySettings & settings );
109
110template<typename T, template<typename, typename...> class C, typename... args>
112 const GLuint program_shader,
113 const char * name,
114 GlBuffer & buf,
115 const C<T, args...>& V,
116 int baseTypeElementsNumber,
117 bool refresh,
118 bool forceUse = false )
119{
121 {
122 .program_shader = program_shader,
123 .name = name,
124 .buf = buf,
125 .arr = (const char*)V.data(),
126 .arrSize = sizeof(T) * V.size(),
127 .baseTypeElementsNumber = baseTypeElementsNumber,
128 .refresh = refresh,
129 .forceUse = forceUse,
130 .isColor = std::is_same_v<Color, T>
131 };
132 return bindVertexAttribArray( settings );
133}
134
135template <typename T, std::size_t N>
137 const GLuint program_shader,
138 const char * name,
139 GlBuffer & buf,
140 const std::array<T, N>& V,
141 int baseTypeElementsNumber,
142 bool refresh,
143 bool forceUse = false )
144{
146 {
147 .program_shader = program_shader,
148 .name = name,
149 .buf = buf,
150 .arr = (const char*)V.data(),
151 .arrSize = sizeof(T) * N,
152 .baseTypeElementsNumber = baseTypeElementsNumber,
153 .refresh = refresh,
154 .forceUse = forceUse,
155 .isColor = std::is_same_v<Color, T>
156 };
157 return bindVertexAttribArray( settings );
158}
159
160// return real GL value for DepthFunction
161// default is less
163{
164 switch ( funcType )
165 {
168 return GL_LESS;
170 return GL_NEVER;
172 return GL_LEQUAL;
174 return GL_EQUAL;
176 return GL_GEQUAL;
178 return GL_GREATER;
180 return GL_ALWAYS;
182 return GL_NOTEQUAL;
183 default:
184 return 0;
185 }
186}
187
188// return real GL value for DepthFunction
189// default is less or equal
191{
192 if ( funcType == DepthFunction::Default )
193 return GL_LEQUAL;
194 return getDepthFunctionLess( funcType );
195}
196
197// class for easier rendering in framebuffer texture
198class MRVIEWER_CLASS FramebufferData
199{
200public:
201 // generates framebuffer and associated data
202 // msaaPow - 2^msaaPow samples, msaaPow < 0 - use same default amount of samples
203 // to resize: del(); gen( newSize, msaaPow );
204 MRVIEWER_API void gen( const Vector2i& size, int msaaPow );
205 // binds this framebuffer as main rendering target
206 // clears it if `clear` flag is set
207 MRVIEWER_API void bind( bool clear = true );
208 // binds default framebuffer (and read/draw framebuffers)
209 // make sure to bind correct framebuffer `getViewerInstance().bindSceneTexture( true )`
210 MRVIEWER_API void bindDefault();
211 // marks the texture to reading
212 MRVIEWER_API void bindTexture();
213 // copies picture rendered in this framebuffer to associated texutre for further use
214 // and binds default framebuffer (and read/draw framebuffers)
215 // make sure to bind correct framebuffer afterwards
216 MRVIEWER_API void copyTextureBindDef();
217 // removes this framebuffer
218 MRVIEWER_API void del();
219 // gets texture id for binding in other shaders
220 unsigned getTexture() const { return resTexture_.getId(); }
221
222 const Vector2i& getSize() const { return size_; }
223private:
224 void resize_( const Vector2i& size, int msaaPow );
225
226 unsigned mainFramebuffer_{ 0 };
227 unsigned colorRenderbuffer_{ 0 };
228 unsigned depthRenderbuffer_{ 0 };
229 unsigned copyFramebuffer_{ 0 };
230 GlTexture2 resTexture_;
231 Vector2i size_;
232};
233
234// class for rendering simple texture
235class MRVIEWER_CLASS QuadTextureVertexObject
236{
237public:
238 // generates simple quad for rendering
239 MRVIEWER_API void gen();
240 // binds simple quad vertex data
241 MRVIEWER_API void bind();
242 // removes this object
243 MRVIEWER_API void del();
244private:
245 unsigned vao_;
246 unsigned vbo_;
247};
248
249} //namespace MR
Definition MRRenderGLHelpers.h:199
MRVIEWER_API void gen(const Vector2i &size, int msaaPow)
MRVIEWER_API void bindDefault()
MRVIEWER_API void copyTextureBindDef()
const Vector2i & getSize() const
Definition MRRenderGLHelpers.h:222
MRVIEWER_API void bind(bool clear=true)
unsigned getTexture() const
Definition MRRenderGLHelpers.h:220
MRVIEWER_API void bindTexture()
MRVIEWER_API void del()
Definition MRRenderGLHelpers.h:16
MRVIEWER_API void gen()
MRVIEWER_API void loadData(GLenum target, const char *arr, size_t arrSize)
MRVIEWER_API void del()
void loadData(GLenum target, const T *arr, size_t arrSize)
Definition MRRenderGLHelpers.h:43
void loadData(GLenum target, const C &cont)
Definition MRRenderGLHelpers.h:45
GlBuffer(GlBuffer &&r)
Definition MRRenderGLHelpers.h:21
auto getId() const
Definition MRRenderGLHelpers.h:27
GlBuffer()=default
void loadDataOpt(GLenum target, bool refresh, const C &cont)
Definition MRRenderGLHelpers.h:52
GlBuffer & operator=(const GlBuffer &)=delete
~GlBuffer()
Definition MRRenderGLHelpers.h:22
GlBuffer(const GlBuffer &)=delete
void loadDataOpt(GLenum target, bool refresh, const T *arr, size_t arrSize)
Definition MRRenderGLHelpers.h:50
size_t size() const
Definition MRRenderGLHelpers.h:29
MRVIEWER_API void loadDataOpt(GLenum target, bool refresh, const char *arr, size_t arrSize)
MRVIEWER_API void bind(GLenum target)
bool valid() const
Definition MRRenderGLHelpers.h:28
Definition MRRenderGLHelpers.h:88
GlTexture2DArray()
Definition MRRenderGLHelpers.h:90
Definition MRRenderGLHelpers.h:65
GlTexture2()
Definition MRRenderGLHelpers.h:67
static Vector3i ToResolution(const Vector2i &value)
Definition MRRenderGLHelpers.h:69
Definition MRRenderGLHelpers.h:79
GlTexture3()
Definition MRRenderGLHelpers.h:81
Definition MRGLTexture.h:17
Definition MRRenderGLHelpers.h:236
MRVIEWER_API void gen()
MRVIEWER_API void del()
MRVIEWER_API void bind()
Definition MRCameraOrientationPlugin.h:8
MRVIEWER_API GLint bindVertexAttribArray(const BindVertexAttribArraySettings &settings)
int getDepthFunctionLEqual(DepthFunction funcType)
Definition MRRenderGLHelpers.h:190
DepthFunction
Definition MRIRenderObject.h:18
int getDepthFunctionLess(DepthFunction funcType)
Definition MRRenderGLHelpers.h:162
Definition MRRenderGLHelpers.h:96
const char * arr
Definition MRRenderGLHelpers.h:100
GlBuffer & buf
Definition MRRenderGLHelpers.h:99
const char * name
Definition MRRenderGLHelpers.h:98
size_t arrSize
Definition MRRenderGLHelpers.h:101
int baseTypeElementsNumber
Definition MRRenderGLHelpers.h:102
GLuint program_shader
Definition MRRenderGLHelpers.h:97
bool refresh
Definition MRRenderGLHelpers.h:103
bool isColor
Definition MRRenderGLHelpers.h:105
bool forceUse
Definition MRRenderGLHelpers.h:104