nCine  2025.04.r498-9d36345
A cross-platform 2D game engine
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends | List of all members
ncine::Viewport Class Reference

The class handling a viewport and its corresponding render target texture. More...

#include <Viewport.h>

Collaboration diagram for ncine::Viewport:
Collaboration graph
[legend]

Public Types

enum class  Type { WITH_TEXTURE , NO_TEXTURE , SCREEN }
 The different types of viewports available. More...
 
enum class  ClearMode {
  EVERY_DRAW , EVERY_FRAME , THIS_FRAME_ONLY , NEXT_FRAME_ONLY ,
  NEVER
}
 
enum class  DepthStencilFormat { NONE , DEPTH16 , DEPTH24 , DEPTH24_STENCIL8 }
 The depth and stencil format for a viewport with a texture or for the screen.
 

Public Member Functions

 Viewport (const char *name, Texture *texture, DepthStencilFormat depthStencilFormat)
 Creates a new viewport with the specified name and texture, plus a depth and stencil renderbuffer.
 
 Viewport (Texture *texture, DepthStencilFormat depthStencilFormat)
 Creates a new viewport with the specified texture, plus a depth and stencil renderbuffer.
 
 Viewport (const char *name, Texture *texture)
 Creates a new viewport with the specified name and texture.
 
 Viewport (Texture *texture)
 Creates a new viewport with the specified texture.
 
 Viewport ()
 Creates a new viewport with no texture.
 
Type type () const
 Returns the viewport type.
 
Texturetexture (unsigned int index)
 Returns the texture at the specified viewport's FBO color attachment index, if any.
 
Texturetexture ()
 Returns the texture at the first viewport's FBO color attachment index.
 
bool setTexture (unsigned int index, Texture *texture)
 Adds or removes a texture at the specified viewport's FBO color attachment index. More...
 
bool setTexture (Texture *texture)
 Adds or removes a texture at the first viewport's FBO color attachment index.
 
DepthStencilFormat depthStencilFormat () const
 Returns the depth and stencil format of the viewport's FBO renderbuffer.
 
bool setDepthStencilFormat (DepthStencilFormat depthStencilFormat)
 Sets the depth and stencil format of the viewport's FBO renderbuffer. More...
 
bool removeAllTextures ()
 Removes all textures and the depth stencil renderbuffer from the viewport's FBO. More...
 
Vector2i size () const
 Returns viewport's FBO size as a Vector2i object, or a zero vector if no texture is present.
 
int width () const
 Returns viewport's FBO width or zero if no texture is present.
 
int height () const
 Returns viewport's FBO height or zero if no texture is present.
 
unsigned int numColorAttachments () const
 Returns the number of color attachments of the viewport's FBO.
 
Recti viewportRect () const
 Returns the OpenGL viewport rectangle.
 
void setViewportRect (Recti viewportRect)
 Sets the OpenGL viewport rectangle through a Recti object.
 
void setViewportRect (int x, int y, int width, int height)
 Sets the OpenGL viewport rectangle with four integers.
 
void setViewportRect (const Vector2i &size)
 Resizes the OpenGL viewport rectangle through a Vector2i object.
 
void setViewportRect (int width, int height)
 Resizes the OpenGL viewport rectangle with two integers.
 
Recti scissorRect () const
 Returns the OpenGL scissor test rectangle.
 
void setScissorRect (Recti scissorRect)
 Sets the OpenGL scissor test rectangle through a Recti object.
 
void setScissorRect (int x, int y, int width, int height)
 Sets the OpenGL scissor test rectangle with four integers.
 
void setScissorRect (const Vector2i &size)
 Resizes the OpenGL scissor test rectangle through a Vector2i object.
 
void setScissorRect (int width, int height)
 Resizes the OpenGL scissor test rectangle with two integers.
 
Rectf cullingRect () const
 Returns the rectangle for screen culling.
 
unsigned long int lastFrameCleared () const
 Returns the last frame this viewport was cleared.
 
ClearMode clearMode () const
 Returns the viewport clear mode.
 
void setClearMode (ClearMode clearMode)
 Sets the viewport clear mode.
 
Colorf clearColor () const
 Returns the viewport clear color as a Colorf object.
 
void setClearColor (float red, float green, float blue, float alpha)
 Sets the viewport clear color through four floats.
 
void setClearColor (const Colorf &color)
 Sets the viewport clear color through a Colorf object.
 
const SceneNoderootNode () const
 Returns the root node as a constant.
 
