PDA

View Full Version : Mac OpenGL Tutorial




tmptplayer
Jul 1, 2013, 03:22 PM
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/mac/#documentation/graphicsimaging/conceptual/OpenGL-MacProgGuide/opengl_drawing/opengl_drawing.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:


//
// 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.

http://www.t1weather.com/temp/openglscreenshot.png

Here is my output:

http://www.t1weather.com/temp/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!



thekev
Jul 1, 2013, 04:01 PM
I hate those stupid teapots.:mad:

Blakeasd
Jul 2, 2013, 01:14 PM
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!

tmptplayer
Jul 2, 2013, 05:45 PM
Yup, I have the app is checked for the target membership. This must be something absurdly simple!

So frustrating...

Analog Kid
Jul 10, 2013, 12:26 PM
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
Jul 10, 2013, 12:37 PM
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
Jul 10, 2013, 12:53 PM
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?

tmptplayer
Jul 10, 2013, 01:30 PM
but did you properly subclass NSOpenGLView?

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

Analog Kid
Jul 10, 2013, 01:59 PM
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
Jul 10, 2013, 02:04 PM
I think we have your .m file, can you list your MyOpenGLView.h?

Here:

//
// MyOpenGLView.h
// Golden Triangle

#import <Cocoa/Cocoa.h>

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

Pretty simple?

Analog Kid
Jul 10, 2013, 02:10 PM
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
Jul 10, 2013, 02:15 PM
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
Jul 10, 2013, 03:46 PM
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
Jul 10, 2013, 04:04 PM
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!