I've been getting into OpenGL on OS X lately (tl;dr: I'm a OpenGL noob) and got some code working that draws a cube. However, I don't see any of the faces (besides the front) since my view isn't translated. I try to use the gluLookAt
function to do this translation, but I get an GL_INVALID_OPERATION
error. This is how I do my rendering:
// Activate and lock context
[_glView.openGLContext makeCurrentContext];
CGLLockContext(_glView.openGLContext.CGLContextObj);
// Update camera position
gluLookAt(0, 2.0, 0, 0, 0, 0, 0, 1, 0);
gl_GetError();
// Update viewport and render
glViewport(0, 0, _glView.frame.size.width, _glView.frame.size.height);
[_renderer doRender:time];
// Unlock and flush context
CGLUnlockContext(_glView.openGLContext.CGLContextObj);
[_glView.openGLContext flushBuffer];
This code works when I comment out the gluLookAt
call, and from what I can gather from the docs, this error is caused by executing gluLookAt
between glBegin
and glEnd
. I don't know if where these are getting called, as I do not call those myself, and wrapping the call to gluLookAt
in glBegin
and glEnd
does not solve the issue.
If it makes a difference, I'm using OpenGL 3.2 Core Profile.
By the way, gluLookAt (...)
(and GLU in general) is not a part of OpenGL. This is why you will not find documentation directly explaining the cause of this error.
The only reason it generates GL_INVALID_OPERATION
is because behind the scenes it does this: glMultMatrixf (...)
(which was a part of GL once upon a time). That is invalid in a core profile context, because there is no matrix stack anymore; anything that was deprecated in GL 3.0 or 3.1 is removed from GL 3.2 (core profile).
If you need gluLookAt
/ matrix stack functionality on OS X, GLKit provides a suitable collection of replacement utilities. Alternatively, you can use a much more portable (C++ based) library called GLM if you compile using Objective C++.
Now, the far simpler solution here is not to use a core profile context. If you are using things like gluLookAt (...)
you are likely learning legacy OpenGL. You need a context that supports deprecated parts of OpenGL, and on OS X this means you need a 2.1 context.