SceneNoderootNode ()
 Returns the root node.
 
void setRootNode (SceneNode *rootNode)
 Sets or removes the root node.
 
const Cameracamera () const
 Returns the camera used for rendering as a constant.
 
Cameracamera ()
 Returns the camera used for rendering.
 
void setCamera (Camera *camera)
 Sets the camera to be used for rendering. More...
 
void setGLFramebufferLabel (const char *label)
 Sets the OpenGL object label for the viewport framebuffer object.
 

Static Public Member Functions

static nctl::Array< Viewport * > & chain ()
 Returns the reverse ordered array of viewports to be drawn before the screen.
 

Protected Types

enum  StateBitPositions { UpdatedBit = 0 , VisitedBit = 1 , CommittedBit = 2 }
 Bit positions inside the state bitset.
 

Protected Member Functions

 Viewport (const Viewport &)=delete
 Deleted copy constructor.
 
Viewportoperator= (const Viewport &)=delete
 Deleted assignment operator.
 
void calculateCullingRect ()
 
void update ()
 
void visit ()
 
void sortAndCommitQueue ()
 
void draw (unsigned int nextIndex)
 

Protected Attributes

Type type_
 
int width_
 
int height_
 
Recti viewportRect_
 
Recti scissorRect_
 
Rectf cullingRect_
 
DepthStencilFormat depthStencilFormat_
 
unsigned long int lastFrameCleared_
 The last frame this viewport was cleared.
 
ClearMode clearMode_
 
Colorf clearColor_
 
nctl::UniquePtr< RenderQueue > renderQueue_
 The render queue of commands for this viewport/RT.
 
nctl::UniquePtr< GLFramebufferObject > fbo_
 
Texturetextures_ [MaxNumTextures]
 
SceneNoderootNode_
 The root scene node for this viewport/RT.
 
Cameracamera_
 The camera used by this viewport. More...
 
nctl::BitSet< uint8_t > stateBits_
 Bitset that stores the various states bits.
 

Static Protected Attributes

static nctl::Array< Viewport * > chain_
 The reverse ordered array of viewports to be drawn before the screen.
 
static const unsigned int MaxNumTextures = 4
 

Friends

class Application
 
class ScreenViewport
 

Detailed Description

The class handling a viewport and its corresponding render target texture.

Member Enumeration Documentation

◆ ClearMode

The clear mode for a viewport with a texture or for the screen

Note
If a viewport appears multiple times in a chain, the frame modes will clear it the first time only.
Enumerator
EVERY_DRAW 

The viewport is cleared every time it is drawn.

EVERY_FRAME 

The viewport is cleared once per frame (default behavior)

THIS_FRAME_ONLY 

The viewport is cleared only once, at this frame.

NEXT_FRAME_ONLY 

The viewport is cleared only once, at next frame.

NEVER 

The viewport is never cleared.

◆ Type

enum ncine::Viewport::Type
strong

The different types of viewports available.

Enumerator
WITH_TEXTURE 

The viewport renders in one or more textures.

NO_TEXTURE 

The viewport has no texture of its own, it uses the one from the previous viewport.

SCREEN 

The viewport is the screen.

Member Function Documentation

◆ removeAllTextures()

bool ncine::Viewport::removeAllTextures ( )

Removes all textures and the depth stencil renderbuffer from the viewport's FBO.

Returns
True if the viewport is not the screen and all textures have been removed

◆ setCamera()

void ncine::Viewport::setCamera ( Camera camera)
inline

Sets the camera to be used for rendering.

Note
The passed parameter can be a nullptr to use the default camera

◆ setDepthStencilFormat()

bool ncine::Viewport::setDepthStencilFormat ( DepthStencilFormat  depthStencilFormat)

Sets the depth and stencil format of the viewport's FBO renderbuffer.

Note
It can remove the depth and stencil render buffer of the viewport's FBO by specifying DepthStencilFormat::NONE.
Returns
True if a new depth and stencil format has been successfully set

◆ setTexture()

bool ncine::Viewport::setTexture ( unsigned int  index,
Texture texture 
)

Adds or removes a texture at the specified viewport's FBO color attachment index.

Note
Adding more textures enables the use of multiple render targets (MRTs).
Returns
True if a texture has been added or removed successfully

Member Data Documentation

◆ camera_

Camera* ncine::Viewport::camera_
protected

The camera used by this viewport.

Note
If set to nullptr it will use the default camera

The documentation for this class was generated from the following files: