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

Text Languages Example

// -------------------------------------------------------------------------
// 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[])
{
// 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("ExampleTextDirections"));
// 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 in text
effectDesc.setVertexShaderFromFile("res/ramses-example-text-languages-effect.vert");
effectDesc.setFragmentShaderFromFile("res/ramses-example-text-languages-effect.frag");
const ramses::Effect* textEffect = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "simpleTextShader");
// create font instances
const ramses::FontId hebrewFont = fontRegistry.createFreetype2Font("res/ramses-example-text-languages-Arimo-Regular.ttf");
const ramses::FontId japaneseFont = fontRegistry.createFreetype2Font("res/ramses-example-text-languages-WenQuanYiMicroHei.ttf");
const ramses::FontId cyrillicFont = fontRegistry.createFreetype2Font("res/ramses-example-text-languages-Roboto-Regular.ttf");
const ramses::FontId arabicFont = fontRegistry.createFreetype2Font("res/ramses-example-text-languages-DroidKufi-Regular.ttf");
const ramses::FontInstanceId cyrillicFontInst = fontRegistry.createFreetype2FontInstance(cyrillicFont, 40);
const ramses::FontInstanceId japaneseFontInst = fontRegistry.createFreetype2FontInstance(japaneseFont, 40);
const ramses::FontInstanceId hebrewFontInst = fontRegistry.createFreetype2FontInstance(hebrewFont, 40);
const ramses::FontInstanceId arabicFontInst = fontRegistry.createFreetype2FontInstance(arabicFont, 36);
const ramses::FontInstanceId arabicFontInst_shaped = fontRegistry.createFreetype2FontInstanceWithHarfBuzz(arabicFont, 36);
// Using UTF32 string to store translations of the word "chocolate" in different languages
const std::u32string cyrillicString = { 0x00000448, 0x0000043e, 0x0000043a, 0x0000043e, 0x0000043b, 0x00000430, 0x00000434 }; // "шоколад"
const std::u32string japaneseString = { 0x000030e7, 0x000030b3, 0x000030ec, 0x000030fc, 0x000030c8, }; // "チョコレート"
const std::u32string hebrewString = { 0x000005e9, 0x000005d5, 0x000005e7, 0x000005d5, 0x000005dc, 0x000005d3, }; // "שוקולד"
const std::u32string arabicString = { 0x00000634, 0x00000648, 0x00000643, 0x00000648, 0x00000644, 0x00000627, 0x0000062a, 0x00000629, }; //"شوكولاتة"
// create RAMSES meshes/texture page to hold the glyphs and text geometry
const ramses::TextLineId textId1 = textCache.createTextLine(textCache.getPositionedGlyphs(cyrillicString, cyrillicFontInst), *textEffect);
const ramses::TextLineId textId2 = textCache.createTextLine(textCache.getPositionedGlyphs(japaneseString, japaneseFontInst), *textEffect);
const ramses::TextLineId textId3 = textCache.createTextLine(textCache.getPositionedGlyphs(hebrewString, hebrewFontInst), *textEffect);
const ramses::TextLineId textId4 = textCache.createTextLine(textCache.getPositionedGlyphs(arabicString, arabicFontInst), *textEffect);
const ramses::TextLineId textId5 = textCache.createTextLine(textCache.getPositionedGlyphs(arabicString, arabicFontInst_shaped), *textEffect);
// position meshes for each text line on screen
ramses::TextLine* textLine1 = textCache.getTextLine(textId1);
ramses::TextLine* textLine2 = textCache.getTextLine(textId2);
ramses::TextLine* textLine3 = textCache.getTextLine(textId3);
ramses::TextLine* textLine4 = textCache.getTextLine(textId4);
ramses::TextLine* textLine5 = textCache.getTextLine(textId5);
for (auto textLine : { textLine1, textLine2, textLine3, textLine4, textLine5 })
{
}
textLine1->meshNode->setTranslation(20.0f, 220.0f, -0.5f);
textLine2->meshNode->setTranslation(20.0f, 170.0f, -0.5f);
textLine3->meshNode->setTranslation(20.0f, 120.0f, -0.5f);
textLine4->meshNode->setTranslation(20.0f, 70.0f , -0.5f);
textLine5->meshNode->setTranslation(20.0f, 20.0f , -0.5f);
renderGroup->addMeshNode(*textLine1->meshNode);
renderGroup->addMeshNode(*textLine2->meshNode);
renderGroup->addMeshNode(*textLine3->meshNode);
renderGroup->addMeshNode(*textLine4->meshNode);
renderGroup->addMeshNode(*textLine5->meshNode);
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 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.
FontInstanceId createFreetype2FontInstanceWithHarfBuzz(FontId fontId, uint32_t size, bool forceAutohinting=false)
Create Freetype2 font instance with Harfbuzz shaping.
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
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
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