iPhone and C++ OpenGL programming

I am currently working in my last big game for the iPhone with so much to do and so few time 😛

And during this last months I received many comments about writing a detailed post about C++ programming with OpenGL and the iPhone SDK.

Well… here it is!

I created a new OpenGL ES Application Project within Xcode.

I then added a test C++ class to the project. The trick here is to change the extension of the implementation file from “.cpp” to “.mm”, this way we can freely mix C++ code with ObjectiveC code.

I also renamed the “EAGLView.m” file to “EAGLView.mm” for the same reason. Well, in my project every implementation file ends with “.mm” to avoid headaches!

This is how the test class looks like:

class TestClass : public Singleton<testClass>

    friend class Singleton<testClass>;


    EAGLContext* m_pContext;
    GLuint m_uiViewRenderbuffer;
    GLuint m_uiViewFramebuffer;
    GLuint m_uiDepthRenderbuffer;



    void Init(EAGLContext* pContext, GLuint renderBuffer, GLuint frameBuffer, GLuint depthBuffer);
    void Cleanup(void);

    void Update(void);


    void BeginRender(void);
    void EndRender(void);

    void DrawSomething(void);


As you can see, the class is a singleton for simplicity and convenience.

Then I changed the “createFramebuffer”, “destroyFramebuffer” and “drawView” methods of “EAGLView.mm” in order to call the wrapper methods of our test class.

- (BOOL)createFramebuffer {

    glGenFramebuffersOES(1, &viewFramebuffer);
    glGenRenderbuffersOES(1, &viewRenderbuffer);

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer];
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);

        glGenRenderbuffersOES(1, &depthRenderbuffer);
        glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
        glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
        glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);

        NSLog(@"failed to make complete framebuffer object %x",     glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;

    TestClass::Instance().Init(context, viewRenderbuffer, viewFramebuffer, depthRenderbuffer);

    return YES;
- (void)destroyFramebuffer {


    glDeleteFramebuffersOES(1, &viewFramebuffer);
    viewFramebuffer = 0;
    glDeleteRenderbuffersOES(1, &viewRenderbuffer);
    viewRenderbuffer = 0;

    if(depthRenderbuffer) {
        glDeleteRenderbuffersOES(1, &depthRenderbuffer);
        depthRenderbuffer = 0;
- (void)drawView {


You can wrap the input methods of “EAGLView.mm” in the same simple way.

And I’m afraid that’s all folks :mrgreen:

You can grab the full source code from here.

Hope this helps!

5 thoughts on “iPhone and C++ OpenGL programming

Comments are closed.