KGLLib
Public Member Functions | Static Public Member Functions | Protected Member Functions
KGLLib::Batch Class Reference

A set of geometry. More...

#include <batch.h>

Inheritance diagram for KGLLib::Batch:
KGLLib::Mesh KGLLib::SimpleTerrain

List of all members.

Public Member Functions

 Batch ()
 Batch (GeometryBuffer *buffer, int offset, int indexOffset)
virtual ~Batch ()
virtual void render ()
virtual void bind ()
virtual void renderOnce ()
virtual void unbind ()
void setVertexCount (int count)
int vertexCount () const
void setVertices (Eigen::Vector2f *vertices)
void setVertices (Eigen::Vector3f *vertices)
void setVertices (Eigen::Vector4f *vertices)
const void * verticesArray () const
void setColors (Eigen::Vector3f *colors)
void setColors (Eigen::Vector4f *colors)
const void * colorsArray () const
void setNormals (Eigen::Vector3f *normals)
const void * normalsArray () const
void setTexcoords (float *texcoords)
void setTexcoords (Eigen::Vector2f *texcoords)
void setTexcoords (Eigen::Vector3f *texcoords)
void setTexcoords (Eigen::Vector4f *texcoords)
const void * texcoordsArray () const
void setIndices (unsigned int *indices, int count)
const void * indicesArray () const
int indicesCount () const
virtual void setPrimitiveType (GLenum type)
GLenum primitiveType () const
virtual void update ()
GeometryBufferbuffer () const
void setBuffer (GeometryBuffer *buffer, int offset, int indexOffset)
GeometryBufferFormat bestBufferFormat () const

Static Public Member Functions

static GeometryBuffercreateSharedBuffer (const QList< Batch * > &batches)

Protected Member Functions

void setVertices (void *vertices, int size)
void setColors (void *colors, int size)
void setTexcoords (void *texcoords, int size)
void init ()

Detailed Description

A set of geometry.

Batch class contains geometry data which is necessary for rendering something. This data can include vertices, normals, texture coordinates and vertex colors.

Batch object can automatically use VBOs (vertex buffer objects) for improved rendering performance while falling back to traditional vertex arrays on systems where VBOs are not supported.

To use it, you need to construct the Batch object, set the necessary arrays and then call the render() method:

  // Create the Batch object
  Batch* b = new Batch();
  b->setVertices(myvertices);
  b->setTexCoords(mytexcoords);
  b->setPrimitiveType(GL_TRIANGLES);
  ...
  // In your rendering loop:
  b->render();
  // some textured triangles will be rendered.

Modes

Batch uses a GeometryBuffer object to store its data and can be used in two modes: with internal or shared buffer.

With internal buffer

