# Doubt on meshes

If we draw a mesh at one place and then draw it again somewhere else then why doesn’t the first mesh disappear and come at the second place. Here’s the code it’ll give you some idea of what I am trying to say. And why does the alpha of the wall keep changing its less in the begging and then for some angles it becomes 255 any reason for that??

``````
--# Main
function setup()
CreateImage()
w,h = i.width/2,i.height/2
m = mesh()
m.texture = i
m.vertices = {vec3(-w,-h,0),vec3(-w,h,0),vec3(w,h,0),vec3(w,-h,0),vec3(-w,-h,0),vec3(w,h,0)}
m.texCoords = {vec2(0,0),vec2(0,1),vec2(1,1),vec2(1,0),vec2(0,0),vec2(1,1)}
mf = mesh()
w,h = i.width,i.height
mf.texture = i
mf.vertices = {vec3(-w,-h,0),vec3(-w,h,0),vec3(w,h,0),vec3(w,-h,0),vec3(-w,-h,0),vec3(w,h,0)}
mf.texCoords = {vec2(0,0),vec2(0,1),vec2(1,1),vec2(1,0),vec2(0,0),vec2(1,1)}
X = 0
Z = 0
angle = 0
end

function draw()
background(255)
perspective(45,WIDTH/HEIGHT)
fill(255)
angle = angle+1
camera(X,0,Z,0,0,-1000,0,1,0)
pushMatrix()
translate(0,0,-1000)
m:draw()
popMatrix()
pushMatrix()
translate(100,0,-1100)
rotate(90,0,1,0)
m:draw()
popMatrix()
pushMatrix()
translate(-100,0,-1100)
rotate(90,0,1,0)
m:draw()
popMatrix()
pushMatrix()
translate(0,0,-1200)
m:draw()
popMatrix()
pushMatrix()
translate(0,-50,-1100)
rotate(90,1,0,0)
mf:draw()
popMatrix()
end

function CreateImage()
i = image(200,100)
local w,h = i.width,i.height
setContext(i)
rectMode(CORNER)
fill(255, 194, 0, 71)
rect(0,0,w,h)
fill(255, 0, 0, 255)
ellipse(w/2,h/2,h/2)
setContext()
end

``````

Think about a real world case. You are drawing on a t-shirt and you want to draw the same image several times, so you create a rubber stamp. Then you can put ink on the stamp, and press it on the shirt in different places, to make copies of the image.

In Codea, images and meshes are like the rubber stamps. They define what the image will look like, but they are not the image. When you draw or sprite onto the screen, you are putting paint on the screen using the image or mesh, but what is left on the screen when you are done is not the image or mesh itself, it’s just paint.

This is different to (say) normal Windows programming, where if you put an object on the screen, you can talk to it and change its properties. In Codea, you use tools to put paint on the screen, so if you move to six different places and draw your mesh at each of them, it is exactly like stamping an image on a t-shirt in six places.

Thanks @Ignatz, but I still didn’t understand why does the transparency keep on changing. You’ll notice it’s only one wall that is not completely transparent the other three are, though I am using the same image for all of them.

I’m not sure, to be honest

But you’re clearly getting the idea of drawing stuff in 3D, nice work!

Thanks!! @Ignatz

This is due to the interaction between z-buffering and transparency.

When OpenGL (in the guise of Codea) draws something on the screen, it saves the z-coordinate of the pixel. Then when a new thing is to be drawn, the new z-coordinate is compared with the saved one. If the new one is lower than the saved one, the assumption is that the new information is behind the existing one and thus occluded. It is therefore not drawn. Thus the actual order of drawing doesn’t matter: new stuff is only drawn if it is in front of the stuff already there.

Where this breaks down is with transparency. The system takes no notice of the transparency of the objects (for efficiency reasons). So if the new object is behind the current front object, but that current front object is transparent, it still doesn’t get drawn.

So your cube sides are transparent. It’s just that there’s nothing being drawn behind them to show that transparency because the stuff that ought to be behind is being drawn afterwards and therefore not drawn.

The solution to this is to sort the z-orders before drawing the meshes. This can get complicated when there are lots of meshes to do, but shouldn’t be hard with the code you have. Note that you only need to do this for the transparent parts of the image.

Thanks @Andrew_Stacey. Understood.

If you are drawing a box, this would seem to require that you have separate meshes for each wall, so you can sort them.

Why is it that if I make a mesh using a custom image having width or height greater than 2048 then codea is unable to draw it properly is it like the limit to the width and height of images because my iPads resolution is 2048 by ‘something’? Here’s the code, I am not able to set Boolean to change image width or height cause everything’s done in setup so you’ll have to change it yourself. Just Change any width or height from 2048 to 2049 on the line I’ve marked in setup function.

``````
--# Main
function setup()
imageFloor = image(2048,2048)-- change any to 2049
setContext(imageFloor)
pushStyle()
sprite("SpaceCute:Background",0,0,imageFloor.width,imageFloor.height)
popStyle()
Wall(0,0,0,imageFloor)
end

function draw()
background(21, 108, 209, 255)
perspective(45,WIDTH/HEIGHT)
camera(0,3000,600,0,50,-100,0,1,0)
pushMatrix()
translate(m.pos.x,m.pos.y,m.pos.z)
rotate(90,1,0,0)
m:draw()
popMatrix()
end

function Wall(x,y,z,i)
m = mesh()
fill(255)
m.texture = i
m.vertices = {vec3(0,0,0),vec3(i.width,0,0),vec3(0,i.height,0),
vec3(i.width,0,0),vec3(i.width,i.height,0),vec3(0,i.height,0)}
m.texCoords = {vec2(0,0),vec2(1,0),vec2(0,1),vec2(1,0),vec2(1,1),vec2(0,1)}
m.pos = vec3(x,y,z)
end
``````

I know I can make the size of the image double by changing the sprites width and height. But what’s the reason for this?

2048 is a built in limit. You just have to live with it…

Oh okay cool. Thanks @Jmv38

If you can wait a few days, I am going to post a solution to that on my blog, as part of my 3D series.

Okay thanks @Ignatz