Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on an OpenGL application and some routines render textured objects, some are just colored primitives. I was wondering if there was a standard convention for how to deal with setting the OpenGL 'state'.

For example, consider this routine (using OpenGL ES / OpenTK):

/// <summary>
/// Renders the given tile at the corresponding screen-tile location.
/// </summary>
public static void RenderTile(float[] vertices, float xOffset, float yOffset, uint textureId)
{
    GL.EnableClientState(All.VertexArray);
    GL.EnableClientState(All.ColorArray);
    GL.EnableClientState(All.TextureCoordArray);

    GL.PushMatrix();

    GL.Translate(xOffset, yOffset, 0.0f);

    GL.BindTexture(All.Texture2D, textureId);
    GL.VertexPointer(2, All.Float, 0, vertices);
    GL.ColorPointer(4, All.Float, 0, SQUARE_COLORS);
    GL.TexCoordPointer(2, All.Float, 0, SQUARE_TEXTURE_COORDS);

    GL.DrawArrays(All.TriangleStrip, 0, 4);

    GL.PopMatrix();
}

I'm curious about that GL.EnableClientState(All.TextureCoordArray); bit. Should I just get into the habit of calling GL.Enable(...) / Gl.EnableClientState(...) for everything the current function needs? Even if it is largely redundant? Or is it better to just assume that the application has a certain set of things enabled (blending, back-face culling, 2D textures, etc.).

I know this question is pretty subjective, so I'm looking for advice from someone who has worked with a large OpenGL codebase. (As all my experience is for solo projects.)

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Redundant state changes can add some overhead, and whether or not your driver filters them depends on the driver (so far as I'm aware the GL spec says nothing about this).

Generally I like to set state at the start of each drawing function, and implement wrappers that do their own filtering. I don't unset state at the end because to do so would mean that separation between drawing functions becomes broken - each function should definitely not have knowledge of what's coming next.

share|improve this answer
    
But not unsetting things would cause problems too, right? For example: Render X with blending, render Y which is now blended since GL_BLEND wasn't disabled. –  Chris Smith Dec 22 '11 at 1:56
    
Your state wrapper would handle this, of course. –  Jimmy Shelter Dec 22 '11 at 10:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.