25 #include "glfBatch.hh"
26 #include "glfScreen.hh"
27 #include "glfException.hh"
31 const GLenum INVALID_PRIMITIVE_TYPE = 0xffffffff;
34 MAX_TEXTURE_UNITS_(-1),
35 MAX_TEXTURE_COORDS_(-1),
37 maxUsedTextureIndex_(-1),
38 maxUsedTextureMatrixIndex_(-1)
44 shaderProgram_ = NULL;
47 elementBuffer_ = NULL;
48 primitiveType_ = INVALID_PRIMITIVE_TYPE;
61 BIASASSERT(renderTarget != NULL);
62 renderTarget_ = renderTarget;
67 BIASASSERT(viewport != NULL);
75 BIASASSERT(textureUnit >= 0 && textureUnit < MAX_TEXTURE_UNITS_);
77 if((
int)textures_.size() <= textureUnit) {
78 textures_.resize(textureUnit+1, NULL);
80 if(maxUsedTextureIndex_<textureUnit && texture!= NULL)
81 maxUsedTextureIndex_=textureUnit;
83 if(textureUnit == maxUsedTextureIndex_-1 && texture == NULL) {
84 maxUsedTextureIndex_--;
87 textures_[textureUnit] = texture;
92 for(
int i=maxUsedTextureIndex_;
93 i>=
static_cast<int>(fromIndex); i--){
96 maxUsedTextureIndex_ = fromIndex-1;
102 return maxUsedTextureIndex_;
108 BIASASSERT(depthBufferMode != NULL);
109 depthBufferMode_ = depthBufferMode;
114 BIASASSERT(stencilBufferMode != NULL);
115 stencilBufferMode_ = stencilBufferMode;
120 BIASASSERT(blendMode != NULL);
121 blendMode_ = blendMode;
126 shaderProgram_ = shaderProgram;
131 vertexBuffer_ = vertexBuffer;
136 elementBuffer_ = elementBuffer;
141 primitiveType_ = primitiveType;
152 BIASASSERT(matrix != NULL);
153 modelViewMatrix_ = matrix;
158 BIASASSERT(matrix != NULL);
159 projectionMatrix_ = matrix;
165 BIASASSERT(index >= 0 && index < MAX_TEXTURE_COORDS_);
167 if((
int)textureMatrix_.size() <= index) {
168 textureMatrix_.resize(index+1, NULL);
172 if(maxUsedTextureMatrixIndex_<index && matrix != NULL)
173 maxUsedTextureMatrixIndex_=index;
175 textureMatrix_[index] = matrix;
177 if(index == maxUsedTextureMatrixIndex_-1 && matrix == NULL) {
178 maxUsedTextureMatrixIndex_--;
184 for(
int i=maxUsedTextureMatrixIndex_;
185 i>=
static_cast<int>(fromIndex); i--){
189 maxUsedTextureMatrixIndex_ = fromIndex-1;
195 return maxUsedTextureMatrixIndex_;
204 if(static_cast<int>(textureMatrix_.size())>MAX_TEXTURE_COORDS_)
205 GLF_THROW_EXCEPTION(
"Too many texture matrices assigned!");
206 if(static_cast<int>(textures_.size())>MAX_TEXTURE_UNITS_)
207 GLF_THROW_EXCEPTION(
"Too many textures assigned!");
209 if(static_cast<int>(textureMatrix_.size())<MAX_TEXTURE_COORDS_) {
210 textureMatrix_.resize(MAX_TEXTURE_COORDS_, NULL);
212 if(static_cast<int>(textures_.size())<MAX_TEXTURE_UNITS_) {
213 textures_.resize(MAX_TEXTURE_UNITS_, NULL);
222 if (elementBuffer_ != NULL) {
224 elementBuffer_->
Draw(first_, count_);
231 BIASASSERT(primitiveType_ != INVALID_PRIMITIVE_TYPE);
232 glDrawArrays(primitiveType_, first_, count);
237 GLF_THROW_ON_OPENGL_ERROR;
241 void glfBatch::BindRenderStates()
248 if(renderTarget_ != NULL)
249 renderTarget_->
Bind();
252 if(viewport_ != NULL)
256 for (
int i = 0; i <= maxUsedTextureIndex_; i++) {
257 if (textures_[i] != NULL) {
258 glActiveTexture(GL_TEXTURE0 + i);
259 textures_[i]->Bind();
264 if(depthBufferMode_ != NULL)
265 depthBufferMode_->
Bind();
267 if(stencilBufferMode_ != NULL)
268 stencilBufferMode_->
Bind();
271 if(blendMode_ != NULL)
275 if (shaderProgram_ != NULL) {
276 shaderProgram_->
Bind();
280 if (textures_[0] != NULL) {
281 glActiveTexture(GL_TEXTURE0);
282 glEnable(textures_[0]->GetTextureTarget());
284 glActiveTexture(GL_TEXTURE0);
285 glDisable(GL_TEXTURE_1D);
286 glDisable(GL_TEXTURE_2D);
287 glDisable(GL_TEXTURE_3D);
292 if(vertexBuffer_ != NULL)
293 vertexBuffer_->
Bind();
296 if (modelViewMatrix_ != NULL) {
297 glMatrixMode(GL_MODELVIEW);
298 modelViewMatrix_->
Load();
302 if (projectionMatrix_ != NULL) {
303 glMatrixMode(GL_PROJECTION);
304 projectionMatrix_->
Load();
309 for (
int i = 0; i < MAX_TEXTURE_COORDS_; i++) {
310 if (textureMatrix_[i] != NULL) {
311 glActiveTexture(GL_TEXTURE0 + i);
312 glMatrixMode(GL_TEXTURE);
313 textureMatrix_[i]->Load();
316 glActiveTexture(GL_TEXTURE0 + i);
317 glMatrixMode(GL_TEXTURE);
326 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &TUs);\
327 return
static_cast<int>(TUs);
333 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &matrices);
334 return static_cast<int>(matrices);
344 GLF_THROW_ON_OPENGL_ERROR
347 for (
int i = 0; i < MAX_TEXTURE_UNITS; i++) {
348 glActiveTexture(GL_TEXTURE0 + i);
349 glBindTexture(GL_TEXTURE_1D, 0);
350 glBindTexture(GL_TEXTURE_2D, 0);
351 glBindTexture(GL_TEXTURE_3D, 0);
367 glBindBuffer(GL_ARRAY_BUFFER, 0);
368 GLint maxTextureCoords;
369 glGetIntegerv(GL_MAX_TEXTURE_COORDS, &maxTextureCoords);
370 GLint maxVertexAttribs;
371 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);
372 glDisableClientState(GL_VERTEX_ARRAY);
373 glDisableClientState(GL_NORMAL_ARRAY);
374 glDisableClientState(GL_COLOR_ARRAY);
381 for (
int i = 0; i < maxVertexAttribs; i++) {
382 glDisableVertexAttribArray(i);
386 glMatrixMode(GL_MODELVIEW);
390 glMatrixMode(GL_PROJECTION);
394 for (
int i = 0; i < MAX_TEXTURE_COORDS; i++) {
395 glActiveTexture(GL_TEXTURE0 + i);
396 glMatrixMode(GL_TEXTURE);
401 glActiveTexture(GL_TEXTURE0);
404 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
405 GLF_THROW_ON_OPENGL_ERROR
An element buffer contains vertex indices that form primitives.
void SetVertexBuffer(const glfVertexBuffer *vertexBuffer)
Sets the vertex buffer.
Defines the usage of the stencil buffer.
void SetModelViewMatrix(const glfMatrix *matrix)
Sets the modelview matrix.
Defines the usage of the depth buffer.
int GetNumVertices() const
Returns the number of vertices in the vertex buffer.
static const glfMatrix IDENTITY
The identity matrix.
void SetPrimitiveType(GLenum primitiveType)
Sets the primitive type to rendering.
virtual void Bind() const
Binds the viewport.
static int GetMaxSupportedTextureUnits()
void SetDepthBufferMode(const glfDepthBufferMode *depthBufferMode)
Sets the depth buffer mode.
void Draw()
Draws the batch.
void ReleaseTextureMatrices(unsigned int fromIndex)
Releases texture matrices including the matrix associated with the argument index.
static const glfBlendMode DEFAULT
The default blend mode.
A vertex buffer contains an array of vertices that can be used for rendering.
unsigned int GetMaxUsedTextureIndex()
void Bind() const
Binds the shader program.
A shader program composed of several shaders.
void SetViewport(const glfViewport *viewport)
Sets the viewport.
static const glfDepthBufferMode DEFAULT
The default depth buffer mode.
Defines the function that blends rendered geometry with its background.
static glfScreen & GetInstance()
Returns the global screen instance.
virtual void Bind() const =0
Makes this render target the currently used render target.
void SetTexture(const glfTexture *texture, int textureUnit)
Sets the texture of a texture unit.
unsigned int GetMaxUsedTextureMatrixIndex() const
class for setting viewports
void Bind() const
Binds the vertex buffer in OpenGL.
void SetRenderTarget(const glfRenderTarget *renderTarget)
Sets the render target.
virtual void Bind() const
Makes this render target the currently used render target.
void SetShaderProgram(const glfShaderProgram *shaderProgram)
Sets the shader program.
A 4x4 matrix in native OpenGL format.
void Draw(int first=0, int count=-1) const
Draw elements from the buffer.
void Bind() const
Binds the stencil buffer mode.
void SetProjectionMatrix(const glfMatrix *matrix)
Sets the projection matrix.
void ReleaseTextures(unsigned int fromIndex)
Releases texture including the matrix associated with the argument index.
void SetElementBuffer(const glfElementBuffer *elementBuffer)
Sets the element buffer.
static int GetMaxSupportedTextureMatrices()
void SetStencilBufferMode(const glfStencilBufferMode *stencilBufferMode)
Sets the stencil buffer mode.
Interface for render targets.
virtual void Load() const
Loads the matrix in OpenGL.
static const glfStencilBufferMode DEFAULT
The default stencil buffer mode.
void Bind() const
Binds the depth buffer mode.
static void SetDefaultRenderStates()
Sets all render states possibly modified by Batch to the OpenGL defaults.
void Bind() const
Binds the blend mode.
void SetRange(int first, int count)
Sets the range of vertices to be rendered if no element buffer is set, or the range of vertex indices...
void SetTextureMatrix(const glfMatrix *matrix, int index)
Sets a texture matrix.
void SetBlendMode(const glfBlendMode *blendMode)
Sets the blend mode.