SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLGLState Class Reference

Singleton class holding all OpenGL states. More...

#include <SLGLState.h>

Public Member Functions

void onInitialize (const SLCol4f &clearColor)
 On init GL. More...
 
void initAll ()
 
bool hasMultiSampling () const
 
void unbindAnythingAndFlush ()
 finishes all GL commands More...
 
SLbool pixelFormatIsSupported (SLint pixelFormat)
 Returns true if the according GL pixel format is valid in the GL context. More...
 
void readPixels (void *buffer)
 Reads the front framebuffer pixels into the passed buffer. More...
 
void depthTest (SLbool state)
 
void depthMask (SLbool state)
 
void depthFunc (SLenum func)
 
void cullFace (SLbool state)
 
void blend (SLbool state)
 
void blendFunc (SLenum blendFuncSFactor, SLenum blendFuncDFactor)
 Sets new blend function source and destination factors. More...
 
void multiSample (SLbool state)
 
void polygonLine (SLbool state)
 
void polygonOffsetPoint (SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
 
void polygonOffsetLine (SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
 
void polygonOffsetFill (SLbool enabled, SLfloat factor=-1.0f, SLfloat units=-1.0f)
 
void viewport (SLint x, SLint y, SLsizei width, SLsizei height)
 
void colorMask (GLboolean r, GLboolean g, GLboolean b, GLboolean a)
 
void useProgram (SLuint progID)
 
void bindTexture (SLenum target, SLuint textureID)
 
void activeTexture (SLenum textureUnit)
 
void clearColor (const SLCol4f &c)
 
void currentMaterial (SLMaterial *mat)
 
void clearColorBuffer ()
 
void clearDepthBuffer ()
 
void clearColorDepthBuffer ()
 
SLbool blend () const
 
SLstring glVersion ()
 
SLstring glVersionNO ()
 
SLfloat glVersionNOf () const
 
SLstring glVendor ()
 
SLstring glRenderer ()
 
SLstring glSLVersion ()
 
SLstring glSLVersionNO ()
 
SLbool glIsES () const
 
SLbool glIsES2 () const
 
SLbool glIsES3 () const
 
SLint glMaxTexUnits () const
 
SLint glMaxTexSize () const
 
SLbool glHasGeometryShaders () const
 
SLbool hasExtension (const SLstring &e)
 
SLVec4i viewport ()
 
SLMat4f viewportMatrix ()
 
SLMaterialcurrentMaterial ()
 
SLstring getGLVersionNO ()
 Returns the OpenGL version number as a string. More...
 
SLstring getSLVersionNO ()
 Returns the OpenGL Shading Language version number as a string. More...
 

Static Public Member Functions

static SLGLStateinstance ()
 Public static instance getter for singleton pattern. More...
 
static void deleteInstance ()
 global destruction More...
 
static void getGLError (const char *file, int line, bool quit)
 Checks if an OpenGL error occurred. More...
 

Public Attributes

SLMat4f modelMatrix
 Init all states. More...
 
SLMat4f projectionMatrix
 matrix for projection transform More...
 
SLMat4f viewMatrix
 matrix for the active cameras view transform More...
 
SLMat4f textureMatrix
 matrix for the texture transform More...
 

Private Member Functions

 SLGLState ()
 private onetime constructor More...
 
 ~SLGLState ()
 destruction in ~SLScene More...
 

Private Attributes

SLbool _isInitialized
 flag for first init More...
 
SLstring _glVersion
 OpenGL Version string. More...
 
SLstring _glVersionNO
 OpenGL Version number string. More...
 
SLfloat _glVersionNOf
 OpenGL Version number as float. More...
 
SLstring _glVendor
 OpenGL Vendor string. More...
 
SLstring _glRenderer
 OpenGL Renderer string. More...
 
SLstring _glSLVersion
 GLSL Version string. More...
 
SLstring _glSLVersionNO
 GLSL Version number string. More...
 
SLstring _glExtensions
 OpenGL extensions string. More...
 
SLbool _glIsES2
 Flag if OpenGL ES2. More...
 
SLbool _glIsES3
 Flag if OpenGL ES3. More...
 
SLint _glMaxTexUnits
 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits); More...
 
SLint _glMaxTexSize
 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize); More...
 
SLbool _blend
 blending default false; More...
 
SLenum _blendFuncSfactor
 blend function source factor enum More...
 
SLenum _blendFuncDfactor
 blend function destination factor enum More...
 
SLbool _depthTest
 GL_DEPTH_TEST state. More...
 
SLbool _depthMask
 glDepthMask state More...
 
SLenum _depthFunc
 depth buffer comparison function More...
 
SLbool _cullFace
 Face culling state. More...
 
SLbool _multisample
 Multisampling state. More...
 
SLint _multiSampleSamples
 NO. of multisampling samples. More...
 
SLbool _polygonLine
 Line polygon state. More...
 
SLbool _polygonOffsetPointEnabled
 GL_POLYGON_OFFSET_POINT state enabled. More...
 
SLbool _polygonOffsetLineEnabled
 GL_POLYGON_OFFSET_LINE state enabled. More...
 
SLbool _polygonOffsetFillEnabled
 GL_POLYGON_OFFSET_FILL state enabled. More...
 
SLVec4i _viewport
 viewport size (x,y,w,h) of the framebuffer More...
 
SLCol4f _clearColor
 clear color More...
 
SLuint _programID
 current shader program id More...
 
SLenum _textureUnit
 current texture unit More...
 
SLenum _textureTarget
 current texture target More...
 
SLuint _textureID
 current texture id More...
 
GLboolean _colorMaskR
 current color mask for R More...
 
GLboolean _colorMaskG
 current color mask for G More...
 
GLboolean _colorMaskB
 current color mask for B More...
 
GLboolean _colorMaskA
 current color mask for A More...
 
SLVstring errorTexts
 vector for error texts collected in getGLError More...
 
SLVlong errorCounts
 vector for counts for the corresponding errorTexts More...
 
SLMaterial_currentMaterial
 

