VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Public Member Functions | Static Public Member Functions | List of all members

Class managing GL state switches and draw calls. More...

Public Member Functions

void begin ()
 Signal the Renderer that the frame has begun. More...
 
void end ()
 Signals that rendering this frame should end. More...
 
const Matrix4getProjectionMatrix () const
 Returns the projection matrix that will be used when rendering sprites. More...
 
void setProjectionMatrix (const Matrix4 &matrix)
 Sets the projection matrix that will be used when rendering sprites. More...
 
void drawSprite (const TexturePtr &texture, const Vector2 &pos, float width, float height, const Vector2 &srcPos, float srcWidth, float srcHeight, float rotation, float opacity)
 Draws a rectangular quadrilateral, textured with a Texture. More...
 
void drawMesh (const MeshInstancePtr &meshInstance, const ShaderPtr &shader)
 Draw a MeshInstance with a given Shader. More...
 
void flush ()
 Flush GL draw calls and state, and deselect objects. More...
 
void selectTexture (const TexturePtr &texture, int stage)
 Selects and binds a Texture to a texture stage. More...
 
void selectMesh (const MeshInstancePtr &meshInstance)
 Selects and binds a MeshInstance. More...
 
void selectShader (const ShaderPtr &shader)
 Selects a Shader as current. More...
 
void clearTexture (int stage)
 Unbinds and clears the Texture at the given stage. More...
 
void clearTextures ()
 Clears all texture stages. More...
 
void clearMesh ()
 Unbinds and clears the current MeshInstance. More...
 
void clearShader ()
 Unbinds and clears the current Shader. More...
 
DebugRenderergetDebugRenderer () const
 Returns the DebugRenderer managed by this Renderer. More...
 
void attachAsCurrent ()
 Sets this Renderer as current, allowing it to be retrieved by getCurrent(). More...
 
void detachAsCurrent ()
 Unsets this Renderer as current. More...
 

Static Public Member Functions

static std::string addCompatibilityPrefixToShaderCode (const char *code)
 Prefixes the given shader code with code that normalizes shaders across some platforms. More...
 
static void detachCurrent ()
 Unsets the current Renderer, if any. More...
 
static RenderergetCurrent ()
 Returns the Renderer that was set as current through a call to attachAsCurrent(). More...
 

Detailed Description

Class managing GL state switches and draw calls.

This class stores currently selected Textures, MeshInstance, and Shader. The select family of methods switches these currently selected objects (for example, selectShader()). These internally call bind() and unbind() (or begin() and end()) methods as necessary. The draw family of methods in this class (for example drawMesh()) in turn use these select methods to set up state, and then issue GL draw calls to perform rendering.

This class also provides functionality to render basic rectangular sprites. This is done through drawSprite(). Drawing done through this method will be transformed by the current projection matrix (setProjectionMatrix()). Sprites are rendered using SpriteShader. Drawing done through drawMesh() must be transformed manually in the user's Shader.

To interleave rendering performed by this class and other renderers, call flush(). This deselects all objects managed by this class putting GL into a clean state.

This class also creates and provides a DebugRenderer object which can be used to quickly implement debug shape rendering.

See also
Shader
MeshInstance
Texture

Member Function Documentation

static std::string Renderer::addCompatibilityPrefixToShaderCode ( const char *  code)
static

Prefixes the given shader code with code that normalizes shaders across some platforms.

For example, some versions of desktop OpenGL do not have or have reserved the precision specifiers highp, mediump, and lowp.

Parameters
codeShader code to adjust
Returns
Adjusted shader code
void Renderer::attachAsCurrent ( )

Sets this Renderer as current, allowing it to be retrieved by getCurrent().

The Renderer should be set as current before rendering RenderLists and RenderNodes.

void Renderer::begin ( )

Signal the Renderer that the frame has begun.

This initializes any internal GL resources if necessary.

void Renderer::clearMesh ( )

Unbinds and clears the current MeshInstance.

void Renderer::clearShader ( )

Unbinds and clears the current Shader.

void Renderer::clearTexture ( int  stage)

Unbinds and clears the Texture at the given stage.

Parameters
stagetexture stage to clear
void Renderer::clearTextures ( )

Clears all texture stages.

void Renderer::detachAsCurrent ( )

Unsets this Renderer as current.

static void Renderer::detachCurrent ( )
static

Unsets the current Renderer, if any.

void Renderer::drawMesh ( const MeshInstancePtr meshInstance,
const ShaderPtr shader 
)

Draw a MeshInstance with a given Shader.

The Shader and MeshInstance are bound. The Mesh vertices are then drawn with the set GL primitive type (see MeshInstance::setMode()). If the given Shader does not exactly match the current Shader in the MeshInstance, rendering will still behave correctly if the two Shaders' AttributeSets are equivalent.

Parameters
meshInstanceMesh to draw
shaderShader to draw the mesh with
void Renderer::drawSprite ( const TexturePtr texture,
const Vector2 pos,
float  width,
float  height,
const Vector2 srcPos,
float  srcWidth,
float  srcHeight,
float  rotation,
float  opacity 
)

Draws a rectangular quadrilateral, textured with a Texture.

The destination rectangle is in world coordinates and gets rotated by rotation and then the Renderer's projection matrix. The source rectangle specifies which part of the source texture gets mapped on to the destination rectangle. The source rectangle is in GL texture coordinate space ((0.0f, 0.0f) is lower left).

These are rendered using SpriteShader.

Parameters
textureTexture to draw
posPosition of the destination rectangle
widthWidth of the destination rectangle
heightHeight of the destination rectangle
srcPosOffset of the source rectangle in the source Texture
srcWidthWidth of the source rectangle in the source Texture
srcHeightHeight of the source rectangle in the source Texture
rotationRendered sprite rotation in radians
opacityRendered sprite transparency, in the range [0.0f, 1.0f]
void Renderer::end ( )

Signals that rendering this frame should end.

This flushes any draw calls and selected objects. This method does not flush the DebugRenderer.

void Renderer::flush ( )

Flush GL draw calls and state, and deselect objects.

This can be called to signal that GL calls will be issued by code other than this Renderer. It is automatically called by end().

static Renderer* Renderer::getCurrent ( )
static

Returns the Renderer that was set as current through a call to attachAsCurrent().

Returns
Current attached Renderer
DebugRenderer* Renderer::getDebugRenderer ( ) const
inline

Returns the DebugRenderer managed by this Renderer.

const Matrix4& Renderer::getProjectionMatrix ( ) const
inline

Returns the projection matrix that will be used when rendering sprites.

void Renderer::selectMesh ( const MeshInstancePtr meshInstance)

Selects and binds a MeshInstance.

If meshInstance is the current MeshInstance, this method performs no work. If meshInstance is nullptr this call is equivalent to clearing the current mesh instance.

Parameters
meshInstanceMeshInstance to select
void Renderer::selectShader ( const ShaderPtr shader)

Selects a Shader as current.

If shader is the current Shader, this method performs no work. If shader is nullptr this call is equivalent to clearing the current Shader.

Parameters
shaderShader to select
void Renderer::selectTexture ( const TexturePtr texture,
int  stage 
)

Selects and binds a Texture to a texture stage.

If texture is the current Texture, this method performs no work. If texture is nullptr this call is equivalent to clearing the current texture for that stage.

Parameters
textureTexture to select
stageTexture stage to bind the Texture to
void Renderer::setProjectionMatrix ( const Matrix4 matrix)

Sets the projection matrix that will be used when rendering sprites.

Parameters
matrixMatrix to set