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

Defines a 2D-Background for the OpenGL framebuffer background. More...

#include <SLBackground.h>

Inheritance diagram for SLBackground:
[legend]

Public Member Functions

 SLBackground (SLstring shaderDir)
 The constructor initializes to a uniform BLACK background color. More...
 
 SLBackground (SLGLProgram *textureOnlyProgram, SLGLProgram *colorAttributeProgram)
 The constructor initializes to a uniform gray background color. More...
 
 ~SLBackground ()
 
void render (SLint widthPX, SLint heightPX)
 Draws the background as 2D rectangle with OpenGL buffers. More...
 
void renderInScene (const SLMat4f &wm, const SLVec3f &LT, const SLVec3f &LB, const SLVec3f &RT, const SLVec3f &RB)
 Draws the background as a quad on the far clipping plane. More...
 
SLCol4f colorAtPos (SLfloat x, SLfloat y, SLfloat width, SLfloat height)
 Returns the interpolated color at the pixel position p[x,y] used in raytracing. More...
 
void rebuild ()
 
void colors (const SLCol4f &uniformColor)
 Sets a uniform background color. More...
 
void colors (const SLCol4f &topColor, const SLCol4f &bottomColor)
 Sets a gradient top-down background color. More...
 
void colors (const SLCol4f &topLeftColor, const SLCol4f &bottomLeftColor, const SLCol4f &topRightColor, const SLCol4f &bottomRightColor)
 Sets a gradient background color with a color per corner. More...
 
void texture (SLGLTexture *backgroundTexture, bool fixAspectRatio=false)
 If flag _repeatBlurred is true the texture is not distorted if its size does not fit to screen aspect ratio. Instead it is repeated. More...
 
SLVCol4f colors ()
 
SLCol4f avgColor ()
 
SLbool isUniform () const
 
SLGLTexturetexture ()
 
const SLRectfrect () const
 
- Public Member Functions inherited from SLObject
 SLObject (const SLstring &Name="", const SLstring &url="")
 
virtual ~SLObject ()
 
void name (const SLstring &Name)
 
void url (const SLstring &url)
 
const SLstringname () const
 
const SLstringurl () const
 

Private Attributes

SLbool _isUniform
 Flag if background has uniform color. More...
 
SLVCol4f _colors
 Vector of 4 corner colors {TL,BL,TR,BR}. More...
 
SLCol4f _avgColor
 Average color of all 4 corner colors. More...
 
SLGLTexture_texture
 Pointer to a background texture. More...
 
SLGLTexture_textureError
 Pointer to a error texture if background texture is not available. More...
 
SLint _resX
 Background resolution in x-dir. More...
 
SLint _resY
 Background resolution in y-dir. More...
 
SLRectf _rect
 Background rect (it may be different to viewport size) More...
 
SLGLVertexArray _vao
 OpenGL Vertex Array Object for drawing. More...
 
SLGLProgram_textureOnlyProgram = nullptr
 
SLGLProgram_colorAttributeProgram = nullptr
 
bool _deletePrograms = false
 
bool _fixAspectRatio = false
 if true, the background is centered and stretched to the screen boarders while keeping the textures aspect ratio More...
 

Additional Inherited Members

- Protected Attributes inherited from SLObject
SLstring _name
 name of an object More...
 
SLstring _url
 uniform resource locator More...
 

Detailed Description

Defines a 2D-Background for the OpenGL framebuffer background.

The background can either be defined with a texture or with 4 colors for the corners of the frame buffer. For a uniform background color the color at index 0 of the _colors vector is taken. Every instance of SLCamera has a background that is displayed on the far clipping plane if the camera is not the active one. The OpenGL rendering is done in SLSceneView::draw3DGL for the active camera or in SLCamera::drawMeshes for inactive ones.

Definition at line 28 of file SLBackground.h.

Constructor & Destructor Documentation

◆ SLBackground() [1/2]

SLBackground::SLBackground ( SLstring  shaderDir)

The constructor initializes to a uniform BLACK background color.

Definition at line 17 of file SLBackground.cpp.

