Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
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.

openglscreenshot.png


Here is my output:

opengloutput.png


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!
 

Blakeasd

macrumors 6502a
Dec 29, 2009
643
0
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!
 
Last edited:

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
Yup, I have the app is checked for the target membership. This must be something absurdly simple!

So frustrating...
 

Analog Kid

macrumors G3
Mar 4, 2003
8,854
11,368
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.
 

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
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.

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

Analog Kid

macrumors G3
Mar 4, 2003
8,854
11,368
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?
 

Analog Kid

macrumors G3
Mar 4, 2003
8,854
11,368
Hmm... I'm not sure what you mean here. What am I looking for? Thanks for trying to help me troubleshoot!
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?
 

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
I think we have your .m file, can you list your MyOpenGLView.h?

Here:

Code:
//
//  MyOpenGLView.h
//  Golden Triangle

#import <Cocoa/Cocoa.h>

@interface MyOpenGLView : NSOpenGLView
{
}
- (void) drawRect: (NSRect) bounds;
@end

Pretty simple?
 

Analog Kid

macrumors G3
Mar 4, 2003
8,854
11,368
Pretty simple?
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.
 

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
When I get a chance, I'll see if I can reproduce it.

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.
 

Ap0ks

macrumors 6502
Aug 12, 2008
316
93
Cambridge, UK
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.
 

tmptplayer

macrumors newbie
Original poster
Jul 1, 2013
7
0
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.

Yes! That was it! Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.