RAMSES Documentation  27.0.130
Information for RAMSES users and developers
ramses-example-data-buffers-vertices/src/main.cpp

Basic Data Buffer Example

// -------------------------------------------------------------------------
// Copyright (C) 2017 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 <cstdio>
#include <thread>
#include <chrono>
#include <cmath>
#include <vector>
int main(int argc, char* argv[])
{
// register at RAMSES daemon
ramses::RamsesFramework framework(argc, argv);
ramses::RamsesClient& ramses(*framework.createClient("ramses-example-data-buffers-vertices"));
framework.connect();
// create a scene for distributing content
ramses::Scene* scene = ramses.createScene(ramses::sceneId_t(123u), ramses::SceneConfig(), "triangle scene");
// 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, 35.0f);
ramses::RenderPass* renderPass = scene->createRenderPass("my render pass");
renderPass->setCamera(*camera);
ramses::RenderGroup* renderGroup = scene->createRenderGroup();
renderPass->addRenderGroup(*renderGroup);
// create an appearance for triangles
effectDesc.setVertexShaderFromFile("res/ramses-example-data-buffers-vertices.vert");
effectDesc.setFragmentShaderFromFile("res/ramses-example-data-buffers-vertices.frag");
ramses::Effect* effect = scene->createEffect(effectDesc, ramses::ResourceCacheFlag_DoNotCache, "glsl shader");
ramses::Appearance* appearance = scene->createAppearance(*effect, "triangle appearance data buffer");
ramses::GeometryBinding* geometry = scene->createGeometryBinding(*effect, "triangle geometry data buffer");
// get input data of appearance and set color values
effect->findUniformInput("color", colorInput);
appearance->setInputValueVector4f(colorInput, 0.0f, 1.0f, 1.0f, 1.0f);
// the raw data for vertices and indices
const float vertexPositions[] = {
-1.0, 4.0, -1.0, 0.0, 4.0, -1.0, 1.0, 4.0, -1.0,
-2.0, 2.0, -1.0, 2.0, 2.0, -1.0,
-4.0, 1.0, -1.0, 4.0, 1.0, -1.0,
-4.0, 0.0, -1.0, 4.0, 0.0, -1.0,
-4.0, -1.0, -1.0, 4.0, -1.0, -1.0,
-2.0, -2.0, -1.0, 2.0, -2.0, -1.0,
-1.0, -4.0, -1.0, 0.0, -4.0, -1.0, 1.0, -4.0, -1.0 };
const uint16_t indexData[] = {
3, 1, 0, 4, 2, 1, 4, 1, 3,
8, 6, 4, 12, 10, 8, 12, 8, 4,
14, 15, 12, 11, 13, 14, 11, 14, 12,
7, 9, 11, 3, 5, 7, 3, 7, 11,
4, 3, 11, 11, 12, 4
};
//
// 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.
// Create the DataBuffers
// The data buffers need more information about size
const uint32_t NumVertices = 16u;
const uint32_t NumIndices = 42u;
// then create the buffers via the _scene_
ramses::ArrayBuffer* vertices = scene->createArrayBuffer( ramses::EDataType::Vector3F, NumVertices, "some varying vertices");
ramses::ArrayBuffer* indices = scene->createArrayBuffer( ramses::EDataType::UInt16, NumIndices, "some varying indices");
// finally set/update the data
vertices->updateData(0u, NumVertices, vertexPositions);
indices->updateData( 0u, NumIndices, indexData);
// applying the vertex/index data to the geometry binding is the same for both
ramses::AttributeInput positionsInput;
effect->findAttributeInput("a_position", positionsInput);
geometry->setInputBuffer(positionsInput, *vertices);
geometry->setIndices(*indices);
// create a mesh node to define the triangle with chosen appearance
ramses::MeshNode* meshNode = scene->createMeshNode("triangle mesh node data buffers");
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);
// signal the scene it is in a state that can be rendered
scene->flush();
// distribute the scene to RAMSES
scene->publish();
// application logic
auto translateVertex = [&vertexPositions](float* updatedValues, uint32_t i, uint64_t timeStamp, float scaleFactor){
const float currentFactor = static_cast<float>(std::sin(0.005f*timeStamp));
const uint32_t index = i * 3u;
const float xValue = vertexPositions[index+0];
const float yValue = vertexPositions[index+1];
const float zValue = vertexPositions[index+2];
updatedValues[0] = xValue + scaleFactor * xValue * currentFactor;
updatedValues[1] = yValue + scaleFactor * yValue * currentFactor;
updatedValues[2] = zValue;
};
const std::vector<uint32_t> ridges = { 1, 7, 8, 14 };
const std::vector<uint32_t> notches = { 3, 4, 11, 12 };
float updatedValues[3];
for (uint64_t timeStamp = 0u; timeStamp < 10000u; timeStamp += 20u)
{
for(auto i: ridges)
{
translateVertex(updatedValues, i, timeStamp, 0.25f);
vertices->updateData(i, 1, updatedValues);
}
for(auto i: notches)
{
translateVertex(updatedValues, i, timeStamp, -0.4f);
vertices->updateData(i, 1, updatedValues);
}
// signal the scene it is in a state that can be rendered
scene->flush();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
// shutdown: stop distribution, free resources, unregister
scene->unpublish();
ramses.destroy(*scene);
framework.disconnect();
return 0;
}
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.
The ArrayBuffer is a data object used to provide vertex or index data to ramses::GeometryBinding::set...
Definition: ArrayBuffer.h:27
status_t updateData(uint32_t firstElement, uint32_t numElements, const void *bufferData)
Update data of the ArrayBuffer object.
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.
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
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 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....
status_t unpublish()
Unpublish the scene from the ramses system.
GeometryBinding * createGeometryBinding(const Effect &effect, const char *name=nullptr)
Creates a new GeometryBinding.
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.
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.
ArrayBuffer * createArrayBuffer(EDataType dataType, uint32_t maxNumElements, const char *name=nullptr)
Create a new ramses::ArrayBuffer. The created object is a mutable buffer object that can be used as i...
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
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
@ EClearFlags_None
Definition: RamsesFrameworkTypes.h:257
@ ModelViewProjectionMatrix
Model-view-projection matrix 4x4.
@ 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