macos 没有 XCode 的 Mac 上的 OpenGL Hello World
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7879843/
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
OpenGL Hello World on Mac without XCode
提问by clwen
I'm trying to write a hello world for OpenGL on Mac (Lion). I've found some related postor youtube tutorial, but most of them require XCode. I'm OK with XCode, but I thought there should be some simpler approaches to write just a hello world, similar to write a .cpp file in vim under terminal, compile and run. (of course install libraries if needed beforehand)
我正在尝试为 Mac (Lion) 上的 OpenGL 编写一个 hello world。我找到了一些相关的帖子或youtube 教程,但其中大部分都需要 XCode。我对 XCode 没问题,但我认为应该有一些更简单的方法来编写一个 hello world,类似于在终端下的 vim 中编写一个 .cpp 文件,编译和运行。(当然,如果需要,请事先安装库)
Any idea or suggestion?
有什么想法或建议吗?
回答by whg
If you are using OSX I would highly recommend using XCode and use NSOpenGLView. Thisbook has a lot of material regarding the several APIs you can use. GLUT is definitely the quickest to get to grips with, and to set up.
如果您使用的是 OSX,我强烈建议您使用 XCode 并使用 NSOpenGLView。这本书有很多关于你可以使用的几个 API 的材料。GLUT 绝对是最快掌握和设置的。
If you want to use GLUT and compile at the terminal you could try this:
如果你想使用 GLUT 并在终端编译,你可以试试这个:
#include <GLUT/glut.h>
void display(void) {
//clear white, draw with black
glClearColor(255, 255, 255, 0);
glColor3d(0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//this draws a square using vertices
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(0, 128);
glVertex2i(128, 128);
glVertex2i(128, 0);
glEnd();
//a more useful helper
glRecti(200, 200, 250, 250);
glutSwapBuffers();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set the coordinate system, with the origin in the top left
gluOrtho2D(0, width, height, 0);
glMatrixMode(GL_MODELVIEW);
}
void idle(void) {
glutPostRedisplay();
}
int main(int argc, char *argv) {
//a basic set up...
glutInit(&argc, &argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
//create the window, the argument is the title
glutCreateWindow("GLUT program");
//pass the callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMainLoop();
//we never get here because glutMainLoop() is an infinite loop
return 0;
}
and then compile with:
然后编译:
gcc /System/Library/Frameworks/GLUT.framework/GLUT /System/Library/Frameworks/OpenGL.framework/OpenGL main.c -o myGlutApp
That should do the trick. I would say though don't try and fight XCode work with it, it will save you time and frustration.
这应该够了吧。我会说虽然不要尝试和它对抗 XCode 工作,但它会节省你的时间和挫折。
回答by matt
This is a bit old but I'll answer it because I just learned how to do it. First there are two errors in the sample code above, they're easy to fix. On OS X 10.8 using clang this works.
这有点旧,但我会回答它,因为我刚刚学会了如何去做。首先,上面的示例代码中有两个错误,它们很容易修复。在 OS X 10.8 上使用 clang 可以正常工作。
clang++ -framework glut -framework opengl hello.cpp
The above code should compile. The signature of the main function is incorrect though.
上面的代码应该可以编译。但是 main 函数的签名是不正确的。
<int main(int argc, char *argv)
>int main(int argc, char **argv)
Which makes the glutInit
wrong,
这是glutInit
错误的,
<glutInit(&argc, &argv);
>glutInit(&argc, argv);
The output sucks, but it makes a window and it shows how to use the frameworks with clang. Thanks for the starter file.
输出很糟糕,但它创建了一个窗口,并显示了如何使用带有 clang 的框架。感谢您的启动文件。
回答by rgngl
Try using GLUT and writing a small C++ program. You can use something like macports to install necessary libraries.
尝试使用 GLUT 并编写一个小的 C++ 程序。您可以使用类似 macports 的东西来安装必要的库。
回答by Matt Phillips
Look online for the example/tutorial code of any book on OpenGL. They all start with very simple programs that require at most GLUT (as mentioned by another poster), and generally have instructions on how to build them. So just download one of these simple programs and get it up and running.
在线查找有关 OpenGL 的任何书籍的示例/教程代码。它们都从非常简单的程序开始,最多需要 GLUT(如另一张海报所述),并且通常有关于如何构建它们的说明。因此,只需下载这些简单程序之一并启动并运行即可。