title: OpenGL ES iPhone, corrupt image when using glClearColor
body: <p>I'm just starting to learn about OpenGL ES on the iPhone. I trying to get a very simple example working that sets up an EAGLContext and render buffer then simply uses glClearColor to set the screen color. </p>
  
  <p>My code compiles and executes but unfortunately, instead of the expected grey screen, I see a a white image with a random corruption pattern. I'm guessing I havent set things up correctly and I'm hoping that my mistake will be obvious to someone with a little experience in this area.</p>
  
  <p>My code is:</p>
  
  <p>AppDelegate.h</p>
  
  <pre><code>#import "GLView.h"
  #import &lt;UIKit/UIKit.h&gt;
  
  @interface AppDelegate : NSObject &lt;UIApplicationDelegate&gt; {
  @private
    UIWindow* m_window;
    GLView* m_view;
  }
  
  @end
  </code></pre>
  
  <p>AppDelegate.m</p>
  
  <pre><code>#import "AppDelegate.h"
  #import "AppDelegate.h"
  #import "GLView.h"
  
  @implementation AppDelegate
  
  - (void) applicationDidFinishLaunching: (UIApplication*) application
  {
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
  
    m_window = [[UIWindow alloc] initWithFrame: screenBounds];
    m_view = [[GLView alloc] initWithFrame: screenBounds];
  
    [m_window addSubview: m_view];
    [m_window makeKeyAndVisible];
  }
  
  - (void) dealloc
  {
    [m_view release];
    [m_window release];
    [super dealloc];
  }
  
  @end
  </code></pre>
  
  <p>GLView.h </p>
  
  <pre><code>#import &lt;QuartzCore/QuartzCore.h&gt;
  
  @interface GLView : UIView {
  @private
    EAGLContext* m_context;
  
  }
  
  - (void) drawView;
  
  @end
  </code></pre>
  
  <p>GLView.mm</p>
  
  <pre><code>#include &lt;OpenGLES/ES1/gl.h&gt;
  #include &lt;OpenGLES/ES1/glext.h&gt;
  #import "GLView.h"
  
  @implementation GLView
  
  + (Class) layerClass
  {
    return [CAEAGLLayer class];
  }
  
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
  
        //Set up the layer and context
        CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer;
        eaglLayer.opaque = YES;
  
        m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  
        if (!m_context || ![EAGLContext setCurrentContext:m_context]) {
          [self release];
          return nil;
        }
  
        // Create &amp; bind the color buffer so that the caller can allocate its space.
        GLuint renderbuffer;
        glGenRenderbuffersOES(1, &amp;renderbuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
        [m_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer];
        glViewport(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame));
  
        [self drawView];
  
      }
      return self;
  }
  
  - (void)drawView
  {
    glClearColor(0.5f,0.5f,0.5f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    [m_context presentRenderbuffer:GL_RENDERBUFFER_OES];
  }
  
  - (void) dealloc
  {
    if([EAGLContext currentContext] == m_context)
      [EAGLContext setCurrentContext:nil];
    [m_context release];
    [super dealloc];
  }
  </code></pre>
  
  <p>Many thanks for any help.</p>