KGLLib
KGLLib::Batch Class Reference
#include <batch.h>

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
Definition at line 100 of file batch.h.
Public Member Functions | |
| Batch (GeometryBuffer *buffer, int offset, int indexOffset) | |
| Batch () | |
| GeometryBufferFormat | bestBufferFormat () const |
| virtual void | bind () |
| GeometryBuffer * | buffer () const |
| const void * | colorsArray () const |
| const void * | indicesArray () const |
| int | indicesCount () const |
| const void * | normalsArray () const |
| GLenum | primitiveType () const |
| virtual void | render () |
| virtual void | renderOnce () |
| void | setBuffer (GeometryBuffer *buffer, int offset, int indexOffset) |
| void | setColors (Eigen::Vector4f *colors) |
| void | setColors (Eigen::Vector3f *colors) |
| void | setIndices (unsigned int *indices, int count) |
| void | setNormals (Eigen::Vector3f *normals) |
| virtual void | setPrimitiveType (GLenum type) |
| void | setTexcoords (Eigen::Vector4f *texcoords) |
| void | setTexcoords (Eigen::Vector3f *texcoords) |
| void | setTexcoords (Eigen::Vector2f *texcoords) |
| void | setTexcoords (float *texcoords) |
| void | setVertexCount (int count) |
| void | setVertices (Eigen::Vector4f *vertices) |
| void | setVertices (Eigen::Vector3f *vertices) |
| void | setVertices (Eigen::Vector2f *vertices) |
| const void * | texcoordsArray () const |
| virtual void | unbind () |
| virtual void | update () |
| int | vertexCount () const |
| const void * | verticesArray () const |
| virtual | ~Batch () |
Static Public Member Functions | |
| static GeometryBuffer * | createSharedBuffer (const QList< Batch * > &batches) |
Protected Member Functions | |
| void | init () |
| void | setColors (void *colors, int size) |
| void | setTexcoords (void *texcoords, int size) |
| void | setVertices (void *vertices, int size) |
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.
| 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:
-
buffer shared GeometryBuffer object to use offset offset for the vertex data in the buffer indexOffset offset for the index data in the buffer
| 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.
| 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] |
| const void* KGLLib::Batch::colorsArray | ( | ) | const [inline] |
| 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.
| const void* KGLLib::Batch::indicesArray | ( | ) | const [inline] |
| int KGLLib::Batch::indicesCount | ( | ) | const [inline] |
| const void* KGLLib::Batch::normalsArray | ( | ) | const [inline] |
| GLenum KGLLib::Batch::primitiveType | ( | ) | const |
| 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] |
| 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:
-
buffer shared GeometryBuffer object to use. offset offset for the vertex data in the buffer. indexOffset offset for the index data in the buffer.
| void KGLLib::Batch::setColors | ( | void * | colors, | |
| int | size | |||
| ) | [protected] |
| void KGLLib::Batch::setColors | ( | Eigen::Vector4f * | colors | ) | [inline] |
| void KGLLib::Batch::setColors | ( | Eigen::Vector3f * | colors | ) | [inline] |
| void KGLLib::Batch::setIndices | ( | unsigned int * | indices, | |
| int | count | |||
| ) |
| void KGLLib::Batch::setNormals | ( | Eigen::Vector3f * | normals | ) |
| void KGLLib::Batch::setPrimitiveType | ( | GLenum | type | ) | [virtual] |
| void KGLLib::Batch::setTexcoords | ( | void * | texcoords, | |
| int | size | |||
| ) | [protected] |
| void KGLLib::Batch::setTexcoords | ( | Eigen::Vector4f * | texcoords | ) | [inline] |
| void KGLLib::Batch::setTexcoords | ( | Eigen::Vector3f * | texcoords | ) | [inline] |
| void KGLLib::Batch::setTexcoords | ( | Eigen::Vector2f * | texcoords | ) | [inline] |
| void KGLLib::Batch::setTexcoords | ( | float * | texcoords | ) | [inline] |
| void KGLLib::Batch::setVertexCount | ( | int | count | ) |
| void KGLLib::Batch::setVertices | ( | void * | vertices, | |
| int | size | |||
| ) | [protected] |
| void KGLLib::Batch::setVertices | ( | Eigen::Vector4f * | vertices | ) | [inline] |
| void KGLLib::Batch::setVertices | ( | Eigen::Vector3f * | vertices | ) | [inline] |
| void KGLLib::Batch::setVertices | ( | Eigen::Vector2f * | vertices | ) | [inline] |
| const void* KGLLib::Batch::texcoordsArray | ( | ) | const [inline] |
| void KGLLib::Batch::unbind | ( | ) | [virtual] |
| 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] |
| const void* KGLLib::Batch::verticesArray | ( | ) | const [inline] |
The documentation for this class was generated from the following files:
KDE 4.2 API Reference