Cocoa NSOpenGLView

RebelPhoton
Unregistered
 
Post: #1
I'm trying to use an NSOpenGLView in my Cocoa app, but I'm having no luck. I'd like to have a square in my window which I can use OpenGL commands to draw to. I've created an NSOpenGLView in InterfaceBuilder, linked my controller class to it (and given it the name "glView"), and tried to initialize it with:

[[glView openGLContext] makeCurrentContext];
[[glView openGLContext] setView: glView];

Then I used some GL commands (e.g. glBegin(GL_QUADS); glColor4f(1.0, 0.0, 0.0, 1.0); glVertex3f(-0.5, -0.5, 0.0) ... etc.) to draw, then put:

[[glView openGLContext] flushBuffer];

to get it to update. However, nothing shows up in the window/view, not even black. When I resize the window the view turns black, then starts looking weird/having screen artifacts if I resize it smaller/bigger a lot.
I haven't done anything about the pixel format because InterfaceBuilder seems to have options for this, which would make you think it was doing something about it...
I've looked at some of the Apple Cocoa/GL samples but there seems to be a whole lot of junk in there to make it all "nice." I just want to do it as simple as possible. Any ideas?
Thanks,
Quote this message in a reply
ylaporte
Unregistered
 
Post: #2
When I use an NSOPenGLView, I usually subclkass it and put my OPenGL code in the drawRect: method.

However, calling [[glView openGLContext] makeCurrentContext]; should work.

You don't need to do this
[[glView openGLContext] setView: glView];
when you are using an NSOpenGLView from InterfaceBuilder.

Are you calling glFlush() ?
Quote this message in a reply
Member
Posts: 104
Joined: 2002.04
Post: #3
Also, make sure you call -lockFocus on the view before drawing and - unlockFocus after.
Quote this message in a reply
RebelPhoton
Unregistered
 
Post: #4
Thanks guys, it works now! All I needed was that glFlush().
I'm not sure what the lock/unlock focus does but it doesn't seem necessary for this project.

Thanks again,
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Ask any Apple engineer, and they'll tell you never to call glFlush if you have a double-buffered context... you do have a double-buffered context, don't you?

Where are you using it?
Quote this message in a reply
ylaporte
Unregistered
 
Post: #6
Quote:Originally posted by OneSadCookie
Ask any Apple engineer, and they'll tell you [b]never to call glFlush if you have a double-buffered context... you do have a double-buffered context, don't you?

Where are you using it? [/b]

Really? Oops... I tought it was glFinish we shouldn't use... I found that most of my apps don't draw as they should when I don't call glFlush at the end of a frame. I have to start looking around again for a solution.

I must say I am puzzled at why we shouldn't flush command buffers. especially at the end of one frame when we want to be sure that every OpenGL command will get on that frame.

As for locking and unlocking the view, it is already done for you when you put your graphic code in drawRect:.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
-[NSOpenGLContext flushBuffer] calls glFlush for you. If you call it just before you swap, you're doing extra work.

I don't think you should ever be calling glFinish, either Wink
Quote this message in a reply
ylaporte
Unregistered
 
Post: #8
Quote:Originally posted by OneSadCookie
-[NSOpenGLContext flushBuffer] calls glFlush for you. If you call it just before you swap, you're doing extra work.
I don't think you should ever be calling glFinish, either ;)


Sadly this doesn't fit with what I experience here. The method:

- (void)drawFrame:(id)timer{
[[view openGLContext] makeCurrentContext];
jeu->processFrame();
[[view openGLContext] flushBuffer];
}

just displays a black rectangle for the view where

- (void)drawFrame:(id)timer{
[[view openGLContext] makeCurrentContext];
jeu->processFrame();
glFlush();
}

Works just fine...
I do remember not using glFinish being part of some Apple speach I heard once. But obviously I am missing something here...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #9
Possibly missing a double-buffered context. Are you specifying NSOpenGLPFADoubleBuffer in your pixel format? If you aren't, you should be Wink
Quote this message in a reply
ylaporte
Unregistered
 
Post: #10
Quote:Originally posted by OneSadCookie
Possibly missing a double-buffered context. Are you specifying NSOpenGLPFADoubleBuffer in your pixel format? If you aren't, you should be Wink
mmhhh.. that doesn't ring a bell so am most certainly not. I am just using the standard initialization from IB... and use the context associated with that view.
I'll follow that track and I'll write back what I find.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
You might like to download the GLCubes source from this site. It 's a very good, clear example of how to set up OpenGL in Cocoa.
Quote this message in a reply
Feanor
Unregistered
 
Post: #12
Hrrrm, the GLCubes sample is fine but it has problems. Use Bryan Blackburn's ports of the NeHe tutorials, please! He has a great style, and all the programs follow a similar structure, making it easy to see where he has added new features. GLCubes has too much in it for a new user to see what's really going on, I think.

Plus Bryan solves the annoying hack people use of initializing the context on the first run through -drawRect: -- he creates his view and context programmatically, in a very clean way, and everything sets up nicely. But he creates the window with IB, so you get a sense how things work together. And he separates the view from the controller. He could go farther with this, but I still like it.

If you want to see how to make a whole suite of objects work together in a Cocoa OpenGL app, try my GLObjects example.
FÎanor's OpenGL Demo Downloads
It's a bit of overkill, but it's fun. I have to go back and fix a bunch of stuff with it someday.
Quote this message in a reply
RebelPhoton
Unregistered
 
Post: #13
Well thanks to everyone for your help...
As it stands now it works with glFlush(); and doesn't work without it. So I think I'll stick with that for now - I have bigger fish to fry. Once I've got more of the "real" program done I'll probably come back and clean up the GL part; I've wasted so much time making great frameworks and no content already...
Quote this message in a reply
ZekeMan
Unregistered
 
Post: #14
Quote:Originally posted by Feanor
[b]Hrrrm, the GLCubes sample is fine but it has problems. Use Bryan Blackburn's ports of the NeHe tutorials, please! He has a great style, and all the programs follow a similar structure, making it easy to see where he has added new features. GLCubes has too much in it for a new user to see what's really going on, I think.


Feanor,

Which tutorial are you referring to on NeHe? Can you give us a link please? Grin

Thanks!
Quote this message in a reply
ZekeMan
Unregistered
 
Post: #15
Quote:Originally posted by Feanor
[b]Hrrrm, the GLCubes sample is fine but it has problems. Use Bryan Blackburn's ports of the NeHe tutorials, please! He has a great style, and all the programs follow a similar structure, making it easy to see where he has added new features. GLCubes has too much in it for a new user to see what's really going on, I think.


Feanor,

Which tutorial are you referring to on NeHe? Can you give us a link please? Grin

Thanks!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Nothing showing up in NSOpenGLView [newb] binaryinsomnia 6 5,997 Nov 29, 2011 07:04 PM
Last Post: binaryinsomnia
  Cocoa controls on top of NSOpenGLView wadesworld 5 5,625 Apr 6, 2009 01:38 AM
Last Post: arekkusu
  Antialiasing and NSOpenGLView attributes Jar445 2 6,265 Jan 20, 2009 10:42 AM
Last Post: maximile
  Double-Buffering with NSOpenGLView DesertPenguin 3 5,417 Aug 1, 2006 07:17 AM
Last Post: DesertPenguin
  [NSWindow orderOut] from NSOpenGLView? Fenris 11 6,025 Jul 5, 2005 05:37 PM
Last Post: AnotherJake