Skip to content

Instantly share code, notes, and snippets.

@frostney
Created June 24, 2011 02:38
Show Gist options
  • Save frostney/1044116 to your computer and use it in GitHub Desktop.
Save frostney/1044116 to your computer and use it in GitHub Desktop.
Changes to get dglOpenGL.pas (www.delphigl.com) OpenGL 4.1 header working on Mac OS X
(*
Open dglOpenGL.pas in your favorite text editor and change the following lines
*)
// Don't worry about having absolute filepaths. Mac OS X always installs the OpenGL framework with every
// clean installation of Mac OS X and the dylibs are ALWAYS in that path. (Tested on several Mac OS X
// machines including PowerMac G5 with Leopard, MacBook with Snow Leopard, MacBook Pro with Snow Leopard and Lion)
// While the standard search directory for libraries is /usr/lib on Mac OS X as well, you should not use this
// for developing your application as other Mac users don't have the OpenGL libraries in that folder and your
// application won't launch for them. (/usr/lib on Mac OS X is only used in some special cases, everything
// else relies on frameworks.)
// Lines 12900 - 12912
const
{$IFDEF DGL_WIN}
OPENGL_LIBNAME = 'OpenGL32.dll';
GLU_LIBNAME = 'GLU32.dll';
{$ELSE}
{$IFDEF darwin}
OPENGL_LIBNAME = '/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib';
GLU_LIBNAME = '/System/Library/Frameworks/OpenGL.framework/Libraries/libGLU.dylib';
{$ELSE}
OPENGL_LIBNAME = 'libGL.so.1';
GLU_LIBNAME = 'libGLU.so.1';
{$ENDIF}
{$ENDIF}
// If you don't add the additional Darwin condition, you'll get an access violation when launching your
// application (needed for 32-bit binaries)
// Lines 17855 - 17859
{$IFDEF CPU386}
{$IFNDEF DARWIN}
Set8087CW($133F);
{$ENDIF}
{$ENDIF}
Copy link

ghost commented May 23, 2016

Do you deploy using FPC or Delphi?
Do you have a sample application for window/context creation on OSX?

@frostney
Copy link
Author

@CraigChapman53280 I used to do a lot of FreePascal, but I haven't done a lot in the recent years. For window management I would recommend using SDL2, SDL or GLFW with a strong preference on SDL2. (https://github.com/ev1313/Pascal-SDL-2-Headers)

This is an older example I made 2 1/2 years ago based on the rotating cube example to make sure SDL2 and the Pascal headers are going to work with OS X.

program sdltest;

{$mode objfpc}{$H+}

{$ifdef darwin}
  {$linkframework SDL2}
{$endif}

uses
  sdl, gl, glu;

const
  WIDTH = 800;
  HEIGHT = 600;

var
  window: PSDL_Window;
  renderer: PSDL_Renderer;
  rendererInfo: TSDL_RendererInfo;
  glContext: TSDL_GLContext;
  run: Boolean;
  event: TSDL_Event;
  cube_rotationx: GLFloat;
    cube_rotationy: GLFloat;
    cube_rotationz: GLFloat;

procedure SetViewport(w, h: Integer);
begin
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, w / h, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
end;

procedure DrawStuff();
var
  Speed: Double;
begin
    glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glEnable(GL_DEPTH_TEST);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0.0, 0.0,-6.0);
  glRotatef(cube_rotationx, cube_rotationy, cube_rotationz, 0.0);

  glBegin(GL_QUADS);
          glColor3f(0.0,1.0,0.0);                              // Set The Color To Green
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Top)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Top)
          glVertex3f(-1.0, 1.0, 1.0);                  // Bottom Left Of The Quad (Top)
          glVertex3f( 1.0, 1.0, 1.0);                  // Bottom Right Of The Quad (Top)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.5,0.0);                              // Set The Color To Orange
          glVertex3f( 1.0,-1.0, 1.0);                  // Top Right Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0, 1.0);                  // Top Left Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Bottom)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Bottom)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,0.0);                              // Set The Color To Red
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Right Of The Quad (Front)
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Left Of The Quad (Front)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Front)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Front)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,1.0,0.0);                              // Set The Color To Yellow
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Back)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Back)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Right Of The Quad (Back)
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Left Of The Quad (Back)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(0.0,0.0,1.0);                              // Set The Color To Blue
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Right Of The Quad (Left)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Left)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,1.0);                              // Set The Color To Violet
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Right)
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Right)
  glEnd();

  Speed := 0.02;

  cube_rotationx += 5.15 * Speed;
  cube_rotationy += 5.15 * Speed;
  cube_rotationz += 20.0 * Speed;
end;

procedure HandleEvents();
begin
  while (SDL_PollEvent(@event) = 1) do
  begin
    case event.type_ of
      SDL_QUITEV:
        run := false;
    end;
  end;
end;

begin
  SDL_Init(SDL_INIT_VIDEO);

  //InitOpenGL;
  //ReadExtensions;
  run := true;

  //SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN, @window, @renderer);
  //SDL_StartEventLoop();
  window := SDL_CreateWindow('SDL2 OpenGL Example', SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_OPENGL or SDL_WINDOW_SHOWN);

  glContext := SDL_GL_CreateContext(window);

  (*SDL_GetRendererInfo(renderer, @rendererInfo);

  if (((rendererInfo.flags and SDL_RENDERER_ACCELERATED) = 0) or ((rendererInfo.flags and SDL_RENDERER_TARGETTEXTURE) = 0)) then
  begin
    WriteLn('Problem!');
  end;
  WriteLn(rendererInfo.flags);     *)


  (*window := SDL_CreateWindow('An SDL2 window', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);

  if (window = nil) then
  begin
    Writeln('Error');
    SDL_Quit();
  end;*)


    (*glShadeModel( GL_SMOOTH );

    glClearColor( 0.0, 0.0, 0.0, 0.0 );

    glClearDepth( 1.0 );

    glEnable( GL_DEPTH_TEST );

    glDepthFunc( GL_LEQUAL );

    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );    *)

  while run do
  begin
    HandleEvents();

    //SetViewport(WIDTH, HEIGHT);

    DrawStuff();

    SDL_GL_SwapWindow(window);
  end;

  SDL_GL_DeleteContext(glContext);
  SDL_DestroyWindow(window);

  SDL_Quit();
end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment