Page 1 of 1

OpenGL Extensions with OpenGLGadget

Posted: Wed Mar 02, 2016 5:59 pm
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?

Re: OpenGL Extensions with OpenGLGadget

Posted: Wed Mar 02, 2016 6:26 pm
by wilbert
You might try dlsym_ with #RTLD_DEFAULT

Re: OpenGL Extensions with OpenGLGadget

Posted: Wed Mar 02, 2016 8:48 pm
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)

Re: OpenGL Extensions with OpenGLGadget

Posted: Wed Mar 02, 2016 8:55 pm
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.

Re: OpenGL Extensions with OpenGLGadget

Posted: Thu Mar 03, 2016 11:03 am
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