Carna  Version 3.0.1
Public Types | Public Member Functions | Protected Member Functions | Friends | List of all members
Carna::base::RenderState Class Reference

Manages the OpenGL render state. More...

#include <RenderState.h>

Public Types

enum  CullFace { cullNone, cullBack, cullFront }
 Lists supported face culling modes. More...
 

Public Member Functions

 RenderState ()
 Derives new render state from the current one of the current OpenGL context.
 
 ~RenderState ()
 Restores previous render state.
 
void setDepthTest (bool dt)
 Enables or disables depth testing.
 
void setDepthWrite (bool dw)
 Enables or disables depth writing.
 
void setDepthTestFunction (int dtf)
 Configures how the depth test is to be performed. Depth test only applies if it is activated.. More...
 
void setBlend (bool b)
 Enables or disables alpha blending. Alpha blending is configured through setBlendFunction and setBlendEquation.
 
void setBlendFunction (const BlendFunction &bf)
 Configures how alpha blending is to be done. Alpha blending only applies if it is activated.. More...
 
void setBlendEquation (int be)
 Sets glBlendEquation to be.
 
void setCullFace (CullFace cf)
 Configures face culling. Also see setFrontFace.
 
void setFrontFace (bool ccw)
 Sets whether front faces are winded contra-clockwise. Also see setCullFace.
 
void setPointSize (float pointSize)
 Sets glPointSize to pointSize if point size is non-negative. Enables GL_PROGRAM_POINT_SIZE and leaves glPointSize unchanged otherwise. This enables support for gl_PointSize in shaders.
 

Protected Member Functions

 RenderState (GLContext &glContext)
 Creates the root instance of glContext. For internal usage only.
 
void commit () const
 Commits the full state. For internal usage only.
 

Friends

class GLContext
 

Detailed Description

Manages the OpenGL render state.

Each OpenGL context has a so-called render state. This describes things like whether alpha blending is to be used for rendering primitives or not. The purpose of this class is to reduce the state transitions, that can be quite expansive, and make it easy to enforce a fully-defined state.

The mechanism behind this class exploits the fact that the current OpenGL context is a thread-local variable as described here. This allows us to utilize the execution stack to track the render state:

void render()
{
rs.setDepthTest( false );
rs.setBlend( true );
render1();
render2();
}
void render1()
{
rs.setDepthTest( false );
rs.setBlendFunction( BlendFunction( GL_ONE, GL_ONE ) );
renderSomething();
}
void render2()
{
rs.setDepthTest( false );
renderSomething();
}

The constructor derives a new render state, that immediately becomes the current one and equals the previous state. The method render disables depth testing and enables alpha blending. Whether state transitions actually happen here depends on how the previous state was configured. When render1 turns off depth testing again, nothing happens, because it already was disabled within the previous state. Same is true for render2. The updated blend function of render1 does not affect render2. Note that it is forbidden to modify a render state that is not the current one.

Author
Leonid Kostrykin
Date
22.2.15 - 16.3.15

Definition at line 85 of file RenderState.h.

Member Enumeration Documentation

Lists supported face culling modes.

Enumerator
cullNone 

All polygons will pass the cull test.

cullBack 

Polygons recognized as back-faces will be discarded.

cullFront 

Polygons recognized as front-faces will be discarded.

Definition at line 112 of file RenderState.h.

Member Function Documentation

void Carna::base::RenderState::setBlendFunction ( const BlendFunction bf)

Configures how alpha blending is to be done. Alpha blending only applies if it is activated..

This is the equivalent of glBlendFunc.

void Carna::base::RenderState::setDepthTestFunction ( int  dtf)

Configures how the depth test is to be performed. Depth test only applies if it is activated..

This is the equivalent of glDepthFunc.


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