Static Private Attributes

static SLGLState_instance = nullptr
 global singleton object More...
 

Detailed Description

Singleton class holding all OpenGL states.

The main purpose of the SLGLState class is to replace all the OpenGL states and functionality that has been removed from the core profile of OpenGL. The core profile started from OpenGL version 3.0 has e.g. no more internal matrices, lighting or material states. It also has no more fixed function pipeline on the GPU witch means, that core profile OpenGL only works with custom shader programs written in OpenGL Shading Language (GLSL). The second purpose is to concentrate OpenGL functionality and to reduce redundant state changes.

Definition at line 70 of file SLGLState.h.

Constructor & Destructor Documentation

◆ SLGLState()

SLGLState::SLGLState ( )
private

private onetime constructor

Private constructor should be called only once for a singleton class.

Definition at line 35 of file SLGLState.cpp.

36 {
37  initAll();
38 }
void initAll()
Definition: SLGLState.cpp:42

◆ ~SLGLState()

SLGLState::~SLGLState ( )
private

destruction in ~SLScene

The destructor only empties the stacks

Definition at line 123 of file SLGLState.cpp.

124 {
125 #if _DEBUG
126  SL_LOG("--------------------- Collected OpenGL Error: --------------------");
127  SL_LOG("Destructor : ~SLGLState");
128  for (size_t i = 0; i < errorTexts.size(); ++i)
129  SL_LOG("(%04d) %s", errorCounts[i], errorTexts[i].c_str());
130  if (errorTexts.size() == 0) SL_LOG("None");
131  SL_LOG("------------------------------------------------------------------");
132 #endif
133 }
#define SL_LOG(...)
Definition: SL.h:233
SLVstring errorTexts
vector for error texts collected in getGLError
Definition: SLGLState.h:208
SLVlong errorCounts
vector for counts for the corresponding errorTexts
Definition: SLGLState.h:209

Member Function Documentation

◆ activeTexture()

void SLGLState::activeTexture ( SLenum  textureUnit)

SLGLState::activeTexture sets the current active texture unit

Definition at line 445 of file SLGLState.cpp.

446 {
447  if (((SLint)textureUnit - GL_TEXTURE0) > _glMaxTexUnits)
448  SL_LOG("******* To many texture units: %i used of %i",
449  (SLint)textureUnit - GL_TEXTURE0,
451 
452  assert((textureUnit - GL_TEXTURE0) <= _glMaxTexUnits &&
453  "To many texture units!");
454 
455  glActiveTexture(textureUnit);
456  _textureUnit = textureUnit;
457 
458  GET_GL_ERROR;
459 }
int SLint
Definition: SL.h:170
#define GET_GL_ERROR
Definition: SLGLState.h:56
SLenum _textureUnit
current texture unit
Definition: SLGLState.h:200
SLint _glMaxTexUnits
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits);
Definition: SLGLState.h:178

◆ bindTexture()

void SLGLState::bindTexture ( SLenum  target,
SLuint  textureID 
)

SLGLState::bindTexture sets the current active texture.

Definition at line 423 of file SLGLState.cpp.

424 {
425  // (luc) If we call glActiveTexture and glBindTexture from outside,
426  // This will lead to problems as the global state in SLGLState will not be
427  // equivalent to the OpenGL state.
428  // We should solve this by querying opengl for the last bound texture.
429  // glGetIntegeriv(GL_ACTIVE_TEXTURE, active_texture)
430  // glGetIntegeriv(GL_TEXTURE_BINDING_2D, textureID)
431 
432  // if (target != _textureTarget || textureID != _textureID)
433  {
434  glBindTexture(target, textureID);
435 
436  _textureTarget = target;
437  _textureID = textureID;
438 
439  GET_GL_ERROR;
440  }
441 }
SLuint _textureID
current texture id
Definition: SLGLState.h:202
SLenum _textureTarget
current texture target
Definition: SLGLState.h:201

◆ blend() [1/2]

SLbool SLGLState::blend ( ) const
inline

Definition at line 126 of file SLGLState.h.

126 { return _blend; }
SLbool _blend
blending default false;
Definition: SLGLState.h:182

◆ blend() [2/2]

void SLGLState::blend ( SLbool  stateNew)

SLGLState::blend enables or disables alpha blending but only if the state really changes.

Definition at line 236 of file SLGLState.cpp.

237 {
238  if (_blend != stateNew)
239  {
240  if (stateNew)
241  glEnable(GL_BLEND);
242  else
243  glDisable(GL_BLEND);
244  _blend = stateNew;
245  GET_GL_ERROR;
246  }
247 }

◆ blendFunc()

void SLGLState::blendFunc ( SLenum  blendFuncSFactor,
SLenum  blendFuncDFactor 
)

Sets new blend function source and destination factors.

Definition at line 250 of file SLGLState.cpp.

252 {
253  if (_blendFuncSfactor != newBlendFuncSFactor ||
254  _blendFuncDfactor != newBlendFuncDFactor)
255  {
256  glBlendFunc(newBlendFuncSFactor, newBlendFuncDFactor);
257  _blendFuncSfactor = newBlendFuncSFactor;
258  _blendFuncDfactor = newBlendFuncDFactor;
259  GET_GL_ERROR;
260  }
261 }
SLenum _blendFuncSfactor
blend function source factor enum
Definition: SLGLState.h:183
SLenum _blendFuncDfactor
blend function destination factor enum
Definition: SLGLState.h:184

◆ clearColor()

void SLGLState::clearColor ( const SLCol4f c)

Definition at line 157 of file SLGLState.cpp.

158 {
159  if (_clearColor != newColor)
160  {
161  glClearColor(newColor.r, newColor.g, newColor.b, newColor.a);
162  _clearColor = newColor;
163 
164  GET_GL_ERROR;
165  }
166 }
SLCol4f _clearColor
clear color
Definition: SLGLState.h:196

◆ clearColorBuffer()

void SLGLState::clearColorBuffer ( )
inline

Definition at line 121 of file SLGLState.h.

