Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 1, 2013, 03:22 PM   #1
tmptplayer
macrumors newbie
 
Join Date: Jul 2013
Mac OpenGL Tutorial

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



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!
tmptplayer is offline   0 Reply With Quote
Old Jul 1, 2013, 04:01 PM   #2
thekev
macrumors 603
 
thekev's Avatar
 
Join Date: Aug 2010
I hate those stupid teapots.
__________________
world's largest manufacturer of tin foil hats, none of that aluminum foil crap.
thekev is online now   0 Reply With Quote
Old Jul 2, 2013, 01:14 PM   #3
Blakeasd
macrumors 6502a
 
Join Date: Dec 2009
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 by Blakeasd; Jul 2, 2013 at 02:01 PM.
Blakeasd is offline   0 Reply With Quote
Old Jul 2, 2013, 05:45 PM   #4
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Yup, I have the app is checked for the target membership. This must be something absurdly simple!

So frustrating...
tmptplayer is offline   0 Reply With Quote
Old Jul 10, 2013, 12:26 PM   #5
Analog Kid
macrumors 68030
 
Analog Kid's Avatar
 
Join Date: Mar 2003
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.
__________________
Only trolls use the word "fanboy".
Analog Kid is offline   0 Reply With Quote
Old Jul 10, 2013, 12:37 PM   #6
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Quote:
Originally Posted by Analog Kid View Post
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.
tmptplayer is offline   0 Reply With Quote
Old Jul 10, 2013, 12:53 PM   #7
Analog Kid
macrumors 68030
 
Analog Kid's Avatar
 
Join Date: Mar 2003
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?
__________________
Only trolls use the word "fanboy".
Analog Kid is offline   0 Reply With Quote
Old Jul 10, 2013, 01:30 PM   #8
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Quote:
Originally Posted by Analog Kid View Post
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!
tmptplayer is offline   0 Reply With Quote
Old Jul 10, 2013, 01:59 PM   #9
Analog Kid
macrumors 68030
 
Analog Kid's Avatar
 
Join Date: Mar 2003
Quote:
Originally Posted by tmptplayer View Post
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?
__________________
Only trolls use the word "fanboy".
Analog Kid is offline   0 Reply With Quote
Old Jul 10, 2013, 02:04 PM   #10
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Quote:
Originally Posted by Analog Kid View Post

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?
tmptplayer is offline   0 Reply With Quote
Old Jul 10, 2013, 02:10 PM   #11
Analog Kid
macrumors 68030
 
Analog Kid's Avatar
 
Join Date: Mar 2003
Quote:
Originally Posted by tmptplayer View Post
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.
__________________
Only trolls use the word "fanboy".
Analog Kid is offline   0 Reply With Quote
Old Jul 10, 2013, 02:15 PM   #12
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Quote:
Originally Posted by Analog Kid View Post
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.
tmptplayer is offline   0 Reply With Quote
Old Jul 10, 2013, 03:46 PM   #13
Ap0ks
macrumors regular
 
Join Date: Aug 2008
Location: 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.
Ap0ks is offline   1 Reply With Quote
Old Jul 10, 2013, 04:04 PM   #14
tmptplayer
Thread Starter
macrumors newbie
 
Join Date: Jul 2013
Quote:
Originally Posted by Ap0ks View Post
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!
tmptplayer is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
OpenGL for OS X Tutorial retsgorf297 Mac Programming 9 Oct 7, 2013 12:25 PM
OpenGL 3.0 on Windows running virtualized on a Mac Schnigges Mac Programming 1 Jul 13, 2013 03:29 AM
OpenGL on new mac mini? VideoBeagle Mac mini 6 Apr 14, 2013 02:54 PM
How to Use Your iPhone as a MIDI Keyboard on Mac - Tutorial pavlovonline Digital Audio 0 Sep 23, 2012 12:56 PM
How to make a video tutorial on mac coolmacman MacBook Pro 2 Sep 18, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 02:35 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC