Edit AllPages

How about some basic OpenGL Examples for MacOSX. Please comment your code.

Neon Helium Productions has an excellent series of OpenGL tutorials at There is Cocoa source code supplied for most of the tutorials.

Most might be overstating it; at least, most of the really interesting ones (volumetric shadows, cel shaders, et cetera) don’t have a Cocoa version. Good way to get you up to speed and ready to experiment, though. I’m still working on getting picking working properly.

There are 38 Cocoa OpenGL lessons and code based on NeHe’s tutorials, including cel shading, along with a lot of other interesting Cocoa OpenGL stuff here:

Katherine Tattersall created the NeHe tutorials in Cocoa. Go to – JimCath

However, her tutorials end at number 9, which is hardly the extent of NeHe’s tutorials, and those ported at

FadeFromBlack is a simple OpenGL screen saver. The code might be commented.

CustomGLView is a simple demonstration of drawing with OpenGL in a custom NSView without subclassing NSOpenGLView.

Cocoa Z-Buffer

I used the CocoaInitGL example (availible from the apple sample code site) to init an opengl context into and NSOpenGLView…but then - when I start making…let’s say, a cube - all the sides are rendered wrong…there ain’t no Z-Buffer…so some sides shine through some others… to turn on the Z-buffer?


You need to call glEnable(GL_DEPTH_TEST) even though the example gives you a context that has depth buffers might not mean that opengl knows that it is supposed to do depth buffering.

Interestingly enough in the NSOpenGLView example this works if you put that call into the contextCreateContextButton method it does not work when the call is in initWithFrame, somehow the context that is created the first time does not contain a depth buffer.

To see for yourself, add a button to the sample in the button call glEnable and glDisable with GL_DEPTH_TEST, first context it does not work any other context created afterwards (with depth buffers enabled) this will work.

You only need to call glEnable once during your program (unless you turn it off again)

– HaRald

If you use glEnable(GL_DEPTH_TEST); then there is a z-buffer, but you need to construct the cube in order (back to front). Refer to the Open GL Red/Blue book.

– DavidRemahl

You don’t need to draw the cube in any particular order if there is a z-buffer. That’s one of the main benefits of a Z-buffer. In fact, it would be less efficient to construct the cube from back to front order on some architectures when you have a z-buffer enabled. A depth test is faster than a depth test then setting a pixel. Not drawing back to front is one of the main purposes of a z-buffer, since there are many times when you can’t draw shapes if they require such an ordering.

You also need to remember when you call glClear to use the GL_DEPTH_BUFFER_BIT as well as the standard GL_COLOR_BUFFER_BIT, otherwise you can get some of the strangest drawing bugs you will ever see in your life.

– DaveFayram

I’ve tried to write some opengl code but no luck. I guess it is all about that thing called z-buffer. Could someone please tell me what i am doing wrong? I am using this code with CustomGLView. Here is my code:


I’m no OpenGL expert, nor am I a cocoa expert, but my work is deeply tied to opengl for visualization, and as such I’ve written a basic GLView class which does what I need (tm).

My guess is that you’ve not set the correct pixel attributes, since as far as I can tell, that’s where (in Cocoa) you turn on the depth buffer functionality (later you actually turn it on with a glEnable( GL_DEPTH_TEST ) and glDepthFunc( GL_LESS )

Here’s my initWithFrame:

/* Call this in your awakeFromNib or somewhere else. */

I could be wrong, but this has worked for me. –[[ShamylZakariya —-

Could someone give me a link to a simple OpenGL tutorial which draws simble cube with different textures on every side? Is it difficult?

NeHe is mentioned above. Go there. They have a texture-mapped cube example. Go there, it’s not that hard. They even have cocoa example code for most of their tutorials. Go there.


I want to select a 3D object in Ortho mode, but it is not working. the input argument is already transform to the OpenGL coordinate.

-(void)select:(NSPoint)clickPoint { GLuint selectBuffer[64] = {0}; GLint hits, viewPort[4];


glSelectBuffer(64, selectBuffer);
glGetIntegerv(GL_VIEWPORT, viewPort);



gluPickMatrix(clickPoint.x, clickPoint.y, 20, 20, viewPort);

GLfloat height	= viewPort[3];
GLfloat width	= viewPort[2];
GLfloat nRange	= 100.0f;
glOrtho(-nRange, nRange, 

[self cleanScreen];
[self showAll];  // call all glLists here and [context flushbuffer] at tail

hits = glRenderMode(GL_RENDERER);



NSLog(@"hits = %d", hits);
NSLog(@"buffer = %d,%d,%d",selectBuffer[0],selectBuffer[1],selectBuffer[2]);
[self showAll];

After call this method I can not [self showAll] any more, it’s similar as “can not quit the GL_SELECT mode”.