[Solved] Odd glitch wherein a mesh's texture changes

I’ve managed to introduce an unusual glitch into my 3D blast-em-up. I introduced a new class of object (a missile weapon). But what happens is that occasionally the texture of the missiles will switch to be the texture of the explosion mesh. Then, after a few seconds, it switches back to the correct texture. As a last resort I even tried putting self.mesh.texture = "Documents:missileTexture.jpg", immediately before each self.mesh:draw() in the missile class’ update loop, but it still glitches. How can the mesh be displaying with the wrong texture, even if I set the texture right before the draw call? Has anyone experienced a glitch like this before?

All of the meshes are defined and set up once only (verts, normals, texcoords, shaders etc), in the asset loading function, and are recycled for each entity in the game. ie every missile is drawn with the same missile mesh, each ship with the same ship mesh, by translating and rotating to the required position and setting the lighting shader appropriately (I’ve tried it without the shader and I get the same bug).

can you post a cut down version of the code that shows the problem?

PS why do you have “.jpg” on the end of the image name? Codea normally leaves that off.

I’ve experienced a similar thing a couple of times before when I’ve tried to use undefined images - I would double check the images - as @Ignatz suggests.

Struggling to recreate it though

Well, you’re both right, it was something to do with the way the image was being stored, and with the superfluous “.jpg” suffix. First, I substituted the missile model for one of the other ship models, and I didn’t see the glitch, so that confirmed it was an issue with the model.

I think the problem ultimately lies in my PLY extension to @Ignatz 's OBJ importer. It’s not truncating the extension name from the image file. Here is the line from @Ignatz 's code that I also need to add to the PLY importer:

                  if string.find(u[1],"%.") then

Initially, I’d imported the model as a PLY, then when I saw the glitch, I re-exported and imported it as an OBJ, but I forgot to delete the image (ie to force the OBJ importer to reimport it and store it correctly). The OBJ importer was able to see and apply the old image (the one with the superfluous suffix), but it rendered with weird and unpredictable glitches.

So I guess the moral is, make sure your assets don’t have superfluous suffixes?

I’ve fixed it for now by deleting the image and the model and reimporting it as an OBJ, thanks to you both for bringing my attention to the stray suffix.

If I have time to fix the PLY importer, I’ll update its gist. (Although, I suppose you could just not include the suffix in the ply text file? I.e. the syntax would be comment texture missileTexture [path] )