Mac OpenGL Tutorial

Discussion in 'Mac Programming' started by tmptplayer, Jul 1, 2013.

  1. macrumors newbie

    Hi all,

    I'm brand new to Objective-C/Cocoa/OpenGL/XCode (coming from windows web developing environment) and I was trying to run the most basic of OpenGL tutorials from the apple website (

    All I want to is make a colored triangle, yet my output is blank.

    Here is my OpenGLView Class:

    //  MyOpenGLView.m
    //  Golden Triangle
    #import "MyOpenGLView.h"
    #include <OpenGL/gl.h>
    @implementation MyOpenGLView
    static void drawAnObject ()
        glColor3f(1.0f, 0.85f, 0.35f);
            glVertex3f(  0.0,  0.6, 0.0);
            glVertex3f( -0.2, -0.3, 0.0);
            glVertex3f(  0.2, -0.3 ,0.0);
    -(void) drawRect: (NSRect) bounds
        glClearColor(0, 0, 0, 0);
    Here is a screenshot of how I set up the interface.


    Here is my output:


    I'm probably doing something insanely basic incorrectly, but I've been banging my head against the wall for hours. I have break points in my drawRect class that aren't being caught (though this could be because I'm not properly running xCode in debug mode...) and I have a label "Test" underneath the OpenGL object on the interface to see if it is getting covered up by a blank drawn window, and as you can see it is not. What am I doing wrong!?

    Thanks for your help and patience!
  2. macrumors 603


    I hate those stupid teapots.:mad:
  3. Blakeasd, Jul 2, 2013
    Last edited: Jul 2, 2013

    macrumors 6502a

    Your code seems to work for me. When you select GLView.m is Target Membership checked for your app? If it isn't then your triangle won't draw.

    Hope it works!
  4. macrumors newbie

    Yup, I have the app is checked for the target membership. This must be something absurdly simple!

    So frustrating...
  5. macrumors 68030

    Analog Kid

    I'd start by taking out the drawAnObject call. It bothers me that it's not even clearing the view with black.

    And drop a breakpoint in the drawRect: to make sure it's even being called.
  6. macrumors newbie

    Tried it, breakpoint is not met and output is the same.
  7. macrumors 68030

    Analog Kid

    Sorry, I just saw that comment at the bottom of your original post too...

    Well, your problem isn't OpenGL then. For some reason your view isn't even trying to update.

    I don't see your interface, but did you properly subclass NSOpenGLView?
  8. macrumors newbie

    Hmm... I'm not sure what you mean here. What am I looking for? Thanks for trying to help me troubleshoot!
  9. macrumors 68030

    Analog Kid

    No problem. I'm not sure how much I'll be able to help, but we'll see.

    I think we have your .m file, can you list your MyOpenGLView.h?
  10. macrumors newbie


    //  MyOpenGLView.h
    //  Golden Triangle
    #import <Cocoa/Cocoa.h>
    @interface MyOpenGLView : NSOpenGLView
    - (void) drawRect: (NSRect) bounds;
    Pretty simple?
  11. macrumors 68030

    Analog Kid

    Yep, but not simple enough. I was hoping I'd see you forgot to put NSOpenGLView as your base class.

    Problem must be in Interface Builder somewhere. The code is simple enough. When I get a chance, I'll see if I can reproduce it.
  12. macrumors newbie

    Appreciate it!

    If it helps at all, I have tried other simple Objective C tutorials and I haven't been able to get any of them to work either. They all break down with the interface-code connection (for example, when making a simple action connection between a button and code via the drag-and-drop method, I get a broken connection error at runtime despite everything looking in order earlier). I don't know if this means I have something configured wrong or if my specific instance of X-Code just really hates me.
  13. macrumors regular

    Your interface isn't setup quite right, from your screenshot it looks like you've dragged the OpenGL View onto the sidebar (the black sphere to the left of your interface) rather than onto the actual NSWindow.

    If you select and delete that black sphere then drag an OpenGL View onto the grey window, resize as desired and then set the class to be MyOpenGLView it should work as you expect.

    I'd imagine the reason for your breakpoint not currently being hit is because your OpenGL View isn't within the bounds of the window (I'm guessing here since I don't have any real experience with OpenGL), but the code you've given works fine and shows a gold triangle on a black background.
  14. macrumors newbie

    Yes! That was it! Thanks!

Share This Page