macos - Mac 上的 OpenGL Hello World(无需 XCode)

标签 macos opengl

我正在尝试在 Mac (Lion) 上为 OpenGL 编写一个 hello world。我找到了一些related postyoutube tutorial ,但大多数都需要 XCode。我对 XCode 没问题,但我认为应该有一些更简单的方法来编写一个 hello world,类似于在终端下的 vim 中编写 .cpp 文件,编译并运行。 (当然如果需要的话提前安装库)

有什么想法或建议吗?

最佳答案

如果您使用 OSX,我强烈建议您使用 XCode 并使用 NSOpenGLView。 This本书有很多关于您可以使用的几个 API 的 Material 。 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;

}

然后编译:

 gcc /System/Library/Frameworks/GLUT.framework/GLUT /System/Library/Frameworks/OpenGL.framework/OpenGL main.c -o myGlutApp

这应该可以解决问题。我想说的是,虽然不要尝试用它来对抗 XCode,但它会节省你的时间和挫败感。

关于macos - Mac 上的 OpenGL Hello World(无需 XCode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7879843/

相关文章:

c++ - GLEW 未初始化;抛出 "Missing OpenGL Version"

c++ - 如何开始使用 GPU 体素化器?

c++ - 带有像素图图像的 QLabel 变得模糊

c++ - Opengl 3.3 不绘制任何东西。使用 GLSL 330 核心

c++ - IGL错误: undefined symbols for architecture x86_64

objective-c - 向 NSTableView 添加行不会更改其框架

opengl - OpenGL 着色器是在 GPU 还是 CPU 上编译的?二进制输出如何因 GPU 制造商而异?

ios - Apple 框架 ISA 分类

objective-c - Cocoa 在分发时保存文本文件

macos - 如何获取另一个进程的已加载符号列表