clwen
clwen

Reputation: 20909

OpenGL Hello World on Mac without XCode

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?

Upvotes: 11

Views: 13867

Answers (5)

Sami Ben
Sami Ben

Reputation: 556

/*
 *  As a first example of using OpenGL in C,
 *  https://math.hws.edu/graphicsbook/source/glut/first-triangle.c
 */
 
#include <GLUT/glut.h>   // freeglut.h might be a better alternative, if available.
#include <stdio.h>
#include <stdlib.h>

void display(void) {  // Display function will draw the image.
 
    glClearColor( 0, 0, 0, 1 );  // (In fact, this is the default.)
    glClear( GL_COLOR_BUFFER_BIT );
    
    glBegin(GL_TRIANGLES);
    glColor3f( 1, 0, 0 ); // red
    glVertex2f( -1, -1 );
    glColor3f( 0, 1, 0 ); // green
    glVertex2f( 1, -1 );
    glColor3f( 0, 0, 1 ); // blue
    glVertex2f( 0, 1 );
    glEnd();
    
    glutSwapBuffers(); // Required to copy color buffer onto the screen.
}

int main( int argc, char** argv ) {  // Initialize GLUT and
    ///GL 3D drawing
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);    // Use single color buffer and no depth buffer.
    glutInitWindowSize(500,500);         // Size of display area, in pixels.
    glutInitWindowPosition(300,100);     // Location of window in screen coordinates.
    glutCreateWindow("Hello OpenGL"); // Parameter is window title.
    glutDisplayFunc(display);            // Called when the window needs to be redrawn.
    
    glutMainLoop(); // Run the event loop!  This function does not return.
                    // Program ends when user closes the window.
    return EXIT_SUCCESS;
}

Upvotes: 0

matt
matt

Reputation: 12347

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.

clang++ -framework glut -framework opengl hello.cpp

The above code should compile. The signature of the main function is incorrect though.

<int main(int argc, char *argv)
>int main(int argc, char **argv)

Which makes the glutInit wrong,

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

Upvotes: 8

whg
whg

Reputation: 894

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.

Upvotes: 19

Matt Phillips
Matt Phillips

Reputation: 9691

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.

Upvotes: -1

rgngl
rgngl

Reputation: 5423

Try using GLUT and writing a small C++ program. You can use something like macports to install necessary libraries.

Upvotes: 0

Related Questions