18  : SLObject("Background")
19 {
20  _colors.push_back(SLCol4f::BLACK); // bottom left
21  _colors.push_back(SLCol4f::BLACK); // bottom right
22  _colors.push_back(SLCol4f::BLACK); // top right
23  _colors.push_back(SLCol4f::BLACK); // top left
25  _isUniform = true;
26  _texture = nullptr;
27  _textureError = nullptr;
28  _resX = -1;
29  _resY = -1;
30 
32  shaderDir + "TextureOnly.vert",
33  shaderDir + "TextureOnly.frag");
34 
36  shaderDir + "ColorAttribute.vert",
37  shaderDir + "Color.frag");
38  _deletePrograms = true;
39 }
SLint _resY
Background resolution in y-dir.
Definition: SLBackground.h:68
SLCol4f _avgColor
Average color of all 4 corner colors.
Definition: SLBackground.h:64
SLGLProgram * _colorAttributeProgram
Definition: SLBackground.h:73
SLVCol4f _colors
Vector of 4 corner colors {TL,BL,TR,BR}.
Definition: SLBackground.h:63
SLGLProgram * _textureOnlyProgram
Definition: SLBackground.h:72
SLGLTexture * _texture
Pointer to a background texture.
Definition: SLBackground.h:65
SLGLTexture * _textureError
Pointer to a error texture if background texture is not available.
Definition: SLBackground.h:66
bool _deletePrograms
Definition: SLBackground.h:74
SLbool _isUniform
Flag if background has uniform color.
Definition: SLBackground.h:62
SLint _resX
Background resolution in x-dir.
Definition: SLBackground.h:67
Generic Shader Program class inherited from SLGLProgram.
SLObject(const SLstring &Name="", const SLstring &url="")
Definition: SLObject.h:25
static SLVec4 BLACK
Definition: SLVec4.h:213

◆ SLBackground() [2/2]

SLBackground::SLBackground ( SLGLProgram textureOnlyProgram,
SLGLProgram colorAttributeProgram 
)

The constructor initializes to a uniform gray background color.

Definition at line 42 of file SLBackground.cpp.

44  : SLObject("Background"),
45  _textureOnlyProgram(textureOnlyProgram),
46  _colorAttributeProgram(colorAttributeProgram),
47  _deletePrograms(false)
48 {
49  _colors.push_back(SLCol4f::BLACK); // bottom left
50  _colors.push_back(SLCol4f::BLACK); // bottom right
51  _colors.push_back(SLCol4f::BLACK); // top right
52  _colors.push_back(SLCol4f::BLACK); // top left
54  _isUniform = true;
55  _texture = nullptr;
56  _textureError = nullptr;
57  _resX = -1;
58  _resY = -1;
59 }

◆ ~SLBackground()

SLBackground::~SLBackground ( )

Definition at line 61 of file SLBackground.cpp.

62 {
63  if (_deletePrograms)
64  {
65  delete _textureOnlyProgram;
67  }
68 }

Member Function Documentation

◆ avgColor()

SLCol4f SLBackground::avgColor ( )
inline

Definition at line 56 of file SLBackground.h.

56 { return _avgColor; }

◆ colorAtPos()

SLCol4f SLBackground::colorAtPos ( SLfloat  x,
SLfloat  y,
SLfloat  width,
SLfloat  height 
)

Returns the interpolated color at the pixel position p[x,y] used in raytracing.

Returns the interpolated color at the pixel position p[x,y] for ray tracing. x is expected to be between 0 and width of the RT-frame. y is expected to be between 0 and height of the RT-frame. width is the width of the RT-frame height is the height of the RT-frame

C w B +--—+ | p /| | * / | h | / | | / | |/ | 0 +--—+ A 0

Definition at line 331 of file SLBackground.cpp.

