Simple OpenGL Program

Discussion in 'Mac Programming' started by dbrayford, Apr 22, 2010.

  1. dbrayford macrumors member

    Joined:
    Feb 22, 2010
    #1
    I am looking for a very simple OpenGL program that uses cocoa to create the native window rather than glut. That doesn't use xcode, as I need to be able to build the OpenGL inside a larger project using make. I am new to cocoa so have been struggling to create the OpenGL window.I have no problems writing the OpenGL code, but have spent a lot of time unsuccessfully trying to get a simple OpenGL window.

    David

    The ogl code goes something like this:
    Code:
    void display()
    {
       glClear (GL_COLOR_BUFFER_BIT);
    
    
       glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
       glBegin(GL_POLYGON);
          glVertex3f (0.25f, 0.25f, 0.0f);
          glVertex3f (0.75f, 0.25f, 0.0f);
          glVertex3f (0.75f, 0.75f, 0.0f);
          glVertex3f (0.25f, 0.75f, 0.0f);
       glEnd();
    
       glFlush ();
    }
    
    void init() 
    {
       glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
    
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
    }
    
    
    int main(int argc, char** argv)
    {
       create_window();
       init();
       display(); 
       swapbuffer();
       return 0;
    }
    
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
  3. dbrayford thread starter macrumors member

    Joined:
    Feb 22, 2010
    #3
    Unfortunately, I need to embed the OpenGL window into a Java frame using JNI. I have all the JNI code written, but am struggling to create a simple OpenGL window using cocoa. So am looking for a super simple OpenGL example code that doesn't use xcode or glut to create the window.
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    What do you mean by doesn't use Xcode? Do you mean no Objective-C?
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    If you mean creating the window/view without a nib, then here is a sample I modified from an existing sample I've had. If you want to build on the command line, use xcodebuild.
     

    Attached Files:

  6. dbrayford thread starter macrumors member

    Joined:
    Feb 22, 2010
    #6
    I mean that the objective-C create window function is called via a C/C++ function. I just don't know how to create an OpenGL window.

    something like this:
    Code:
    int main(int argc, const char *argv[])
    {
        // this function calls the relevant Objective-C functions to create the   
       //  OpenGL window
        CreateWindow();
        InitGL();
        DisplayGL();
        SwapBuffers();
        return 0;
    }
    
    
    Rather than:
    Code:
    int main(int argc, const char *argv[])
    {
        return NSApplicationMain(argc, argv);
    }
    
     

Share This Page