#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# An easy way to draw an image as an mesh.

edited April 2014 Posts: 7

Hey there, im Wies. Im pretty new to the forums although i had this account already when i first started using codea, i never used this account though, i probably havr codea for a year now and just been messing around with it from the start.
But to the point i was just messing and learning how to use meshes, its quite a pain but i think i got the basics going. I felt like it was quite a bit of code to create just one image, after a day i figured out that you can use the same mesh for everything, this made my stuff alot easier. Now i wanted an easy way of using a mesh to draw an image, since its alot more efficient and less cpu eating the the normal sprite(), so i was thinking of a function or a class, i found using a function a bit easier. So here is the function i wrote:

``````-- a function to draw images as a mesh and making your life easier!
-- img stands for the image you are going to draw on the screen.
-- x, y is the position it will be drawn. (NOTE: this is the center, not the corner of the image)
-- w, h are the width and the height, these are optional, if you dont enter this or put nil here it will read the default size of the image you choose.
-- r is the rotation of the image and is also optional, if not entered or you put nil there it will not affect your image.
function imageMesh(img, x, y, w, h, r)
-- here we test if w and h are entered, if not it wil take the default size of the image.
if w == nil and h == nil then
-- store the sizes of the image.
meshW, meshH = spriteSize(img)
else
-- use the inputted sizes.
meshW, meshH = w, h
end
-- check if there is a rotation inputted, if not set it to 0.
if r == nil then
r = 0
end
-- finally create the mesh.
drawingMesh = mesh()
-- create a rectangle with the inputted sizes and rotation.
-- give the rectangle the inputted image.
drawingMesh.texture = img
-- and finally draw your inputted image as a mesh in less then a milli-second!
drawingMesh:draw()
end
``````

and a clean version without all the useless tags: (on pastebin to avoid chaos)
http://pastebin.com/tAVVDiQQ

I know there may have already been something like this in the past, but i just wanted to share this with you guys.
And ofcourse, opinions or tips are always welcome!

Tagged:

• Posts: 425

• Mod
Posts: 5,396

@Wiesdendarm - unless the image is going to change, you should create the mesh just once, when the program starts.

Then put the drawing command drawingMesh:draw() in the draw function.

This is much faster.

• Posts: 7

Thanks @Ignatz, but the whole point of the function is to use it like you would use sprite(), else it wouldn't have much use as a function and you would need to create a function for every mesh you would want to create.

• Posts: 2,042

@Wiesswndarm, if you create a new mesh every draw, that is wasteful and it likely loses all advantage over sprite

• Posts: 7

@JakAttak, i'm pretty sure im not creating a new mesh, just resetting the original mesh.

• Posts: 425

Isn't sprite() just the same as using a mesh and the only advantage of a mesh being that you can use it instead of lots of sprite() calls

• Mod
Posts: 5,396

@Wiesendarm - nope, you're creating a new mesh continually, which is extremely inefficient. You only need to create it once, then you can draw it as often as you like. But there is no point redefining the same things, 60 times per second.

• edited April 2014 Posts: 7

@Coder A basic mesh is a triangle, not just an image. If you would take a 3D model out of a game (like skyrim or battlefield 4) and zoom in on it, you would see alot of triangles, these are meshes, these are not only used on 3D things though, they can be used for 2D things too! But if you would print a picture on a mesh (a triangle) you would only see a part of it, so you would need the other parts too to create a rectangle so you can view the full image. Codea is smart, it adds `addRect` wich instantly creates an rectangle created out of meshes. Still not convinced its better? Well `sprite()` does not have the ability to rotate images and add shaders too them!

Hope this helped, im not the best at explaining stuff and even i am still struggling with meshes.

@Ignatz, hmm, well i thought a mesh was just a table and you could manipulate the table, but codea would handle it as a "special" table so it could draw it. Also where does all the old information of the old mesh go then?

• Mod
Posts: 5,396

@Wiesdendarm - a sprite is also a mesh, and was included to make things easier for users who aren't used to meshes, or where you just want simple images. It is just as fast as using a mesh (even including batching behind the scenes to speed things up), unless you have a large number of images.

As I understand it, a mesh is an OpenGL object, not a table, and if you recreate it continually, you are destroying and recreating it all the time, which is wasteful.

As a result, your function is effectively an inefficient version of the sprite command.

I would generally stick to sprite unless you need the advanced functionality of meshes, such as for 3D drawing.