121 { glClear(GL_COLOR_BUFFER_BIT); }

◆ clearColorDepthBuffer()

void SLGLState::clearColorDepthBuffer ( )
inline

Definition at line 123 of file SLGLState.h.

123 { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }

◆ clearDepthBuffer()

void SLGLState::clearDepthBuffer ( )
inline

Definition at line 122 of file SLGLState.h.

122 { glClear(GL_DEPTH_BUFFER_BIT); }

◆ colorMask()

void SLGLState::colorMask ( GLboolean  r,
GLboolean  g,
GLboolean  b,
GLboolean  a 
)

SLGLState::colorMask sets the OpenGL colorMask for framebuffer masking

Definition at line 394 of file SLGLState.cpp.

395 {
396  if (r != _colorMaskR || g != _colorMaskG || b != _colorMaskB || a != _colorMaskA)
397  {
398  glColorMask(r, g, b, a);
399  _colorMaskR = r;
400  _colorMaskG = g;
401  _colorMaskB = b;
402  _colorMaskA = a;
403 
404  GET_GL_ERROR;
405  }
406 }
GLboolean _colorMaskA
current color mask for A
Definition: SLGLState.h:206
GLboolean _colorMaskR
current color mask for R
Definition: SLGLState.h:203
GLboolean _colorMaskB
current color mask for B
Definition: SLGLState.h:205
GLboolean _colorMaskG
current color mask for G
Definition: SLGLState.h:204

◆ cullFace()

void SLGLState::cullFace ( SLbool  stateNew)

SLGLState::cullFace sets the GL_CULL_FACE state but only if the state really changes. If face culling is turned on no back faces are processed.

Definition at line 219 of file SLGLState.cpp.

220 {
221  if (_cullFace != stateNew)
222  {
223  if (stateNew)
224  glEnable(GL_CULL_FACE);
225  else
226  glDisable(GL_CULL_FACE);
227  _cullFace = stateNew;
228 
229  GET_GL_ERROR;
230  }
231 }
SLbool _cullFace
Face culling state.
Definition: SLGLState.h:188

◆ currentMaterial() [1/2]

SLMaterial* SLGLState::currentMaterial ( )
inline

Definition at line 152 of file SLGLState.h.

152 { return _currentMaterial; }
SLMaterial * _currentMaterial
Definition: SLGLState.h:211

◆ currentMaterial() [2/2]

void SLGLState::currentMaterial ( SLMaterial mat)
inline

Definition at line 120 of file SLGLState.h.

120 { _currentMaterial = mat; }

◆ deleteInstance()

void SLGLState::deleteInstance ( )
static

global destruction

Public static destruction.

Definition at line 27 of file SLGLState.cpp.

28 {
29  delete _instance;
30  _instance = nullptr;
31 }
static SLGLState * _instance
global singleton object
Definition: SLGLState.h:164

◆ depthFunc()

void SLGLState::depthFunc ( SLenum  func)

SLGLState::depthFunc specifies the depth comparison function. Symbolic constants GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, and GL_ALWAYS are accepted. The initial value is GL_LESS.

Definition at line 205 of file SLGLState.cpp.

206 {
207  if (_depthFunc != func)
208  {
209  glDepthFunc(func);
210  _depthFunc = func;
211 
212  GET_GL_ERROR;
213  }
214 }
SLenum _depthFunc
depth buffer comparison function
Definition: SLGLState.h:187

◆ depthMask()

void SLGLState::depthMask ( SLbool  stateNew)

SLGLState::depthTest enables or disables depth buffer writing but only if the state really changes. Turning on depth masking freezes the depth test but keeps all values in the depth buffer.

Definition at line 190 of file SLGLState.cpp.

191 {
192  if (_depthMask != stateNew)
193  {
194  glDepthMask(stateNew ? GL_TRUE : GL_FALSE);
195  _depthMask = stateNew;
196 
197  GET_GL_ERROR;
198  }
199 }
SLbool _depthMask
glDepthMask state
Definition: SLGLState.h:186

◆ depthTest()

void SLGLState::depthTest ( SLbool  stateNew)

SLGLState::depthTest enables or disables depth testing but only if the state really changes. The depth test decides for each pixel in the depth buffer which polygon is the closest to the eye.

Definition at line 172 of file SLGLState.cpp.

173 {
174  if (_depthTest != stateNew)
175  {
176  if (stateNew)
177  glEnable(GL_DEPTH_TEST);
178  else
179  glDisable(GL_DEPTH_TEST);
180  _depthTest = stateNew;
181 
182  GET_GL_ERROR;
183  }
184 }
SLbool _depthTest
GL_DEPTH_TEST state.
Definition: SLGLState.h:185

◆ getGLError()

void SLGLState::getGLError ( const char *  file,
int  line,
bool  quit 
)
static

Checks if an OpenGL error occurred.

Definition at line 484 of file SLGLState.cpp.

487 {
488  GLenum err = glGetError();
489  if (err != GL_NO_ERROR)
490  {
491  string errStr;
492  switch (err)
493  {
494  case GL_INVALID_ENUM:
495  errStr = "GL_INVALID_ENUM";
496  break;
497  case GL_INVALID_VALUE:
498  errStr = "GL_INVALID_VALUE";
499  break;
500  case GL_INVALID_OPERATION:
501  errStr = "GL_INVALID_OPERATION";
502  break;
503  case GL_INVALID_FRAMEBUFFER_OPERATION:
504  errStr = "GL_INVALID_FRAMEBUFFER_OPERATION";
505  break;
506  case GL_OUT_OF_MEMORY:
507  errStr = "GL_OUT_OF_MEMORY";
508  break;
509  default:
510  errStr = "Unknown error";
511  }
512 
513  // Build error string as a concatenation of file, line & error
514  string newErr("OpenGL Error in scene: ");
515  newErr += std::to_string(AppCommon::sceneID);
516  newErr += ": file: ";
517  newErr += Utils::getFileName(file);
518  newErr += ": line:";
519  newErr += std::to_string(line);
520  newErr += ": ";
521  newErr += errStr;
522 
523  // Check if error exists already
524  SLGLState* state = SLGLState::instance();
525  auto foundErr = std::find(state->errorTexts.begin(),
526  state->errorTexts.end(),
527  newErr);
528 
529  // Add error if not found in errorTexts
530  if (foundErr == state->errorTexts.end())
531  {
532  state->errorTexts.push_back(newErr);
533  state->errorCounts.push_back(1);
534  SL_LOG("**** %s ****", newErr.c_str());
535  }
536  else // error occurred already
537  {
538  long errorIndex = foundErr - state->errorTexts.begin();
539  state->errorCounts[errorIndex]++;
540  }
541 
542  if (quit)
543  exit(1);
544  }
545 }
static SLSceneID sceneID
ID of currently loaded scene.
Definition: AppCommon.h:89
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
Definition: Utils.cpp:580

