# Screen shots of 3D stuff...setContext()

Hi, have been looking at 3D stuff, using the labs and @Ignatz’s tutorials…and have a few questions but will only ask one for the moment…if it’s answered elsewhere please direct me if you can - I have looked but can’t seem to find it…

The following code produces two ‘entwined’ tetrahedra as something to try to understand the sequence of 3D construction…

``````-- tetrahedron/stella octangular

function setup()
screen=image(WIDTH,HEIGHT)
parameter.number("Angle",0,360)

v={}

v[1]=vec3(-1000,-1000,-1000)
v[2]=vec3(1000,-1000,1000)
v[3]=vec3(1000,1000,-1000)
v[4]=vec3(-1000,1000,1000)

tetra1=mesh()
tetra1.vertices =  {v[1],v[2],v[3],
v[2],v[3],v[4],
v[3],v[4],v[1],
v[4],v[1],v[2]}

tetra1.texCoords=  {vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0)}

tetra1.texture=("Documents:floo")
tetra1:setColors (255,255,255,255)

p={}

p[1]=vec3(1000,1000,1000)
p[2]=vec3(-1000,1000,-1000)
p[3]=vec3(-1000,-1000,1000)
p[4]=vec3(1000,-1000,-1000)

tetra2=mesh()
tetra2.vertices =  {p[1],p[2],p[3],
p[2],p[3],p[4],
p[3],p[4],p[1],
p[4],p[1],p[2]}

tetra2.texCoords=  {vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0),
vec2(0,0),vec2(.5,1),vec2(1,0)}

tetra2:setColors (255,255,255,255)

end

function draw()

background(0)
perspective(100, WIDTH/HEIGHT)
camera(0,3000,-3000, 0,0,0, 0,1,0)
rotate(Angle,0,1,0)

--    setContext(screen)
tetra1:draw()
tetra2:draw()
--    setContext()

--    spriteMode(CENTER)
--    sprite(screen,WIDTH/2,HEIGHT/2)

end

``````

so obviously it draws okay…

You can also see that I’ve tried drawing the tetras onto a dummy “screen” to try and get an image copy so that I can then play around with that but keep failing…Any suggestions much appreciated.

@TheAbstractMan - Images drawn with setContext don’t render depth properly (technically, it doesn’t use a z buffer), which probably explains it.

This is a known problem.

Okay, thanks @Ignatz. So…is there a way of setting up the 3D view so that it is just occurring in a particular rectanglular area lets say in the top right hand corner of the screen…i.e. keeping exactly what’s on the screen at the moment but scaling it down and moving it, effectively providing a ‘frame’ or ‘window’ for it…as I was thinking that taking an image shot would effectively allow me to do this… and also lay it back into another 3D scene almost like an animated T.V. screen within the scene, or upon an object…If that makes any sense…

@TheAbstractMan - this link may help

http://twolivesleft.com/Codea/Talk/discussion/comment/37718

That’s spot on @Ignatz, many thanks.

@TheAbstractMan Setting the color of tetra1 to red and tetra2 to green makes it easier to see what’s happening.

Sorry, I realise I keep posting code using my own images, I should use the ones that are common and preview it first…will try to remember for future.

@TheAbstractMan Sorrry, I didn’t even notice the texture code. Normally there’s an error when it doesn’t find something. Apparently the texture didn’t matter. I just ended up with all white.

With the risk of rehashing things in other posts…am I being a complete bozo or is it not a straight forward task to draw a simple line in 3d space? I am playing around with the rotation command, and wish to illustrate the axis of rotation about which my 3d object is rotating…having drawn a 3d object I thought placing a line in would be child’s play…

Also I understand that a rotation defined as (angle,0,0,0) is not a valid defined axis but should it necessarily cause codea to crash? Seems a bit dramatic…

You cause an error if you try to rotate 3D space around nothing and codea doesn’t fool proof commands like that.

@TheAbstractMan - placing anything in 3D space is not child’s play. There is no such thing as a line. Everything is made up of triangles (even if you use addRect or sprite commands to draw flat 2D rectangles, they use triangles behind the scenes).

Yes my solution is just to place in a 1x1x10000 cuboid I was just wondering if I’d missed something obvious.

@TheAbstractMan Ignatz is right beyond creating triangulated shapes can be quite brain racking, if you’re interested here’s a good article: http://prideout.net/blog/?p=61

Thanks @Luatee, will study, looks fascinating.

I have a fair amount of code on CC for adding various shapes to meshes, including cylinders which I use to make axes in a 3D surface viewing program. Look for Library Utilities, and in there for MeshUtilities.