# Render Loop

##### <a class="anchor" id="bkmrk--31"></a>Description

After loading an asset and creating scenes, this chapter will now explain how an application can trigger and control rendering of 3D and 2D scenes.

Basically it is up to the application to define an event loop triggering render calls. [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") provides interfaces to render specific or all cameras, but scheduling and triggering these render calls is what the application is responsible for.

##### **3D Render Loop** 

##### <a class="anchor" id="bkmrk--33"></a>3D Render Calls

Static class [Candera::Renderer](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer.html "The class Renderer represents the unique Candera render interface to the application. Each render call an base of Candera shall be triggered via the Renderer interface.") is part of [Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "[DataBinding_RefTypeSample]") 3D Engine and provides two render call interfaces:

<div class="contents" id="bkmrk-render-all-3d-camera"><div class="contents"><div class="textblock">1. Render all 3D cameras: [Candera::Renderer::RenderAllCameras](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer.html#aed822ba1d66620f2d1e794973e30f354)
2. Render a specific camera only: [Candera::Renderer::RenderCamera](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer.html#a10dbe66b15c60f44cce3aaccd38d2ee5)

</div></div></div>Usually, rendering of all cameras is the safer and most convenient way, hence chosen here for the tutorial:

```
    <a class="code" href="http://dev.doc.cgistudio.at/APILINK/group___f_e_a_t_s_t_d___u_t_i_l_s.html#gacfaf93353b58715fbfa32e84c98af894" title="Suppress message_number for next expression.">FEATSTD_LINT_NEXT_EXPRESSION</a>(534, "return value not needed");
    Renderer::RenderAllCameras();
```

##### <a class="anchor" id="bkmrk--34"></a>Render Buffer Swapping

If the draw target operates on multiple buffers after each rendering cycle the presentation buffer is exchanged with the current background buffer, on which was written during the cycle.

For this, three interfaces are available:

<div class="contents" id="bkmrk-candera%3A%3Arendertarge"><div class="contents"><div class="textblock">- Candera::RenderTarget::SetAutoSwapEnabled(bool enabled): if *true*, render target buffers are swapped automatically, when a camera within the render target has been rendered. Default: *false*
- [Candera::Camera::SetSwapEnabled(bool enable)](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_camera.html#a4d8422ed9703ddb9e0803eae61fe0bc6): if *true*, buffers are swapped automatically after rendering the according camera. Default: *false*
- Candera::RenderTarget::SwapBuffers(): Trigger buffer swapping 'by hand'

</div></div></div>It is very important to control buffer swapping in the application correctly:

<div class="contents" id="bkmrk-if-swapping-is-never"><div class="contents"><div class="textblock">- If swapping is never done, the display will stay black
- If buffers are swapped too often, the display will flicker.

</div></div></div>The application doesn't need to swap its render targets in case:

<div class="contents" id="bkmrk-the-render-target-is"><div class="textblock">- the render target is set to swap automatically. For this, set the render target property "SetAutoSwapEnabled" to *true*.
- the proper camera is set to swap automatically. For this, set the camera property "SwapEnabled" to *true*.

</div></div><div class="contents" id="bkmrk-if-you-use-more-than"><div class="textblock"><dl class="note"><dd><p class="callout info">If you use more than one camera on a render target, take care to enable swapping only for the last camera (highest sequence number).</p>

</dd></dl></div></div>#####   
**2D Render Loop**   


##### <a class="anchor" id="bkmrk--35"></a>2D Render Calls

2D Render Call interfaces are provided by [Candera::Renderer2D](http://dev.doc.cgistudio.at/APILINK/class_candera_1_1_renderer2_d.html "The class Renderer2D represents the unique Candera 2D render interface to the application. Each render call shall be triggered via the Renderer2D interface.").

This is the 2D specific render call equivalent to 3D.

```
    Renderer2D::RenderAllCameras();
```

##### <a class="anchor" id="bkmrk--36"></a>Render Buffer Swapping

For 2D, render buffer swapping is analogous to 3D: The default value of 2D RenderTarget property "AutoSwapEnabled" and 2D Camera property "SwapEnabled" is *false*.

So take care to

<div class="contents" id="bkmrk-either-swap-render-t"><div class="textblock">- either swap render target
- or swap one of the cameras used
- or trigger buffer swapping on the render target 'by hand'.

</div></div>