Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Dec 15, 2018 1:53 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Cocoa and OpenGLSurface.Configure
PostPosted: Wed Dec 05, 2012 2:40 pm 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
I need to micromanage the details of the NSOpenGLPixelFormat used to create the NSOpenGLContext that I assume to be behind the Cocoa version of OpenGLSurface. I suspect this is what the "Configure" event is for. The documentation is a little thin, to say the least. Does anyone have a clue or an example of how to construct an appropriate memory block to return? Can I just create an appropriate NSOpenGLPixelFormat somehow and pass back a pointer to it or something?


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Wed Dec 05, 2012 2:55 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
dbronson wrote:
I need to micromanage the details of the NSOpenGLPixelFormat used to create the NSOpenGLContext that I assume to be behind the Cocoa version of OpenGLSurface. I suspect this is what the "Configure" event is for. The documentation is a little thin, to say the least. Does anyone have a clue or an example of how to construct an appropriate memory block to return? Can I just create an appropriate NSOpenGLPixelFormat somehow and pass back a pointer to it or something?


Great question and definitely something that could use more documentation. The MemoryBlock you pass back is treated as the value passed to -[NSOpenGLPixelFormat initWithAttributes:]. If you don't implement this event (or pass back nil), you get some default format attributes we create from the OpenGLSurface's properties.

If we fail to create a valid NSOpenGLPixelFormat (or a valid NSOpenGLContext from that), we fire the error event. If you return true from that, we go back to the beginning of this whole process.

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Wed Dec 05, 2012 3:45 pm 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
So, I can probably bang together an NSOpenGLPixelFormat* using declares, but then how do I return it as a memory block? Do I have to coerce it somehow, or can I just pass back the pointer?


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Wed Dec 05, 2012 3:52 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
dbronson wrote:
So, I can probably bang together an NSOpenGLPixelFormat* using declares, but then how do I return it as a memory block? Do I have to coerce it somehow, or can I just pass back the pointer?


Ah no, you don't need to create an NSOpenGLPixelFormat object. You need to create a MemoryBlock that contains an array (in the C sense) of NSOpenGLPixelFormatAttributes (Int32s). For example, to do something simple:
dim attrs as new MemoryBlock( 28 )
attrs.Int32Value( 0 ) = 8 // NSOpenGLPFAColorSize
attrs.Int32Value( 4 ) = 24
attrs.Int32Value( 8 ) = 12 // NSOpenGLPFADepthSize
attrs.Int32Value( 12 ) = 24
attrs.Int32Value( 16 ) = 13 // NSOpenGLPFAStencilSize
attrs.Int32Value( 20 ) = 8
attrs.Int32Value( 24 ) = 0 // terminates the array
return attrs

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Wed Dec 05, 2012 4:05 pm 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
Thanks! I'll give it a shot as soon as I have a spare moment. If I get it to work, I'll post it here, and voila - documentation/example for you.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Thu Dec 06, 2012 10:54 am 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
Hmm. I started a new project, set it to Cocoa, put an OpenGLSurface in the main window, and pasted your exact code above into the Configure event, and put "Break" into the Error event. The Error event triggers every time I launch. Did I miss something, or was that not meant to be a fully functional example?


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Thu Dec 06, 2012 3:15 pm 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
Interesting. While poking around in Feedback, I found your example for using a Canvas rather than an OpenGLSurface. I adapted the Open event of that example as follows;

soft declare function objc_getClass lib "/usr/lib/libobjc.dylib" ( name as CString ) as ptr
soft declare function NSObject_alloc lib "Foundation" selector "alloc" ( classPtr as ptr ) as ptr
soft declare sub NSObject_release lib "Foundation" selector "release" ( obj as ptr )
soft declare function NSOpenGLView_defaultPixelFormat lib "AppKit" selector "defaultPixelFormat" ( classPtr as ptr ) as ptr
soft declare function NSOpenGLPixelFormat_init lib "AppKit" selector "initWithAttributes:" ( obj as ptr, attrs as ptr ) as ptr
soft declare function NSOpenGLContext_initWithFormat_shareContext lib "AppKit" selector "initWithFormat:shareContext:" ( obj as ptr, pixelFormat as ptr, share as ptr ) as ptr
soft declare sub NSOpenGLContext_setView lib "AppKit" selector "setView:" ( obj as ptr, view as ptr )

dim NSOpenGLContext as ptr = objc_getClass( "NSOpenGLContext" )
dim NSOpenGLView as ptr = objc_getClass( "NSOpenGLView" )

dim attrs as new MemoryBlock( 28 )
attrs.Int32Value( 0 ) = 8 // NSOpenGLPFAColorSize
attrs.Int32Value( 4 ) = 24
attrs.Int32Value( 8 ) = 12 // NSOpenGLPFADepthSize
attrs.Int32Value( 12 ) = 24
attrs.Int32Value( 16 ) = 13 // NSOpenGLPFAStencilSize
attrs.Int32Value( 20 ) = 8
attrs.Int32Value( 24 ) = 0 // terminates the array

dim NSOpenGLPixelFormat as ptr = objc_getClass( "NSOpenGLPixelFormat" )
dim myPixelFormat as ptr = NSOpenGLPixelFormat_init( NSObject_alloc( NSOpenGLPixelFormat ), attrs )

mOpenGLContext = NSOpenGLContext_initWithFormat_shareContext( NSObject_alloc( NSOpenGLContext ), myPixelFormat, nil )
NSOpenGLContext_setView( mOpenGLContext, ptr( self.handle ) )

NSObject_release( myPixelFormat )

raiseEvent open()
raiseEvent paintGL()


Note that the MemoryBlock "attrs" is created exactly as you specify above. This code works perfectly. Passing the same "attrs" back from the OpenGLSurface.Configure event fires the Error event. Not sure what to make of this.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Fri Dec 07, 2012 10:18 am 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
Never mind. It works just fine in 2012r1.2. Problem solved.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Fri Dec 07, 2012 11:07 am 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
Also, I created a quick convenience module/class to assist creating these MemoryBlocks;

https://dl.dropbox.com/u/28082075/NSOpenGLPixelFormatAttributes.rbp.zip

Anyone can feel free to use it for any purpose. See notes inside the project file.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Fri Dec 07, 2012 12:32 pm 
Offline
Real Software Engineer

Joined: Fri Jan 28, 2011 5:24 pm
Posts: 145
dbronson wrote:
Never mind. It works just fine in 2012r1.2. Problem solved.


Glad to hear everything's working correctly. 2012r1 was the release where the Cocoa framework began using NSOpenGLContext instead of AGL (which amusingly enough, did happen to work through sheer luck and reliance on implementation details).

_________________
Joe Ranieri
Mac Frameworks & Compiler
REAL Software, Inc.


Top
 Profile  
Reply with quote  
 Post subject: Re: Cocoa and OpenGLSurface.Configure
PostPosted: Fri Dec 07, 2012 12:42 pm 
Offline

Joined: Mon Sep 21, 2009 9:33 am
Posts: 29
jranieri wrote:
Glad to hear everything's working correctly. 2012r1 was the release where the Cocoa framework began using NSOpenGLContext instead of AGL (which amusingly enough, did happen to work through sheer luck and reliance on implementation details).


No problem. The thought of 2012 hadn't occurred to me. I'm still on the final version of 2011 mostly because I haven't had time to fully test against the newer ones, and to be honest, there are quirks. I'll have to dive in now, however. Thanks for all your help.

And honestly, you should simply copy and paste your post above with the code snippet into the documentation. That would be considerably better than what's there now.

:D


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group