◆ getGLVersionNO()

SLstring SLGLState::getGLVersionNO ( )

Returns the OpenGL version number as a string.

The string returned by glGetString can contain additional vendor information such as the build number and the brand name. For the OpenGL version string "4.5.0 NVIDIA 347.68" the function returns "4.5"

Definition at line 552 of file SLGLState.cpp.

553 {
554  SLstring versionStr = SLstring((const char*)glGetString(GL_VERSION));
555  size_t dotPos = versionStr.find('.');
556  SLchar NO[4];
557  NO[0] = versionStr[dotPos - 1];
558  NO[1] = '.';
559  NO[2] = versionStr[dotPos + 1];
560  NO[3] = 0;
561 
562  if (versionStr.find("OpenGL ES") != string::npos)
563  {
564  return SLstring(NO) + "ES";
565  }
566  else
567  return SLstring(NO);
568 }
char SLchar
Definition: SL.h:162
string SLstring
Definition: SL.h:158

◆ getSLVersionNO()

SLstring SLGLState::getSLVersionNO ( )

Returns the OpenGL Shading Language version number as a string.

The string returned by glGetString can contain additional vendor information such as the build number and the brand name. For the shading language string "Nvidia GLSL 4.5" the function returns "450"

Definition at line 575 of file SLGLState.cpp.

