Cocoa OpenGL

Sage
Posts: 1,066
Joined: 2004.07
Post: #16
I'm having some trouble. The program's getting the values from the sliders, but when I move them the box doesn't change colors until I click on MyOpenGLView. Here's the code from the drawing (some OpenGL).

Code:
- (void)drawRect: (NSRect)aRect
{
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glBegin(GL_QUADS);
    {
        glColor3ub([redAmount intValue],
                   [greenAmount intValue],
                   [blueAmount intValue]);
        glVertex2f(50,50);
        glVertex2f(250,50);
        glVertex2f(250,250);
        glVertex2f(50,250);
        glEnd();
    }
    
    glFinish();
    
    glFlush();
}
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #17
one thing you might want to add:
[[self openGLContext] makeCurrentContext];
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #18
I put it in but it didn't solve my problem. What does that do then?
Quote this message in a reply
Moderator
Posts: 335
Joined: 2002.04
Post: #19
In the method where you're getting the slider input, remember to do

[self setNeedsDisplay:YES];

to tell your view to update.

Also, doing

[[self openGLContext] flushBuffer];

rather than glFinish() and glFlush() should be faster

(use [myFullScreenContext flushBuffer]; if you're in fullscreen mode)
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #20
skyhawk Wrote:any advantage to this over awakeFromNib?
-prepareOpenGL gets called when a context is attached to a view. So this is good for setting up a view's context if you are creating the views in your code rather than in IB. -awakeFromNib is a one time thing for the IB instanciated object; not so hot if you're creating multiple views in code.

I do all my gl client state setup in prepareOpenGL. it makes things a little more organized and easy to edit.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
webwarrior
Unregistered
 
Post: #21
Hmm I've chosen a different approach. Creating the NSApp and adding a delegate. The delegate will then be notified when the app has finished starting and before it terminates. You can the do your init and your cleanup stuff at the correct time.
I don't know, but I had lots of troubles the whole last week when I was trying to port my stuff from SDL to Cocoa. For some stupid reasons awakeFromNib got called two times and I couldn't figure out why.
Quote:I do all my gl client state setup in prepareOpenGL. it makes things a little more organized and easy to edit.
That's fine, but your program will not even run on all OS X then.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #22
kelvin Wrote:I do all my gl client state setup in prepareOpenGL. it makes things a little more organized and easy to edit.

Or you can use initWithCoder if you want your code to be compatible with all versions of OS X

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #23
webwarrior Wrote:That's fine, but your program will not even run on all OS X then.
I guess you haven't had the lecture from Travis B about why its a good idea to target the latest OS for new projects. In any case, you only really have to worry about supporting prior systems if you already have an prior installed userbase.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Opengl/Cocoa text rendering tesil 15 16,408 Mar 20, 2012 11:16 AM
Last Post: OneSadCookie
  OpenGL Text Rendering (in Cocoa) daveh84 5 7,474 Feb 19, 2009 12:44 PM
Last Post: TomorrowPlusX
  OpenGL & Cocoa - Improving frame rate daveh84 4 5,321 Feb 2, 2009 06:53 AM
Last Post: backslash
  bad depth sorting in Cocoa OpenGL aldermoore 2 4,449 Dec 30, 2008 03:07 PM
Last Post: ThemsAllTook
  Loading and using textures with alpha in OpenGL with Cocoa corporatenewt 4 5,688 Dec 8, 2007 02:06 PM
Last Post: Malarkey