没有XCode的Mac上的OpenGL Hello World

clw*_*wen 11 opengl macos

我正在尝试在Mac(Lion)上为OpenGL编写一个hello world.我发现了一些相关的帖子youtube教程,但大多数都需要XCode.我对XCode很满意,但我认为应该有一些更简单的方法来编写一个hello world,类似于在终端下编写一个.cpp文件,编译并运行.(当然,如果需要事先安装库)

有什么想法或建议吗?

whg*_*whg 17

如果您使用的是OSX,我强烈建议您使用XCode并使用NSOpenGLView.本书有很多关于你可以使用的API的资料.GLUT绝对是最快掌握和设置的.

如果你想使用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;

}
Run Code Online (Sandbox Code Playgroud)

然后编译:

 gcc /System/Library/Frameworks/GLUT.framework/GLUT /System/Library/Frameworks/OpenGL.framework/OpenGL main.c -o myGlutApp
Run Code Online (Sandbox Code Playgroud)

这应该够了吧.我会说虽然不尝试与XCode一起工作,它会节省你的时间和挫折.

  • 当OP问题明确指出"没有XCode"并且最佳答案和许多评论说"使用XCode"时,这很令人难过.我们中的一些人并不真正关心GUI IDE和为Mac开发应用程序 - OSX恰好是我们在练习编码时使用的平台. (6认同)

mat*_*att 5

这有点旧,但我会回答它,因为我刚刚学会了如何做到这一点.首先,上面的示例代码中有两个错误,它们很容易修复.在OS X 10.8上使用clang这是有效的.

clang++ -framework glut -framework opengl hello.cpp
Run Code Online (Sandbox Code Playgroud)

上面的代码应该编译.但主要功能的签名不正确.

<int main(int argc, char *argv)
>int main(int argc, char **argv)
Run Code Online (Sandbox Code Playgroud)

这是glutInit错的,

<glutInit(&argc, &argv);
>glutInit(&argc, argv);
Run Code Online (Sandbox Code Playgroud)

输出很糟糕,但它创建了一个窗口,它显示了如何使用clang框架.感谢您的入门文件.