#### Howdy, Stranger!

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

# Pic v3 - December 14 2011 - flood fill added

Mod
edited December 2011 Posts: 1,557

Will upload this evening (no outgoing email from my ipad at work for reasons to nutty to go into).

Added 3D spheres. Screenshot:

Tagged:

## Comments

• Posts: 2,161

Fantastic! I should add these to my "balls on sticks" code to make the vertices more realistic.

• Mod
Posts: 1,557

Add to the Pic class (or just modify and include the code):

``````-- http://rosettacode.org/wiki/Draw_a_sphere
function Pic:sphere(radius, k, ambient)
local function normalize (vec)
len = math.sqrt(vec[1]^2 + vec[2]^2 + vec[3]^2)
return {vec[1]/len, vec[2]/len, vec[3]/len}
end

local light = normalize{30, -30, -50}

local function dot (vec1, vec2)
d = vec1[1]*vec2[1] + vec1[2]*vec2[2] + vec1[3]*vec2[3]
return d < 0 and -d or 0
end

local function m255(a, b)
local r = a * b
if (r>255) then r=255 end
return r
end

local i,x,j,vec,b
for i = math.floor(-radius),-math.floor(-radius) do
x = i + .5
for j = math.floor(-2*radius),-math.floor(-2*radius) do
y = j / 2 + .5
if x^2 + y^2 <= radius^2 then
vec = normalize{x, y, math.sqrt(radius^2 - x^2 - y^2)}
b = dot(light,vec) ^ k + ambient
--intensity = math.floor ((1 - b) * 255)
local ba = b+ambient
self.image:set(x+self.width/2, y+self.height/2,
m255(self.r, ba), m255(self.g, ba),
m255(self.b, ba), self.a)
--line = line .. (shades[intensity] or shades[1])
end
end
end
end
``````

Syntax is:

``````s = Pic(99, 99)
s:setcolor(255, 100, 0, 255)
s:sphere(45, 2, 0.1) -- radius, k, ambient light
sprite(s.image, WIDTH/2, HEIGHT/2)
``````
• Mod
Posts: 1,557
• Admin Mod
Posts: 5,193

I'm kind of scared that you might write a ray-tracer next, @Bortels.

• Posts: 88

@Bortels: maybe a stupid question: is it possible to create facets with your routines? Let's assume I do have a mesh grid, and I want to fill this grid with a certain color, or colormap. How can I use the image or pic class for doing that? I am currently working on a version using the scanline algorithm with normal line commands. This works, but is a bit slow (and still buggy...). I was thinking that using the image or pic class could do the job faster. How difficult would it be, if you want to update the shape of the bounding box e.g. Due to rotation?

• Mod
Posts: 1,557

You wouldn't get good 3d shapes from this; it's pretty much a sphere. And the filled poly stuff won't help, because there's no support for shading.

ray tracer, huh? :-)

• edited December 2011 Posts: 88

This is what I currently have.

http://crazyed.posterous.com/betabeta3dmeshv10

It's beta square, since it does not work perfectly yet. For closed surfaces there are rendering issues (swap between inner and outer side ...), and for open structures there is a specific angle alpha at about -7°, where you can see rendering effects. If you go further to -180° situations improves again. I have included a set of functions, which you might want to try.

Currently I have no idea where to seek the problems. The detection, which facet is showing top or bottom works so lala, say almost 90%. If somebody of the more experienced math guys could help me out here, it would be perfect!!! I guess this is still the crucial part in the whole code.

Maybe in one of the next Codea releases there will be support of such 3D plotting as well? Would be simply great!

@Bortels: this is the background of my question about using the fill polygon code of you. It's anyway fascinating, how such rendering works, and much work is behind all those beautiful math function drawings with colored facets ... Wish, I could crack the nut.

Can somebody tell me how to place a photo here? I made a screenshot, but I am not able to copy that here ... Any trick?

• Mod
Posts: 1,557

So - as I research ray tracing, I realize that that's what this sphere is, grossly oversimplified - it figures out the color at any point by comparing the vector of the viewer with the vector of the normal to the sphere at that point. NO REALLY.

Just sharing. :-)

• Posts: 273

@CrazyEd... Simeon posted the syntax for placing a photo in the forum earlier today in another thread...

``````![Image Name](url)

For example