335 {
336  if (_isUniform)
337  return _colors[0];
338 
339  if (_texture)
340  return _texture->getTexelf(x / width, y / height);
341 
342  // top-down gradient
343  if (_colors[0] == _colors[2] && _colors[1] == _colors[3])
344  {
345  SLfloat f = y / height;
346  return f * _colors[0] + (1 - f) * _colors[1];
347  }
348  // left-right gradient
349  if (_colors[0] == _colors[1] && _colors[2] == _colors[3])
350  {
351  SLfloat f = x / width;
352  return f * _colors[0] + (1 - f) * _colors[2];
353  }
354 
355  // Quadrilateral interpolation
356  // First check with barycentric coords if p is in the upper left triangle
357  SLVec2f p(x, y);
358  SLVec3f bc(p.barycentricCoords(SLVec2f(0, 0),
359  SLVec2f(width, height),
360  SLVec2f(0, height)));
361  SLfloat u = bc.x;
362  SLfloat v = bc.y;
363  SLfloat w = 1 - bc.x - bc.y;
364 
365  SLCol4f color;
366 
367  if (u > 0 && v > 0 && u + v <= 1)
368  color = w * _colors[0] + u * _colors[1] + v * _colors[2]; // upper left triangle
369  else
370  {
371  u = 1 - u;
372  v = 1 - v;
373  w = 1 - u - v;
374  color = w * _colors[3] + v * _colors[1] + u * _colors[2]; // lower right triangle
375  }
376 
377  return color;
378 }
float SLfloat
Definition: SL.h:173
SLVec2< SLfloat > SLVec2f
Definition: SLVec2.h:141
SLCol4f getTexelf(SLfloat u, SLfloat v, SLuint imgIndex=0)
SLGLTexture::getTexelf returns a pixel color from u & v texture coordinates.

◆ colors() [1/4]

SLVCol4f SLBackground::colors ( )
inline

Definition at line 55 of file SLBackground.h.

55 { return _colors; }

◆ colors() [2/4]

void SLBackground::colors ( const SLCol4f topColor,
const SLCol4f bottomColor 
)

Sets a gradient top-down background color.

Definition at line 84 of file SLBackground.cpp.

86 {
87  _colors[0].set(topColor);
88  _colors[1].set(bottomColor);
89  _colors[2].set(topColor);
90  _colors[3].set(bottomColor);
91  _avgColor = (topColor + bottomColor) / 2.0f;
92  _isUniform = false;
93  _texture = nullptr;
94  //_vao.clearAttribs(); Not allowed here during assembly
95 }

◆ colors() [3/4]

void SLBackground::colors ( const SLCol4f topLeftColor,
const SLCol4f bottomLeftColor,
const SLCol4f topRightColor,
const SLCol4f bottomRightColor 
)

Sets a gradient background color with a color per corner.

Definition at line 98 of file SLBackground.cpp.

102 {
103  _colors[0].set(topLeftColor);
104  _colors[1].set(bottomLeftColor);
105  _colors[2].set(topRightColor);
106  _colors[3].set(bottomRightColor);
107  _avgColor = (_colors[0] + _colors[1] + _colors[2] + _colors[3]) / 4.0f;
108  _isUniform = false;
109  _texture = nullptr;
110  _vao.clearAttribs();
111 }
SLGLVertexArray _vao
OpenGL Vertex Array Object for drawing.
Definition: SLBackground.h:70
void clearAttribs()
Clears the attribute definition.

◆ colors() [4/4]

void SLBackground::colors ( const SLCol4f uniformColor)

Sets a uniform background color.

Definition at line 71 of file SLBackground.cpp.

72 {
73  _colors[0].set(uniformColor);
74  _colors[1].set(uniformColor);
75  _colors[2].set(uniformColor);
76  _colors[3].set(uniformColor);
77  _avgColor = uniformColor;
78  _isUniform = true;
79  _texture = nullptr;
81 }

◆ isUniform()

SLbool SLBackground::isUniform ( ) const
inline

Definition at line 57 of file SLBackground.h.

57 { return _isUniform; }

◆ rebuild()

void SLBackground::rebuild ( )
inline

Definition at line 42 of file SLBackground.h.

42 { _vao.clearAttribs(); }

◆ rect()

const SLRectf& SLBackground::rect ( ) const
inline

Definition at line 59 of file SLBackground.h.

59 { return _rect; }
SLRectf _rect
Background rect (it may be different to viewport size)
Definition: SLBackground.h:69

◆ render()

void SLBackground::render ( SLint  widthPX,
SLint  heightPX 
)

Draws the background as 2D rectangle with OpenGL buffers.

