Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Hello World on Mac without XCode

Tags:

macos

opengl

I'm trying to write a hello world for OpenGL on Mac (Lion). I've found some related post or 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)

Any idea or suggestion?

like image 707
clwen Avatar asked Oct 24 '11 18:10

clwen


1 Answers

If you are using OSX I would highly recommend using XCode and use NSOpenGLView. This book 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.

If you want to use GLUT and compile at the terminal you could try this:

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

like image 181
whg Avatar answered Nov 02 '22 09:11

whg