Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dbrayford

macrumors member
Original poster
Feb 22, 2010
41
0
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;
}
 
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.
 
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.
 

Attachments

  • CocoaGL.zip
    46.2 KB · Views: 134
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);
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.