Install & Compile opengl / glut / freeglut on Mac OSX Mavericks

Posted by on April 15, 2014 in C/C++ | 0 comments

Install & Compile opengl / glut / freeglut on Mac OSX Mavericks

Install / / on Mac OSX Mavericks

$ brew install 

Download example source code from: http://openglsamples.sourceforge.net/triangle.html


/*
 * OpenGLSamples (openglsamples.sf.net) Examples
 * VC++ users should create a Win32 Console project and link 
 * the program with 32.lib, glu32.lib, 32.lib
 *
 *  can be downloaded from http://www.xmission.com/~nate/.html
 *  is by default installed on your system.
 * For an installation of  on windows for MS Visual Studio 2010 see: http://nafsadh.wordpress.com/2010/08/20/-in-ms-visual-studio-2010-msvs10/
 *
 */

#include <stdio.h>
//#include </gl.h>       // Open Graphics Library () header
#include </.h>     // The  Utility Toolkit () Header

#define KEY_ESCAPE 27

typedef struct {
    int width;
  int height;
  char* title;

  float field_of_view_angle;
  float z_near;
  float z_far;
} glutWindow;

glutWindow win;

void display() 
{
  glClear(_COLOR_BUFFER_BIT | _DEPTH_BUFFER_BIT);        // Clear Screen and Depth Buffer
  glLoadIdentity();
  glTranslatef(0.0f,0.0f,-3.0f);      

  /*
   * Triangle code starts here
   * 3 verteces, 3 colors.
   */
  glBegin(_TRIANGLES);          
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);    
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f(-1.0f,-1.0f, 0.0f);    
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 1.0f,-1.0f, 0.0f);    
  glEnd();        

  glutSwapBuffers();
}

void initialize () 
{
    glMatrixMode(_PROJECTION);                        // select projection matrix
    glViewport(0, 0, win.width, win.height);                  // set the viewport
    glMatrixMode(_PROJECTION);                        // set matrix mode
    glLoadIdentity();                             // reset projection matrix
    GLfloat aspect = (GLfloat) win.width / win.height;
  gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);   // set up a perspective projection matrix
    glMatrixMode(_MODELVIEW);                         // specify which matrix is the current matrix
    glShadeModel( _SMOOTH );
    glClearDepth( 1.0f );                           // specify the clear value for the depth buffer
    glEnable( _DEPTH_TEST );
    glDepthFunc( _LEQUAL );
    glHint( _PERSPECTIVE_CORRECTION_HINT, _NICEST );            // specify implementation-specific hints
  glClearColor(0.0, 0.0, 0.0, 1.0);                     // specify clear values for the color buffers               
}

void keyboard ( unsigned char key, int mousePositionX, int mousePositionY )   
{ 
  switch ( key ) 
  {
    case KEY_ESCAPE:        
      exit ( 0 );   
      break;      

    default:      
      break;
  }
}

int main(int argc, char **argv) 
{
  // set window values
  win.width = 640;
  win.height = 480;
  win.title = "/ Example. Visit http://openglsamples.sf.net ";
  win.field_of_view_angle = 45;
  win.z_near = 1.0f;
  win.z_far = 500.0f;

  // initialize and run program
  glutInit(&argc, argv);                                      //  initialization
  glutInitDisplayMode(_RGB | _DOUBLE | _DEPTH );  // Display Mode
  glutInitWindowSize(win.width,win.height);         // set window size
  glutCreateWindow(win.title);                // create Window
  glutDisplayFunc(display);                 // register Display Function
  glutIdleFunc( display );                  // register Idle Function
    glutKeyboardFunc( keyboard );               // register Keyboard Handler
  initialize();
  glutMainLoop();                       // run  mainloop
  return 0;
}

gist: https://gist.github.com/NAzT/10642267

Include & Link libraries then compile!

$  nat.  -I/usr/local/Cellar//2.8.1/include  -L/usr/local/Cellar//2.8.1/lib -w -framework  -lGLUT

to run ./a.out

glut-triangle example

Ref:

Leave a Comment

Your email address will not be published. Required fields are marked *