G95 Fortran interface to OpenGL and GLUT

I developed the direct G95 fortran bindings to OpenGL+GLUT libraries. This is much faster and uses much less memory than does the traditional import library method, expecially when linking large libraries or applications. This version implements the interface for OpenGL 1.2+, GLU 1.2+, GLUT 3.7, FreeGlut/OpenGlut and several extensions. It supports several Unix workstations and Windows 98/NT/2000/Me/XP.

The G95 bindings for OpenGL are an alternative to the older Fortran 90 bindings. This increases the similarity between the Fortran and C interfaces to OpenGL.

I got the Dr. William F. Mitchell examples and remade it for G95. The examples fortran source files and Fortran interface modules you can download here:
Unix Fortran sourcesWin32 Fortran sources
Unix OpenGL moduleWindows OpenGL module

Details

  1. Character strings should come to an end in zero byte as in C.
  2. glutInit command you should use in the next way
    call glutinit(1,loc('thePgm'//char(0)))
  3. For functions, which return a character string, you should use pointer. See example:
            character(100), pointer :: str
            ..............................
            str=>glGetString(GL_VERSION)
            l=min(len(str),index(str,char(0)))
            print *, 'OpenGL version is ',str(1:l)
    
  4. In some commands you have to pass pointers of the different types. Use function loc to get address.Example
     call glVertexPointer( 2, GL_FLOAT, 0, loc(P) ) 
    where P is a real array of vertices with coordinates x,y.
  5. In GLU some functions return the pointer on structure. I remade it in integer(C_INTPTR_T). Example
            integer(C_INTPTR_T) :: nurb
            nurb = gluNewNurbsRenderer()
    
    See details in molehill.f90
  6. Some GLUT commands begin with underscoring, for example __glutInitWithExit (old glut 3.7). Use g_glutInitWithExit instead.
  7. CALLBACK subroutines. If the callback subroutine has arguments, you have to pass it by value (as in C). Example:
            subroutine motion(x, y) bind(c)
            integer(GLint), intent(in), value :: x, y
            ....................................
            end subroutine motion
    
  8. Rest. View examples.

What you need

For compiling you need in file opengl.mod. You have to get the OpenGLu.f03 file for Unix32/64 platform or OpenGLw.f03 for Windows platform. You have to create opengl.mod with command line

g95 OpenGLx.f03 -S 

For Windows platform you need in FGlut32.dll. In principle, this is the same as FreeGlut/OpenGlut, but I remade CALLBACKs for stdcall calling convention. If you have MinGW gcc, you can make it very easy:

  1. Download and unpack FreeGlut source.
  2. Download MakeFGlutDll.bat in src directory.
  3. Change in file freeglut_internal.h the strings
    typedef void (* FGC
    
    to
    typedef void (__stdcall * FGC
    
  4. Run MakeFGlutDll.bat.
If it is very complicated you can download FGlut32.dll here.

Compiling

For compiling on Unix32/64 platform you may use the next command lines

g95  -fno-underscoring  %1.f90 -o %1 -Wl,/usr/lib/libGL.so,/usr/lib/libGLU.so,/usr/lib/libglut.so.3
g95  -fno-underscoring  %1.f90 -o %1 -Wl,/usr/lib64/libGL.so,/usr/lib64/libGLU.so,/usr/lib64/libglut.so.3
where %1 is file name.

For compiling on Win32 platform you may use the next command line

g95.exe -mrtd -fno-underscoring %1.f90 -o %1 -Wl,%windir%/system32/opengl32.dll,%windir%/system32/glu32.dll,%windir%/system32/fglut32.dll
where %1 is file name.


Go to my home page