C++ 如何让 OpenGL 在 OSX 上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3188554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get OpenGL running on OSX
提问by Ernesto Rojo Jr
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?
我通常在 Windows 上编程,但我从学校得到了 macbook pro,所以我正在尝试为 OSX 制作一个 OpenGL 应用程序。我下载并安装了 XCode,但我不知道如何运行一个简单的 OpenGL 应用程序。我不想使用Objective-C,但我绝对不想使用GLUT。有人可以指出我正确的方向吗?
采纳答案by Niki Yoshiuchi
The biggest difference between OpenGL on OS X compared to pretty much everything else is the location of the header files. On OS X:
与其他几乎所有东西相比,OS X 上的 OpenGL 之间的最大区别在于头文件的位置。在 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 SDLwhich is a cross platform gaming library (windows, 2D graphics, sound, input, etc.).
如果您想远离 Objective-C/Cocoa 和 GLUT,您可以尝试SDL,它是一个跨平台游戏库(窗口、2D 图形、声音、输入等)。
Edit: forgot about the compiler flags, which mipadi posted, namely:
编辑:忘记了 mipadi 发布的编译器标志,即:
-framework OpenGL
回答by rwols
This question is extremely old in internet time, but the most straightforward way... is to just dabble your feet in Objective-C... Sorry.
这个问题在互联网时代非常古老,但最直接的方法......就是涉足Objective-C......对不起。
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,
MyRendere
r. - 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 leastthe 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.
- 您可以继续使用 C++ 进行所有核心编程,没问题。
- 在 XCode 中创建一个新的“Cocoa 应用程序”。
- 在界面构建器(您编辑 .xib 文件的地方)中,
NSOpenGLView
在对象浏览器中找到一个对象,然后将它敲在您的窗口上。 - 这一点很重要:在对象检查器中,将子类更改为您自己的子类(仍然需要制作)。例如,您可以将其命名为任何您喜欢的名称
MyRenderer
。 - 按 [cmd]+[n] 并创建一个新的 Objective-C 类
MyRendere
r。 - 重要的!将扩展名从 MyRenderer.m 更改为 MyRenderer.mm。这样,XCode 就知道它必须为 Objective-C++ 而不是 Objective-C 编译。
- 在MyRenderer.mm,覆盖至少下面的方法
NSOpenGLView
。- (void) awakeFromNib
: 在这里做你的类初始化。不要在这里初始化OpenGL,否则你的程序会崩溃。- (void) drawRect:(NSRect)dirtRect
: 在这里画画。- (void) prepareOpenGL
: 在这里初始化 OpenGL。- (void) reshape:(NSRect)bounds
:当视图被调整大小时。
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.
好消息是您可以在 MyRenderer.mm 中自由混合 C++ 函数和类。您还可以将 C++#include
指令与 Objective-C#import
指令一起使用。你可以在里面做 C++drawRect
没有问题。
The bad news is that NSOpenGLView
does not redrawthe 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.
坏消息是它NSOpenGLView
不会每 x 毫秒重绘一次场景。它只会在觉得有必要时重新绘制场景。NSTimer
为此,您必须使用 Cocoa 的类。
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.
编辑:额外注意:在drawRect
方法中,确保调用glFlush()
以及[[self openGLContext] flushBuffer]
. 出于某种原因,只有调用[[self openGLContext] flushBuffer]
不会为我在视图上绘制任何内容。
回答by Ali
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
我一直在使用这个网站作为我的 Window、Mac 和 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++.
终点是,您必须使用 Objective-C 来创建窗口,并且可以使用 C++ 编写其余代码。
回答by mipadi
When using OpenGL on Mac OS X, there are two things to keep in mind:
在 Mac OS X 上使用 OpenGL 时,需要记住两件事:
One, you have to link the OpenGL framework. Outside of Xcode, you can pass the -framework
flag to the linker:
一,你必须链接OpenGL框架。在 Xcode 之外,您可以将-framework
标志传递给链接器:
$ gcc -framework OpenGL -o my_opengl_program my_opengl_program.c
(Note that this flag only works on OS X.)
(请注意,此标志仅适用于 OS X。)
If you're using Xcode, you can just add OpenGL.framework
to your linked frameworks.
如果您使用的是 Xcode,则只需添加OpenGL.framework
到链接的框架即可。
Two, you prefix OpenGL/
> in front of your OpenGL headers. For example, to include gl.h
, use:
第二,OpenGL/
在 OpenGL 头文件前加上>前缀。例如,要包含gl.h
,请使用:
#include <OpenGL/gl.h>
Otherwise, programming with OpenGL on Mac OS X is pretty much the same.
否则,在 Mac OS X 上使用 OpenGL 编程几乎是一样的。
回答by Alan
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).
由于您在 mac 上编程,因此您可以使用您熟悉的任何语言。XCode 支持编译 C++,因此如果您熟悉 Windows 上的 OpenGL,那么这是一个直接的过渡,尽管您需要使用正确的方法来创建 OSX 窗口(最有可能是可可)。
If python is your thang, PyOpenGL is a python binding to OpenGL that is cross platform.
如果你喜欢 python,那么 PyOpenGL 是一个 python 绑定到跨平台的 OpenGL。