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

Mac OSX specific forum
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

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

Post 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.
Niffo
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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()
Niffo
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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
Niffo
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post by Niffo »

Maybe you can start with the excellent work of "remi_meier" :
http://www.purebasic.fr/english/viewtop ... 261#223261
Niffo
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Post 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.
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Post 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.
Niffo
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re:

Post 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
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

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

Post by luis »

Yup, I saw that, for basic usage seems to be a good shortcut!

Thanks.
"Have you tried turning it off and on again ?"
A little PureBasic review
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

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

Post by Niffo »

Isn't this "markiecork" a spambot ?

http://forum.purebasic.com/english/view ... 31#p322031
Last edited by Niffo on Tue Apr 20, 2010 2:03 pm, edited 1 time in total.
Niffo
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

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

Post by Foz »

It certainly appears that way, copy and pasting certain strings it can find.
Post Reply