Adding texture to a single rectangle mesh

Can someone explain to me why it matters whether you add a texture to the mesh BEFORE you define the vertices on that mesh, or AFTER? In the sample code below, copied directly from the wiki, if you swap the order of the .texture assignment and the addRect() call, you get nothing - a blank viewport. Yet defining the texture first works great.

img = readImage("Planet Cute:Icon")
cardMesh = mesh()    
cardMesh.texture = img

Can someone help me understand? Or is this just a bug or idiosyncrasy of the API?

It’s an API idiosyncrasy that has been changed in the next version of Codea (1.5).

The reason it behaves like this in Codea 1.4.6 is because the mesh doesn’t know you want to use textures until you assign something to its texture property.

So the mesh does not allocate a texture coordinate buffer, and thus addRect does not set up texture coordinates. This means that when you want to use an untextured mesh, it never unnecessarily allocates a texture coordinate buffer.

In Codea 1.5 we have changed mesh to always allocate all of its internal buffers regardless of whether you use textures or colours. This leads to more consistent behaviour at the expense of a very slight performance and memory penalty.

.@Simeon interesting. But why should there be an allocation if i do not use texture? We all understand that when we create something it takes longer, so creation has to take place in the setup for instance. If it was possible, why not allocate the required memory for a mesh field only at first request of this field? In some case i am at the limit of the ipad (eg butterflies), and i would like to have all the fps possible…

It’s true that it uses a bit more memory, but we felt the tradeoff wasn’t that bad.

We can compute the amount of memory a mesh will use (per-vertex) as follows:

Codea 1.4.6:

    Vertices only: vec3
    Vertices + Colors: vec3 + vec4
    Vertices + Textures: vec3 + vec2
    Vertices + Textures + Colors: vec3 + vec4 + vec2

Codea 1.5:

    Per vertex: vec3 + vec2 + vec4 + vec3

A vector is comprised of floats, which are 4 bytes each. So a vec3 is 3 * 4 = 12 bytes.

So in Codea 1.4.6, a mesh will use this many bytes per-vertex:

Codea 1.4.6:

    Vertices only: 12 bytes
    Vertices + Colors: 28 bytes
    Vertices + Textures: 20 bytes
    Vertices + Textures + Colors: 36 bytes

Codea 1.5

    Default: 48 bytes
    Goes up if you add additional shaders with "attribute" variables

So if we have 100 triangles in our mesh, that is 100 * 3 vertices. So 300 vertices. In 1.4.6 this would be around 6 to 11 KB (depending on features used), and in Codea 1.5 about ~14 KB.

So if we go up to 50,000 vertices we hit 1 - 2 MB (depending on features used) and 2.3 MB in Codea 1.5. It’s not a huge difference in memory, even for very large meshes.

Because Codea 1.5 uses a new buffer system for meshes, it might be possible for you to manually resize the buffers you are not interested in using. But I’m not sure if that will gain any noticeable performance improvements.

Ok, thanks for taking the time to explain.
If i use only vertice+colors then it is 28 against 48, nearly a x2 factor. But the good news is that i can manually resize that if i really nead it, so that’s fine.

…but if your butterflies have glowing particles emitted while the wings flap and they leave a trail, it’s probably worth the extra memory. :slight_smile:

Awesome! Thanks for the explains!