SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLBackground.h
Go to the documentation of this file.
1 /**
2  * \file SLBackground.h
3  * \date August 2015
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLBACKGROUND_H
11 #define SLBACKGROUND_H
12 
13 #include <SLGLVertexArray.h>
14 #include <SLObject.h>
15 #include <math/SLRect.h>
16 
17 class SLGLTexture;
18 class SLGLProgram;
19 //-----------------------------------------------------------------------------
20 //! Defines a 2D-Background for the OpenGL framebuffer background.
21 /*! The background can either be defined with a texture or with 4 colors for
22 the corners of the frame buffer. For a uniform background color the color at
23 index 0 of the _colors vector is taken. Every instance of SLCamera has a
24 background that is displayed on the far clipping plane if the camera is not the
25 active one. The OpenGL rendering is done in SLSceneView::draw3DGL for the active
26 camera or in SLCamera::drawMeshes for inactive ones.
27 */
28 class SLBackground : public SLObject
29 {
30 public:
31  SLBackground(SLstring shaderDir);
32  SLBackground(SLGLProgram* textureOnlyProgram, SLGLProgram* colorAttributeProgram);
33  ~SLBackground();
34 
35  void render(SLint widthPX, SLint heightPX);
36  void renderInScene(const SLMat4f& wm,
37  const SLVec3f& LT,
38  const SLVec3f& LB,
39  const SLVec3f& RT,
40  const SLVec3f& RB);
41  SLCol4f colorAtPos(SLfloat x, SLfloat y, SLfloat width, SLfloat height);
42  void rebuild() { _vao.clearAttribs(); }
43 
44  // Setters
45  void colors(const SLCol4f& uniformColor);
46  void colors(const SLCol4f& topColor, const SLCol4f& bottomColor);
47  void colors(const SLCol4f& topLeftColor,
48  const SLCol4f& bottomLeftColor,
49  const SLCol4f& topRightColor,
50  const SLCol4f& bottomRightColor);
51  //! If flag _repeatBlurred is true the texture is not distorted if its size does not fit to screen aspect ratio. Instead it is repeated
52  void texture(SLGLTexture* backgroundTexture, bool fixAspectRatio = false);
53 
54  // Getters
55  SLVCol4f colors() { return _colors; }
56  SLCol4f avgColor() { return _avgColor; }
57  SLbool isUniform() const { return _isUniform; }
58  SLGLTexture* texture() { return _texture; }
59  const SLRectf& rect() const { return _rect; }
60 
61 private:
62  SLbool _isUniform; //!< Flag if background has uniform color
63  SLVCol4f _colors; //!< Vector of 4 corner colors {TL,BL,TR,BR}
64  SLCol4f _avgColor; //!< Average color of all 4 corner colors
65  SLGLTexture* _texture; //!< Pointer to a background texture
66  SLGLTexture* _textureError; //!< Pointer to a error texture if background texture is not available
67  SLint _resX; //!< Background resolution in x-dir.
68  SLint _resY; //!< Background resolution in y-dir.
69  SLRectf _rect; //!< Background rect (it may be different to viewport size)
70  SLGLVertexArray _vao; //!< OpenGL Vertex Array Object for drawing
71 
74  bool _deletePrograms = false;
75  //! if true, the background is centered and stretched to the screen boarders while keeping the textures aspect ratio
76  bool _fixAspectRatio = false;
77 };
78 //-----------------------------------------------------------------------------
79 #endif
static SLbool fixAspectRatio
Flag if wnd aspect ratio should be fixed.
Definition: AppGLFW.cpp:42
float SLfloat
Definition: SL.h:173
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
Wrapper class around OpenGL Vertex Array Objects (VAO)
vector< SLCol4f > SLVCol4f
Definition: SLVec4.h:241
Defines a 2D-Background for the OpenGL framebuffer background.
Definition: SLBackground.h:29
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
SLBackground(SLstring shaderDir)
The constructor initializes to a uniform BLACK background color.
SLVCol4f colors()
Definition: SLBackground.h:55
SLGLProgram * _textureOnlyProgram
Definition: SLBackground.h:72
void rebuild()
Definition: SLBackground.h:42
SLGLTexture * _texture
Pointer to a background texture.
Definition: SLBackground.h:65
const SLRectf & rect() const
Definition: SLBackground.h:59
SLGLTexture * texture()
Definition: SLBackground.h:58
SLGLVertexArray _vao
OpenGL Vertex Array Object for drawing.
Definition: SLBackground.h:70
void render(SLint widthPX, SLint heightPX)
Draws the background as 2D rectangle with OpenGL buffers.
SLCol4f avgColor()
Definition: SLBackground.h:56
SLRectf _rect
Background rect (it may be different to viewport size)
Definition: SLBackground.h:69
SLCol4f colorAtPos(SLfloat x, SLfloat y, SLfloat width, SLfloat height)
Returns the interpolated color at the pixel position p[x,y] used in raytracing.
bool _fixAspectRatio
if true, the background is centered and stretched to the screen boarders while keeping the textures a...
Definition: SLBackground.h:76
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
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.
SLbool isUniform() const
Definition: SLBackground.h:57
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
SLGLVertexArray encapsulates the core OpenGL drawing.
void clearAttribs()
Clears the attribute definition.
Base class for all other classes.
Definition: SLObject.h:23