Quick question on meshes

I am starting to get a handle on meshes, but I have a couple of conceptual questions. It seems as though meshes have two main functions

  • 3D graphics, through the use of triangles and everything that goes with that
  • Faster 2D graphics, by adding rectangles to a mesh and then adding image based textures to them

First, is the ability to add rectangles purely aimed at 2D speed (it seems a bit odd to be adding a rectangle to a structure made up of triangles), or does it have some function in 3D as well? Or am I completely misunderstanding something?

My second question is about using a mesh to speed up 2D graphics. I can see how it works with rectangular images or shapes, but what if you’re drawing lines, ellipses or other odd shapes as well? How would you get them into a mesh, or even into a texture? To make it clearer, suppose I have this code in draw()

sprite(img,100,100)
ellipse(200,200,50)
line(400,450,500,550)

…and I want to do this with a mesh instead, having defined this in setup already

g=mesh()
g.texture="ImageName"

So in draw, I put

local d=g:addRect(100,100,img.width,img.height) --add image to mesh
g:setRectTex(d,0,0,1,1)
      --<< it's here I'm stuck. What do I do with the ellipse and line?
g:draw() --draw

You can first draw to an image, and then use that image as the texture for the mesh.

meshImage = image(500,500) -- size needed for your graphics
setContext(meshImage) -- draw to meshImage instead of to the screen
sprite(img,100,100)
ellipse(200,200,50)
line(400,450,500,550)
setContext()

m = mesh()
m.texture = meshImage

So you create the image in for example setup, and then just call m:draw() in the draw function. The reason why meshes are faster than sprite or rect is that you’ve done the preparations for drawing when you created the mesh, like loading the correct image and making it in a format ready for the graphics pipeline. Well, a simplified description atleast. :slight_smile:

But I would recommend starting with using sprite, rect, etc until you have a need for meshes, when your code gets to slow, or you need shader effects.

With regard to rectangles, then m:addRect(...) is just a shorthand because it’s quite common to want to add rectangles. It actually adds two triangles.

Another way to do ellipses and lines is to triangulate them and draw them with the mesh. A line can be simulated with two triangles as it is, in effect, a long rectangle. If you want anti-aliasing then it’s a little more complicated (or use a suitable shader). An ellipse can be triangulated, but you’d want to vary the number of triangles depending on the size of the ellipse to ensure that it still looks smooth round the circumference.

The Harmony drawing program uses these methods for its drawing. Lines and ellipses are just meshes. One reason is speed, another is that you get much more flexibility. You can define a line that changes colour (set the colours at the ends to be different), one that tapers, you can customise the joins, … all sorts of things.

So to sound a note of contrast to tnlogy, I would recommend getting your head round meshes early on and never looking back. I rarely use lines and ellipses any more.

I put a brief explanation of meshes in my shader examples: http://loopspace.mathforge.org/discussion/19/shader-examples/?Focus=57#Comment_57

Thank you both, I will get there with this, it’s just I’ve been programming in 2D for decades 8-X