576 {
577  SLstring versionStr = SLstring((const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
578  size_t dotPos = versionStr.find('.');
579  SLchar NO[4];
580  NO[0] = versionStr[dotPos - 1];
581  NO[1] = versionStr[dotPos + 1];
582  NO[2] = '0';
583  NO[3] = 0;
584  return SLstring(NO);
585 }

◆ glHasGeometryShaders()

SLbool SLGLState::glHasGeometryShaders ( ) const
inline

Definition at line 139 of file SLGLState.h.

139  { return (_glIsES3 && _glVersionNOf >= 3.2f) ||
140  (!glIsES() && _glVersionNOf >= 4.1f); }
SLfloat _glVersionNOf
OpenGL Version number as float.
Definition: SLGLState.h:170
SLbool _glIsES3
Flag if OpenGL ES3.
Definition: SLGLState.h:177
SLbool glIsES() const
Definition: SLGLState.h:134

◆ glIsES()

SLbool SLGLState::glIsES ( ) const
inline

Definition at line 134 of file SLGLState.h.

134 { return _glIsES2 || _glIsES3; }
SLbool _glIsES2
Flag if OpenGL ES2.
Definition: SLGLState.h:176

◆ glIsES2()

SLbool SLGLState::glIsES2 ( ) const
inline

Definition at line 135 of file SLGLState.h.

135 { return _glIsES2; }

◆ glIsES3()

SLbool SLGLState::glIsES3 ( ) const
inline

Definition at line 136 of file SLGLState.h.

136 { return _glIsES3; }

◆ glMaxTexSize()

SLint SLGLState::glMaxTexSize ( ) const
inline

Definition at line 138 of file SLGLState.h.

138 { return _glMaxTexSize; }
SLint _glMaxTexSize
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize);
Definition: SLGLState.h:179

◆ glMaxTexUnits()

SLint SLGLState::glMaxTexUnits ( ) const
inline

Definition at line 137 of file SLGLState.h.

137 { return _glMaxTexUnits; }

◆ glRenderer()

SLstring SLGLState::glRenderer ( )
inline

Definition at line 131 of file SLGLState.h.

131 { return _glRenderer; }
SLstring _glRenderer
OpenGL Renderer string.
Definition: SLGLState.h:172

◆ glSLVersion()

SLstring SLGLState::glSLVersion ( )
inline

Definition at line 132 of file SLGLState.h.

132 { return _glSLVersion; }
SLstring _glSLVersion
GLSL Version string.
Definition: SLGLState.h:173

◆ glSLVersionNO()

SLstring SLGLState::glSLVersionNO ( )
inline

Definition at line 133 of file SLGLState.h.

133 { return _glSLVersionNO; }
SLstring _glSLVersionNO
GLSL Version number string.
Definition: SLGLState.h:174

◆ glVendor()

SLstring SLGLState::glVendor ( )
inline

Definition at line 130 of file SLGLState.h.

130 { return _glVendor; }
SLstring _glVendor
OpenGL Vendor string.
Definition: SLGLState.h:171

◆ glVersion()

SLstring SLGLState::glVersion ( )
inline

Definition at line 127 of file SLGLState.h.

127 { return _glVersion; }
SLstring _glVersion
OpenGL Version string.
Definition: SLGLState.h:168

◆ glVersionNO()

SLstring SLGLState::glVersionNO ( )
inline

Definition at line 128 of file SLGLState.h.

128 { return _glVersionNO; }
SLstring _glVersionNO
OpenGL Version number string.
Definition: SLGLState.h:169

◆ glVersionNOf()

SLfloat SLGLState::glVersionNOf ( ) const
inline

Definition at line 129 of file SLGLState.h.

129 { return _glVersionNOf; }

◆ hasExtension()

SLbool SLGLState::hasExtension ( const SLstring e)
inline

Definition at line 141 of file SLGLState.h.

141 { return _glExtensions.find(e) != string::npos; }
SLstring _glExtensions
OpenGL extensions string.
Definition: SLGLState.h:175

◆ hasMultiSampling()

bool SLGLState::hasMultiSampling ( ) const
inline

Definition at line 95 of file SLGLState.h.

95 { return _multiSampleSamples > 0; }
SLint _multiSampleSamples
NO. of multisampling samples.
Definition: SLGLState.h:190

◆ initAll()

void SLGLState::initAll ( )

Initializes all states.

Definition at line 42 of file SLGLState.cpp.

43 {
48 
49  _glVersion = SLstring((const char*)glGetString(GL_VERSION));
51  _glVersionNOf = (SLfloat)atof(_glVersionNO.c_str());
52  _glVendor = SLstring((const char*)glGetString(GL_VENDOR));
53  _glRenderer = SLstring((const char*)glGetString(GL_RENDERER));
54  _glSLVersion = SLstring((const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
56  _glIsES2 = (_glVersion.find("OpenGL ES 2") != string::npos);
57  _glIsES3 = (_glVersion.find("OpenGL ES 3") != string::npos);
58  glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits);
59  glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize);
60 
61 // Get extensions
62 #ifndef APP_USES_GLES
63  if (_glVersionNOf > 3.0f)
64  {
65  GLint n;
66  glGetIntegerv(GL_NUM_EXTENSIONS, &n);
67  for (SLuint i = 0; i < (SLuint)n; i++)
68  _glExtensions += SLstring((const char*)glGetStringi(GL_EXTENSIONS, i)) + ", ";
69  }
70  else
71 #endif
72  {
73  const GLubyte* ext = glGetString(GL_EXTENSIONS);
74  if (ext) _glExtensions = SLstring((const char*)ext);
75  }
76 
77  // initialize states a unset
78  _blend = false;
79  _blendFuncSfactor = GL_SRC_ALPHA;
80  _blendFuncDfactor = GL_ONE_MINUS_SRC_ALPHA;
81  _cullFace = false;
82  _depthTest = false;
83  _depthMask = false;
84  _depthFunc = GL_LESS;
85  _multisample = false;
86  _polygonLine = false;
90  _viewport.set(-1, -1, -1, -1);
91  _clearColor.set(-1, -1, -1, -1);
92 
93  // Reset all cached states to an invalid state
94  _programID = 0;
95  _colorMaskR = -1;
96  _colorMaskG = -1;
97  _colorMaskB = -1;
98  _colorMaskA = -1;
99 
100  _isInitialized = true;
101 
102  glGetIntegerv(GL_SAMPLES, &_multiSampleSamples);
103 
104  /* After over 10 years of OpenGL experience I used once a texture that is
105  not divisible by 4 and this caused distorted texture displays. By default
106  OpenGL has a pixel alignment of 4 which means that all images must be
107  divisible by 4! If you want to use textures of any size you have to set
108  a pixel alignment of 1:*/
109  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
110  glPixelStorei(GL_PACK_ALIGNMENT, 1);
111 
112 #ifndef SL_GLES
113  glEnable(GL_PROGRAM_POINT_SIZE);
114 #endif
115 
116  GET_GL_ERROR;
117 
118  _currentMaterial = nullptr;
119 }
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
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
SLMat4f modelMatrix
Init all states.
Definition: SLGLState.h:89
SLbool _multisample
Multisampling state.
Definition: SLGLState.h:189
SLbool _isInitialized
flag for first init
Definition: SLGLState.h:166
SLMat4f viewMatrix
matrix for the active cameras view transform
Definition: SLGLState.h:91
SLstring getSLVersionNO()
Returns the OpenGL Shading Language version number as a string.
Definition: SLGLState.cpp:575
SLbool _polygonOffsetFillEnabled
GL_POLYGON_OFFSET_FILL state enabled.
Definition: SLGLState.h:194
SLbool _polygonOffsetLineEnabled
GL_POLYGON_OFFSET_LINE state enabled.
Definition: SLGLState.h:193
SLstring getGLVersionNO()
Returns the OpenGL version number as a string.
Definition: SLGLState.cpp:552
SLbool _polygonOffsetPointEnabled
GL_POLYGON_OFFSET_POINT state enabled.
Definition: SLGLState.h:192
SLbool _polygonLine
Line polygon state.
Definition: SLGLState.h:191
SLMat4f projectionMatrix
matrix for projection transform
Definition: SLGLState.h:90
SLuint _programID
current shader program id
Definition: SLGLState.h:199
void identity()
Sets the identity matrix.
Definition: SLMat4.h:1333
void set(const T X, const T Y, const T Z, const T W=1)
Definition: SLVec4.h:49

◆ instance()

static SLGLState* SLGLState::instance ( )
inlinestatic

Public static instance getter for singleton pattern.

Definition at line 74 of file SLGLState.h.

75  {
76  if (!_instance)
77  {
78  _instance = new SLGLState();
79  return _instance;
80  }
81  else
82  return _instance;
83  }
SLGLState()
private onetime constructor
Definition: SLGLState.cpp:35

◆ multiSample()

void SLGLState::multiSample ( SLbool  stateNew)

SLGLState::multiSample enables or disables multisampling but only if the state really changes. Multisampling turns on fullscreen anti aliasing on the GPU witch produces smooth polygon edges, lines and points.

Definition at line 267 of file SLGLState.cpp.

268 {
269 #ifndef SL_GLES3
270  if (_multisample != stateNew)
271  {
272  if (_multiSampleSamples > 0)
273  {
274  if (stateNew)
275  glEnable(GL_MULTISAMPLE);
276  else
277  glDisable(GL_MULTISAMPLE);
278  _multisample = stateNew;
279  }
280 
281  GET_GL_ERROR;
282  }
283 #endif
284 }

◆ onInitialize()

void SLGLState::onInitialize ( const SLCol4f clearColor)

On init GL.

One time initialization

Definition at line 137 of file SLGLState.cpp.

138 {
139  // Reset all internal states
140  if (!_isInitialized) initAll();
141 
142  // enable depth_test
143  glDepthFunc(GL_LESS);
144  glEnable(GL_DEPTH_TEST);
145 
146  // set blend function for classic transparency
147  glBlendFunc(_blendFuncSfactor, _blendFuncDfactor);
148 
149  // set background color
150  glClearColor(clearColor.r,
151  clearColor.g,
152  clearColor.b,
153  clearColor.a);
154  GET_GL_ERROR;
155 }
void clearColor(const SLCol4f &c)
Definition: SLGLState.cpp:157

◆ pixelFormatIsSupported()

SLbool SLGLState::pixelFormatIsSupported ( SLint  pixelFormat)

Returns true if the according GL pixel format is valid in the GL context.

Definition at line 588 of file SLGLState.cpp.

589 { /*
590  #define SL_ALPHA 0x1906 // ES2 ES3 GL2
591  #define SL_LUMINANCE 0x1909 // ES2 ES3 GL2
592  #define SL_LUMINANCE_ALPHA 0x190A // ES2 ES3 GL2
593  #define SL_INTENSITY 0x8049 // GL2
594  #define SL_GREEN 0x1904 // GL2
595  #define SL_BLUE 0x1905 // GL2
596 
597  #define SL_RED 0x1903 // ES3 GL2 GL3 GL4
598  #define SL_RG 0x8227 // ES3 GL3 GL4
599  #define SL_RGB 0x1907 // ES2 ES3 GL2 GL3 GL4
600  #define SL_RGBA 0x1908 // ES2 ES3 GL2 GL3 GL4
601  #define SL_BGR 0x80E0 // GL2 GL3 GL4
602  #define SL_BGRA 0x80E1 // GL2 GL3 GL4 (iOS defines it but it doesn't work)
603 
604  #define SL_RG_INTEGER 0x8228 // ES3 GL4
605  #define SL_RED_INTEGER 0x8D94 // ES3 GL4
606  #define SL_RGB_INTEGER 0x8D98 // ES3 GL4
607  #define SL_RGBA_INTEGER 0x8D99 // ES3 GL4
608  #define SL_BGR_INTEGER 0x8D9A // GL4
609  #define SL_BGRA_INTEGER 0x8D9B // GL4
610  */
611  switch (pixelFormat)
612  {
613  case PF_rgb:
614  case PF_rgba: return true;
615  case PF_red: return (!_glIsES2);
616  case PF_bgr:
617  case PF_bgra: return (!_glIsES2 && !_glIsES3);
618  case PF_luminance:
619  case PF_luminance_alpha:
620  case PF_alpha: return (_glIsES2 || _glIsES3 || (((SLint)_glVersionNOf) == 2));
621  case PF_intensity:
622  case PF_green:
623  case PF_blue: return (!_glIsES2 && !_glIsES3 && (((SLint)_glVersionNOf) == 2));
624  case PF_rg: return (!_glIsES2 && _glVersionNOf >= 3);
625  case PF_rg_integer:
626  case PF_red_integer:
627  case PF_rgb_integer:
628  case PF_rgba_integer: return (!_glIsES2 && _glVersionNOf >= 4);
629  case PF_bgr_integer:
630  case PF_bgra_integer: return (_glVersionNOf >= 4);
631  default: return false;
632  }
633 }
@ PF_rgb_integer
Definition: CVImage.h:42
@ PF_luminance
Definition: CVImage.h:28
@ PF_bgr_integer
Definition: CVImage.h:44
@ PF_alpha
Definition: CVImage.h:27
@ PF_rgba_integer
Definition: CVImage.h:43
@ PF_green
Definition: CVImage.h:31
@ PF_rg_integer
Definition: CVImage.h:40
@ PF_red
Definition: CVImage.h:34
@ PF_luminance_alpha
Definition: CVImage.h:29
@ PF_rgb
Definition: CVImage.h:36
@ PF_bgra_integer
Definition: CVImage.h:45
@ PF_rg
Definition: CVImage.h:35
@ PF_bgra
Definition: CVImage.h:39
@ PF_rgba
Definition: CVImage.h:37
@ PF_bgr
Definition: CVImage.h:38
@ PF_red_integer
Definition: CVImage.h:41
@ PF_blue
Definition: CVImage.h:32
@ PF_intensity
Definition: CVImage.h:30

◆ polygonLine()

void SLGLState::polygonLine ( SLbool  stateNew)

SLGLState::polygonMode sets the polygonMode to GL_LINE but only if the state really changes. OpenGL ES doesn't support glPolygonMode. It has to be mimicked with GL_LINE_LOOP drawing.

Definition at line 290 of file SLGLState.cpp.

291 {
292 #ifndef SL_GLES3
293  if (_polygonLine != stateNew)
294  {
295  if (stateNew)
296  glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
297  else
298  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
299  _polygonLine = stateNew;
300 
301  GET_GL_ERROR;
302  }
303 #endif
304 }

◆ polygonOffsetFill()

void SLGLState::polygonOffsetFill ( SLbool  enabled,
SLfloat  factor = -1.0f,
SLfloat  units = -1.0f 
)

SLGLState::polygonOffsetFill turns on/off polygon offset for filled polygons and sets the factor and unit for glPolygonOffset but only if the state really changes. Polygon offset is used to reduce z-fighting due to parallel planes or lines. See: http://www.zeuscmd.com/tutorials/opengl/15-PolygonOffset.php

Definition at line 359 of file SLGLState.cpp.

360 {
361  if (_polygonOffsetFillEnabled != enabled)
362  {
363  if (enabled)
364  {
365  glEnable(GL_POLYGON_OFFSET_FILL);
366  glPolygonOffset(factor, units);
367  }
368  else
369  glDisable(GL_POLYGON_OFFSET_FILL);
370  _polygonOffsetFillEnabled = enabled;
371 
372  GET_GL_ERROR;
373  }
374 }

◆ polygonOffsetLine()

void SLGLState::polygonOffsetLine ( SLbool  enabled,
SLfloat  factor = -1.0f,
SLfloat  units = -1.0f 
)

SLGLState::polygonOffsetLine turns on/off polygon offset for lines and sets the factor and unit for glPolygonOffset but only if the state really changes. Polygon offset is used to reduce z-fighting due to parallel planes or lines. See: http://www.zeuscmd.com/tutorials/opengl/15-PolygonOffset.php

Definition at line 335 of file SLGLState.cpp.

336 {
337 #ifndef SL_GLES3
338  if (_polygonOffsetLineEnabled != enabled)
339  {
340  if (enabled)
341  {
342  glEnable(GL_POLYGON_OFFSET_LINE);
343  glPolygonOffset(factor, units);
344  }
345  else
346  glDisable(GL_POLYGON_OFFSET_LINE);
347  _polygonOffsetLineEnabled = enabled;
348 
349  GET_GL_ERROR;
350  }
351 #endif
352 }

◆ polygonOffsetPoint()

void SLGLState::polygonOffsetPoint ( SLbool  enabled,
SLfloat  factor = -1.0f,
SLfloat  units = -1.0f 
)

SLGLState::polygonOffsetPoint turns on/off polygon offset for points and sets the factor and unit for glPolygonOffset but only if the state really changes. Polygon offset is used to reduce z-fighting due to parallel planes or lines. See: http://www.zeuscmd.com/tutorials/opengl/15-PolygonOffset.php

Definition at line 311 of file SLGLState.cpp.

312 {
313 #ifndef SL_GLES3
314  if (_polygonOffsetPointEnabled != enabled)
315  {
316  if (enabled)
317  {
318  glEnable(GL_POLYGON_OFFSET_POINT);
319  glPolygonOffset(factor, units);
320  }
321  else
322  glDisable(GL_POLYGON_OFFSET_POINT);
323  _polygonOffsetPointEnabled = enabled;
324 
325  GET_GL_ERROR;
326  }
327 #endif
328 }

◆ readPixels()

void SLGLState::readPixels ( void buffer)

Reads the front framebuffer pixels into the passed buffer.

Parameters
bufferPointer to a 4 byte aligned buffer with the correct size.

Definition at line 639 of file SLGLState.cpp.

640 {
641  glPixelStorei(GL_PACK_ALIGNMENT, 4);
642 
643 #ifndef SL_EMSCRIPTEN
644  glReadBuffer(GL_FRONT);
645 #endif
646 
647  // Get viewport size
648  GLint vp[4];
649  glGetIntegerv(GL_VIEWPORT, vp);
650 
651  glReadPixels(vp[0],
652  vp[1],
653  vp[2],
654  vp[3],
656  GL_UNSIGNED_BYTE,
657  buffer);
658 }
#define SL_READ_PIXELS_GL_FORMAT
Definition: SLGLTexture.h:59

◆ unbindAnythingAndFlush()

void SLGLState::unbindAnythingAndFlush ( )

finishes all GL commands

SLGLState::unbindAnythingAndFlush unbinds all shaderprograms and buffers in use and calls glFinish. This should be the last call to GL before buffer swapping.

Definition at line 465 of file SLGLState.cpp.

466 {
467  useProgram(0);
468 
469  // reset the bound texture unit
470  // This is needed since leaving one texture unit bound over multiple windows
471  // sometimes (most of the time) causes bugs
472  // glBindTexture(GL_TEXTURE_2D, 0);
473  // glBindVertexArray(0);
474  // glBindBuffer(GL_ARRAY_BUFFER, 0);
475  // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
476 
477  // The iOS OpenGL ES Analyzer suggests not to use flush or finish
478  // glFlush();
479  // glFinish();
480 
481  GET_GL_ERROR;
482 }
void useProgram(SLuint progID)
Definition: SLGLState.cpp:410

◆ useProgram()

void SLGLState::useProgram ( SLuint  progID)

SLGLState::useProgram sets the _rent active shader program

Definition at line 410 of file SLGLState.cpp.

411 {
412  if (_programID != progID)
413  {
414  glUseProgram(progID);
415  _programID = progID;
416 
417  GET_GL_ERROR;
418  }
419 }

◆ viewport() [1/2]

SLVec4i SLGLState::viewport ( )
inline

Definition at line 142 of file SLGLState.h.

142 { return _viewport; }

◆ viewport() [2/2]

void SLGLState::viewport ( SLint  x,
SLint  y,
SLsizei  width,
SLsizei  height 
)

SLGLState::viewport sets the OpenGL viewport position and size

Definition at line 378 of file SLGLState.cpp.

379 {
380  if (_viewport.x != x ||
381  _viewport.y != y ||
382  _viewport.z != width ||
383  _viewport.w != height)
384  {
385  glViewport(x, y, width, height);
386  _viewport.set(x, y, width, height);
387 
388  GET_GL_ERROR;
389  }
390 }
T w
Definition: SLVec4.h:32
T z
Definition: SLVec4.h:32
T y
Definition: SLVec4.h:32
T x
Definition: SLVec4.h:32

◆ viewportMatrix()

SLMat4f SLGLState::viewportMatrix ( )
inline

Definition at line 143 of file SLGLState.h.

144  {
145  SLMat4f vpm;
149  (SLfloat)_viewport.w);
150  return vpm;
151  }
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

