Mac OpenGL Tutorial

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

  1. macrumors newbie

    Joined:
    Jul 1, 2013
    #1
    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 (https://developer.apple.com/library...html#//apple_ref/doc/uid/TP40001987-CH404-SW8)

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

    Here is my OpenGLView Class:

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

    [​IMG]

    Here is my output:

    [​IMG]

    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

    thekev

    Joined:
    Aug 5, 2010
  3. Blakeasd, Jul 2, 2013
    Last edited: Jul 2, 2013

    macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #4
    Yup, I have the app is checked for the target membership. This must be something absurdly simple!

    So frustrating...
     
  5. macrumors 68030

    Analog Kid

    Joined:
    Mar 4, 2003
    #5
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #6
    Tried it, breakpoint is not met and output is the same.
     
  7. macrumors 68030

    Analog Kid

    Joined:
    Mar 4, 2003
    #7
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #8
    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

    Joined:
    Mar 4, 2003
    #9
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #10
    Here:

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

    Analog Kid

    Joined:
    Mar 4, 2003
    #11
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #12
    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

    Joined:
    Aug 12, 2008
    Location:
    Cambridge, UK
    #13
    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. thread starter macrumors newbie

    Joined:
    Jul 1, 2013
    #14
    Yes! That was it! Thanks!
     

Share This Page