SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLText.cpp
Go to the documentation of this file.
1 /**
2  * \file SLText.cpp
3  * \authors Marcus Hudritsch
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 #include <SLScene.h>
12 #include <SLText.h>
13 
14 //-----------------------------------------------------------------------------
15 /**
16  * @brief Construct a new SLText::SLText object
17  * @remarks It is important that during instantiation NO OpenGL functions (gl*)
18  * get called because this constructor will be most probably called in a parallel
19  * thread from within an SLScene::registerAssetsToLoad or SLScene::assemble
20  * function. All objects that get rendered have to do their OpenGL initialization
21  * when they are used the first time during rendering in the main thread.
22  * @param text Text to be rendered
23  * @param font SLTexTont to be used
24  * @param color Color for font
25  * @param maxWidth Max. line width in pixels
26  * @param lineHeightFactor Line hight factor >= 1.0
27  */
29  SLTexFont* font,
30  SLCol4f color,
31  SLfloat maxWidth,
32  SLfloat lineHeightFactor)
33  : SLNode("Text")
34 {
35  assert(font);
36  _font = font;
37  _text = text;
38  _color = color;
39  _maxW = maxWidth;
40  _lineH = lineHeightFactor;
41 }
42 //-----------------------------------------------------------------------------
43 /*!
44 SLText::shapeDraw draws the text buffer objects
45 */
47 {
48  if (_drawBits.get(SL_DB_HIDDEN) || !SLGLState::instance()->blend()) return;
49 
50  // create buffer object for text once
51  if (!_vao.vaoID())
52  {
55  _font->magFiler(GL_LINEAR);
56  }
57  // Enable & build font texture with active OpenGL context
58  _font->bindActive();
59 
60  // Setup shader
62  SLGLState* state = SLGLState::instance();
63  sp->useProgram();
64  sp->uniformMatrix4fv("u_mMatrix", 1, (const SLfloat*)&updateAndGetWM());
65  sp->uniformMatrix4fv("u_vMatrix", 1, (const SLfloat*)&state->viewMatrix);
66  sp->uniformMatrix4fv("u_pMatrix", 1, (const SLfloat*)&state->projectionMatrix);
67  sp->uniform4fv("u_textColor", 1, (float*)&_color);
68  sp->uniform1i("u_matTexture0", 0);
69 
70  _vao.drawElementsAs(PT_triangles, (SLuint)_text.length() * 2 * 3);
71 }
72 //-----------------------------------------------------------------------------
73 /*!
74 SLText::statsRec updates the statistics.
75 */
77 {
78  stats.numBytes += (SLuint)sizeof(SLText);
79  stats.numBytes += (SLuint)_text.length();
80  stats.numNodes++;
81  stats.numTriangles += (SLuint)_text.length() * 2 + 2;
82 }
83 //-----------------------------------------------------------------------------
84 /*!
85 SLText::buildAABB builds and returns the axis-aligned bounding box.
86 */
88 {
90 
91  // calculate min & max in object space
92  SLVec3f minOS(0, 0, -0.01f);
93  SLVec3f maxOS(size.x, size.y, 0.01f);
94 
95  // apply world matrix: this overwrites the AABB of the group
96  if (updateAlsoAABBinOS)
97  _aabb.fromOStoWS(minOS, maxOS, updateAndGetWM());
98 
99  return _aabb;
100 }
101 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
#define SL_DB_HIDDEN
Flags an object as hidden.
Definition: SLDrawBits.h:20
@ PT_triangles
Definition: SLGLEnums.h:35
#define SL_ANISOTROPY_MAX
Definition: SLGLTexture.h:34
Defines an axis aligned bounding box.
Definition: SLAABBox.h:34
void fromOStoWS(const SLVec3f &minOS, const SLVec3f &maxOS, const SLMat4f &wm)
Recalculate min and max after transformation in world coords.
Definition: SLAABBox.cpp:46
SLbool get(SLuint bit)
Returns the specified bit.
Definition: SLDrawBits.h:69
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 uniform4fv(const SLchar *name, SLsizei count, const SLfloat *value) const
Passes 4 float values py pointer 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
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
void blend(SLbool state)
Definition: SLGLState.cpp:236
SLMat4f projectionMatrix
matrix for projection transform
Definition: SLGLState.h:90
void minFiler(SLint minF)
Definition: SLGLTexture.h:203
void magFiler(SLint magF)
Definition: SLGLTexture.h:204
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.
SLuint vaoID() const
Returns either the VAO id or the VBO id.
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
SLDrawBits _drawBits
node level drawing flags
Definition: SLNode.h:359
SLAABBox _aabb
axis aligned bounding box
Definition: SLNode.h:360
const SLMat4f & updateAndGetWM() const
Definition: SLNode.cpp:703
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
Texture Font class inherits SLGLTexture for alpha blended font rendering.
Definition: SLTexFont.h:40
void buildTextBuffers(SLGLVertexArray &vao, const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeight=1.5f)
Definition: SLTexFont.cpp:345
SLGLProgram * fontTexProgram()
Definition: SLTexFont.h:56
SLVec2f calcTextSize(const SLstring &text, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.5f)
Definition: SLTexFont.cpp:243
SLText creates a mesh using a textured font from SLTexFont.
Definition: SLText.h:30
SLstring text()
Definition: SLText.h:48
SLfloat _lineH
Line height factor for wrapped text.
Definition: SLText.h:61
void drawText(SLSceneView *sv)
Definition: SLText.cpp:46
SLVec2f size()
Definition: SLText.h:50
SLstring _text
Text of the button.
Definition: SLText.h:57
SLGLVertexArray _vao
Vertex array for rendering.
Definition: SLText.h:62
SLText(SLstring text, SLTexFont *font, SLCol4f txtCol=SLCol4f::WHITE, SLfloat maxWidth=0.0f, SLfloat lineHeightFactor=1.3f)
Construct a new SLText::SLText object.
Definition: SLText.cpp:28
void statsRec(SLNodeStats &stats) override
Definition: SLText.cpp:76
SLCol4f color()
Definition: SLText.h:49
SLTexFont * _font
Font pointer of the preloaded font.
Definition: SLText.h:58
SLfloat _maxW
Max. width in pix. for wrapped text.
Definition: SLText.h:60
SLCol4f _color
RGBA-Color of the text.
Definition: SLText.h:59
SLAABBox & updateAABBRec(SLbool updateAlsoAABBinOS) override
Definition: SLText.cpp:87
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30
Struct for scene graph statistics.
Definition: SLNode.h:37
SLuint numNodes
NO. of children nodes.
Definition: SLNode.h:38
SLuint numTriangles
NO. of triangles in mesh.
Definition: SLNode.h:47
SLuint numBytes
NO. of bytes allocated.
Definition: SLNode.h:39