Member Data Documentation

◆ _blend

SLbool SLGLState::_blend
private

blending default false;

Definition at line 182 of file SLGLState.h.

◆ _blendFuncDfactor

SLenum SLGLState::_blendFuncDfactor
private

blend function destination factor enum

Definition at line 184 of file SLGLState.h.

◆ _blendFuncSfactor

SLenum SLGLState::_blendFuncSfactor
private

blend function source factor enum

Definition at line 183 of file SLGLState.h.

◆ _clearColor

SLCol4f SLGLState::_clearColor
private

clear color

Definition at line 196 of file SLGLState.h.

◆ _colorMaskA

GLboolean SLGLState::_colorMaskA
private

current color mask for A

Definition at line 206 of file SLGLState.h.

◆ _colorMaskB

GLboolean SLGLState::_colorMaskB
private

current color mask for B

Definition at line 205 of file SLGLState.h.

◆ _colorMaskG

GLboolean SLGLState::_colorMaskG
private

current color mask for G

Definition at line 204 of file SLGLState.h.

◆ _colorMaskR

GLboolean SLGLState::_colorMaskR
private

current color mask for R

Definition at line 203 of file SLGLState.h.

◆ _cullFace

SLbool SLGLState::_cullFace
private

Face culling state.

Definition at line 188 of file SLGLState.h.

◆ _currentMaterial

SLMaterial* SLGLState::_currentMaterial
private

Definition at line 211 of file SLGLState.h.

◆ _depthFunc

SLenum SLGLState::_depthFunc
private

depth buffer comparison function

Definition at line 187 of file SLGLState.h.

◆ _depthMask

SLbool SLGLState::_depthMask
private

glDepthMask state

Definition at line 186 of file SLGLState.h.

◆ _depthTest

SLbool SLGLState::_depthTest
private

GL_DEPTH_TEST state.

Definition at line 185 of file SLGLState.h.

◆ _glExtensions

SLstring SLGLState::_glExtensions
private

OpenGL extensions string.

Definition at line 175 of file SLGLState.h.

◆ _glIsES2

SLbool SLGLState::_glIsES2
private

Flag if OpenGL ES2.

Definition at line 176 of file SLGLState.h.

◆ _glIsES3

SLbool SLGLState::_glIsES3
private

Flag if OpenGL ES3.

Definition at line 177 of file SLGLState.h.

◆ _glMaxTexSize

SLint SLGLState::_glMaxTexSize
private

glGetIntegerv(GL_MAX_TEXTURE_SIZE, &_glMaxTexSize);

Definition at line 179 of file SLGLState.h.

◆ _glMaxTexUnits

