RAMSES Documentation  27.0.130
Information for RAMSES users and developers
ramses-example-local-client-dcsm/src/main.cpp

Local Client with DCSM Example

// -------------------------------------------------------------------------
// Copyright (C) 2021 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/.
// -------------------------------------------------------------------------
static const char* const providerFragmentShaderCode = R"GLSL(
#version 100
uniform highp vec4 color;
void main(void)
{
gl_FragColor = color + vec4(0.1);
}
)GLSL";
static const char* const providerVertexShaderCode = R"GLSL(
#version 100
uniform highp mat4 mvpMatrix;
attribute vec3 a_position;
void main()
{
gl_Position = mvpMatrix * vec4(a_position, 1.0);
}
)GLSL";
static const char* const consumerVertShader = R"GLSL(
#version 100
uniform highp mat4 mvpMatrix;
attribute vec3 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = mvpMatrix * vec4(a_position, 1.0);
v_texcoord = a_texcoord;
}
)GLSL";
static const char* const consumerFragshader = R"GLSL(
#version 100
precision mediump float;
uniform sampler2D textureSampler;
varying lowp vec2 v_texcoord;
void main(void)
{
gl_FragColor = vec4(0.1) + texture2D(textureSampler, v_texcoord);
}
)GLSL";
#include "ramses-client.h"
#include <unordered_set>
#include <thread>
#include <array>
using namespace ramses;
{
public:
LocalDCSMExample(int32_t argc, char const* const* argv)
{
// framework
ramses::RamsesFrameworkConfig config(argc, argv);
config.setRequestedRamsesShellType(ramses::ERamsesShellType_Console); //needed for automated test of examples
m_framework = new RamsesFramework(config);
// provider side
client = m_framework->createClient("ramses-local-client-test");
dcsmProvider = m_framework->createDcsmProvider();
// consumer / renderer side
ramses::RendererConfig rendererConfig(argc, argv);
renderer = m_framework->createRenderer(rendererConfig);
dcsmControl = renderer->createDcsmContentControl();
renderer->startThread();
ramses::DisplayConfig displayConfig(argc, argv);
display = renderer->createDisplay(displayConfig);
ob = renderer->createOffscreenBuffer(display, 640, 480);
dcsmControl->addContentCategory(localCategory, display, {{ 1280, 480 }, { 0, 0, 1280, 480 }, {0,0,0,0}} );
dcsmControl->update(0, *this);
renderer->flush();
}
~LocalDCSMExample() override
{
renderer->stopThread();
m_framework->destroyRenderer(*renderer);
m_framework->destroyClient(*client);
delete m_framework;
}
// Provider side callbacks
void contentHide(ContentID /*contentID*/, AnimationInformation /*animInfo*/) override
{
}
void contentShow(ContentID /*contentID*/, AnimationInformation /*animInfo*/) override
{
}
void stopOfferAccepted(ContentID /*contentID*/, AnimationInformation /*animInfo*/) override
{
}
void contentSizeChange(ContentID /*contentID*/, const CategoryInfoUpdate& /*categoryInfo*/, AnimationInformation /*animInfo*/) override
{
}
void contentReadyRequested(ContentID /*contentID*/) override
{
}
void contentRelease(ContentID /*contentID*/, AnimationInformation /*animInfo*/) override
{
}
// consumer side callbacks
void contentAvailable(ContentID contentID, Category ) override
{
// ramp up content
dcsmControl->requestContentReady(contentID, 0);
}
void contentReady(ContentID contentID, DcsmContentControlEventResult ) override
{
if (contentID == providerContentID)
{
dcsmControl->assignContentToDisplayBuffer(contentID, ob);
m_providerSceneReady = true;
linkOffscreenBufferIfReady();
}
if (contentID == consumerContentID)
{
m_consumerSceneReady = true;
linkOffscreenBufferIfReady();
}
dcsmControl->showContent(contentID, {});
}
void offscreenBufferCreated(displayId_t /*displayId*/, displayBufferId_t /*offscreenBufferId*/, ERendererEventResult result) override
{
if (result == ERendererEventResult_OK)
{
m_offscreenBufferCreated = true;
linkOffscreenBufferIfReady();
}
}
void windowClosed(displayId_t /*displayId*/) override
{
m_windowClosed = true;
}
void run()
{
createLocalConsumerScene();
offerScenesViaDcsm();
while (!m_windowClosed)
{
dcsmControl->update(0, *this);
renderer->dispatchEvents(*this);
dcsmProvider->dispatchEvents(*this);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
private:
{
ramses::Scene* scene = client->createScene(providerSceneId, ramses::SceneConfig(), "local client example scene");
auto* camera = scene->createPerspectiveCamera("my camera");
camera->setViewport(0, 0, 640u, 480u);
camera->setFrustum(19.f, 1280.f / 480.f, 0.1f, 1500.f);
ramses::RenderPass* renderPass = scene->createRenderPass("my render pass");
renderPass->setCamera(*camera);
ramses::RenderGroup* renderGroup = scene->createRenderGroup();
renderPass->addRenderGroup(*renderGroup);
// prepare triangle geometry: vertex position array and index array
float vertexPositionsArray[] = { -1.f, 0.f, -6.f, 1.f, 0.f, -6.f, 0.f, 1.f, -6.f };
ramses::ArrayResource* vertexPositions = scene->createArrayResource(ramses::EDataType::Vector3F, 3, vertexPositionsArray);
uint16_t indicesArray[] = { 0, 1, 2 };
// create an appearance for red triangle
effectDesc.setVertexShader(providerVertexShaderCode);
effectDesc.setFragmentShader(providerFragmentShaderCode);
ramses::Effect* effect = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "glsl shader");
ramses::Appearance* appearance = scene->createAppearance(*effect, "triangle appearance");
ramses::GeometryBinding* geometry = scene->createGeometryBinding(*effect, "triangle geometry");
geometry->setIndices(*indices);
ramses::AttributeInput positionsInput;
effect->findAttributeInput("a_position", positionsInput);
geometry->setInputBuffer(positionsInput, *vertexPositions);
effect->findUniformInput("color", colorInput);
// create a mesh node to define the triangle with chosen appearance
ramses::MeshNode* meshNode = scene->createMeshNode("triangle mesh node");
meshNode->setTranslation(0.0f, 0.0f, -5.0f);
meshNode->setAppearance(*appearance);
meshNode->setGeometryBinding(*geometry);
// mesh needs to be added to a render group that belongs to a render pass with camera in order to be rendered
renderGroup->addMeshNode(*meshNode);
// create splines with animation keys
ramses::SplineLinearFloat* spline1 = animationSystem->createSplineLinearFloat("spline1");
spline1->setKey(0u, 0.f);
spline1->setKey(4000u, 360.f);
// create animated property for each translation node with single component animation
// create three animations
ramses::Animation* animation1 = animationSystem->createAnimation(*animProperty1, *spline1, "animation1");
// create animation sequence and add animation
ramses::AnimationSequence* sequence = animationSystem->createAnimationSequence();
sequence->addAnimation(*animation1);
// set animation properties (optional)
sequence->setAnimationLooping(*animation1);
sequence->setPlaybackSpeed(0.5f);
// start animation sequence
animationSystem->updateLocalTime();
sequence->start();
appearance->setInputValueVector4f(colorInput, 1.0f, 0.0f, 0.3f, 1.0f);
scene->flush();
}
void createLocalConsumerScene()
{
ramses::Scene* scene = client->createScene(consumerSceneId, ramses::SceneConfig(), "local client example scene");
camera->setTranslation(0.0f, 0.0f, 5.0f);
camera->setFrustum(-2.f, 2.f, -2.f, 2.f, 0.1f, 100.f);
camera->setViewport(0, 0u, 1280, 480);
ramses::RenderPass* renderPass = scene->createRenderPass("my render pass");
renderPass->setCamera(*camera);
ramses::RenderGroup* renderGroup = scene->createRenderGroup();
renderPass->addRenderGroup(*renderGroup);
// prepare quad geometry: vertex position array and index array
const uint16_t indicesArray[] = { 0, 1, 2, 2, 1, 3 };
const ramses::ArrayResource* sharedIndices = scene->createArrayResource(ramses::EDataType::UInt16, 6, indicesArray);
const float vertexPositionsArray[] =
{
-1.f, -1.f, 0.f,
1.f, -1.f, 0.f,
-1.f, 1.f, 0.f,
1.f, 1.f, 0.f
};
const ramses::ArrayResource* sharedVertexPositions = scene->createArrayResource(ramses::EDataType::Vector3F, 4, vertexPositionsArray);
const float textureCoordsArray[] = { 0.f, 1.f, 1.f, 1.f, 0.f, 0.f, 1.f, 0.f };
const ramses::ArrayResource* sharedTextureCoords = scene->createArrayResource(ramses::EDataType::Vector2F, 4, textureCoordsArray);
effectDesc.setVertexShader(consumerVertShader);
effectDesc.setFragmentShader(consumerFragshader);
auto effect = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "");
ramses::Appearance* appearance = scene->createAppearance(*effect, "quad appearance");
ramses::GeometryBinding* geometry = scene->createGeometryBinding(*effect, "quad geometry");
geometry->setIndices(*sharedIndices);
ramses::AttributeInput positionsInput;
effect->findAttributeInput("a_position", positionsInput);
geometry->setInputBuffer(positionsInput, *sharedVertexPositions);
ramses::AttributeInput texCoordsInput;
effect->findAttributeInput("a_texcoord", texCoordsInput);
geometry->setInputBuffer(texCoordsInput, *sharedTextureCoords);
ramses::MeshNode* meshNode = scene->createMeshNode();
meshNode->setAppearance(*appearance);
meshNode->setGeometryBinding(*geometry);
const std::array<uint8_t, 4> pxData{ {0xff, 0x0, 0x0, 0xff} };
const ramses::MipLevelData mipLevelData(4, pxData.data());
const ramses::Texture2D* texture = scene->createTexture2D(ramses::ETextureFormat::RGBA8, 1u, 1u, 1, &mipLevelData, false, {}, ramses::ResourceCacheFlag_DoNotCache);
ramses::UniformInput textureUnif;
effect->findUniformInput("textureSampler", textureUnif);
meshNode->getAppearance()->setInputTexture(textureUnif, *textureSampler);
renderGroup->addMeshNode(*meshNode);
scene->createTextureConsumer(*textureSampler, ramses::dataConsumerId_t{1});
scene->flush();
}
void offerScenesViaDcsm()
{
dcsmProvider->markContentReady(providerContentID);
dcsmProvider->markContentReady(consumerContentID);
dcsmControl->update(0, *this);
dcsmProvider->dispatchEvents(*this);
}
void linkOffscreenBufferIfReady()
{
if (m_providerSceneReady && m_consumerSceneReady && m_offscreenBufferCreated)
{
dcsmControl->linkOffscreenBuffer(ob, consumerContentID, ramses::dataConsumerId_t{ 1 });
}
}
ramses::DcsmProvider* dcsmProvider;
bool m_providerSceneReady = false;
bool m_consumerSceneReady = false;
bool m_offscreenBufferCreated = false;
bool m_windowClosed = false;
};
int main(int argc, char* argv[])
{
LocalDCSMExample example (argc, argv);
example.run();
}
Definition: main.cpp:81
The AnimatedProperty holds a reference to data that can be animated.
Definition: AnimatedProperty.h:20
The AnimationSequence is a container for multiple animations. AnimationSequence has its own virtual t...
Definition: AnimationSequence.h:26
status_t start(timeMilliseconds_t offset=0)
Starts the sequence of animations. Offset can be used to postpone start if positive or start sequence...
status_t addAnimation(const Animation &animation, sequenceTimeStamp_t startTimeInSequence=0u, sequenceTimeStamp_t stopTimeInSequence=0u)
Add animation to the sequence. Animation will be placed on to sequence time line at given time stamp ...
status_t setPlaybackSpeed(float playbackSpeed)
Sets sequence playback speed affecting all animations within the sequence. Default sequence playback ...
status_t setAnimationLooping(const Animation &animation, timeMilliseconds_t loopDuration=0)
Enables animation looping. When animation reaches last spline key (or loopDuration passes) it begins ...
The AnimationSystemRealTime is a special version of AnimationSystem that is designed to use system ti...
Definition: AnimationSystemRealTime.h:33
status_t updateLocalTime(globalTimeStamp_t systemTime=0u)
Sets the local animation system to a given time. The time used should always be system time,...
AnimationSequence * createAnimationSequence(const char *name=nullptr)
Creates AnimationSequence that can hold references to multiple animations and control them together.
SplineLinearFloat * createSplineLinearFloat(const char *name=nullptr)
Creates a spline in this animation system using float data type and linear interpolation.
AnimatedProperty * createAnimatedProperty(const Node &propertyOwner, EAnimatedProperty property, EAnimatedPropertyComponent propertyComponent=EAnimatedPropertyComponent_All, const char *name=nullptr)
Create a new animated property for Node.
Animation * createAnimation(const AnimatedProperty &animatedProperty, const Spline &spline, const char *name=nullptr)
Creates Animation that can animate given property using given spline.
The Animation combines spline with one or more AnimatedProperty instances and allows control of the a...
Definition: Animation.h:22
The Appearance describes how an object should look like. This includes GLSL uniform values,...
Definition: Appearance.h:34
status_t setInputValueVector4f(const UniformInput &input, float x, float y, float z, float w)
Sets value of the input.
status_t setInputTexture(const UniformInput &input, const TextureSampler &textureSampler)
Sets texture sampler to the input.
The ArrayResource stores a data array of a given type. The data is immutable. The resource can be use...
Definition: ArrayResource.h:26
The AttributeInput is a description of an attribute effect input.
Definition: AttributeInput.h:22
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.
Convenience empty implementation of IDcsmContentControlEventHandler that can be used to derive from w...
Definition: IDcsmContentControlEventHandler.h:305
DcsmContentControl provides way to interact with both Dcsm (as consumer) and renderer content control...
Definition: DcsmContentControl.h:70
Class used to offer ramses content and meta infos to a consumer and synchronize actions between clien...
Definition: DcsmProvider.h:28
The DisplayConfig holds a set of parameters to be used to initialize a display.
Definition: DisplayConfig.h:22
An effect description holds all necessary information for an effect to be created.
Definition: EffectDescription.h:21
status_t setVertexShader(const char *shaderSource)
Sets vertex shader source from string.
status_t setFragmentShader(const char *shaderSource)
Sets fragment shader source from string.
status_t setUniformSemantic(const char *inputName, EEffectUniformSemantic semanticType)
Sets an uniform semantic. Used for uniforms which are not locally available on the client,...
An effect describes how an object will be rendered to the screen.
Definition: Effect.h:26
status_t findUniformInput(const char *inputName, UniformInput &uniformInput) const
Finds uniform input by input name.
status_t findAttributeInput(const char *inputName, AttributeInput &attributeInput) const
Finds attribute input by input name.
A geometry binding together with an appearance describe how an object will be rendered to the screen.
Definition: GeometryBinding.h:25
status_t setIndices(const ArrayResource &indicesResource)
Assign a data array with data type UInt16 or UInt32 to be used when accessing vertex data.
status_t setInputBuffer(const AttributeInput &attributeInput, const ArrayResource &arrayResource, uint32_t instancingDivisor=0)
Assign a data array resource to a given effect attribute input.
An Interface for a class, whose functions are called as reaction to DcsmConsumer communication after ...
Definition: IDcsmProviderEventHandler.h:24
The MeshNode holds all information which is needed to render an object to the screen.
Definition: MeshNode.h:25
status_t setAppearance(Appearance &appearance)
Sets the Appearance of the MeshNode.
const Appearance * getAppearance() const
Returns the appearance.
status_t setGeometryBinding(GeometryBinding &geometry)
Sets the GeometryBinding of the MeshNode.
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
The RamsesFrameworkConfig holds a set of parameters to be used to initialize ramses.
Definition: RamsesFrameworkConfig.h:23
Class representing ramses framework components that are needed to initialize an instance of ramses cl...
Definition: RamsesFramework.h:35
RamsesRenderer is the main renderer component which provides API to configure and control the way con...
Definition: RamsesRenderer.h:37
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 RendererConfig holds a set of parameters to be used to initialize a renderer.
Definition: RendererConfig.h:26
Convenience empty implementation of IRendererEventHandler that can be used to derive from when only s...
Definition: IRendererEventHandler.h:194
The SceneConfig holds a set of parameters to be used when creating a scene.
Definition: SceneConfig.h:22
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.
MeshNode * createMeshNode(const char *name=nullptr)
Creates a scene graph MeshNode. MeshNode is a Node with additional properties and bindings that repre...
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....
GeometryBinding * createGeometryBinding(const Effect &effect, const char *name=nullptr)
Creates a new GeometryBinding.
ArrayResource * createArrayResource(EDataType type, uint32_t numElements, const void *arrayData, resourceCacheFlag_t cacheFlag=ResourceCacheFlag_DoNotCache, const char *name=nullptr)
Create a new ArrayResource. It makes a copy of the given data of a certain type as a resource,...
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.
Appearance * createAppearance(const Effect &effect, const char *name=nullptr)
Creates a new Appearance.
Texture2D * createTexture2D(ETextureFormat format, uint32_t width, uint32_t height, uint32_t mipMapCount, const MipLevelData mipLevelData[], bool generateMipChain=false, const TextureSwizzle &swizzle={}, resourceCacheFlag_t cacheFlag=ResourceCacheFlag_DoNotCache, const char *name=nullptr)
Create a new Texture2D. It makes a copy of the given data of a certain type as a resource,...
RenderGroup * createRenderGroup(const char *name=nullptr)
Create a RenderGroup instance in the scene.
PerspectiveCamera * createPerspectiveCamera(const char *name=nullptr)
Creates a Perspective Camera in this Scene.
AnimationSystemRealTime * createRealTimeAnimationSystem(uint32_t flags=EAnimationSystemFlags_Default, const char *name=nullptr)
Create a new animation system that is designed to work with system time. The animation system will be...
status_t flush(sceneVersionTag_t sceneVersionTag=InvalidSceneVersionTag)
Commits all changes done to the scene since the last flush or since scene creation....
TextureSampler * createTextureSampler(ETextureAddressMode wrapUMode, ETextureAddressMode wrapVMode, ETextureSamplingMethod minSamplingMethod, ETextureSamplingMethod magSamplingMethod, const Texture2D &texture, uint32_t anisotropyLevel=1, const char *name=nullptr)
Creates a texture sampler object.
status_t createTextureConsumer(const TextureSampler &sampler, dataConsumerId_t dataId)
Annotates a ramses::TextureSampler as a content consumer. Texture provider and texture consumer can b...
The SplineLinearFloat stores spline keys of type float that can be used for animation with linear int...
Definition: SplineLinearFloat.h:21
status_t setKey(splineTimeStamp_t timeStamp, float value)
Sets a spline key at given time with given value.
Helper class to create strongly typed values out of various types.
Definition: StronglyTypedValue.h:23
Texture represents a 2-D texture resource.
Definition: Texture2D.h:24
The TextureSampler holds a texture and its sampling parameters.
Definition: TextureSampler.h:29
The UniformInput is a description of an uniform effect input.
Definition: UniformInput.h:22
The RAMSES namespace contains all client side objects and functions used to implement RAMSES applicat...
Definition: AnimatedProperty.h:15
DcsmContentControlEventResult
ramses::DcsmContentControl event result used in some event handler callbacks
Definition: IDcsmContentControlEventHandler.h:21
@ ETextureSamplingMethod_Linear
Definition: TextureEnums.h:21
@ ETextureAddressMode_Repeat
Definition: TextureEnums.h:33
@ EAnimatedPropertyComponent_Z
Definition: AnimatedProperty.h:68
@ EScenePublicationMode_LocalOnly
Definition: EScenePublicationMode.h:23
StronglyTypedValue< uint64_t, 0, ContentIDTag > ContentID
Identifier for DCSM content. Must be globally unique.
Definition: DcsmApiTypes.h:25
@ EAnimatedProperty_Rotation
Definition: AnimatedProperty.h:77
@ EClearFlags_None
Definition: RamsesFrameworkTypes.h:257
@ EAnimationSystemFlags_Default
Definition: AnimationSystemEnums.h:19
@ ModelViewProjectionMatrix
Model-view-projection matrix 4x4.
@ Vector2F
two components of type float per data element
@ UInt16
one component of type uint16_t per data element
@ Vector3F
three components of type float per data element
@ ERamsesShellType_Console
Definition: RamsesFrameworkTypes.h:169
constexpr const resourceCacheFlag_t ResourceCacheFlag_DoNotCache
Requests the render to not cache a resource. This is the default value.
Definition: RamsesFrameworkTypes.h:212
StronglyTypedValue< uint32_t, std::numeric_limits< uint32_t >::max(), struct DisplayIdTag > displayId_t
Display identifier used to refer to display in renderer API and dispatched callbacks.
Definition: RamsesFrameworkTypes.h:57
StronglyTypedValue< uint64_t, 0, CategoryTag > Category
Category descriptor for DCSM.
Definition: DcsmApiTypes.h:33
ERendererEventResult
Specifies the result of the operation referred to by renderer event.
Definition: Types.h:94
@ ERendererEventResult_OK
Event referring to an operation that succeeded.
Definition: Types.h:95
StronglyTypedValue< uint32_t, std::numeric_limits< uint32_t >::max(), struct DisplayBufferIdTag > displayBufferId_t
Display buffer identifier referring to either a display's framebuffer or a created offscreen buffer.
Definition: RamsesFrameworkTypes.h:60
int main(int argc, char *argv[])
Definition: main.cpp:21
constexpr ramses::ContentID consumerContentID
Definition: main.cpp:77
constexpr ramses::ContentID providerContentID
Definition: main.cpp:76
constexpr ramses::sceneId_t consumerSceneId(2u)
constexpr ramses::sceneId_t providerSceneId(1u)
constexpr ramses::Category localCategory
Definition: main.cpp:78
ramses::Scene * createProviderScene(ramses::RamsesClient &client, ramses::sceneId_t sceneId, const char *rotationNodeName)
Definition: main.cpp:276
Struct containing information about one mip-map level of a texture.
Definition: MipLevelData.h:24