0

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.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Tristan
  • 3,058
  • 6
  • 40
  • 68
  • 1
    Someone looking at the opengl tag must be going around and -1-ing almost every question without comment. This is really bugging me. – thokra Jan 23 '14 at 16:52
  • @thokra i just upvote it again.. if you think a question is bad, explain why, no excuses. – Anton D Jan 23 '14 at 17:06
  • @AntonD: So you downvoted, then upvoted again because you couldn't give an explanation? I'm a little confused. – thokra Jan 23 '14 at 17:11
  • The only downside to doing that is that an upvote has a greater magnitude than down. So the initial downvote causes an individual to lose 2 rep, but then the upvote to restore the nominal score to 0 earns the author 5 rep. Overall the individual earns +3 rep on a question that appears at first glance to be neutral. +3's not a lot, but upvoting to restore the number to 0 does not actually restore status quo. – Andon M. Coleman Jan 23 '14 at 17:11
  • @thokra ofc not, i'm not english so sorry for the confusion. – Anton D Jan 23 '14 at 17:50

1 Answers1

2

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.

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • 1
    +1: Also, [GLM](http://glm.g-truc.net/0.9.5/index.html) is a very good library substituting some GLU functions, including `glmLookAt()`. – thokra Jan 23 '14 at 16:48
  • I considered mentioning that, but I was not sure if it would work in an Objective C application. – Andon M. Coleman Jan 23 '14 at 16:55
  • Oh, I didn't see that is was objective C. Can you interface with C++-code in ObjC? – thokra Jan 23 '14 at 16:59
  • Yeah, the lines that make no sense to most C programmers like this: `[_renderer doRender:time];` are a dead giveaway ;) I just checked and GLM can be used if the software is compiled as "Objective C++". – Andon M. Coleman Jan 23 '14 at 17:16
  • Thanks — I figured the majority of docs on the Internet seem to use legacy OpenGL, and I didn't read the part of the docs that states 3.2 Core removes the deprecated stuff. I'll look into GLM and see how it works! – Tristan Jan 23 '14 at 17:42