Chapter 3: Drawing in Space: Geometric Primitives and Buffers

This chapter introduces the basic objects used to create graphics: points, lines, triangles and polygons, and the different options for each.

I did finally cause an OpenGL error with my code, and logOpenGLError performed as I had hoped. I actually encountered two different invalid operations, one caused by trying to track down the other.

The original one came from incorrect modifications to previously working code. Some of the example programs call glBegin() only once at the beginning of drawRect:, while others call glBegin() once per line (or point) so that something like point size or line width can be changed for each line or point. Naturally, there is a matching glEnd() for each glBegin()In the example that created lines of varying widths, I had forgotten to take out the once-at-the-beginning glBegin call when adding the per-line call, as well as its corresponding glEnd().

When the extra glEnd() was called, it resulted in an OpenGL Error: invalid operation. The first call to drawRect: would work, but later calls would result in the first-drawn, narrowest line to be drawn with the thickest width (matching the last-drawn ine of the previous call to drawRect:).

I decided to add an argument to the logging method, so that the caller can choose whether or not to print a message if there is no error (the original version would print No OpenGL errors found. Here’s the new method:

- (void)logGLErrorQuietly:(BOOL)quietly {
  GLenum errorFlag = glGetError();
  if (errorFlag == GL_NO_ERROR) {
    if (!quietly) {
      NSLog(@"No OpenGL errors found");
    }
    return;
  }
  do {
    NSLog(@"OpenGL Error: %s", gluErrorString(errorFlag));
    errorFlag = glGetError();
    } while (errorFlag != GL_NO_ERROR);
}

The second error came about when adding logOpenGLErrorsQuietly:YES throughout the code to spot the error. It turns out that calling glGetError() isn’t legal between glBegin() and glEnd(). Everything still drew correcly, OpenGL just kept complaining about the invalid call to glGetError().

I hope that logOpenGLErrorsQuietly: will prove handy out there—feel free to use it in your own programs.

Advertisements
This entry was posted in OpenGL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s