With internal buffer, Batch creates is own GeometryBuffer object when update() is called (which is called automatically by render() and bind() when batch's data has changed).

This is the easiest mode to use: you only need to create the Batch objects, give it some data and then call render() to render the geometry.

With shared buffer

The other mode uses a single GeometryBuffer object which is shared between multiple Batch objects. The advantage of this mode is that bind() and unbind(), which might be expensive operations, only need to be called once per frame instead of once per rendered Batch.

To make multiple batches use a shared buffer, the easiest way is to call createSharedBuffer() method. It will automatically create a large enough buffer and make all the batches use it.

 // First create all batches and specify data (using setVertices() and friends)
 QList<Batch*> models = loadAllModels();
 // Then create a shared GeometryBuffer
 Batch::createSharedBuffer(models);

After that you will need to bind() the buffer or batch only once and then just call renderOnce() for each batch that uses the same buffer. Of course finally unbind() has to be called as well:

 // It doesn't matter which Batch we bind() since they all use the same buffer
 models.first()-> bind();
 // Render all models
 foreach (Batch* b, models) {
     b-> renderOnce();
 }
 // Finally unbind the buffer
 models.first()-> unbind();
See also:
Mesh, GeometryBuffer

Constructor & Destructor Documentation

KGLLib::Batch::Batch ( )

Constructs new Batch object.

An internal GeometryBuffer object will be created before rendering. You will need to set at least vertices array and vertex count before the batch can be rendered.

References KGLLib::init().

KGLLib::Batch::Batch ( GeometryBuffer buffer,
int  offset,
int  indexOffset 
)

Constructs new Batch object, using a shared GeometryBuffer. Shared buffers can improve performance since buffers needn't be bound and unbound while rendering.

Parameters:
buffershared GeometryBuffer object to use
offsetoffset for the vertex data in the buffer
indexOffsetoffset for the index data in the buffer

References KGLLib::init().

KGLLib::Batch::~Batch ( ) [virtual]

Deletes the Batch. If an internal GeometryBuffer is used then it is also deleted.


Member Function Documentation

GeometryBufferFormat KGLLib::Batch::bestBufferFormat ( ) const

Returns GeometryBufferFormat object that could be used to create a GeometryBuffer object for this Batch.

The returned format contains all attributes (vertices, colors, etc) which are used by this batch.

References KGLLib::GeometryBufferFormat::addColors(), KGLLib::GeometryBufferFormat::addNormals(), KGLLib::GeometryBufferFormat::addTexCoords(), and KGLLib::GeometryBufferFormat::addVertices().

void KGLLib::Batch::bind ( ) [virtual]

Binds the GeometryBuffer used by this batch.

update() is automatically called if Batch's data has changed.

Reimplemented in KGLLib::Mesh.

GeometryBuffer* KGLLib::Batch::buffer ( ) const [inline]
Returns:
GeometryBuffer object used by this batch
const void* KGLLib::Batch::colorsArray ( ) const [inline]
Returns:
array of colors used for this batch
GeometryBuffer * KGLLib::Batch::createSharedBuffer ( const QList< Batch * > &  batches) [static]

Creates a GeometryBuffer object shared by the given list of Batch objects.

The created buffer is big enough to hold data of all specified batches. The batches are automatically set to use the created buffer.

All the batches must have the same format, i.e. the GeometryBufferFormat objects returned by their bestBufferFormat() method can only differ in vertex and index counts.

References indicesCount(), setBuffer(), and vertexCount().

const void* KGLLib::Batch::indicesArray ( ) const [inline]
Returns:
array of indices used for this batch
int KGLLib::Batch::indicesCount ( ) const [inline]
Returns:
number of indices in this batch

Referenced by createSharedBuffer().

const void* KGLLib::Batch::normalsArray ( ) const [inline]
Returns:
array of normals used for this batch
GLenum KGLLib::Batch::primitiveType ( ) const
Returns:
OpenGL primitive mode used to render this batch.
void KGLLib::Batch::render ( ) [virtual]

Renders the batch.

This is same as calling first bind(), then renderOnce() and finally unbind().

Reimplemented in KGLLib::SimpleTerrain.

void KGLLib::Batch::renderOnce ( ) [virtual]

Renders the batch without doing bind and unbind operations on the geometry buffer.

The geometry buffer used by this batch has to be already bound before you can use this method.

void KGLLib::Batch::setBuffer ( GeometryBuffer buffer,
int  offset,
int  indexOffset 
)

Changes the GeometryBuffer object used by this batch.

With shared buffer, every Batch uses a subsection of the buffer, limitied by its buffer offsets and vertex/index counts.

If buffer is 0, then an internal buffer will be created and used.

Note that the data isn't copied immediately but the next time when update() or bind() is called. Thus all data specified for this Batch must be valid until then.

Parameters:
buffershared GeometryBuffer object to use.
offsetoffset for the vertex data in the buffer.
indexOffsetoffset for the index data in the buffer.

Referenced by createSharedBuffer().

void KGLLib::Batch::setColors ( Eigen::Vector3f *  colors) [inline]

Sets the per-vertex colors array to colors.

References setColors().

Referenced by setColors().

void KGLLib::Batch::setIndices ( unsigned int *  indices,
int  count 
)

Sets the indices array to indices. The array must contain at least count entries (if it contains more, then the remaining ones will be unused).

void KGLLib::Batch::setNormals ( Eigen::Vector3f *  normals)

Sets the normals array to normals.

void KGLLib::Batch::setPrimitiveType ( GLenum  type) [virtual]

Sets the primitive type used to render this batch (e.g. GL_QUADS).

Default value is GL_TRIANGLES.

void KGLLib::Batch::setTexcoords ( float *  texcoords) [inline]

Sets the texture coordinates array to texcoords.

References setTexcoords().

Referenced by setTexcoords().

void KGLLib::Batch::setVertexCount ( int  count)

Set the number of vertices in the batch to count. Each specified array must contain at least count entries (if they contain more, then the remaining ones will be unused).

void KGLLib::Batch::setVertices ( Eigen::Vector2f *  vertices) [inline]

Sets the vertices array to vertices.

References setVertices().

Referenced by setVertices().

const void* KGLLib::Batch::texcoordsArray ( ) const [inline]
Returns:
array of texture coordinates used for this batch
void KGLLib::Batch::unbind ( ) [virtual]

Unbinds the GeometryBuffer used by this batch.

Reimplemented in KGLLib::Mesh.

void KGLLib::Batch::update ( ) [virtual]

Updates the used GeometryBuffer if something has changed.

This method is automatically called from bind() in case something has changed, but you can also call it manually to remove the delay at first rendering.

int KGLLib::Batch::vertexCount ( ) const [inline]
Returns:
number of vertices in this batch

Referenced by createSharedBuffer().

const void* KGLLib::Batch::verticesArray ( ) const [inline]
Returns:
array of vertices used for this batch

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