Draws the background as a flat 2D rectangle with a height and a width on two triangles with zero in the bottom left corner:
w +--—+ | /| | / | h | / | | / | |/ | 0 +--—+ 0

We render the quad as a triangle strip:
0 2 +--—+ | /| | / | | / | | / | |/ | +--—+ 1 3

Definition at line 148 of file SLBackground.cpp.

149 {
150  SLGLState* stateGL = SLGLState::instance();
151 
152  // Set orthographic projection
153  stateGL->projectionMatrix.ortho(0.0f, (SLfloat)widthPX, 0.0f, (SLfloat)heightPX, 0.0f, 1.0f);
154  stateGL->modelMatrix.identity();
155  stateGL->viewMatrix.identity();
156  stateGL->depthTest(false);
157  stateGL->multiSample(false);
158 
159  // Get shader program
161  sp->useProgram();
162  sp->uniformMatrix4fv("u_mMatrix", 1, (SLfloat*)&stateGL->modelMatrix);
163  sp->uniformMatrix4fv("u_vMatrix", 1, (SLfloat*)&stateGL->viewMatrix);
164  sp->uniformMatrix4fv("u_pMatrix", 1, (SLfloat*)&stateGL->projectionMatrix);
165  sp->uniform1f("u_oneOverGamma", SLLight::oneOverGamma());
166 
167  // Create or update buffer for vertex position and indices
168  if (!_vao.vaoID() || _resX != widthPX || _resY != heightPX)
169  {
170  // texture width and height not yet valid on first call
171  _resX = widthPX;
172  _resY = heightPX;
173  _vao.clearAttribs();
174 
175  SLfloat left = 0, right = (float)_resX, bottom = 0, top = (float)_resY;
176 
177  // the background is centered and stretched to the screen boarders while keeping the textures aspect ratio
178  if (_texture && _fixAspectRatio)
179  {
180  SLfloat backgroundW, backgroundH;
182  {
183  // screen is wider than texture -> adjust background width
184  backgroundH = (float)_resY;
185  backgroundW = (float)_resY / (SLfloat)_texture->height() * (SLfloat)_texture->width();
186  }
187  else
188  {
189  // screen is more narrow than texture -> adjust background height
190  backgroundW = (float)_resX;
191  backgroundH = (float)_resX / (SLfloat)_texture->width() * (SLfloat)_texture->height();
192  }
193 
194  left = (_resX - backgroundW) * 0.5f;
195  right = backgroundW + left;
196  bottom = (_resY - backgroundH) * 0.5f;
197  top = backgroundH + bottom;
198 
199  _rect.set(left, bottom, backgroundW, backgroundH);
200  SL_LOG("SLBackground: width:%f height:%f left:%f bottom:%f", rect().width, rect().height, rect().x, rect().x);
201  }
202  else
203  _rect.set(0, 0, (float)widthPX, (float)heightPX);
204 
205  // Float array with vertex X & Y of corners
206  SLVVec2f P = {{left, top},
207  {left, bottom},
208  {right, top},
209  {right, bottom}};
210 
212 
213  // Indexes for a triangle strip
214  SLVushort I = {0, 1, 2, 3};
215  _vao.setIndices(&I, nullptr);
216 
217  if (_texture)
218  { // Float array of texture coordinates
219  SLVVec2f T = {{0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}};
220  _vao.setAttrib(AT_uv1, AT_uv1, &T);
221  _vao.generate(4);
222  }
223  else
224  { // Float array of colors of corners
225  SLVVec3f C = {{_colors[0].r, _colors[0].g, _colors[0].b},
226  {_colors[1].r, _colors[1].g, _colors[1].b},
227  {_colors[2].r, _colors[2].g, _colors[2].b},
228  {_colors[3].r, _colors[3].g, _colors[3].b}};
230  _vao.generate(4);
231  }
232  }
233 
234  // draw a textured or colored quad
235  if (_texture)
236  {
237  _texture->bindActive(0);
238  sp->uniform1i("u_matTexture0", 0);
239  }
240 
241  //////////////////////////////////////
243  //////////////////////////////////////
244 }
#define SL_LOG(...)
Definition: SL.h:233
vector< SLushort > SLVushort
Definition: SL.h:195
@ AT_position
Vertex position as a 2, 3 or 4 component vectors.
Definition: SLGLEnums.h:58
@ AT_color
Vertex color as 3 or 4 component vector.
Definition: SLGLEnums.h:62
@ AT_uv1
Vertex 1st texture coordinate as 2 component vector.
Definition: SLGLEnums.h:60
@ PT_triangleStrip
Definition: SLGLEnums.h:36
vector< SLVec2f > SLVVec2f
Definition: SLVec2.h:143
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
const SLRectf & rect() const
Definition: SLBackground.h:59
bool _fixAspectRatio
if true, the background is centered and stretched to the screen boarders while keeping the textures a...
Definition: SLBackground.h:76
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
SLint uniformMatrix4fv(const SLchar *name, SLsizei count, const SLfloat *value, GLboolean transpose=false) const
Passes a 4x4 float matrix values py pointer to the uniform variable "name".
SLint uniform1f(const SLchar *name, SLfloat v0) const
Passes the float value v0 to the uniform variable "name".
void useProgram()
SLint uniform1i(const SLchar *name, SLint v0) const
Passes the int values v0 to the uniform variable "name".
Singleton class holding all OpenGL states.
Definition: SLGLState.h:71
SLMat4f modelMatrix
Init all states.
Definition: SLGLState.h:89
void multiSample(SLbool state)
Definition: SLGLState.cpp:267
static SLGLState * instance()
Public static instance getter for singleton pattern.
Definition: SLGLState.h:74
SLMat4f viewMatrix
matrix for the active cameras view transform
Definition: SLGLState.h:91
SLMat4f projectionMatrix
matrix for projection transform
Definition: SLGLState.h:90
void depthTest(SLbool state)
Definition: SLGLState.cpp:172
SLuint height()
Definition: SLGLTexture.h:219
SLuint width()
Definition: SLGLTexture.h:218
void bindActive(SLuint texUnit=0)
void drawElementsAs(SLGLPrimitiveType primitiveType, SLuint numIndexes=0, SLuint indexOffsetBytes=0)
Draws the VAO by element indices with a primitive type.
void setAttrib(SLGLAttributeType type, SLint elementSize, SLint location, void *dataPointer, SLGLBufferType dataType=BT_float)
Adds a vertex attribute with data pointer and an element size.
SLuint vaoID() const
Returns either the VAO id or the VBO id.
void setIndices(SLuint numIndicesElements, SLGLBufferType indexDataType, void *indexDataElements, SLuint numIndicesEdges=0, void *indexDataEdges=nullptr)
Adds the index array for indexed element drawing.
void generate(SLuint numVertices, SLGLBufferUsage usage=BU_static, SLbool outputInterleaved=true, SLuint divisor=0)
Generates the VA & VB objects for a NO. of vertices.
static SLfloat oneOverGamma()
Definition: SLLight.h:203
void ortho(T l, T r, T b, T t, T n, T f)
Defines a orthographic projection matrix with a field of view angle.
Definition: SLMat4.h:911
void identity()
Sets the identity matrix.
Definition: SLMat4.h:1333
void set(const T X, const T Y, const T WIDTH, const T HEIGHT)
Definition: SLRect.h:42

