Skip to content
Tags

12. Meshes – what are they?

March 27, 2013

If you’ve been using Codea for any length of time, you’ll have seen references to meshes. But what are they?

2D drawing
Essentially, the sprite and mesh commands are just different ways of drawing to the screen. Underneath, both of them use the same basic approach, as you will see below. The main difference is that mesh can batch its drawing process, whereas sprite draws each image completely separately. So a mesh may be faster at drawing lots (10+) of images. You also need meshes to do more complex things like 3D and “shaders”.

However, meshes are a little more complex to set up, so if you are still starting out, don’t worry about them for the time being. Sprites will do fine until you begin programming that big starship game.

I’ll cover using meshes with 2D in a future post, but for now, here’s Simeon, in a forum post, with more detail on how sprites and meshes work. You don’t have to understand the technicalities, but you should see why mesh performs better if there are lots of images.

Sprite

Every sprite function call –

  • Looks up texture for “MySprite”
  • Allocates memory for six vertices
  • Assigns texture coordinates
  • Sets up OpenGL state <- can be expensive
  • Submits six vertices to OpenGL <- has a lot of overhead

Mesh
If we have a mesh:draw() call which draws 100 “MySprite” rects at once

Once only –

  • Look up texture for “MySprite”
  • Allocate memory for 100 * 6 vertices
  • Assign texture coordinates
  • Every draw call (mesh:draw())
  • Set up OpenGL state
  • Submit 100 * 6 vertices to OpenGL


So if you were to draw 100 sprites, you would have to do all five steps 100 times every draw call. There is a lot of overhead going back and repeating those steps for every sprite on the screen (especially when they are all the same texture). With a mesh, the first three steps happen once, when the mesh is created or modified.

Also, the most important part is that the mesh submits all its data to the GPU in one go — so doesn’t have to keep sending small batches of 6 vertices for every sprite you want to render. There is a lot of overhead in sending data to the GPU, it’s better to send as much as possible in one call.

3D drawing
When it comes to 3D drawing, there is only mesh, and it is all to do with triangles. You also have to learn some cinematography. I’ll cover this later.

Advertisement

From → animation

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 )

Connecting to %s

%d bloggers like this: