NAP
RenderService Class Reference

#include <renderservice.h>

Public Types

enum  State : int { Uninitialized = -1, Initialized = 0, WindowError = 1, SystemError = 2 }
 
using SortFunction = std::function< void(std::vector< RenderableComponentInstance * > &, const CameraComponentInstance &)>
 

Public Member Functions

 RenderService (ServiceConfiguration *configuration)
 
virtual ~RenderService ()
 
void renderObjects (opengl::RenderTarget &renderTarget, CameraComponentInstance &camera)
 
void renderObjects (opengl::RenderTarget &renderTarget, CameraComponentInstance &camera, const SortFunction &sortFunction)
 
void renderObjects (opengl::RenderTarget &renderTarget, CameraComponentInstance &camera, const std::vector< RenderableComponentInstance *> &comps)
 
void renderObjects (opengl::RenderTarget &renderTarget, CameraComponentInstance &camera, const std::vector< RenderableComponentInstance *> &comps, const SortFunction &sortFunction)
 
void clearRenderTarget (opengl::RenderTarget &renderTarget, opengl::EClearFlags flags)
 
void clearRenderTarget (opengl::RenderTarget &renderTarget)
 
virtual void shutdown () override
 
void setPolygonMode (opengl::EPolygonMode mode)
 
const RenderStategetRenderState () const
 
void setRenderState (const RenderState &renderState)
 
void pushRenderState ()
 
void queueResourceForDestruction (std::unique_ptr< opengl::IGLContextResource > resource)
 
void destroyGLContextResources (const std::vector< RenderWindow *> renderWindows)
 
std::shared_ptr< GLWindowaddWindow (RenderWindow &window, utility::ErrorState &errorState)
 
void removeWindow (RenderWindow &window)
 
RenderWindowfindWindow (void *nativeWindow) const
 
RenderWindowfindWindow (uint id) const
 
GLWindowgetPrimaryWindow ()
 
const std::string & getPrimaryWindowID () const
 
void addEvent (WindowEventPtr windowEvent)
 
RenderableMesh createRenderableMesh (IMesh &mesh, MaterialInstance &materialInstance, utility::ErrorState &errorState)
 
- Public Member Functions inherited from Service
 Service (ServiceConfiguration *configuration)
 
virtual ~Service ()
 
CoregetCore ()
 
const std::string getTypeName () const
 

Protected Member Functions

virtual void registerObjectCreators (rtti::Factory &factory) override
 
virtual void getDependentServices (std::vector< rtti::TypeInfo > &dependencies) override
 
virtual bool init (nap::utility::ErrorState &errorState) override
 
virtual void preUpdate (double deltaTime) override
 
virtual void update (double deltaTime) override
 
virtual void resourcesLoaded () override
 
- Protected Member Functions inherited from Service
virtual void created ()
 
virtual void postUpdate (double deltaTime)
 
template<typename SERVICE_CONFIG >
SERVICE_CONFIG * getConfiguration ()
 
template<typename SERVICE_CONFIG >
const SERVICE_CONFIG * getConfiguration () const
 

Detailed Description

Main interface for 2D and 3D rendering operations. This service initializes the render back-end, manages all vertex array buffers, can be used to create RenderableMesh objects and provides an interface to render objects to a specific target (screen or back-buffer). Vertex array object management is handled completely by this service. As a user you only work with the render interface to render a set of render-able components to a target using a camera. The service is shut down automatically on exit, and deatroys all windows and left over resources. When rendering geometry using (most) renderObjects() the service automatically sorts your selection based on the blend mode of the material. Opaque objects are rendered front to back, alpha blended objects are rendered back to front.

Inheritance diagram for RenderService:
[legend]
Collaboration diagram for RenderService:
[legend]

Member Typedef Documentation

◆ SortFunction

using SortFunction = std::function<void(std::vector<RenderableComponentInstance*>&, const CameraComponentInstance&)>

Member Enumeration Documentation

◆ State

enum State : int
strong

Holds current render state

Enumerator
Uninitialized 

The render back end is not initialized.

Initialized 

The render back end initialized correctly.

WindowError 

The render back end produced a window error.

SystemError 

The render back end produced a system error.

Constructor & Destructor Documentation

◆ RenderService()

RenderService ( ServiceConfiguration configuration)

◆ ~RenderService()

virtual ~RenderService ( )
virtual

Member Function Documentation

◆ addEvent()

void addEvent ( WindowEventPtr  windowEvent)

Add a window event that is processed later, ownership is transferred here. The window number in the event is used to find the right render window to forward the event to.

Parameters
windowEventthe window event to add.

◆ addWindow()

std::shared_ptr<GLWindow> addWindow ( RenderWindow window,
utility::ErrorState errorState 
)

Add a new window for the specified resource

Parameters
windowthe window to add as a valid render target
errorStatecontains the error message if the window could not be added

◆ clearRenderTarget() [1/2]

void clearRenderTarget ( opengl::RenderTarget renderTarget,
opengl::EClearFlags  flags 
)

Clears specific parts of the renderTarget using the given flags. The flags can be bitmasked together, for example: EClearFlags::Color | EClearFlags::Color.

Parameters
renderTargetthe opengl target to clear.
flagswhat parts to clear.

◆ clearRenderTarget() [2/2]

void clearRenderTarget ( opengl::RenderTarget renderTarget)

Clears all the renderTarget's associated flags (Color, Depth, Stencil)

Parameters
renderTargetthe opengl target to clear

◆ createRenderableMesh()

RenderableMesh createRenderableMesh ( IMesh mesh,
MaterialInstance materialInstance,
utility::ErrorState errorState 
)

Creates a renderable mesh that represents the coupling between a mesh and material that can be rendered to screen. Internally the renderable mesh manages a vertex array object that is issued by the render service. This function should be called from on initialization of components that work with meshes and materials: ie: all types of RenderableComponent. The result should be validated by calling RenderableMesh.isValid(). Invalid mesh / material representations can't be rendered together.

Parameters
meshThe mesh that is used in the mesh-material combination.
materialInstanceThe material instance that is used in the mesh-material combination.
errorStateIf this function returns an invalid renderable mesh, the error state contains error information.
Returns
A RenderableMesh object that can be used in setMesh calls. Check isValid on the object to see if creation succeeded or failed.

◆ destroyGLContextResources()

void destroyGLContextResources ( const std::vector< RenderWindow *>  renderWindows)

Destroys all per-context OpenGL resources that are scheduled for destruction.

Parameters
renderWindowsall render windows that are active, as they hold the GL contexts.

◆ findWindow() [1/2]

RenderWindow* findWindow ( void *  nativeWindow) const

Find a RenderWindowResource by its native handle

Parameters
nativeWindowthe native window handle (i.e. the SDL_Window pointer)
Returns
the render window associated with the native window

◆ findWindow() [2/2]

RenderWindow* findWindow ( uint  id) const

Find a RenderWindow based on a window number.

Parameters
idthe number of the window to find.
Returns
the window, nullptr if not found

◆ getDependentServices()

virtual void getDependentServices ( std::vector< rtti::TypeInfo > &  dependencies)
overrideprotectedvirtual

Register dependencies, render module depends on scene

Reimplemented from Service.

◆ getPrimaryWindow()

GLWindow& getPrimaryWindow ( )

Get the primary window (i.e. the window that was used to init OpenGL against)

◆ getPrimaryWindowID()

const std::string& getPrimaryWindowID ( ) const
Returns
the id of the primary window

◆ getRenderState()

const RenderState& getRenderState ( ) const

Returns the global render state that is used for all OpenGL contexts. Properties associated with this state (such as the fill mode, point size etc.) are set before rendering a set of objects.

Returns
the global render state.

◆ init()

virtual bool init ( nap::utility::ErrorState errorState)
overrideprotectedvirtual

Initialize the renderer, the service owns the renderer.

Parameters
errorStatecontains the error message if the service could not be initialized
Returns
if the service has been initialized successfully

Reimplemented from Service.

◆ preUpdate()

virtual void preUpdate ( double  deltaTime)
overrideprotectedvirtual

Called before update, ensures the primary window is the active window before update is called.

Parameters
deltaTimetime in seconds in between frames.

Reimplemented from Service.

◆ pushRenderState()

void pushRenderState ( )

Pushes the global render state to the GPU. Note that this is called automatically when rendering objects through the render service using RenderObjects(). Use this call when implementing your own draw call in a component.

◆ queueResourceForDestruction()

void queueResourceForDestruction ( std::unique_ptr< opengl::IGLContextResource resource)

Batches an OpenGL resource that is dependent on GLContext for destruction, to avoid many GL context switches during destruction.

Parameters
resourceobject that is dependent on GL context, that is scheduled for destruction. Notice that ownership is transferred here.

◆ registerObjectCreators()

virtual void registerObjectCreators ( rtti::Factory factory)
overrideprotectedvirtual

Object creation registration

Reimplemented from Service.

◆ removeWindow()

void removeWindow ( RenderWindow window)

Remove a window

Parameters
windowthe window to remove from the render service

◆ renderObjects() [1/4]

void renderObjects ( opengl::RenderTarget renderTarget,
CameraComponentInstance camera 
)

Renders all available RenderableComponents in the scene to a specific renderTarget. The objects to render are sorted using the default sort function (front-to-back for opaque objects, back-to-front for transparent objects). The sort function is provided by the render service itself, using the default NAP DepthSorter. Components that can't be rendered with the given camera are omitted.

Parameters
renderTargetthe target to render to
camerathe camera used for rendering all the available components

◆ renderObjects() [2/4]

void renderObjects ( opengl::RenderTarget renderTarget,
CameraComponentInstance camera,
const SortFunction sortFunction 
)

Renders all available RenderableComponents in the scene to a specific renderTarget. Components that can't be rendered with the given camera are omitted.

Parameters
renderTargetthe target to render to
camerathe camera used for rendering all the available components
sortFunctionThe function used to sort the components to render

◆ renderObjects() [3/4]

void renderObjects ( opengl::RenderTarget renderTarget,
CameraComponentInstance camera,
const std::vector< RenderableComponentInstance *> &  comps 
)

Renders a specific set of objects to a specific renderTarget. The objects to render are sorted using the default sort function (front-to-back for opaque objects, back-to-front for transparent objects) The sort function is provided by the render service itself, using the default NAP DepthSorter.

Parameters
renderTargetthe target to render to
camerathe camera used for rendering all the available components
compsthe components to render to renderTarget

◆ renderObjects() [4/4]

void renderObjects ( opengl::RenderTarget renderTarget,
CameraComponentInstance camera,
const std::vector< RenderableComponentInstance *> &  comps,
const SortFunction sortFunction 
)

Renders a specific set of objects to a specific renderTarget.

Parameters
renderTargetthe target to render to
camerathe camera used for rendering all the available components
compsthe components to render to renderTarget
sortFunctionThe function used to sort the components to render

◆ resourcesLoaded()

virtual void resourcesLoaded ( )
overrideprotectedvirtual

Performs a flush to ensure all recent opengl commands are processed

Reimplemented from Service.

◆ setPolygonMode()

void setPolygonMode ( opengl::EPolygonMode  mode)

Changes the polygon draw mode for the next set of draw calls. Updates the render state internally.

Parameters
modethe polygon mode to use for the next set of draw calls

◆ setRenderState()

void setRenderState ( const RenderState renderState)

Sets the global render state. This state is used for all OpenGL contexts and settings are pushed before a render call. You can change settings (such as point-size etc.) at runtime, before rendering objects.

Parameters
renderStatethe new state of the renderer to use when rendering objects.

◆ shutdown()

virtual void shutdown ( )
overridevirtual

Shuts down the managed renderer

Reimplemented from Service.

◆ update()

virtual void update ( double  deltaTime)
overrideprotectedvirtual

Process all received window events.

Parameters
deltaTimetime in seconds in between frames.

Reimplemented from Service.