◆ renderInScene()

void SLBackground::renderInScene ( const SLMat4f wm,
const SLVec3f LT,
const SLVec3f LB,
const SLVec3f RT,
const SLVec3f RB 
)

Draws the background as a quad on the far clipping plane.

We render the quad as a triangle strip:
LT RT +--—+ | /| | / | | / | | / | |/ | +--—+ LB RB

Definition at line 258 of file SLBackground.cpp.

263 {
264  SLGLState* stateGL = SLGLState::instance();
265 
266  // Get shader program
267  SLGLProgram* sp = _texture
270  sp->useProgram();
271  sp->uniformMatrix4fv("u_mMatrix", 1, (const SLfloat*)&wm);
272  sp->uniformMatrix4fv("u_vMatrix", 1, (const SLfloat*)&stateGL->viewMatrix);
273  sp->uniformMatrix4fv("u_pMatrix", 1, (const SLfloat*)&stateGL->projectionMatrix);
274 
275  // Create or update buffer for vertex position and indices
276  _vao.clearAttribs();
277 
278  // Float array with vertices
279  SLVVec3f P = {LT, LB, RT, RB};
281 
282  // Indexes for a triangle strip
283  SLVushort I = {0, 1, 2, 3};
284  _vao.setIndices(&I);
285 
286  if (_texture)
287  { // Float array of texture coordinates
288  SLVVec2f T = {{0.0f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f}, {1.0f, 0.0f}};
289  _vao.setAttrib(AT_uv1, AT_uv1, &T);
290  _vao.generate(4);
291  }
292  else
293  { // Float array of colors of corners
294  SLVVec3f C = {{_colors[0].r, _colors[0].g, _colors[0].b},
295  {_colors[1].r, _colors[1].g, _colors[1].b},
296  {_colors[2].r, _colors[2].g, _colors[2].b},
297  {_colors[3].r, _colors[3].g, _colors[3].b}};
299  _vao.generate(4);
300  }
301 
302  // draw a textured or colored quad
303  if (_texture)
304  {
305  _texture->bindActive(0);
306  sp->uniform1i("u_matTexture0", 0);
307  }
308 
309  ///////////////////////////////////////
311  ///////////////////////////////////////
312 }

