SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AppDemoScene2Dand3DText.cpp
Go to the documentation of this file.
1 /**
2  * \file AppDemoScene2Dand3DText.cpp
3  * \brief Implementation for an SLScene inherited class
4  * \details For more info about App framework and the scene assembly see:
5  * https://cpvrlab.github.io/SLProject4/app-framework.html
6  * \date May 2024
7  * \authors Marcus Hudritsch, Marino von Wattenwyl
8  * \copyright http://opensource.org/licenses/GPL-3.0
9  * \remarks Please use clangformat to format the code. See more code style on
10  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
11 */
12 
14 #include <AppCommon.h>
15 #include <SLAssetManager.h>
16 #include <SLLightSpot.h>
17 #include <SLText.h>
18 #include <SLSphere.h>
19 
20 //-----------------------------------------------------------------------------
22 {
23  info("All 3D objects are in the _root3D scene and the center text is in the _root2D scene "
24  "and rendered in orthographic projection in screen space.");
25 }
26 //-----------------------------------------------------------------------------
27 //! All assets the should be loaded in parallel must be registered in here.
29 {
30 }
31 //-----------------------------------------------------------------------------
32 //! After parallel loading of the assets the scene gets assembled in here.
34 {
35  SLMaterial* m1 = new SLMaterial(am, "m1", SLCol4f::RED);
36 
37  SLCamera* cam1 = new SLCamera("Camera 1");
38  cam1->clipNear(0.1f);
39  cam1->clipFar(100);
40  cam1->translation(0, 0, 5);
41  cam1->lookAt(0, 0, 0);
42  cam1->focalDist(5);
43  cam1->background().colors(SLCol4f(0.1f, 0.1f, 0.1f));
44  cam1->setInitialState();
46 
47  SLLightSpot* light1 = new SLLightSpot(am,
48  this,
49  10,
50  10,
51  10,
52  0.3f);
53  light1->powers(0.2f, 0.8f, 1.0f);
54  light1->attenuation(1, 0, 0);
55 
56  // Because all text objects get their sizes in pixels we have to scale them down
57  SLfloat scale = 0.01f;
58  SLstring txt = "This is text in 3D with font07";
60  SLNode* t07 = new SLText(txt, SLAssetManager::font07);
61  t07->translate(-size.x * 0.5f * scale, 1.0f, 0);
62  t07->scale(scale);
63 
64  txt = "This is text in 3D with font09";
66  SLNode* t09 = new SLText(txt, SLAssetManager::font09);
67  t09->translate(-size.x * 0.5f * scale, 0.8f, 0);
68  t09->scale(scale);
69 
70  txt = "This is text in 3D with font12";
72  SLNode* t12 = new SLText(txt, SLAssetManager::font12);
73  t12->translate(-size.x * 0.5f * scale, 0.6f, 0);
74  t12->scale(scale);
75 
76  txt = "This is text in 3D with font20";
78  SLNode* t20 = new SLText(txt, SLAssetManager::font20);
79  t20->translate(-size.x * 0.5f * scale, -0.8f, 0);
80  t20->scale(scale);
81 
82  txt = "This is text in 3D with font22";
84  SLNode* t22 = new SLText(txt, SLAssetManager::font22);
85  t22->translate(-size.x * 0.5f * scale, -1.2f, 0);
86  t22->scale(scale);
87 
88  // Now create 2D text but don't scale it (all sizes in pixels)
89  txt = "This is text in 2D with font16";
91  SLNode* t2D16 = new SLText(txt, SLAssetManager::font16);
92  t2D16->translate(-size.x * 0.5f, 0, 0);
93 
94  // Assemble 3D scene as usual with camera and light
95  SLNode* scene3D = new SLNode("root3D");
96  this->root3D(scene3D);
97  scene3D->addChild(cam1);
98  scene3D->addChild(light1);
99  scene3D->addChild(new SLNode(new SLSphere(am,
100  0.5f,
101  32,
102  32,
103  "Sphere",
104  m1)));
105  scene3D->addChild(t07);
106  scene3D->addChild(t09);
107  scene3D->addChild(t12);
108  scene3D->addChild(t20);
109  scene3D->addChild(t22);
110 
111  // Assemble 2D scene
112  SLNode* scene2D = new SLNode("root2D");
113  this->root2D(scene2D);
114  scene2D->addChild(t2D16);
115 
116  sv->camera(cam1);
117  sv->doWaitOnIdle(true);
118 }
119 //-----------------------------------------------------------------------------
The AppCommon class holds the top-level instances of the app-demo.
Class declaration for an SLScene inherited class.
float SLfloat
Definition: SL.h:173
string SLstring
Definition: SL.h:158
SLVec4< SLfloat > SLCol4f
Definition: SLVec4.h:237
static SLDeviceRotation devRot
Mobile device rotation from IMU.
Definition: AppCommon.h:64
static SLDeviceLocation devLoc
Mobile device location from GPS.
Definition: AppCommon.h:65
void assemble(SLAssetManager *am, SLSceneView *sv) override
After parallel loading of the assets the scene gets assembled in here.
void registerAssetsToLoad(SLAssetLoader &al) override
All scene specific assets have to be registered for async loading in here.
Toplevel holder of the assets meshes, materials, textures and shaders.
static SLTexFont * font20
20 pixel high fixed size font
static SLTexFont * font09
9 pixel high fixed size font
static SLTexFont * font22
22 pixel high fixed size font
static SLTexFont * font07
7 pixel high fixed size font
static SLTexFont * font12
12 pixel high fixed size font
static SLTexFont * font16
16 pixel high fixed size font
void colors(const SLCol4f &uniformColor)
Sets a uniform background color.
Active or visible camera node class.
Definition: SLCamera.h:54
void clipFar(const SLfloat cFar)
Definition: SLCamera.h:109
void clipNear(const SLfloat cNear)
Definition: SLCamera.h:108
void devRotLoc(SLDeviceRotation *devRot, SLDeviceLocation *devLoc)
Definition: SLCamera.h:120
void focalDist(const SLfloat f)
Definition: SLCamera.h:116
SLBackground & background()
Definition: SLCamera.h:165
void attenuation(const SLfloat kConstant, const SLfloat kLinear, const SLfloat kQuadratic)
Definition: SLLight.h:116
void powers(SLfloat ambiPow, SLfloat diffPow, SLfloat specPow, const SLCol4f &ambiDiffSpecCol=SLCol4f::WHITE)
Sets the ambient, diffuse and specular powers all with the same color.
Definition: SLLight.h:74
SLLightSpot class for a spot light source.
Definition: SLLightSpot.h:36
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
void addChild(SLNode *child)
Definition: SLNode.cpp:207
void translation(const SLVec3f &pos, SLTransformSpace relativeTo=TS_parent)
Definition: SLNode.cpp:828
void scale(SLfloat s)
Definition: SLNode.h:640
void setInitialState()
Definition: SLNode.cpp:1084
void lookAt(SLfloat targetX, SLfloat targetY, SLfloat targetZ, SLfloat upX=0, SLfloat upY=1, SLfloat upZ=0, SLTransformSpace relativeTo=TS_world)
Definition: SLNode.h:652
void translate(const SLVec3f &vec, SLTransformSpace relativeTo=TS_object)
Definition: SLNode.cpp:906
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
SLNode * root2D()
Definition: SLScene.h:100
SLNode * root3D()
Definition: SLScene.h:99
friend class SLNode
Definition: SLScene.h:48
SLstring & info()
Definition: SLScene.h:102
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69
void camera(SLCamera *camera)
Definition: SLSceneView.h:145
void doWaitOnIdle(SLbool doWI)
Definition: SLSceneView.h:149
SLSphere creates a sphere mesh based on SLSpheric w. 180 deg polar angle.
Definition: SLSphere.h:33
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
T x
Definition: SLVec2.h:30
static SLVec4 RED
Definition: SLVec4.h:216