3d Sphere example?

So could I use that code you just posted on its own, or do I have to write a lot of other stuff in from the Jmv’s code too?

Nope, just use what I gave you

If you want tutorials and explanations, there are pages like
http://sol.gfxile.net/sphere/

From Stackoverflow:
"If there are M lines of latitude (horizontal) and N lines of longitude (vertical), then put dots at
(x, y, z) = (sin(Pi * m/M) cos(2Pi * n/N), sin(Pi * m/M) sin(2Pi * n/N), cos(Pi * m/M))
for each m in { 0, …, M } and n in { 0, …, N-1 } and draw the line segments between the dots, accordingly.

edit: maybe adjust M by 1 or 2 as required, because you should decide whether or not to count “latitude lines” at the poles"

Thanks for the tutorial, and the help @ignatz. If I learn anything interesting, I’ll be sure to share it.

@Progrmr235 Not sure if this is what you’re after. This calculates 2500 points to create a sphere and allows you to rotate the sphere.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    parameter.number("AngleX",0,1,.5)
    parameter.number("AngleY",0,1,.2)
    parameter.number("AngleZ",0,1,.33)
    tab={}  -- table of points
    r=50    -- radius of sphere
    M=50
    N=50
    -- calculate 2500 points of a sphere
    for n=0,N do
        for m=0,M do
            -- calculate the x,y,z point position
            x=r * math.sin(math.pi * m/M) * math.cos(2*math.pi * n/N)
            y=r * math.sin(math.pi * m/M) * math.sin(2*math.pi * n/N)
            z=r * math.cos(math.pi * m/M)
            -- create a small triangle for each point
            table.insert(tab,vec3(x,y,z))
            table.insert(tab,vec3(x,y+1,z))
            table.insert(tab,vec3(x+1,y,z))
        end
    end
    sphere = mesh()
    sphere.vertices = { unpack(tab) }
    sphere:setColors(0,255,255)           
end

function draw()  
    background(40, 40, 50)
    perspective(2,WIDTH/HEIGHT)
    camera(0,0,-2000,0,0,0,0,1,0)
    rotate(70,AngleX,AngleY,AngleZ)
    scale(.5,.5,.5)
    sphere:draw()
end

Actually @Dave1707, I was thinking of making a solid sphere, but this will definitely help me understand how spheres work. Thanks.

@dave1707 Your triangles don’t look as if they match up. The vertices of one don’t look as if they agree with the vertices if their neighbour. Is that intentional?

@Progrmr235 Solid is “impossible”. But it’s hard from what you’ve said to see what it is you actually want. Maybe if you could be more precise, you’d get more directed help.

@LoopSpace What I was trying to do is plot a point at each of the calculated positions to show a 3D sphere, but the only way I could think of doing that was to create a small triangle at each point and color it. I wasn’t trying to connect one point to it’s surrounding points to create the skin, at least not in this example. @Progrmr235 This example was just the start of your sphere. The next step is to connect three of the calculated points so you can fill in that triangle. Then you connect another 3 points and keep doing that until you created a skin over the sphere. Each of those triangles could be a different color or texture. I haven’t figured out an easy way to do that yet, but I’m thinking about it. I haven’t played around with 3D that much, so I’m learning too.

@Progrmr235 I worked on my example and was able to create the skin ( random colors) on the sphere. I try to use the least amount of code for examples to make it easier to understand what’s happening.


--# Main

supportedOrientations(LANDSCAPE_ANY)

function setup()
    parameter.number("view",1.5,10,3)
    parameter.integer("level",10,200,12)
    parameter.action("new sphere",setup1)
    parameter.number("AngleX",0,360,90)
    parameter.number("AngleY",0,360,140) 
    parameter.number("AngleZ",0,360,90)
    setup1()
end

function setup1()    
    tab={}  -- table of points
    r=50    -- radius of sphere
    M=level
    N=level
    for n=0,N do
        tab[n]={}
        for m=0,M do
            -- calculate the x,y,z point position
            x=r * math.sin(math.pi * m/M) * math.cos(2*math.pi * n/N)
            y=r * math.sin(math.pi * m/M) * math.sin(2*math.pi * n/N)
            z=r * math.cos(math.pi * m/M)
            -- 2 dimension table of sphere points
            tab[n][m]=vec3(x,y,z)
        end
    end
    sph={}
    for n=0,N-1 do
        for m=0,M-1 do
            -- loop thru sphere table to create a rectangle
            -- create 2 triangles from 4 points of a rectangle
            -- create 1st triangle of the rectangle
            table.insert(sph,tab[n][m])
            table.insert(sph,tab[n][m+1])
            table.insert(sph,tab[n+1][m+1])  
            -- create 2nd triangle of a rectangle        
            table.insert(sph,tab[n][m])
            table.insert(sph,tab[n+1][m])
            table.insert(sph,tab[n+1][m+1])
        end
    end
    -- create table of random colors for each triangle
    cols={}
    for z=1,#sph,6 do
        col=vec4(math.random(),math.random(),math.random(),1)
        for q=1,6 do
            table.insert(cols,col)
        end
    end
    -- create mesh
    sphere=mesh()
    sphere.vertices=sph
    sphere.colors=cols    
end

function draw()  
    background(40, 40, 50)
    perspective(view,WIDTH/HEIGHT)
    camera(2000,0,0,0,0,0,0,1,0)
    rotate(AngleX,1,0,0)
    rotate(AngleY,0,1,0)
    rotate(AngleZ,0,0,1)
    sphere:draw()
end

Here’s a project that I wrote which has some sphere’s in it.

https://gist.github.com/loopspace/5937499

@LoopSpace Nice demo. There’s a lot of things in there to learn about. @Progrme235 was after something simple, just a sphere, which is why I made my demo as simple as possible. There’s a lot that I don’t know about 3D yet, and find looking thru your code kind of hard to understand, but it will be a great example to dissect later on.

Thanks guys, @LoopSpace, I what I meant by “solid” was more like the illusion of solid

Like this
http://commons.wikimedia.org/wiki/File:Sphere-with-blender.png

I know it’s not really solid because it’s nothing more then photons. And @dave1707 you are right, I simply want a example code to give me a starting point in my learning. Thanks guys, you have been a great help

I meant that solid is impossible because you only ever see the surface. So there’s no difference between a solid sphere and a hollow one. My code above gives proper spheres like that image.

Again when I said “solid” I meant the sphere only has the appearance of solid. I agree that I couldn’t make an actual solid sphere on any computer

@Progrmr235 That wasn’t using “photons” or anything, that was a normal triangulated sphere (with a lot of vertices, or a large subsurf modifier) and some basic lighting, with some antialiasing. Closest you could get to “photons” would be volumetric rendering, but that would be very slow if not impossible in Codea.

I think we’re all agreed that Codea creates a wireframe mesh which is then textured to create the impression of a sphere.