![My Image](http://twolivesleft.com/myImage.png)
``````
• Posts: 88

@Bortels: would have been a miracle if it would work that simple ... Any idea how to improve? Or is the basic concept already crap?

• Posts: 88

@Blanchot: many thanks for this hint. But I fear I have not got the point. I have the screenshot in my photo archive on my iPad, so still the question how to post that.

• Admin Mod
Posts: 5,193

@CrazyEd you'll have to upload it somewhere, like posterous or twitpic (or your own server). Then you can get the image address and use that to embed it in a post here.

• Mod
Posts: 1,557

If you're running a recent iOS, just tweet it - it's built-in, and fast and easy.

• Posts: 88

@Bortels: do you intend of writing a Ray tracer code? Basically with my 3D mesh project I had had several issues, which I was not able to solve. First I am sure that the current 3D to 2D method is just an approximation. I have the feeling that the perspective is somewhat wrong. On the other side I wanted to use just azimuth and elevation as rotation angles, which means to kick away one go the three angles as defined in your cube code. Do you have any idea how a transform could look alike? Secondly I was thinking that calculating the normal of a plane as defined by three points would already give a good hint, if the facet is visible or not. As it seems for non-closed objects this gives some reasonable result, but I start to doubt, based on your statement about grossly simplification, that this is the correct way of doing that. For a ray tracer, I guess you also need the normal vector of a facet, or not? If you would like to introduce colored facets on your cube, how would you determine the visibility of a facet, and if the object is even more complex, detect, if a facet is hidden (either partly or completely) by another one?

@Simeon, @Bortels: thanks for the hints!

• Mod
Posts: 1,557

Me writing (or really, porting) a ray tracer would just be a stunt - I have no real interest in taking something like that to where it needs to be, or more correctly - no real time. For every project I publish, I have 2 that I got halfway done with and are sitting waiting for me to finish (really!). I did read up on it yesterday, found some good links - I'll try to post them later. My understanding is that you're basically correct - you trace the ray back to the object it hits, take a normal to that surface, and calculate the light intensity based on the angle between the ray and the normal of the surface.

• Mod
Posts: 1,557

Uploaded version 3 with flood fill

http://bortels.posterous.com/pic-v3

• Admin Mod
Posts: 5,193

Just to be clear my ray tracer comment was a joke

• Posts: 88

@Simeon: but it would be great to have such tools available. And Codea is such great platform which for sure would allow that. Might be that my 3D mesh routine is not 100% perfect, and it contains bugs, but I was impressed how fast in the end one can realize that. I simply would love to use Codea more and more for such kind of problems, I only need to learn more about the capabilities and possibilities of Codea. But it seems to me that you can do almost anything with Codea, if you just know how to do it. I guess I did not mention that yet, but thanks a lot for this app, Simeon!!!

• Mod
Posts: 1,557

Joke or not, I am still thinking of doing it, just to be awesome :-)

• Posts: 622

Just got a chance to download it. Another keeper. I can see much use for this.

• Posts: 88

Please find a screenshot of my first trials for a RayTracer ...

http://crazyed.posterous.com/raytracer

Unfortunately the rendering is really slow ... I have also taken Andrews particles project and replaced the ellipse() with my spheres. As long as the diameter is small, and number of spheres is little one can see some particle movement already ...

After I have figured out how to share the code, I will do that. Too sad that .codea files do not work anymore ... It would be simply great if someone could assist in further improving the code and allowing also multiple reflections, although I fear that this will be simply too slow.

Is there any possibility to get someday access to the GPU commands? As the Apps like RenderDuck show, the rendering can be really fast ...

• Mod
Posts: 1,557

Totally cool, crazyed!

In the pre .codea days, I favored gist and github. In particular, gist makes it easy for people to copy and paste code.

Ray tracing is supposed to be slow :-)

There's great enthusiasm for GLSL access - I know it's on TLL's radar. My impression was that there was interest, but they have other stuff in the pipeline (physics, native fonts, and this .codea kerfluffle) to get dealt with first. Fingers crossed.

• Posts: 88

What, if I simply place the code in here?

• Mod
Posts: 1,557

You could, but it means there's no easy way to select all. It also is harder to bookmark. If its short, here is fine.

• edited January 2012 Posts: 88

Tried to post the code as a .lua file at the same link as above (http://crazyed.posterous.com ). But it seems as if my editor placed fancy characters in it... Can't correct it now, since with my iPad I cannot correct the posterous page ...

• Mod
Posts: 1,557

The .lua file you posted looks ok to me - I'll try adding to my ipad later (my ipad has been kidnapped by little girls for a while...)

• Posts: 622

We could try some experiments with cut/paste in the forum.

I think if the first --comment has a blank line before the first function it makes iPad selection go wonky.

Other than that I've found if I click on a whitespace to the right of code contained in a pre statement it seems to select it all fine.

Sign In or Register to comment.