In need of a straight forward tutorial for setting up OpenGL in XCode

Discussion in 'Mac Programming' started by chrono1081, Dec 17, 2010.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    Hi guys.

    I finally have to start using OpenGL in XCode. I've worked with OpenGL but mostly just adjusting code so I'm no expert at it. I thought it would be super easy to set up XCode to use OpenGL but it looks like I was wrong on that.

    Does anyone have a link to a tutorial by chance that has a straight forward way of setting up OpenGL in XCode? Everything I am running across has a ton of terminal commands and downloading all kinds of stuff to make it work. Is it really that complicated or am I (like usual) finding the most difficult way to do things : /
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Would you be using GLUT or OpenGL context in more normal Mac Windows.

    Would you be programing on ObjC, ObjC++ and Cocoa or if GLUT ObjC, ObjC++, C++?
     
  3. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3
    Oops sorry I didn't give enough info :eek:

    I would be programming in C++, I think I will be using GLUT. It will be for games so I think I'll be using it. (Sorry I could be wrong really new to setting up OpenGL :/)

    I'm pretty confused because some websites will give you a string of things to set up, then sites like OpenGL.org say that MacOS comes with it just by installing XCode even though I don't see any options to start an OpenGL application (OpenGL ES is there but no OpenGL).

    I'm used to windows programming but I really want to do this next semester on my Mac only :)
     
  4. lloyddean, Dec 17, 2010
    Last edited: Dec 17, 2010

    lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #4
    In that case it is rather easy to setup.

    Start Xcode.
    Select MacOS X, Application, Command Line Tool and Type - 'C++ stdc++' from the project template.
    Add the OpenGL and GLUT frameworks to the project file.

    Replace the contents of the main.cpp file provided in the template with your own code or start with the code below.

    Enjoy

    Code:
    /* =============================================================================
     * File - main.cpp
     * -----------------------------------------------------------------------------
     * GLbyte            8-bit integer                      signed char
     * GLshort          16-bit integer                      short
     * GLint            32-bit integer                      long
     * GLsizei          32-bit integer                      long
     * GLfloat          32-bit floating point               float
     * GLclampf         32-bit floating point               float
     * GLdouble         64-bit floating point               double
     * GLclampd         64-bit floating point               double
     * GLubyte           8-bit unsigned integer             unsigned char
     * GLboolean         8-bit unsigned integer             unsigned char
     * GLushort         16-bit unsigned integer             unsigned short
     * GLuint           32-bit unsigned integer             unsigned long
     * GLenum           32-bit unsigned integer             unsigned long
     * GLbitfield       32-bit unsigned integer             unsigned long
     */
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    #include <GLUT/glut.h>
    
    #include <OpenGL/gl.h>
    
    
    #if 0
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    inline bool IsButtonUp(int button_state) {  
        return ((button_state bitand GLUT_UP) ? true : false);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    inline bool IsButtonDown(int button_state) {    
        return ((button_state bitand GLUT_DOWN) ? true : false);
    }
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Example usage -
     *  IsShiftKeyDown(glutGetModifiers())
     */
    
    inline bool IsShiftKeyDown(int key_modifier) {
        return ((key_modifier bitand GLUT_ACTIVE_SHIFT) ? true : false);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Example usage -
     *  IsControlKeyDown(glutGetModifiers())
     */
    
    inline bool IsControlKeyDown(int key_modifier) {
        return ((key_modifier bitand GLUT_ACTIVE_CTRL) ? true : false);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Example usage -
     *  IsAltKeyDown(glutGetModifiers())
     */
    
    inline bool IsAltKeyDown(int key_modifier) {
        return ((key_modifier bitand GLUT_ACTIVE_ALT) ? true : false);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutOverlayDisplayFunc(OverlayDisplayHandler);
     */
    
    static void OverlayDisplayHandler()
    {
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutMotionFunc(MotionHandler);
     */
    
    static void MotionHandler(int x, int y)
    {
    #pragma unused(x, y)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutPassiveMotionFunc(PassiveMotionHandler);
     */
    
    static void PassiveMotionHandler(int x, int y)
    {
    #pragma unused(x, y)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutVisibilityFunc(VisibilityHandler);
     */
    
    static void VisibilityHandler(int state)
    {
        if ( GLUT_NOT_VISIBLE == state)
        {
            ;
        } else if ( GLUT_VISIBLE == state)
        {
            ;
        }
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutEntryFunc(EntryHandler);
     */
    
    static void EntryHandler(int state)
    {
        if ( GLUT_LEFT == state )
        {
            ;
        }
        else if ( GLUT_ENTERED == state)
        {
            ;
        }
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutSpecialFunc(SpecialKeyDownHandler);
     *
     * May want to disable key repeat with -
     *  glutIgnoreKeyRepeat(1);
     */
    
    static void SpecialKeyDownHandler(int key, int x, int y)
    {
    #pragma unused(x, y)
        
        int key_modifier = glutGetModifiers();
        
        switch ( key )
        {
            case GLUT_KEY_F1:
                break;
                
            case GLUT_KEY_F2:
                break;
                
            case GLUT_KEY_F3:
                break;
                
            case GLUT_KEY_F4:
                break;
                
            case GLUT_KEY_F5:
                break;
                
            case GLUT_KEY_F6:
                break;
                
            case GLUT_KEY_F7:
                break;
                
            case GLUT_KEY_F8:
                break;
                
            case GLUT_KEY_F9:
                break;
                
            case GLUT_KEY_F10:
                break;
                
            case GLUT_KEY_F11:
                break;
                
            case GLUT_KEY_F12:
                break;
                
            case GLUT_KEY_LEFT:
                break;
                
            case GLUT_KEY_UP:
                break;
                
            case GLUT_KEY_RIGHT:
                break;
                
            case GLUT_KEY_DOWN:
                break;
                
            case GLUT_KEY_PAGE_UP:
                break;
                
            case GLUT_KEY_PAGE_DOWN:
                break;
                
            case GLUT_KEY_HOME:
                break;
                
            case GLUT_KEY_END:
                break;
                
            case GLUT_KEY_INSERT:
                break;
        }
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutSpecialUpFunc(SpecialKeyUpHandler);
     *
     * May want to disable key repeat with -
     *  glutIgnoreKeyRepeat(1);
     */
    
    static void SpecialKeyUpHandler(int key, int x, int y)
    {
    #pragma unused(x, y)
        
        int key_modifier = glutGetModifiers();
        
        switch ( key )
        {
            case GLUT_KEY_F1:
                break;
                
            case GLUT_KEY_F2:
                break;
                
            case GLUT_KEY_F3:
                break;
                
            case GLUT_KEY_F4:
                break;
                
            case GLUT_KEY_F5:
                break;
                
            case GLUT_KEY_F6:
                break;
                
            case GLUT_KEY_F7:
                break;
                
            case GLUT_KEY_F8:
                break;
                
            case GLUT_KEY_F9:
                break;
                
            case GLUT_KEY_F10:
                break;
                
            case GLUT_KEY_F11:
                break;
                
            case GLUT_KEY_F12:
                break;
                
            case GLUT_KEY_LEFT:
                break;
                
            case GLUT_KEY_UP:
                break;
                
            case GLUT_KEY_RIGHT:
                break;
                
            case GLUT_KEY_DOWN:
                break;
                
            case GLUT_KEY_PAGE_UP:
                break;
                
            case GLUT_KEY_PAGE_DOWN:
                break;
                
            case GLUT_KEY_HOME:
                break;
                
            case GLUT_KEY_END:
                break;
                
            case GLUT_KEY_INSERT:
                break;
        }
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutSpaceballMotionFunc(SpaceBallMotionHandler);
     */
    
    static void SpaceBallMotionHandler(int x, int y, int z)
    {
    #pragma unused(x, y, z)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutSpaceballRotateFunc(SpaceBallRotateHandler);
     */
    
    static void SpaceBallRotateHandler(int x, int y, int z)
    {
    #pragma unused(x, y, z)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutSpaceballButtonFunc(SpaceBallButtonHandler);
     */
    
    static void SpaceBallButtonHandler(int button, int button_state)
    {
    #pragma unused(button, button_state)
        
        // glutDeviceGet(GLUT_NUM_SPACEBALL_BUTTONS)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutButtonBoxFunc(ButtonBoxHandler);
     */
    
    static void ButtonBoxHandler(int button, int button_state)
    {
    #pragma unused(button, button_state)
        
        // glutDeviceGet(GLUT_NUM_BUTTON_BOX_BUTTONS)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutDialsFunc(DialsHandler);
     */
    
    static void DialsHandler(int dial, int value)
    {
    #pragma unused(dial, value)
        
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutTabletMotionFunc(TabletMotionHandler);
     */
    
    static void TabletMotionHandler(int x, int y)
    {
    #pragma unused(x, y)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutTabletButtonFunc(TabletButtonHandler);
     */
    
    static void TabletButtonHandler(int button, int button_state, int x, int y)
    {
    #pragma unused(button, button_state, x, y)
        
        // glutDeviceGet(GLUT_NUM_TABLET_BUTTONS)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutMenuStatusFunc(MenuStatusHandler);
     */
    
    static void MenuStatusHandler(int status, int x, int y)
    {
    #pragma unused(x, y)
        
        if ( GLUT_MENU_IN_USE == status )
        {
            ;
        }
        else if ( GLUT_MENU_NOT_IN_USE == status )
        {
            ;
        }
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutIdleFunc(IdleHandler);
     */
    
    static void IdleHandler()
    {
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutTimerFunc(unsigned int msecs, TimerHandler, value);
     */
    
    static void TimerHandler(int value)
    {
    #pragma unused(value)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutReshapeFunc(ReshapeHandler);
     */
    
    static void ReshapeHandler(int xWidth, int yHeight)
    {
    #pragma unused(xWidth, yHeight)
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutMouseFunc(MouseHandler);
     */
    
    static void MouseHandler(int button, int button_state, int x, int y)
    {
    #pragma unused(button, button_state, x, y)
        
        
        int key_modifier = glutGetModifiers();
    }
    
    #endif
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutKeyboardFunc(KeyboardHandler);
     *
     * May want to disable key repeat with -
     *  glutIgnoreKeyRepeat(1);
     */
    
    static void KeyboardHandler(unsigned char key, int x, int y)
    {
    #pragma unused(key, x, y)
        
    //  int key_modifier = glutGetModifiers();
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     * Callback Handler registered with -
     *  void glutDisplayFunc(DisplayHandler);
     */
    
    static void DisplayHandler()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        
        glColor3f(1.0f, 0.0f, 0.0f);
        glBegin(GL_LINES);
        
        glVertex2i(180, 15);
        glVertex2i(10, 45);
        
        glEnd();
        
        glFlush();  
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    static void ApplicationSetUp()
    {
        glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        
        glMatrixMode(GL_PROJECTION);
        gluOrtho2D(0.0, 200.0, 0.0, 150.0);
    }
    
    
    /* =============================================================================
     * -----------------------------------------------------------------------------
     */
    
    int main( int argc, char** argv )
    {
        glutInit(&argc, argv);
        
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        
        glutInitWindowPosition(50, 100);
        glutInitWindowSize(400, 300);
        glutCreateWindow("An Example");
        
        ApplicationSetUp();
        
        glutDisplayFunc(DisplayHandler);
        glutKeyboardFunc(KeyboardHandler);
        
        glutMainLoop();
        
        return 0;
    }
    
     
  5. chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Were you able to get something up and running?
     
  7. chrono1081, Dec 19, 2010
    Last edited: Dec 19, 2010

    chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #7
    I haven't got to try yet but I'm downloading the new XCode now (i'm a version behind) and will be trying as soon as I do :) I'll post back shorty :D


    EDIT: Yes it worked! Thank you so much! (I got a red line with a title saying "An Example" :D

    I just hope my professor next semester lets me turn projects in in XCode : / I may have to set this up in Netbeans (I'm weird I like Netbeans) or Visual Studio instead though :(
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    You might want to grab an example working project from an earlier forum response.
     

Share This Page