Reputation: 163
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?
Upvotes: 13
Views: 31636
Reputation: 297
I have been using this website as my reference for my project for Window, Mac and Linux. http://ysflight.in.coocan.jp/programming/fssimplewindow/e.html
The author managed to write bunch of good examples which helps you get going with your project in all platforms.
The end point is that, you have to use Objective-C for your window creation and you can program the rest of your code by using C++.
Upvotes: 2
Reputation: 3078
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.
Upvotes: 7
Reputation: 17551
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
Upvotes: 12
Reputation: 46813
Since you're programming on a mac, you can use any language you're familiar with. XCode supports compiling C++, so if you're familiar with OpenGL on windows, then it's a straight forward transition, though you will need to use the proper methods for creating an OSX Window (cocoa most likely).
If python is your thang, PyOpenGL is a python binding to OpenGL that is cross platform.
Upvotes: 1
Reputation: 410662
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
(Note that this flag only works on OS X.)
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:
#include <OpenGL/gl.h>
Otherwise, programming with OpenGL on Mac OS X is pretty much the same.
Upvotes: 1