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

Basic File Loading Example

// -------------------------------------------------------------------------
// Copyright (C) 2014 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 "ramses-utils.h"
#include <thread>
{
// prepare triangle geometry: vertex position array and index array
float vertexPositionsData[] = { -0.3f, 0.f, -0.3f, 0.3f, 0.f, -0.3f, 0.f, 0.3f, -0.3f };
ramses::ArrayResource* vertexPositions = scene.createArrayResource(ramses::EDataType::Vector3F, 3, vertexPositionsData);
uint16_t indexData[] = { 0, 1, 2 };
// create an appearance for red triangle
effectDesc.setVertexShaderFromFile("res/ramses-example-basic-file-loading-basic.vert");
effectDesc.setFragmentShaderFromFile("res/ramses-example-basic-file-loading-red.frag");
ramses::Effect* effect = scene.createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "glsl shader anim");
ramses::Appearance* appearance = scene.createAppearance(*effect, "triangle appearance anim");
// set vertex positions directly in geometry
ramses::GeometryBinding* geometry = scene.createGeometryBinding(*effect, "triangle geometry");
geometry->setIndices(*indices);
ramses::AttributeInput positionsInput;
effect->findAttributeInput("a_position", positionsInput);
geometry->setInputBuffer(positionsInput, *vertexPositions);
// create a mesh nodes to define the triangles with chosen appearance
ramses::MeshNode* meshNode1 = scene.createMeshNode("red triangle mesh node1");
meshNode1->setAppearance(*appearance);
meshNode1->setGeometryBinding(*geometry);
ramses::MeshNode* meshNode2 = scene.createMeshNode("red triangle mesh node2");
meshNode2->setAppearance(*appearance);
meshNode2->setGeometryBinding(*geometry);
ramses::MeshNode* meshNode3 = scene.createMeshNode("red triangle mesh node3");
meshNode3->setAppearance(*appearance);
meshNode3->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(*meshNode1);
renderGroup.addMeshNode(*meshNode2);
renderGroup.addMeshNode(*meshNode3);
// create animation system
// create splines with animation keys
ramses::SplineLinearFloat* spline1 = animationSystem->createSplineLinearFloat("spline1");
spline1->setKey(0u, 0.f);
spline1->setKey(5000u, -1.f);
spline1->setKey(10000u, 0.f);
ramses::SplineLinearFloat* spline2 = animationSystem->createSplineLinearFloat("spline2");
spline2->setKey(0u, 0.f);
spline2->setKey(5000u, 1.f);
spline2->setKey(10000u, 0.f);
// create animated property for each translation node with single component animation
// create three animations
ramses::Animation* animation1 = animationSystem->createAnimation(*animProperty1, *spline1, "animation1");
ramses::Animation* animation2 = animationSystem->createAnimation(*animProperty2, *spline2, "animation2");
ramses::Animation* animation3 = animationSystem->createAnimation(*animProperty3, *spline1, "animation3"); // we can reuse spline1 for animating Y component of the third translation node
// create animation sequence
ramses::AnimationSequence* animSequence = animationSystem->createAnimationSequence();
// add animations to a sequence
animSequence->addAnimation(*animation1);
animSequence->addAnimation(*animation2);
animSequence->addAnimation(*animation3);
// set animation properties (optional)
animSequence->setAnimationLooping(*animation1);
animSequence->setAnimationLooping(*animation2);
animSequence->setAnimationLooping(*animation3);
// set playbackSpeed
animSequence->setPlaybackSpeed(5.f);
// start animation sequence
animSequence->startAt(0u);
animationSystem->setTime(20800u);
}
int main(int argc, char* argv[])
{
// create a scene and write it to a file
{
ramses::RamsesFramework framework(argc, argv);
ramses::RamsesClient& ramses(*framework.createClient("ramses-example-file-loading"));
ramses::Scene* scene = ramses.createScene(ramses::sceneId_t(123u), ramses::SceneConfig(), "basic scene loading from file");
// every scene needs a render pass with camera
auto* camera = scene->createPerspectiveCamera("my camera");
camera->setViewport(0, 0, 1280u, 480u);
camera->setFrustum(19.f, 1280.f / 480.f, 0.1f, 1500.f);
camera->setTranslation(0.0f, 0.0f, 5.0f);
ramses::RenderPass* renderPass = scene->createRenderPass("my render pass");
renderPass->setCamera(*camera);
ramses::RenderGroup* renderGroup = scene->createRenderGroup();
renderPass->addRenderGroup(*renderGroup);
float vertexPositionsArray[] = { -0.5f, -0.5f, -1.f, 0.5f, -0.5f, -1.f, -0.5f, 0.5f, -1.f, 0.5f, 0.5f, -1.f };
ramses::ArrayResource* vertexPositions = scene->createArrayResource(ramses::EDataType::Vector3F, 4, vertexPositionsArray);
float textureCoordsArray[] = { 0.f, 1.f, 1.f, 1.f, 0.f, 0.f, 1.f, 0.f };
ramses::ArrayResource* textureCoords = scene->createArrayResource(ramses::EDataType::Vector2F, 4, textureCoordsArray);
uint16_t indicesArray[] = { 0, 1, 2, 2, 1, 3 };
ramses::Texture2D* texture = ramses::RamsesUtils::CreateTextureResourceFromPng("res/ramses-example-basic-file-loading-texture.png", *scene);
*texture);
effectDesc.setVertexShaderFromFile("res/ramses-example-basic-file-loading-texturing.vert");
effectDesc.setFragmentShaderFromFile("res/ramses-example-basic-file-loading-texturing.frag");
ramses::Effect* effectTex = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "glsl shader");
ramses::Appearance* appearance = scene->createAppearance(*effectTex, "triangle appearance");
ramses::GeometryBinding* geometry = scene->createGeometryBinding(*effectTex, "triangle geometry");
geometry->setIndices(*indices);
ramses::AttributeInput positionsInput;
ramses::AttributeInput texcoordsInput;
effectTex->findAttributeInput("a_position", positionsInput);
effectTex->findAttributeInput("a_texcoord", texcoordsInput);
geometry->setInputBuffer(positionsInput, *vertexPositions);
geometry->setInputBuffer(texcoordsInput, *textureCoords);
ramses::UniformInput textureInput;
effectTex->findUniformInput("textureSampler", textureInput);
appearance->setInputTexture(textureInput, *sampler);
ramses::Node* scaleNode = scene->createNode("scale node");
ramses::MeshNode* meshNode = scene->createMeshNode("textured triangle mesh node");
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);
scaleNode->addChild(*meshNode);
initializeAnimationContent(*scene, *renderGroup);
scene->saveToFile("tempfile.ramses", false);
scene->destroy(*vertexPositions);
scene->destroy(*textureCoords);
scene->destroy(*indices);
ramses.destroy(*scene);
}
// load the saved file
{
ramses::RamsesFramework framework(argc, argv);
ramses::RamsesClient& ramses(*framework.createClient("ramses-example-file-loading"));
// 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.
ramses::Scene* loadedScene = ramses.loadSceneFromFile("tempfile.ramses");
// make changes to loaded scene
ramses::RamsesObject* loadedObject = loadedScene->findObjectByName("scale node");
ramses::Node* loadedScaleNode = ramses::RamsesUtils::TryConvert<ramses::Node>(*loadedObject);
framework.connect();
loadedScene->publish();
loadedScaleNode->setScaling(2, 2, 2);
ramses::AnimationSystem* loadedAnimSystem = ramses::RamsesUtils::TryConvert<ramses::AnimationSystem>(*loadedScene->findObjectByName("animation system"));
const ramses::globalTimeStamp_t currTimeState = loadedAnimSystem->getTime();
loadedAnimSystem->setTime(currTimeState + 3333u);
loadedScene->flush();
std::this_thread::sleep_for(std::chrono::seconds(30));
loadedScene->unpublish();
ramses.destroy(*loadedScene);
framework.disconnect();
}
return 0;
}
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 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 ...
status_t startAt(globalTimeStamp_t timeStamp)
Starts the sequence of animations at given time.
The AnimationSystem holds all animation related data.
Definition: AnimationSystem.h:57
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.
globalTimeStamp_t getTime() const
Gets the current animation system time. The time stamp retrieved is the time stamp that was previousl...
Animation * createAnimation(const AnimatedProperty &animatedProperty, const Spline &spline, const char *name=nullptr)
Creates Animation that can animate given property using given spline.
status_t setTime(globalTimeStamp_t timeStamp)
Sets the animation system to a given time. Any unsigned integral values that are used in an increment...
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 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.
An effect description holds all necessary information for an effect to be created.
Definition: EffectDescription.h:21
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
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.
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.
status_t setGeometryBinding(GeometryBinding &geometry)
Sets the GeometryBinding of the MeshNode.
The Node is the base class of all nodes and provides scene graph functionality which propagates to it...
Definition: Node.h:23
status_t setScaling(float x, float y, float z)
Sets the absolute scale in all three dimensions.
status_t addChild(Node &node)
Adds child Node to this node.
Entry point of RAMSES client API.
Definition: RamsesClient.h:34
Class representing ramses framework components that are needed to initialize an instance of ramses cl...
Definition: RamsesFramework.h:35
The RamsesObject is a base class for all client API objects owned by the framework.
Definition: RamsesObject.h:21
static Texture2D * CreateTextureResourceFromPng(const char *pngFilePath, Scene &scene, const TextureSwizzle &swizzle={}, const char *name=nullptr)
Creates a Texture from the given png file.
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 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
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....
Node * createNode(const char *name=nullptr)
Creates a scene graph node. The basic purpose of Node is to define topology in scene graph by links t...
status_t unpublish()
Unpublish the scene from the ramses system.
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.
status_t saveToFile(const char *fileName, bool compress) const
Saves all scene contents to a file.
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.
AnimationSystem * createAnimationSystem(uint32_t flags=EAnimationSystemFlags_Default, const char *name=nullptr)
Create a new animation system. The animation system will be updated on renderer side after calls to A...
status_t destroy(SceneObject &object)
Destroys a previously created object using this scene The object must be owned by this scene in order...
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.
const RamsesObject * findObjectByName(const char *name) const
Get an object from the scene by name.
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.
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
@ ETextureSamplingMethod_Linear
Definition: TextureEnums.h:21
@ ETextureAddressMode_Repeat
Definition: TextureEnums.h:33
@ EAnimatedPropertyComponent_Y
Definition: AnimatedProperty.h:67
@ EAnimatedPropertyComponent_X
Definition: AnimatedProperty.h:66
@ EAnimatedProperty_Translation
Definition: AnimatedProperty.h:76
uint64_t globalTimeStamp_t
Definition: AnimationTypes.h:17
@ 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
constexpr const resourceCacheFlag_t ResourceCacheFlag_DoNotCache
Requests the render to not cache a resource. This is the default value.
Definition: RamsesFrameworkTypes.h:212
int main(int argc, char *argv[])
Definition: main.cpp:21
void initializeAnimationContent(ramses::Scene &scene, ramses::RenderGroup &renderGroup)
Definition: main.cpp:18