RAMSES Documentation  27.0.130
Information for RAMSES users and developers
ramses-example-text-basic/src/main.cpp

Minimal text example. Shows the string "Hello World!" on the screen.

// -------------------------------------------------------------------------
// Copyright (C) 2018 BMW Car IT GmbH
// -------------------------------------------------------------------------
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// -------------------------------------------------------------------------
#include "ramses-client.h"
#include <thread>
int main(int argc, char* argv[])
{
// IMPORTANT NOTE: For simplicity and readability the example code does not check return values from API calls.
// This should not be the case for real applications.
// Text is rendered in a screen-space pixel precise coordinate system
// To achieve a texel-to-pixel ratio of 1 when rendering the text, it has to be
// rendered with an orthographic camera with planes matching the display size (pixel buffer)
const uint32_t displayWidth(1280);
const uint32_t displayHeight(480);
ramses::RamsesFramework framework(argc, argv);
ramses::RamsesClient& client(*framework.createClient("ExampleTextBasic"));
// create font registry to hold font memory and text cache to cache text meshes
ramses::FontRegistry fontRegistry;
ramses::TextCache textCache(*scene, fontRegistry, 1024u, 1024u);
framework.connect();
// Create orthographic camera, so that text pixels will match screen pixels
camera->setFrustum(0.0f, static_cast<float>(displayWidth), 0.0f, static_cast<float>(displayHeight), 0.1f, 1.f);
camera->setViewport(0, 0, displayWidth, displayHeight);
// create render pass
ramses::RenderPass* renderPass = scene->createRenderPass();
renderPass->setCamera(*camera);
ramses::RenderGroup* renderGroup = scene->createRenderGroup();
renderPass->addRenderGroup(*renderGroup);
// create appearance to be used for text
effectDesc.setVertexShaderFromFile("res/ramses-example-text-basic-effect.vert");
effectDesc.setFragmentShaderFromFile("res/ramses-example-text-basic-effect.frag");
ramses::Effect* textEffect = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "simpleTextShader");
// create font instance
ramses::FontId font = fontRegistry.createFreetype2Font("res/ramses-example-text-basic-Roboto-Bold.ttf");
const int32_t fontSize = 64;
ramses::FontInstanceId fontInstance = fontRegistry.createFreetype2FontInstance(font, fontSize);
// load rasterized glyphs for each character
const std::u32string string = U"Hello World!";
ramses::GlyphMetricsVector positionedGlyphs = textCache.getPositionedGlyphs(string, fontInstance);
ramses::GlyphMetricsVector trackedPositionedGlyphs = textCache.getPositionedGlyphs(string, fontInstance);
// add some character tracking (unit is em --> relative to fontSize)
// that reduces the spaces between the individual glyphs
ramses::TextCache::ApplyTrackingToGlyphs(trackedPositionedGlyphs, -50, fontSize);
// create RAMSES meshes/texture page to hold the glyphs and text geometry
const ramses::TextLineId textId = textCache.createTextLine(positionedGlyphs, *textEffect);
ramses::TextLine* textLine = textCache.getTextLine(textId);
const ramses::TextLineId trackedTextId = textCache.createTextLine(trackedPositionedGlyphs, *textEffect);
ramses::TextLine* trackedTextLine = textCache.getTextLine(trackedTextId);
// add the text meshes to the render pass to show them
textLine->meshNode->setTranslation(20.0f, 100.0f, -0.5f);
renderGroup->addMeshNode(*textLine->meshNode);
trackedTextLine->meshNode->setTranslation(20.0f, 20.0f, -0.5f);
renderGroup->addMeshNode(*trackedTextLine->meshNode);
// apply changes
scene->flush();
scene->publish();
std::this_thread::sleep_for(std::chrono::seconds(30));
scene->unpublish();
framework.disconnect();
return 0;
}
status_t setBlendingOperations(EBlendOperation operationColor, EBlendOperation operationAlpha)
Sets blending operation for color and alpha. Blending factors need to be set as well in order to enab...
status_t setBlendingFactors(EBlendFactor srcColor, EBlendFactor destColor, EBlendFactor srcAlpha, EBlendFactor destAlpha)
Sets blending factors for source/destination color/alpha. Blending operations need to be set as well ...
status_t setViewport(int32_t x, int32_t y, uint32_t width, uint32_t height)
Sets the viewport to be used when rendering with this camera.
status_t setFrustum(float leftPlane, float rightPlane, float bottomPlane, float topPlane, float nearPlane, float farPlane)
Sets camera frustum planes of the Camera.
An effect description holds all necessary information for an effect to be created.
Definition: EffectDescription.h:21
status_t setAttributeSemantic(const char *inputName, EEffectAttributeSemantic semanticType)
Sets an attribute semantic. Used to mark attributes as special inputs (eg. text specific inputs)....
status_t setVertexShaderFromFile(const char *shaderSourceFileName)
Reads and sets vertex shader source from file.
status_t setUniformSemantic(const char *inputName, EEffectUniformSemantic semanticType)
Sets an uniform semantic. Used for uniforms which are not locally available on the client,...
status_t setFragmentShaderFromFile(const char *shaderSourceFileName)
Reads and sets fragment shader source from file.
An effect describes how an object will be rendered to the screen.
Definition: Effect.h:26
Font registry can be used to load Freetype2 fonts and create font instances (optionally with Harfbuzz...
Definition: FontRegistry.h:22
FontInstanceId createFreetype2FontInstance(FontId fontId, uint32_t size, bool forceAutohinting=false)
Create Freetype2 font instance.
FontId createFreetype2Font(const char *fontPath)
Load Freetype2 font from file.
const Appearance * getAppearance() const
Returns the appearance.
status_t setTranslation(float x, float y, float z)
Sets the absolute translation the absolute values.
The OrthographicCamera is a local camera which defines an orthographic view into the scene.
Definition: OrthographicCamera.h:22
Entry point of RAMSES client API.
Definition: RamsesClient.h:34
Scene * createScene(sceneId_t sceneId, const SceneConfig &sceneConfig=SceneConfig(), const char *name=nullptr)
Create a new empty Scene.
Class representing ramses framework components that are needed to initialize an instance of ramses cl...
Definition: RamsesFramework.h:35
status_t connect()
Tries to establish a connection to the RAMSES system.
RamsesClient * createClient(const char *applicationName)
Create a new RamsesClient linked to this framework. Creation of multiple clients is supported....
status_t disconnect()
Disconnects the RamsesClient from the system.
The RenderGroup is a container used to collect renderables which are supposed to be rendered together...
Definition: RenderGroup.h:31
status_t addMeshNode(const MeshNode &mesh, int32_t orderWithinGroup=0)
Add a mesh to this RenderGroup. If a mesh is already contained in this RenderGroup only its render or...
The RenderPass is a container used to collect meshes which are supposed to be rendered together.
Definition: RenderPass.h:31
status_t setCamera(const Camera &camera)
Set the camera to use for rendering the objects of this renderpass.
status_t setClearFlags(uint32_t clearFlags)
Set the clear flags which enable/disable the clearing of the render target assigned to this RenderPas...
status_t addRenderGroup(const RenderGroup &renderGroup, int32_t orderWithinPass=0)
Add a RenderGroup to this RenderPass for rendering.
The Scene holds a scene graph. It is the essential class for distributing content to the ramses syste...
Definition: Scene.h:83
OrthographicCamera * createOrthographicCamera(const char *name=nullptr)
Creates a Orthographic Camera in this Scene.
Effect * createEffect(const EffectDescription &effectDesc, resourceCacheFlag_t cacheFlag=ResourceCacheFlag_DoNotCache, const char *name=nullptr)
Create a new Effect by parsing a GLSL shader described by an EffectDescription instance....
status_t unpublish()
Unpublish the scene from the ramses system.
status_t publish(EScenePublicationMode publicationMode=EScenePublicationMode_LocalAndRemote)
Publishes the scene to the ramses system.
RenderPass * createRenderPass(const char *name=nullptr)
Create a render pass in the scene.
RenderGroup * createRenderGroup(const char *name=nullptr)
Create a RenderGroup instance in the scene.
status_t flush(sceneVersionTag_t sceneVersionTag=InvalidSceneVersionTag)
Commits all changes done to the scene since the last flush or since scene creation....
Helper class to create strongly typed values out of various types.
Definition: StronglyTypedValue.h:23
Stores text data - texture atlas, meshes, glyph bitmap data. It is a cache because the content can be...
Definition: TextCache.h:84
static void ApplyTrackingToGlyphs(GlyphMetricsVector &glyphMetrics, int32_t trackingFactor, int32_t fontSize)
Apply character tracking (positive or negative) to each glyph in provided GlyphMetricsVector....
TextLineId createTextLine(const GlyphMetricsVector &glyphs, const Effect &effect)
Create the scene objects, e.g., mesh and appearance...etc, needed for rendering a text line (represen...
TextLine const * getTextLine(TextLineId textId) const
Get a const pointer to a (previously created) text line object.
GlyphMetricsVector getPositionedGlyphs(const std::u32string &str, FontInstanceId font)
Create and get glyph metrics for a string using a font instance.
@ EClearFlags_None
Definition: RamsesFrameworkTypes.h:257
@ ModelViewProjectionMatrix
Model-view-projection matrix 4x4.
constexpr const resourceCacheFlag_t ResourceCacheFlag_DoNotCache
Requests the render to not cache a resource. This is the default value.
Definition: RamsesFrameworkTypes.h:212
@ TextPositions
Text specific - vertex positions input. MUST be of type vec2.
@ TextTextureCoordinates
Text specific - texture coordinates input. MUST be of type vec2.
@ EBlendFactor_OneMinusSrcAlpha
Definition: AppearanceEnums.h:74
@ EBlendFactor_SrcAlpha
Definition: AppearanceEnums.h:73
@ EBlendOperation_Add
Definition: AppearanceEnums.h:58
std::vector< GlyphMetrics > GlyphMetricsVector
Vector of GlyphMetrics elements.
Definition: GlyphMetrics.h:42
int main(int argc, char *argv[])
Definition: main.cpp:21
Groups the scene objects needed to render a text line.
Definition: TextLine.h:34
MeshNode * meshNode
Mesh node that represents the text.
Definition: TextLine.h:36