6

I just call glEnableClientState() once in onSurfaceCreated() method of GLSurfaceView.Renderer interface. Eg:

public class GLRenderer implements GLSurfaceView.Renderer {
   @Override
   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
      gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
      ...
}

After that I don't invoke them again. I never invoke the glDisableClientState() method. But I see many programmers call both methods often wrapping them around all drawing calls.

Is anything wrong with my approach? Or is it a good practice or maybe more efficient to use the approach of wrapping them around all drawing calls?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Leonel Machava
  • 1,501
  • 11
  • 19

1 Answers1

3

I don't think there's anything wrong with your approach provided that all of your draw calls require the same state. If you're drawing something without normals/colors, you don't want to have the normal/color array enabled, etc.

If all your objects are sure to use the same arrays, then your method is likely the best, as you can eliminate unnecessary opengl calls. Disabling everything after each object is potentially worse for performance, but it is safer in general that you won't accidentally leave something enabled that you don't want.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Thank you. I have figured out `glEnableClientState()`/`glDisableClientState()` are most used in example code and libraries probably to be safe that the needed arrays are enabled and of course they disable the used arrays after the drawing to avoid a negative interference with another drawing code (that may not use some of the enabled arrays). – Leonel Machava Apr 07 '12 at 14:34
  • Also keep in mind that on systems using NVIDIA cards, if you neglect to wrap the enable/disable calls around the draw calls, you'll get an unstable application and/or crashes. See: https://stackoverflow.com/q/30406971/2398263 – Brian_Entei Jan 22 '22 at 02:36