A Fortran interface to OpenGL is provided by the F03GL library (mirror). Even if OpenGL has been replaced by Vulkan, F03GL can still be used to create 3D graphics in the old and conservative way (without vertex shaders and other bollocks).

Fortran GLUT Sphere on FreeBSD
Fig. 1: The example OpenGL programme on FreeBSD

An example OpenGL application that draws a red light sphere (fig. 1):

! sphere.f90
! Fortran GLUT programme to draw a red light sphere.
! Example is taken from:
subroutine display() bind(c)
    use opengl_gl
    use opengl_glut
    implicit none

    call glcalllist(1)
    call glutswapbuffers
end subroutine display

subroutine gfxinit
    use opengl_gl
    use opengl_glu
    use opengl_glut

    implicit none

    real(glfloat) :: diffuse(4) = (/1.0, 0.0, 0.0, 1.0/)
    real(glfloat) :: pos(4)     = (/1.0, 1.0, 1.0, 0.0/)

    call glnewlist(1, GL_COMPILE)
    call glutsolidsphere(1.0_gldouble, 20, 20)
    call glendlist

    call gllightfv(GL_LIGHT0, GL_DIFFUSE, diffuse)
    call gllightfv(GL_LIGHT0, GL_POSITION, pos)
    call glenable(GL_LIGHTING)
    call glenable(GL_LIGHT0)
    call glenable(GL_DEPTH_TEST)
    call glmatrixmode(GL_PROJECTION)

    call gluperspective(40.0_gldouble,  1.0_gldouble, &
                         1.0_gldouble, 10.0_gldouble)

    call glmatrixmode(GL_MODELVIEW)

    call glulookat(0.0_gldouble, 0.0_gldouble, 5.0_gldouble, &
                   0.0_gldouble, 0.0_gldouble, 0.0_gldouble, &
                   0.0_gldouble, 1.0_gldouble, 1.0_gldouble)

    call gltranslatef(0.0, 0.0, -1.0)
end subroutine gfxinit

program main
    use opengl_glut
    implicit none

        subroutine display() bind(c)
        end subroutine display
    end interface

    integer :: i

    call glutinit()
    call glutinitdisplaymode(GLUT_DOUBLE + GLUT_RGB + GLUT_DEPTH)

    i = glutcreatewindow('Fortran GLUT sphere')

    call gfxinit
    call glutdisplayfunc(display)
    call glutmainloop
end program main

The compilation process can be simplified by writing a Makefile. The following verbose example uses Clang and Flang:

CC = clang
FC = flang

CFLAGS     = -c -Wall -I/usr/local/include
LDFLAGS    = -L/usr/local/lib -lX11 -lXext -lGL -lGLU -lGLU -lglut
OBJECTS    = GLUT_fonts.o OpenGL_gl.o OpenGL_glu.o OpenGL_glut.o
SOURCES    = sphere.f90



GLUT_fonts.o: GLUT_fonts.c
	$(CC) $(CFLAGS) GLUT_fonts.c

OpenGL_gl.o: OpenGL_gl.f90
	$(FC) $(CFLAGS) OpenGL_gl.f90

OpenGL_glu.o: OpenGL_glu.f90
	$(FC) $(CFLAGS) OpenGL_glu.f90

OpenGL_glut.o: OpenGL_glut.f90
	$(FC) $(CFLAGS) OpenGL_glut.f90

	rm *.o *.mod $(EXECUTABLE)

On FreeBSD, the Makefile has to be excuted with make:

$ make

Run the demo with:

$ ./sphere