variable setCcontext image

Hi guys,

Was wondering if there is a solution to:

for i = 1, 10 do
for j = 1,10 do
sprite.. j*(i-1)+i = image(255,255)
setContext(sprite..j*(i-1)+i)

--code

setContext()
end
end

What is it meant to do?

@archistudent That looks similar to code I used to create a deck of cards. I had 52 individual cards with their value and suit image.

It intends to cut a large image (larger than 2048 pixels) into an X number of pieces, and then make them into individual 2048 images in void setup. Then in the draw loop it pieces them back together. The best anology may be what google maps does.

I think I may have solved it by making each piece then storing it into a table, then calling the table in draw loop.

void setup()

spritegroup = {}

for i = 1, 10 do
for j = 1, 10 do
img = image(2048,2048)
setContext(img)

--code
table.insert(spritegroup,img)
end
end

void draw()

for i = 1, 10 do
for i = 1, 10 do
sprite(spriteimage[(j*i)-1+i], i*2048,j*2048)
end
end

@dave1707 I was reading about the card code, it seems it is dynamically drawing the cards in void draw() ? My code needs the images to be pre-drawn in setup().

Okay since this is solved, my next objective is to solve pre-drawing up to 100 2048x2048 sprites using setContext() without crashing the app, perhaps an idea is to do 5 stacks at a time in multiple loops… does anyone have a better way?

You can only fit about one image of that size on the page, so how were you planning to draw 100 of them?

@Ignatz it works like google maps, once I have an array of maximum sized sprites, I use an algorithm to cycle through the list of sprites to show just the ones that should be on screen when I pan around.

Have you tried storing them to disk and loading the ones you need? It won’t be instantaneous, but nor is Google Maps.

Google maps is not caching 400 million pixels worth of satellite images. Nothing on your iPad is doing that.

I think we need more details about what you have in mind. Do these images have to be raster or could they be done with vectors? if they are raster, do they have to be 1-to-1 or can they be blown up? Is there no repetition, no way to tile them? Is there no way to generate the image, on the fly, as they are needed?

I appreciate the help everybody, I’ll try to explain my project as succinctly as I can.

I have the building footprints of a town, in total about 200 or so buildings, all as vector lines at the moment. This is currently represented as a long list of x1y1x2y2 end points of lines in a string (I also have a problem in calling such a long string, about 1500 lines, so I have broken them up into about 200 lines per call).

All these lines are called into different functions in the setup() that turns the lines into extruded buildings with roofs and balconies and so on… Then they are made into sprites using setContext. My knowledge of codea only extends this far so this is the method I am currently using, not to say it cannot be changed if there is a more efficient method.

The reason I am bringing in a long string of x1y1x2y2 lines is because I wanted to make the map variable to any town footprint. perhaps I can use my CAD software to ‘precalculate’ the resulting image, but I was challenging myself to see if I can do it purely in codea, which I think can be done.

My app displays this map of the town at an undetermined scale, I have a global scale value that I can change, which changes the resulting size of my town map. Therefore the town map can be 2k pixels wide or 20k pixels wide. For the final app, it would most likely be around 20k pixels for the buildings to be big enough to see.

If I have missed anything please feel free to raise a question.

@Ignatz I can store images to disk, that requires precalculating the images which i will do if the vectors are reasoned to be too inefficient.

Instead of extruding the lines as raster images, extrude them as 3D meshes. Then you can fly around your town. In my entry to the game jam last month I have a very simple extrude function that turns a list of vec2s in clockwise/anti-clockwise order (ie describing a polygon) into a shape with depth.

agreed, a mesh is the answer. My dungeon has 29 rooms and 5 million pixels but draws quickly.

I see, so rather than one large image of all the meshes rastered, it is better to have all the meshes instead. I already have the meshes.

If your CAD software can output waveform OBJ files or PLY files then @Ignatz wrote an OBJ importer and I hacked on a PLY importer to that (or if you just want to do very simple 2D, or pseudo 3D imaging, even a pdf could be useful. You won’t have access to the vector information of a pdf, but you could rasterize it, or project it on a 3D plane, or draw it lots of times to get an extruded effect. There was a thread at some point where someone, perhaps @Ignatz was creating 3d text by extruding a 2d image)

Thanks for all the suggestions. I’ll try the mesh method first, Each building footprint will be extruded with windows , doors , randomised simple textures, verandah, roof textures, randomised soft palette colour combinations, etc.

Wow, you’re going to do all of that procedurally, in Codea? Aiming high!

I suggest you use a tiling shader so that where you have large (or oddly shaped) surfaces, you can tile an image texture across them multiple times.

https://coolcodea.wordpress.com/2013/06/06/78-shaders-tiling-images/

This can be adapted easily to allow you to scale image textures, because it is unlikely all the textures you want to use are at the exact size you need. I can explain if you want to do this.

If you want to be able to get up close to the objects, I also recommend using texture images that are larger than you need initially, then when you zoom in, they won’t pixelate.

@yojimbo2000 yes I wrote a script that does it in CAD a few years ago for entire cities > 1000 buildings, I will use similar logic to create these, the problem will be managing memory etc

@Ignatz Thanks for the helpful tip, that might come in handy. since all the buildings meshes are defined from a group of single lines, the resulting meshes will always be a 4 sided shape. This will make the math easier. The part that the tiling script will really do work is the roofs with more undefined shapes, interesting…