Page 1 of 1

How to initialise and use OpenGL (not OGRE) in MacOSX

Posted: Mon Feb 02, 2009 11:48 am
by Niffo
Hello,

I have a working Windows and Linux application in PB that uses direct OpenGL API to show 3D objects.

Does someone know how to initialise OpenGL (not OGRE) and use its API from PB in MacOSX (x86) ?

Regards.

Posted: Wed Feb 25, 2009 7:26 pm
by Niffo
Ok, so here is the minimum to initialize and use OpenGL with Glut on MacOS

Code: Select all

EnableExplicit

ImportC "/System/Library/Frameworks/GLUT.framework/GLUT"
   glutInit(*argcp.i, *argv.b)
   glutInitDisplayMode(mode.i)
   glutInitWindowPosition(x.i, y.i)
   glutInitWindowSize(width.i, height.i)
   glutMainLoop()
   glutCreateWindow(title.s)
   glutFullScreen()
   glutDisplayFunc(*Proc)
   glutIdleFunc(*Proc)
   glutSwapBuffers() 
EndImport

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
   glClear(mask.l)
   glClearColor(red.f, green.f, blue.f, alpha.f)
EndImport

;IncludeFile "gl.pbi"
#GL_COLOR_BUFFER_BIT               = $00004000
#GL_DEPTH_BUFFER_BIT               = $00000100

;IncludeFile "glut.pbi"
#GLUT_RGB                           = $0000
#GLUT_DEPTH                         = $0010
#GLUT_DOUBLE                        = $0002

ProcedureC draw()
   glClearColor(1, 0, 0, 0)
   glClear(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
   glutSwapBuffers()
EndProcedure

Define a = 0

glutInit(@a, 0)
glutInitWindowSize(256, 256);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(#GLUT_RGB | #GLUT_DEPTH | #GLUT_DOUBLE);
glutCreateWindow("Title")
glutDisplayFunc(@draw())
;glutIdleFunc(@draw())
;glutKeyboardFunc(@key())
;glutReshapeFunc(@reshape())

glutMainLoop()

Posted: Mon Mar 09, 2009 10:30 am
by Niffo
And here is the minimum to initialize OpenGL on a PB Window without the use of GLUT :

Code: Select all

EnableExplicit

ImportC "/System/Library/Frameworks/OpenGL.framework/OpenGL"
   glClear(mask.l)
   glClearColor(red.f, green.f, blue.f, alpha.f)
EndImport

;IncludeFile "gl.pbi"
#GL_COLOR_BUFFER_BIT               = $00004000
#GL_DEPTH_BUFFER_BIT               = $00000100

ImportC "/System/Library/Frameworks/AGL.framework/AGL"
   aglChoosePixelFormat.l(*gdevs, ndev.i, *attribs.i)
   aglDestroyContext.b(ctx.l);
   aglDestroyPixelFormat(pix.l) 
   aglCreateContext.l(pix.l, share.l)
   aglSetDrawable.b(ctx.l, draw.l)
   aglSetCurrentContext.b(ctx.l)
   aglSwapBuffers(ctx.l)   
EndImport

;IncludeFile "agl.pbi"
#AGL_NONE = 0
#AGL_RGBA = 4
#AGL_DOUBLEBUFFER = 5
#AGL_DEPTH_SIZE = 12

ImportC "/System/Library/Frameworks/Carbon.framework/Carbon"
   GetWindowPort.l(window.l) 
EndImport

OpenWindow (0, 100, 100, 256, 256, "OpenGL Window")

Dim attributes.i(4)
attributes(0) = #AGL_RGBA
attributes(1) = #AGL_DOUBLEBUFFER
attributes(2) = #AGL_DEPTH_SIZE
attributes(3) = 24
attributes(4) = #AGL_NONE
Define.l myAGLContext = #Null
Define.l myAGLPixelFormat
myAGLPixelFormat = aglChoosePixelFormat(#Null, 0, @attributes())
If myAGLPixelFormat
   myAGLContext = aglCreateContext(myAGLPixelFormat, #Null)
   aglSetDrawable(myAGLContext, GetWindowPort(WindowID(0)))
   aglSetCurrentContext(myAGLContext)
   aglDestroyPixelFormat(myAGLPixelFormat)
EndIf

Repeat
   Select WaitWindowEvent(10)
      Case #PB_Event_CloseWindow
         Break
   
      Default
         glClearColor(1, 0, 0, 0)
         glClear(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT) 
         aglSwapBuffers(myAGLContext)
   EndSelect
ForEver

Posted: Mon Mar 09, 2009 1:37 pm
by luis
Thank you for answering to yourself!

Could be useful in the future.

Ehm, please forgive me, but could you share the code you use under linux to open a opengl window with PB ? If possibile both windowed and full screen ? I probably need to look into that in the future and a working example could point me in the right direction from the start.

Thanks.

Posted: Mon Mar 09, 2009 3:21 pm
by Niffo
Maybe you can start with the excellent work of "remi_meier" :
http://www.purebasic.fr/english/viewtop ... 261#223261

Posted: Mon Mar 09, 2009 3:33 pm
by luis
Thank you but I was looking for something not using GLUT or other cross platform libraries.

I thought, looking to your osx example, you had something similar but for linux.

Posted: Mon Mar 09, 2009 4:08 pm
by Niffo
No, unfortunately for the moment I am using GLUT on Linux, but it is less troublesome because it is openglut which is much more comprehensive and usable than the old MacOS GLUT implementation.

Re:

Posted: Sat Mar 27, 2010 1:00 am
by Foz
luis wrote:Ehm, please forgive me, but could you share the code you use under linux to open a opengl window with PB ? If possibile both windowed and full screen ? I probably need to look into that in the future and a working example could point me in the right direction from the start.
Thanks to Fred's little throw away comment in another thread, tis easy - simply set the subsystem to opengl first:

Code: Select all

InitSprite()

ImportC "/usr/lib/libX11.a"+Chr('"')+" -l"+Chr('"')+"GL"
	glClear(color.l)
	glClearColor(r.f,g.f,b.f,a.f)
EndImport

#GL_COLOR_BUFFER_BIT = $00004000
#GL_DEPTH_BUFFER_BIT = $00000100

OpenWindow (0, 100, 100, 256, 256, "OpenGL Window")
OpenWindowedScreen(WindowID(0), 0, 0, 256, 256, 0, 0, 0)

Repeat
   Select WaitWindowEvent(10)
      Case #PB_Event_CloseWindow
         Break
   
      Default
         glClearColor(1, 0.5, 0, 0)
         glClear(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
         
         FlipBuffers()
   EndSelect
ForEver

Re: How to initialise and use OpenGL (not OGRE) in MacOSX

Posted: Sat Mar 27, 2010 1:11 am
by luis
Yup, I saw that, for basic usage seems to be a good shortcut!

Thanks.

Re: How to initialise and use OpenGL (not OGRE) in MacOSX

Posted: Tue Apr 20, 2010 1:41 pm
by Niffo
Isn't this "markiecork" a spambot ?

http://forum.purebasic.com/english/view ... 31#p322031

Re: How to initialise and use OpenGL (not OGRE) in MacOSX

Posted: Tue Apr 20, 2010 1:45 pm
by Foz
It certainly appears that way, copy and pasting certain strings it can find.