SLint SLGLState::_glMaxTexUnits
private

glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_glMaxTexUnits);

Definition at line 178 of file SLGLState.h.

◆ _glRenderer

SLstring SLGLState::_glRenderer
private

OpenGL Renderer string.

Definition at line 172 of file SLGLState.h.

◆ _glSLVersion

SLstring SLGLState::_glSLVersion
private

GLSL Version string.

Definition at line 173 of file SLGLState.h.

◆ _glSLVersionNO

SLstring SLGLState::_glSLVersionNO
private

GLSL Version number string.

Definition at line 174 of file SLGLState.h.

◆ _glVendor

SLstring SLGLState::_glVendor
private

OpenGL Vendor string.

Definition at line 171 of file SLGLState.h.

◆ _glVersion

SLstring SLGLState::_glVersion
private

OpenGL Version string.

Definition at line 168 of file SLGLState.h.

◆ _glVersionNO

SLstring SLGLState::_glVersionNO
private

OpenGL Version number string.

Definition at line 169 of file SLGLState.h.

◆ _glVersionNOf

SLfloat SLGLState::_glVersionNOf
private

OpenGL Version number as float.

Definition at line 170 of file SLGLState.h.

◆ _instance

SLGLState * SLGLState::_instance = nullptr
staticprivate

global singleton object

Definition at line 164 of file SLGLState.h.

◆ _isInitialized

SLbool SLGLState::_isInitialized
private

flag for first init

Definition at line 166 of file SLGLState.h.

◆ _multisample

SLbool SLGLState::_multisample
private

Multisampling state.

Definition at line 189 of file SLGLState.h.

◆ _multiSampleSamples

SLint SLGLState::_multiSampleSamples
private

NO. of multisampling samples.

Definition at line 190 of file SLGLState.h.

◆ _polygonLine

SLbool SLGLState::_polygonLine
private

Line polygon state.

Definition at line 191 of file SLGLState.h.

◆ _polygonOffsetFillEnabled

SLbool SLGLState::_polygonOffsetFillEnabled
private

GL_POLYGON_OFFSET_FILL state enabled.

Definition at line 194 of file SLGLState.h.

◆ _polygonOffsetLineEnabled

SLbool SLGLState::_polygonOffsetLineEnabled
private

GL_POLYGON_OFFSET_LINE state enabled.

Definition at line 193 of file SLGLState.h.

◆ _polygonOffsetPointEnabled

SLbool SLGLState::_polygonOffsetPointEnabled
private

GL_POLYGON_OFFSET_POINT state enabled.

Definition at line 192 of file SLGLState.h.

◆ _programID

SLuint SLGLState::_programID
private

current shader program id

Definition at line 199 of file SLGLState.h.

◆ _textureID

SLuint SLGLState::_textureID
private

current texture id

Definition at line 202 of file SLGLState.h.

◆ _textureTarget

SLenum SLGLState::_textureTarget
private

current texture target

Definition at line 201 of file SLGLState.h.

◆ _textureUnit

SLenum SLGLState::_textureUnit
private

current texture unit

Definition at line 200 of file SLGLState.h.

◆ _viewport

SLVec4i SLGLState::_viewport
private

viewport size (x,y,w,h) of the framebuffer

Definition at line 195 of file SLGLState.h.

◆ errorCounts

SLVlong SLGLState::errorCounts
private

vector for counts for the corresponding errorTexts

Definition at line 209 of file SLGLState.h.

◆ errorTexts

SLVstring SLGLState::errorTexts
private

vector for error texts collected in getGLError

Definition at line 208 of file SLGLState.h.

◆ modelMatrix

SLMat4f SLGLState::modelMatrix

Init all states.

matrix for model to world transform

Definition at line 89 of file SLGLState.h.

◆ projectionMatrix

SLMat4f SLGLState::projectionMatrix

matrix for projection transform

Definition at line 90 of file SLGLState.h.

◆ textureMatrix

SLMat4f SLGLState::textureMatrix

matrix for the texture transform

Definition at line 92 of file SLGLState.h.

◆ viewMatrix

SLMat4f SLGLState::viewMatrix

matrix for the active cameras view transform

Definition at line 91 of file SLGLState.h.


The documentation for this class was generated from the following files: