OpenGL Extensions with OpenGLGadget

Mac OSX specific forum
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

OpenGL Extensions with OpenGLGadget

Post by benmalartre »

I recently get a mac book pro with a openGL 3.3 capable nvidia graphics card
When using the glfw3 library, I successfully load OpenGL extensions via the glfwGetProcAddress()

However I would like to use the OpenGLGadget...
Anyone knows the simplest way to load specific OpenGL extension on MacOSX from PureBasic?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: OpenGL Extensions with OpenGLGadget

Post by wilbert »

You might try dlsym_ with #RTLD_DEFAULT
Windows (x64)
Raspberry Pi OS (Arm64)
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Re: OpenGL Extensions with OpenGLGadget

Post by benmalartre »

Thanks! It works

Code: Select all

CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        ImportC "-lGL"
          glXGetProcAddress(s.p-ascii) As "glXGetProcAddress"
        EndImport 
        
        Macro setGLEXT(var, extname)
          var = glXGetProcAddress(extname)
        EndMacro
        
      CompilerCase #PB_OS_Windows
        ImportC "opengl32.lib"
          wglGetProcAddress(s.p-ascii) As "wglGetProcAddress"
        EndImport 
        
        Macro setGLEXT(var, extname)
          var = wglGetProcAddress(extname)
        EndMacro
        
      CompilerCase #PB_OS_MacOS
        Macro setGLEXT(var, extname)
          var = dlsym_(#RTLD_DEFAULT,extname)
        EndMacro
    CompilerEndSelect
I also found that on MacOS extensions can be loaded directly

Code: Select all

Global macosx_gl_lib = OpenLibrary(#PB_Any,"/System/Library/Frameworks/OpenGL.framework/OpenGL")
...
 Macro setGLEXT(var, extname)
    var = GetFunction(macosx_gl_lib,extname)
 EndMacro
...
 CloseLibrary
I also found that you're restricted to OpenGL 2.1 Context and GLSL1.2
So I had to modify my simple shader
(vertex shader)
from

Code: Select all

#version330
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

in vec3 position;
void main(){
    vec3 p = vec3(view * model * vec4(position,1.0));
    gl_Position = projection * vec4(p,1.0);
}
to

Code: Select all

#version 120
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

varying vec3 position;
void main(){
    vec3 p = vec3(view * model * vec4(position,1.0));
    gl_Position = projection * vec4(p,1.0);
}
(fragment shader)
from

Code: Select all

#version 330
void main()
{
	gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
to

Code: Select all

#version 120
void main()
{
	gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}
But both lead to unpredictables results(flickering geometries)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: OpenGL Extensions with OpenGLGadget

Post by wilbert »

You should be able to use the OpenGL 3.2 Core Profile on OSX with glutInitDisplayMode but I don't know if it works with PureBasic.
Windows (x64)
Raspberry Pi OS (Arm64)
benmalartre
User
User
Posts: 27
Joined: Thu Mar 14, 2013 11:24 am
Location: paris
Contact:

Re: OpenGL Extensions with OpenGLGadget

Post by benmalartre »

the error was in the vertex shader(#version 120)
I wrote

Code: Select all

varying vec3 position
It is

Code: Select all

attribute vec3 position
I successfully use VAOs, VBOs, GLSL program in an OpenGLGadget with MacOSX ElCapitan

:D

One trick to note for using VAOs

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS And Not #USE_GLFW
  setGLEXT( glBindVertexArray,          "glBindVertexArrayAPPLE")
  setGLEXT( glDeleteVertexArrays,       "glDeleteVertexArraysAPPLE")
  setGLEXT( glGenVertexArrays,          "glGenVertexArraysAPPLE")
CompilerElse
  setGLEXT( glBindVertexArray,          "glBindVertexArray")
  setGLEXT( glDeleteVertexArrays,       "glDeleteVertexArrays")
  setGLEXT( glGenVertexArrays,          "glGenVertexArrays")
CompilerEndIf
Post Reply