# OpenGL vs. Quartz



## Mikuro (Aug 18, 2005)

I've always heard that if speed matters, you should use OpenGL. So I just started learning OpenGL for the purpose of some 2D sprite-based gaming work. I've only been using it for a day at this point, but I figured out how to draw a 32-bit image, and that's practically all I need. So I ran some benchmarks.

I drew a 128x128 png (with an 8-bit alpha channel) 1000 times in an NSView. Using OpenGL, this took roughly 0.62 seconds (that's the average of several successive runs). Using Quartz, it took 0.13. Not what I was expecting. Here's the code I used, in an NSOpenGLView's drawFrame method:
	
	



```
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, [self frame].size.width, [self frame].size.height);
glOrtho(0, [self frame].size.width, 0, [self frame].size.height, -1.0, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
NSDate *d = [NSDate date];
for (i=1; i<1000; i++) {
	glRasterPos2f(random() % (int)([self frame].size.width-128),random() % (int)([self frame].size.height-128));
	glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
}
NSLog(@"%f",[d timeIntervalSinceNow]*-1.0);
```
..and...

```
NSDate *d = [NSDate date];
for (i=1; i<1000; i++) {
	[image compositeToPoint:NSMakePoint(random() % (int)([self frame].size.width-128),random() % (int)([self frame].size.height-128)) operation:NSCompositeSourceOver];
}
NSLog(@"%f",[d timeIntervalSinceNow]*-1.0);
```
(Where "pixels" and "image" are an unsigned char pointer to the bitmap data and an NSImage using an NSBitmapImageRep of that same image.)

Is my OpenGL code completely backwards? Or is it really that much slower than Quartz? I hope I'm just doing something stupid, because I was hoping for much better performance than that. Especially since Quartz seems much slower in Tiger than it was in Panther (I was perfectly happy with the speed I was getting in Panther, which is why I never bothered with OpenGL until now).

Relevant system data is in my sig.


----------



## Inverter (Aug 18, 2005)

You should be careful with such very general statements, the exact performance often depends on the exact operations you are using...

That said, I would expect the OpenGL version to be _significantly_ faster if you render the image as texture onto two triangles (or a quad). You load the image into the graphics card once, and then it is used from there during the subsequent texturing operations.

This library might help you getting the image loaded into OpenGL: http://openil.sourceforge.net/ , and there are texture mapping examples on http://www.opengl.org/resources/code/basics/redbook/index.html .


----------



## Viro (Aug 18, 2005)

Look at this page (entry 22.070) for more information on glDrawPixels.


----------



## Mikuro (Aug 18, 2005)

Ahhh, thanks. Looks like I have a lot to learn about OpenGL (surprise, surprise). I actually thought at first that I would need to render 2D images as texture maps in 3D space, but then I found the regular 2D operations and assumed that must be the way to go. Guess not. Rendering a 2D image on a flat 3D object sure seems like a roundabout way of doing things, though. Guess I'll just keep going through The Red Book.

Speaking of The Red Book, I have one more question. I notice that in a lot of their examples, they include "aux.h". This always brings up an error in XCode saying that no file named "aux.h" exists. Does aux.h simply not exist on OS X? And if not, is there any subtitute? It's a little disheartening when every other example in the book doesn't work and I have no idea how to do it myself....


----------



## Viro (Aug 19, 2005)

I don't know what it would be called on OS X. On Windows, this file used to be called glaux.h, and it was where all the auxiliary functions (prefixed with aux) were defined. Might be the same on OS X? The thing to remember though that the auxiliary library has been deprecated, and it has been replaced by GLUT.


----------



## Inverter (Aug 19, 2005)

Mikuro said:
			
		

> Speaking of The Red Book, I have one more question. I notice that in a lot of their examples, they include "aux.h". This always brings up an error in XCode saying that no file named "aux.h" exists. Does aux.h simply not exist on OS X? And if not, is there any subtitute? It's a little disheartening when every other example in the book doesn't work and I have no idea how to do it myself....



To quote from chapter 1 of the 1.1 RedBook (http://www.rush3d.com/reference/opengl-redbook-1.1/):

"_The OpenGL Programming Guide Auxiliary Library was written specifically for this book to make programming examples simpler and yet more complete. It's the subject of the next section, and it's described in more detail in Appendix E . Auxiliary library routines use the prefix aux. "How to Obtain the Sample Code" describes how to obtain the source code for the auxiliary library._"

Or try this first: http://www.opengl.org/resources/code/basics/redbook.zip


----------



## Viro (Aug 19, 2005)

Cool link. I didn't know the Red Book was available online . Thanks


----------



## Krevinek (Aug 29, 2005)

Yes, unfortunately MacOS X isn't always very good about doing 2D blitting through OpenGL. Quartz on the Mac goes through the 2D-optimized codepath of the video drivers,  (or used to before unifying the graphics system under OpenGL entirely), while OpenGL goes through the 3D-optimized codepath. 

Where OpenGL gets massive gains in 2D these days is though VRAM caching. Dumping your series of sprites you need onto the video card, and then simply drawing the sprite you need, where you need it as a quad is /very/ efficient on the CPU. With a bit of fore-thought on sprite layout in a texture store, you can get some gains that cannot be seen using Quartz under any circumstance (Quartz needs to be slightly generic, while your code doesn't have to be if it talks to OpenGL directly). Not to mention that you can easily apply certain effects to your sprites 'for free' such as tint, lighting, environment maps and the like.


----------



## Mikuro (Sep 25, 2005)

I just picked this up again after a few inactive weeks, and I've now discovered the magic of texture objects, which was not detailed in my downloaded copy of The Red Book (you can get a sample chapter from a newer edition of The Red Book that talks about that at the Red Book download page). I'm beginning to see exactly how I'll get the nice speed boosts I was hoping for.  I'm steadily becoming a fan of OpenGL.

I have one more quick question, though: Can anyone point me in the right direction for GLUT learning materials? I searched OpenGL.org but didn't find any documentation. I'm hoping for some online docs, but I could be persuaded to go buy some dead trees instead.


----------



## Viro (Sep 25, 2005)

The OpenGL Superbible is a great resource on learning OpenGL using glut. Sadly, it is only available in print.


----------

