Quantum_Oli
Quantum_Oli

Reputation: 277

Cocoa and OpenGL: fastest way to draw array of rectangles

Background: I'm building a screensaver in Cocoa where some C code is generating a 2d array of values (between 0 and 1) representing amplitudes. I then want to display this on screen as a grid with the color of the cells representing the values in the array.

I started off using a loop with NSBezierPath which was too slow. Then I moved to Rect List which was faster, but still too slow. Now I am considering either nsbitmapimagerep or moving to OpenGL (which I believe will be quicker).

Question: My question therefore is, what is the best way using opengl in cocoa to draw a 2-d array of squares (typically 200x200) where the positions are fixed but the colors change (hopefully up to 30 times per second)?

Attempt so far: I guess I'm going to want to use glDrawArrays or something similar rather than looping glQUAD? Or maybe move to a Vertex Buffer Object (about which I am currently reading)?

Upvotes: 0

Views: 653

Answers (1)

Goz
Goz

Reputation: 62333

Using OpenGL you would just define your 2D array as a texture. You could then use a simple quad draw to draw that texture to the screen.

For best performance its advisable to keep 2 textures around. 1 texture is the one you are displaying, the other texture is the one you are updating. When you've finished the update swap them over. This will probably help as your big slow down will be the copying the texture data from main memory to the graphics memory over the PCIe bus. Obviously saving memory where you can will increase the speed of this transfer. Using a palette would provide a big win as you'd be uploading a fraction of the data...

Upvotes: 1

Related Questions