◆ texture() [1/2]

SLGLTexture* SLBackground::texture ( )
inline

Definition at line 58 of file SLBackground.h.

58 { return _texture; }

◆ texture() [2/2]

void SLBackground::texture ( SLGLTexture backgroundTexture,
bool  fixAspectRatio = false 
)

If flag _repeatBlurred is true the texture is not distorted if its size does not fit to screen aspect ratio. Instead it is repeated.

Sets the background texture.

Definition at line 114 of file SLBackground.cpp.

115 {
116  _texture = backgroundTexture;
118  _isUniform = false;
120 
121  _vao.clearAttribs();
122 }
static SLbool fixAspectRatio
Flag if wnd aspect ratio should be fixed.
Definition: AppGLFW.cpp:42

Member Data Documentation

◆ _avgColor

SLCol4f SLBackground::_avgColor
private

Average color of all 4 corner colors.

Definition at line 64 of file SLBackground.h.

◆ _colorAttributeProgram

SLGLProgram* SLBackground::_colorAttributeProgram = nullptr
private

Definition at line 73 of file SLBackground.h.

◆ _colors

SLVCol4f SLBackground::_colors
private

Vector of 4 corner colors {TL,BL,TR,BR}.

Definition at line 63 of file SLBackground.h.

◆ _deletePrograms

bool SLBackground::_deletePrograms = false
private

Definition at line 74 of file SLBackground.h.

◆ _fixAspectRatio

bool SLBackground::_fixAspectRatio = false
private

if true, the background is centered and stretched to the screen boarders while keeping the textures aspect ratio

Definition at line 76 of file SLBackground.h.

◆ _isUniform

SLbool SLBackground::_isUniform
private

Flag if background has uniform color.

Definition at line 62 of file SLBackground.h.

◆ _rect

SLRectf SLBackground::_rect
private

Background rect (it may be different to viewport size)

Definition at line 69 of file SLBackground.h.

◆ _resX

SLint SLBackground::_resX
private

Background resolution in x-dir.

Definition at line 67 of file SLBackground.h.

◆ _resY

SLint SLBackground::_resY
private

Background resolution in y-dir.

Definition at line 68 of file SLBackground.h.

◆ _texture

SLGLTexture* SLBackground::_texture
private

Pointer to a background texture.

Definition at line 65 of file SLBackground.h.

◆ _textureError

SLGLTexture* SLBackground::_textureError
private

Pointer to a error texture if background texture is not available.

Definition at line 66 of file SLBackground.h.

◆ _textureOnlyProgram

SLGLProgram* SLBackground::_textureOnlyProgram = nullptr
private

Definition at line 72 of file SLBackground.h.

◆ _vao

SLGLVertexArray SLBackground::_vao
private

OpenGL Vertex Array Object for drawing.

Definition at line 70 of file SLBackground.h.


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