开发者

How to get OpenGL running on OSX

开发者 https://www.devze.com 2023-01-06 09:28 出处:网络
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

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开发者_如何学Python me in the right direction?


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:

  • 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.


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++.


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.


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.


The non-Objective C library is AGL.

0

精彩评论

暂无评论...
验证码 换一张
取 消