Howdy, Stranger!

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

Adding texture to a single rectangle mesh

edited December 2012 in Questions Posts: 9

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
cardMesh:addRect(50,50,100,100)

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

Tagged:

Comments

  • SimeonSimeon Admin Mod
    Posts: 5,412

    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.

  • Jmv38Jmv38 Mod
    edited December 2012 Posts: 3,297

    .@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...

  • SimeonSimeon Admin Mod
    edited December 2012 Posts: 5,412

    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:

        Vertices+Colors+Textures+Normals
        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.

  • Jmv38Jmv38 Mod
    Posts: 3,297

    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.

  • edited December 2012 Posts: 666

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

  • Awesome! Thanks for the explains!

Sign In or Register to comment.