Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get OpenGL running on OSX

Tags:

c++

macos

opengl

I normally program on Windows, but I got a macbook pro from my school, so I'm trying to make an OpenGL app for OSX. I downloaded and installed XCode, but I have no clue how to get a simple OpenGL app going. I would prefer not to use Objective-C, but I definitely don't want to use GLUT. Can someone point me in the right direction?

like image 543
Ernesto Rojo Jr Avatar asked Jul 06 '10 17:07

Ernesto Rojo Jr


People also ask

How do I use OpenGL on Mac OS X?

When using OpenGL on Mac OS X, there are two things to keep in mind: One, you have to link the OpenGL framework. Outside of Xcode, you can pass the -framework flag to the linker: $ gcc -framework OpenGL -o my_opengl_program my_opengl_program.c

Why can't I open glxinfo on my Mac?

Now, the problem here is actually that glxinfo uses XQuartz, which does not support OpenGL 3.2+. I would suggest you use the OpenGL Extension Viewer on the Mac App store if you want detailed info about your OpenGL capabilities.

How do I add an OpenGL framework to my project?

One, you have to link the OpenGL framework. Outside of Xcode, you can pass the -framework flag to the linker: If you're using Xcode, you can just add OpenGL.framework to your linked frameworks. Two, you prefix OpenGL/ > in front of your OpenGL headers. For example, to include gl.h, use:

Is Apple still maintaining OpenCL and OpenGL?

However, Apple is still maintaining the standard. OpenGL and OpenCL were officially deprecated in Mojave last year, though that's a little misleading since it implies that Apple had been actively maintaining and updating its support for those standards.


2 Answers

The biggest difference between OpenGL on OS X compared to pretty much everything else is the location of the header files. On OS X:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

If you want to stay away from Objective-C/Cocoa and GLUT you can try SDL which is a cross platform gaming library (windows, 2D graphics, sound, input, etc.).

Edit: forgot about the compiler flags, which mipadi posted, namely:

-framework OpenGL
like image 137
Niki Yoshiuchi Avatar answered Oct 13 '22 03:10

Niki Yoshiuchi


This question is extremely old in internet time, but the most straightforward way... is to just dabble your feet in Objective-C... Sorry.

My general way of approaching this is as follows:

  • You can keep doing all your core programming in C++, no problem.
  • Create a new "Cocoa application" in XCode.
  • In the interface builder (where you edit your .xib file), find an NSOpenGLView object in the object browser, and smack it on your window.
  • This bit is important: In the object inspector change the subclass to your own subclass (that still needs to be made). You can name it anything you like, for example MyRenderer or something.
  • Hit [cmd]+[n] and create a new Objective-C class, MyRenderer.
  • Important! Change the extension from MyRenderer.m to MyRenderer.mm. This way, XCode knows that it must compile for Objective-C++ instead of Objective-C.
  • In MyRenderer.mm, override at least the following methods of NSOpenGLView.
    • - (void) awakeFromNib: Do your class initialization here. Do not initialize OpenGL here, or your program will crash.
    • - (void) drawRect:(NSRect)dirtRect: Do your drawing stuff here.
    • - (void) prepareOpenGL: Initialize OpenGL here.
    • - (void) reshape:(NSRect)bounds: For when the view gets resized.

The good news is that you can freely mix C++ functions and classes inside MyRenderer.mm. You can also make use of C++ #include directives alongside Objective-C #import directives. You can do C++ inside drawRect with no problems.

The bad news is that NSOpenGLView does not redraw the scene every x milliseconds. It will only redraw the scene once it feels it's necessary. You'll have to make use of Cocoa's NSTimer class for that.

Edit: Extra note: In the drawRect method, make sure to call glFlush() as well as [[self openGLContext] flushBuffer]. For some reason, only calling [[self openGLContext] flushBuffer] doesn't draw anything on the view for me.

like image 33
rwols Avatar answered Oct 13 '22 02:10

rwols