SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLState.h
Go to the documentation of this file.
1 /**
2  * \file SLGLState.h
3  * \brief Singleton class for global render state
4  * \date July 2014
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9  */
10 
11 #ifndef SLGLSTATE_H
12 #define SLGLSTATE_H
13 
14 #include <SL.h>
15 
16 #if defined(SL_OS_MACIOS)
17 # include <OpenGLES/ES3/gl.h>
18 # include <OpenGLES/ES3/glext.h>
19 #elif defined(SL_OS_MACOS)
20 # include <GL/gl3w.h>
21 #elif defined(SL_OS_ANDROID)
22 // https://stackoverflow.com/questions/31003863/gles-3-0-including-gl2ext-h
23 # include <GLES3/gl3.h>
24 # include <GLES2/gl2ext.h>
25 # ifndef GL_CLAMP_TO_BORDER // see #define GL_CLAMP_TO_BORDER_OES 0x812D in gl2ext.h
26 # define GL_CLAMP_TO_BORDER GL_CLAMP_TO_BORDER_OES
27 # endif
28 //# include <GLES3/gl31.h>
29 //# include <GLES3/gl3ext.h>
30 #elif defined(SL_OS_WINDOWS)
31 # include <GL/gl3w.h>
32 #elif defined(SL_OS_LINUX)
33 # include <GL/gl3w.h>
34 #elif defined(SL_EMSCRIPTEN)
35 # include <GLES3/gl3.h>
36 #else
37 # error "SL has not been ported to this OS"
38 #endif
39 
40 #include <SLVec3.h>
41 #include <SLVec4.h>
42 #include <SLMat4.h>
43 
44 class SLDrawBits;
45 class SLGLDepthBuffer;
46 class SLMaterial;
47 
48 //-----------------------------------------------------------------------------
49 static const SLint SL_MAX_LIGHTS = 8; //!< max. number of used lights
50 //-----------------------------------------------------------------------------
51 
52 // glGetError turns WebGL rendering into a slideshow, so we disable it when compiling with Emscripten
53 #if (defined(DEBUG) || defined(_DEBUG)) && !defined(SL_EMSCRIPTEN)
54 # define GET_GL_ERROR SLGLState::getGLError((const char*)__FILE__, __LINE__, false)
55 #else
56 # define GET_GL_ERROR
57 #endif
58 //-----------------------------------------------------------------------------
59 //! Singleton class holding all OpenGL states
60 /*!
61  The main purpose of the SLGLState class is to replace all the OpenGL states and
62  functionality that has been removed from the core profile of OpenGL. The core
63  profile started from OpenGL version 3.0 has e.g. no more internal matrices,
64  lighting or material states. It also has no more fixed function pipeline on the
65  GPU witch means, that core profile OpenGL only works with custom shader
66  programs written in OpenGL Shading Language (GLSL).
67  The second purpose is to concentrate OpenGL functionality and to reduce
68  redundant state changes.
69  */
70 class SLGLState
71 {
72 public:
73  //! Public static instance getter for singleton pattern
74  static SLGLState* instance()
75  {
76  if (!_instance)
77  {
78  _instance = new SLGLState();
79  return _instance;
80  }
81  else
82  return _instance;
83  }
84  static void deleteInstance(); //!< global destruction
85  void onInitialize(const SLCol4f& clearColor); //!< On init GL
86  void initAll(); //! Init all states
87 
88  // matrices
89  SLMat4f modelMatrix; //!< matrix for model to world transform
90  SLMat4f projectionMatrix; //!< matrix for projection transform
91  SLMat4f viewMatrix; //!< matrix for the active cameras view transform
92  SLMat4f textureMatrix; //!< matrix for the texture transform
93 
94  // getters
95  inline bool hasMultiSampling() const { return _multiSampleSamples > 0; }
96 
97  // misc.
98  void unbindAnythingAndFlush(); //!< finishes all GL commands
99  SLbool pixelFormatIsSupported(SLint pixelFormat);
100  void readPixels(void* buffer);
101 
102  // state setters
103  void depthTest(SLbool state);
104  void depthMask(SLbool state);
105  void depthFunc(SLenum func);
106  void cullFace(SLbool state);
107  void blend(SLbool state);
108  void blendFunc(SLenum blendFuncSFactor, SLenum blendFuncDFactor);
109  void multiSample(SLbool state);
110  void polygonLine(SLbool state);
111  void polygonOffsetPoint(SLbool enabled, SLfloat factor = -1.0f, SLfloat units = -1.0f);
112  void polygonOffsetLine(SLbool enabled, SLfloat factor = -1.0f, SLfloat units = -1.0f);
113  void polygonOffsetFill(SLbool enabled, SLfloat factor = -1.0f, SLfloat units = -1.0f);
114  void viewport(SLint x, SLint y, SLsizei width, SLsizei height);
115  void colorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a);
116  void useProgram(SLuint progID);
117  void bindTexture(SLenum target, SLuint textureID);
118  void activeTexture(SLenum textureUnit);
119  void clearColor(const SLCol4f& c);
121  void clearColorBuffer() { glClear(GL_COLOR_BUFFER_BIT); }
122  void clearDepthBuffer() { glClear(GL_DEPTH_BUFFER_BIT); }
123  void clearColorDepthBuffer() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }
124 
125  // state getters
126  SLbool blend() const { return _blend; }
129  SLfloat glVersionNOf() const { return _glVersionNOf; }
134  SLbool glIsES() const { return _glIsES2 || _glIsES3; }
135  SLbool glIsES2() const { return _glIsES2; }
136  SLbool glIsES3() const { return _glIsES3; }
137  SLint glMaxTexUnits() const { return _glMaxTexUnits; }
138  SLint glMaxTexSize() const { return _glMaxTexSize; }
139  SLbool glHasGeometryShaders() const { return (_glIsES3 && _glVersionNOf >= 3.2f) ||
140  (!glIsES() && _glVersionNOf >= 4.1f); }
141  SLbool hasExtension(const SLstring& e) { return _glExtensions.find(e) != string::npos; }
142  SLVec4i viewport() { return _viewport; }
144  {
145  SLMat4f vpm;
149  (SLfloat)_viewport.w);
150  return vpm;
151  }
153 
154  //! Checks if an OpenGL error occurred
155  static void getGLError(const char* file, int line, bool quit);
156 
159 
160 private:
161  SLGLState(); //!< private onetime constructor
162  ~SLGLState(); //!< destruction in ~SLScene
163 
164  static SLGLState* _instance; //!< global singleton object
165 
166  SLbool _isInitialized; //!< flag for first init
167 
168  SLstring _glVersion; //!< OpenGL Version string
169  SLstring _glVersionNO; //!< OpenGL Version number string
170  SLfloat _glVersionNOf; //!< OpenGL Version number as float
171  SLstring _glVendor; //!< OpenGL Vendor string
172  SLstring _glRenderer; //!< OpenGL Renderer string
173  SLstring _glSLVersion; //!< GLSL Version string
174  SLstring _glSLVersionNO; //!< GLSL Version number string
175  SLstring _glExtensions; //!< OpenGL extensions string
176  SLbool _glIsES2; //!< Flag if OpenGL ES2
177  SLbool _glIsES3; //!< Flag if OpenGL ES3
178  SLint _glMaxTexUnits; //!< glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits);
179  SLint _glMaxTexSize; //!< glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize);
180 
181  // read/write states
182  SLbool _blend; //!< blending default false;
183  SLenum _blendFuncSfactor; //!< blend function source factor enum
184  SLenum _blendFuncDfactor; //!< blend function destination factor enum
185  SLbool _depthTest; //!< GL_DEPTH_TEST state
186  SLbool _depthMask; //!< glDepthMask state
187  SLenum _depthFunc; //!< depth buffer comparison function
188  SLbool _cullFace; //!< Face culling state
189  SLbool _multisample; //!< Multisampling state
190  SLint _multiSampleSamples; //!< NO. of multisampling samples
191  SLbool _polygonLine; //!< Line polygon state
192  SLbool _polygonOffsetPointEnabled; //!< GL_POLYGON_OFFSET_POINT state enabled
193  SLbool _polygonOffsetLineEnabled; //!< GL_POLYGON_OFFSET_LINE state enabled
194  SLbool _polygonOffsetFillEnabled; //!< GL_POLYGON_OFFSET_FILL state enabled
195  SLVec4i _viewport; //!< viewport size (x,y,w,h) of the framebuffer
196  SLCol4f _clearColor; //!< clear color
197 
198  // states
199  SLuint _programID; //!< current shader program id
200  SLenum _textureUnit; //!< current texture unit
201  SLenum _textureTarget; //!< current texture target
202  SLuint _textureID; //!< current texture id
203  GLboolean _colorMaskR; //!< current color mask for R
204  GLboolean _colorMaskG; //!< current color mask for G
205  GLboolean _colorMaskB; //!< current color mask for B
206  GLboolean _colorMaskA; //!< current color mask for A
207 
208  SLVstring errorTexts; //!< vector for error texts collected in getGLError
209  SLVlong errorCounts; //!< vector for counts for the corresponding errorTexts
210 
212 };
213 //-----------------------------------------------------------------------------
214 #endif
float SLfloat
Definition: SL.h:173
unsigned int SLenum
Definition: SL.h:176
vector< SLlong > SLVlong
Definition: SL.h:198
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
vector< SLstring > SLVstring
Definition: SL.h:201
int SLsizei
Definition: SL.h:172
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
static const SLint SL_MAX_LIGHTS
max. number of used lights
Definition: SLGLState.h:49
Drawing states stored in the bits of an unsigned int.
Definition: SLDrawBits.h:42
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
void clearColor(const SLCol4f &c)
Definition: SLGLState.cpp:157
static void deleteInstance()
global destruction
Definition: SLGLState.cpp:27
SLMaterial * currentMaterial()
Definition: SLGLState.h:152
static SLGLState * _instance
global singleton object
Definition: SLGLState.h:164
SLbool _depthMask
glDepthMask state
Definition: SLGLState.h:186
SLMat4f textureMatrix
matrix for the texture transform
Definition: SLGLState.h:92
SLVec4i _viewport
viewport size (x,y,w,h) of the framebuffer
Definition: SLGLState.h:195
void activeTexture(SLenum textureUnit)
Definition: SLGLState.cpp:445
SLVec4i viewport()
Definition: SLGLState.h:142
SLuint _textureID
current texture id
Definition: SLGLState.h:202
SLstring glVersionNO()
Definition: SLGLState.h:128
SLMat4f modelMatrix
Init all states.
Definition: SLGLState.h:89
void polygonOffsetPoint(SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
Definition: SLGLState.cpp:311
void depthMask(SLbool state)
Definition: SLGLState.cpp:190
SLCol4f _clearColor
clear color
Definition: SLGLState.h:196
SLbool _depthTest
GL_DEPTH_TEST state.
Definition: SLGLState.h:185
void multiSample(SLbool state)
Definition: SLGLState.cpp:267
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
void polygonOffsetLine(SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
Definition: SLGLState.cpp:335
~SLGLState()
destruction in ~SLScene
Definition: SLGLState.cpp:123
SLGLState()
private onetime constructor
Definition: SLGLState.cpp:35
SLbool _multisample
Multisampling state.
Definition: SLGLState.h:189
SLbool _isInitialized
flag for first init
Definition: SLGLState.h:166
SLstring glVersion()
Definition: SLGLState.h:127
SLbool _glIsES2
Flag if OpenGL ES2.
Definition: SLGLState.h:176
SLMat4f viewMatrix
matrix for the active cameras view transform
Definition: SLGLState.h:91
SLstring _glSLVersion
GLSL Version string.
Definition: SLGLState.h:173
void unbindAnythingAndFlush()
finishes all GL commands
Definition: SLGLState.cpp:465
SLVstring errorTexts
vector for error texts collected in getGLError
Definition: SLGLState.h:208
SLenum _textureTarget
current texture target
Definition: SLGLState.h:201
GLboolean _colorMaskA
current color mask for A
Definition: SLGLState.h:206
SLenum _blendFuncSfactor
blend function source factor enum
Definition: SLGLState.h:183
void initAll()
Definition: SLGLState.cpp:42
void blendFunc(SLenum blendFuncSFactor, SLenum blendFuncDFactor)
Sets new blend function source and destination factors.
Definition: SLGLState.cpp:250
void colorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
Definition: SLGLState.cpp:394
SLbool hasExtension(const SLstring &e)
Definition: SLGLState.h:141
SLenum _blendFuncDfactor
blend function destination factor enum
Definition: SLGLState.h:184
SLstring getSLVersionNO()
Returns the OpenGL Shading Language version number as a string.
Definition: SLGLState.cpp:575
SLstring _glVendor
OpenGL Vendor string.
Definition: SLGLState.h:171
GLboolean _colorMaskR
current color mask for R
Definition: SLGLState.h:203
SLstring _glVersionNO
OpenGL Version number string.
Definition: SLGLState.h:169
SLstring _glSLVersionNO
GLSL Version number string.
Definition: SLGLState.h:174
SLstring _glExtensions
OpenGL extensions string.
Definition: SLGLState.h:175
SLbool _polygonOffsetFillEnabled
GL_POLYGON_OFFSET_FILL state enabled.
Definition: SLGLState.h:194
SLstring _glRenderer
OpenGL Renderer string.
Definition: SLGLState.h:172
SLbool blend() const
Definition: SLGLState.h:126
SLbool _blend
blending default false;
Definition: SLGLState.h:182
SLbool glIsES3() const
Definition: SLGLState.h:136
void clearDepthBuffer()
Definition: SLGLState.h:122
SLint glMaxTexUnits() const
Definition: SLGLState.h:137
SLbool _polygonOffsetLineEnabled
GL_POLYGON_OFFSET_LINE state enabled.
Definition: SLGLState.h:193
SLbool glHasGeometryShaders() const
Definition: SLGLState.h:139
SLstring glRenderer()
Definition: SLGLState.h:131
SLstring getGLVersionNO()
Returns the OpenGL version number as a string.
Definition: SLGLState.cpp:552
SLenum _textureUnit
current texture unit
Definition: SLGLState.h:200
bool hasMultiSampling() const
Definition: SLGLState.h:95
SLint _glMaxTexUnits
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits);
Definition: SLGLState.h:178
SLbool pixelFormatIsSupported(SLint pixelFormat)
Returns true if the according GL pixel format is valid in the GL context.
Definition: SLGLState.cpp:588
void depthFunc(SLenum func)
Definition: SLGLState.cpp:205
SLbool _polygonOffsetPointEnabled
GL_POLYGON_OFFSET_POINT state enabled.
Definition: SLGLState.h:192
SLstring glVendor()
Definition: SLGLState.h:130
SLfloat glVersionNOf() const
Definition: SLGLState.h:129
SLenum _depthFunc
depth buffer comparison function
Definition: SLGLState.h:187
SLbool glIsES2() const
Definition: SLGLState.h:135
void bindTexture(SLenum target, SLuint textureID)
Definition: SLGLState.cpp:423
SLMat4f viewportMatrix()
Definition: SLGLState.h:143
SLfloat _glVersionNOf
OpenGL Version number as float.
Definition: SLGLState.h:170
static void getGLError(const char *file, int line, bool quit)
Checks if an OpenGL error occurred.
Definition: SLGLState.cpp:484
void polygonOffsetFill(SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
Definition: SLGLState.cpp:359
SLbool _glIsES3
Flag if OpenGL ES3.
Definition: SLGLState.h:177
void onInitialize(const SLCol4f &clearColor)
On init GL.
Definition: SLGLState.cpp:137
SLbool _polygonLine
Line polygon state.
Definition: SLGLState.h:191
SLstring glSLVersionNO()
Definition: SLGLState.h:133
SLMat4f projectionMatrix
matrix for projection transform
Definition: SLGLState.h:90
SLVlong errorCounts
vector for counts for the corresponding errorTexts
Definition: SLGLState.h:209
SLstring _glVersion
OpenGL Version string.
Definition: SLGLState.h:168
void useProgram(SLuint progID)
Definition: SLGLState.cpp:410
SLMaterial * _currentMaterial
Definition: SLGLState.h:211
void polygonLine(SLbool state)
Definition: SLGLState.cpp:290
SLint _glMaxTexSize
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize);
Definition: SLGLState.h:179
SLbool _cullFace
Face culling state.
Definition: SLGLState.h:188
SLint _multiSampleSamples
NO. of multisampling samples.
Definition: SLGLState.h:190
SLbool glIsES() const
Definition: SLGLState.h:134
void readPixels(void *buffer)
Reads the front framebuffer pixels into the passed buffer.
Definition: SLGLState.cpp:639
void clearColorDepthBuffer()
Definition: SLGLState.h:123
GLboolean _colorMaskB
current color mask for B
Definition: SLGLState.h:205
SLstring glSLVersion()
Definition: SLGLState.h:132
SLint glMaxTexSize() const
Definition: SLGLState.h:138
void currentMaterial(SLMaterial *mat)
Definition: SLGLState.h:120
void clearColorBuffer()
Definition: SLGLState.h:121
GLboolean _colorMaskG
current color mask for G
Definition: SLGLState.h:204
void cullFace(SLbool state)
Definition: SLGLState.cpp:219
void depthTest(SLbool state)
Definition: SLGLState.cpp:172
SLuint _programID
current shader program id
Definition: SLGLState.h:199
void viewport(T x, T y, T ww, T wh, T n=0.0f, T f=1.0f)
Defines the viewport matrix.
Definition: SLMat4.h:930
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
T w
Definition: SLVec4.h:32
T z
Definition: SLVec4.h:32
T y
Definition: SLVec4.h:32
T x
Definition: SLVec4.h:32