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?
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
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.
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:
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.
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
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:
NSOpenGLView
object in the object browser, and smack it on your window.MyRenderer
or something.MyRendere
r.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With