Geometric primitives

“Codea needs triangles!” - Ignatz (3D Techniques in Codea)

Very new to 3D programming here, but looking to change that…

I understand that OpenGL has 10 types of geometric primitives (points, lines, line strip, line loop, triangles, triangle strip, triangle fan, quads, quad strip, polygon). So obviously points, lines, and triangles and all doable in Codea, but are all the others definable in Codea? If not, why not? I understand that triangles can basically do it all (I think, right?), but OpenGL must have them all for a reason.

Also, is all of OpenGL ES capabilities available through Codea somehow? Or, conversely, what 3D limitations will I eventually run into as I look to explore 3D programming with Codea?

Thanks as always!

Ok well, seems I may be answering some of my own question? Are the 10 primitive types only in OpenGL and not in OpenGL ES? And are they even in OpenGL 3.0 or have the quad types been deprecated? Again, should I just see triangles as all I need?

Maybe the more practical question that all this thinking has made me stumble upon is this: how do you draw a 3d line in Codea? The Codea line type is 2d…

Thanks!

I don’t think OpenGL ES 3 offers all that stuff.

To draw a line in 3D, you have at least four options

  1. Draw a long thin 3D box
  2. Draw a 2D line and angle it to face the camera
  3. Draw two 2D lines that start and end on the same place but which are at right angles to each other, ie they make a very long cross shape
  4. Draw the 3D scene, switch back to 2D drawing, then draw a 2D line

Option 1 is probably the easiest even if it seems clumsy
Option 2 is trickier but I have documented ways of doing this
Option 3 can give the illusion of 3D and doesn’t require angling toward the camera.
Option 4 is probably hardest because you have to figure out the screen positions of the ends of the line (and if you want the line to show perspective, ie shrink as it gets further away, this approach won’t work).

@iam3o5am Maybe the reason Codea doesn’t have all of the primitives was to save development time and program size. With triangles, anything else can be done. Here’s an example of drawing a line. Change the parameters for different lengths and angles. This would be option 1 above.

function setup()
    parameter.integer("angle",-360,360, -170)
    parameter.integer("length",10,1000,10)
    parameter.number("x",0,1,.5)
    parameter.number("y",0,1,.5)
    parameter.number("z",0,1,.5)
end

function draw()
    background(0, 0, 0, 255)
    perspective(50)
    camera(-500,0,0,0,0,0,0,1,0)
    rotate(angle,x,y,z)
    createLine(.2,.2,length)
end

function createLine(w,h,d)
    local lineSeg = mesh()    
    local v = { vec3(-w,-h,d),vec3(-w,h,d),vec3(w,-h,d),vec3(w,h,d),
                vec3(-w,-h,-d),vec3(-w,h,-d),vec3(xw,-h,-d),vec3(w,h,-d) }       
    lineSeg.vertices= { v[1],v[2],v[3],v[2],v[3],v[4],v[2],v[4],v[6],v[4],v[6],v[8],
                    v[1],v[2],v[5],v[2],v[5],v[6],v[3],v[4],v[7],v[4],v[7],v[8],
                    v[1],v[3],v[5],v[3],v[5],v[7],v[5],v[6],v[7],v[6],v[7],v[8] }                     
    lineSeg:setColors(255,255,255)
    lineSeg:draw()
end

Thank you! Great food for thought from both of you.

Here’s an example of using line and rotate to draw a 3D line. This is option 3 above, but instead of 2 lines at 90 degree, it’s 3 lines at 60 degrees. Parameter sw is strokewidth to change the line thickness.

function setup()
    parameter.integer("ex",0,60,33)
    parameter.integer("ey",0,30,9)
    parameter.integer("ez",-10,10,-5)
    parameter.integer("sw",1,10,3)
    lineCapMode(SQUARE)
end

function draw()
    background(0)
    perspective(50)
    camera(ex,ey,ez,0,0,0,0,1,0)
    strokeWidth(sw)    
    stroke(255, 0, 0, 255)
    rotate(0,1,0,0)
    line(0,0,-1000,0)    
    stroke(28, 255, 0, 255)
    rotate(60,1,0,0)
    line(0,0,-1000,0)
    stroke(0, 26, 255, 255)
    rotate(60,1,0,0)
    line(0,0,-1000,0)
end

Here is an example of option 1 in a game
https://coolcodea.wordpress.com/2015/07/31/219-world-of-warships-2-shooting/

This is how to do option 2
https://coolcodea.wordpress.com/2015/02/12/198-looking-at-objects-in-3d/

Here is an example of option 3
https://coolcodea.wordpress.com/2015/02/24/200-luke-skywalkers-nightmare/

This is how to do option 4
https://coolcodea.wordpress.com/2014/12/30/189-locating-the-2d-position-of-a-3d-point/