Howdy, Stranger!

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

3D cylinder?

edited July 2013 in General Posts: 1,595

Ive just started 3D, bit late I know but it seems easy enough, I'm just stuck on making cylinders, I know how to create the vertices but not how to triangulate in 3d (dont think thats possible) so it will end up with missing triangles in it, does anyone have a class or a way of making the vertices? Thanks.

Comments

  • Posts: 2,161

    (Untested)

    height = 100
    radius = 20
    corners = {{0,0},{1,0},{1,1},{0,0},{0,1},{1,1}}
    vertices = {}
    hstep = 10
    astep = 20
    for h=hstep,height,hstep do
      for a=astep,360,astep do
        for _,v in ipairs(corners)
          table.insert(vertices,vec3(radius*math.cos(a - astep*v[1]),
                                                 radius*math.sin(a - astep*v[1]),
                                                 h - hstep*v[2]))
        end
      end
    end
    
  • Jmv38Jmv38 Mod
    Posts: 3,295

    You will find some examples for sphere and cilinder there: http://jmv38.comze.com/CODEAbis/server.php in the 8th project

  • Posts: 743

    @Luatee here's a demo of cylinders as an additional tab to the 3D lab. You need to change line 7 in the main tab to

        availableTests = { Test2(), Test1(), Test3() }
    

    And copy the following into a new tab called Test3


    Test3 = class() function Test3:name() return "3D Cylinders" end function Test3:init() -- all the unique vertices that make up a cube local vertices={} local numfaces=24 local top=0.5 local bottom=-0.5 local r=0.5 --corners every second set of faces for face=1,numfaces do rot=face*math.rad(360/numfaces) adj=math.rad(180/numfaces)--was6 vertices[(4*face)-3]=vec3(r*math.sin(rot-adj), bottom, r*math.cos(rot-adj)) -- Left bottom front vertices[(4*face)-2]=vec3( r*math.sin(rot+adj), bottom, r*math.cos(rot+adj)) -- Right bottom front vertices[(4*face)-1]=vec3( r*math.sin(rot+adj), top, r*math.cos(rot+adj)) -- Right top front vertices[(4*face)]=vec3(r*math.sin(rot-adj), top, r*math.cos(rot-adj)) -- Left top front end local cubeverts = {} for face=1,numfaces do table.insert(cubeverts,vertices[(face*4)-3]) table.insert(cubeverts,vertices[(face*4)-2]) table.insert(cubeverts,vertices[(face*4)-1]) table.insert(cubeverts,vertices[(face*4)-3]) table.insert(cubeverts,vertices[(face*4)-1]) table.insert(cubeverts,vertices[(face*4)]) end local texv={} for i=0,numfaces-1 do table.insert(texv,vec2(i/numfaces,0)) table.insert(texv,vec2((i+1)/numfaces,0)) table.insert(texv,vec2(i/numfaces,1)) table.insert(texv,vec2((i+1)/numfaces,1)) end local cubetexCoords={} for face=1,numfaces do table.insert(cubetexCoords,texv[(face*4)-3]) table.insert(cubetexCoords,texv[(face*4)-2]) table.insert(cubetexCoords,texv[(face*4)]) table.insert(cubetexCoords,texv[(face*4)-3]) table.insert(cubetexCoords,texv[(face*4)]) table.insert(cubetexCoords,texv[(face*4)-1]) end -- now we make our 3 different block types self.ms = mesh() self.ms.vertices = cubeverts self.ms.texture = "Cargo Bot:Crate Yellow 3" self.ms.texCoords = cubetexCoords self.ms:setColors(255,255,255,255) self.md = mesh() self.md.vertices = cubeverts self.md.texture = "Cargo Bot:Crate Blue 3" self.md.texCoords = cubetexCoords self.md:setColors(255,255,255,255) self.mg = mesh() self.mg.vertices = cubeverts self.mg.texture = "Cargo Bot:Crate Red 3" self.mg.texCoords = cubetexCoords self.mg:setColors(255,255,255,255) -- currently doesnt work properly without backfaces self.mw = mesh() self.mw.vertices = cubeverts self.mw.texture = "Planet Cute:Water Block" self.mw.texCoords = cubetexCoords self.mw:setColors(255,255,255,100) -- stick 'em in a table self.blocks = { self.mg, self.md, self.ms } -- our scene itself -- numbers correspond to block positions in the blockTypes table -- bottom middle top self.scene = { { {3, 3, 0}, {2, 0, 0}, {0, 0, 0} }, { {3, 3, 3}, {2, 2, 0}, {1, 0, 0} }, { {3, 3, 3}, {2, 2, 2}, {1, 1, 0} } } end function Test3:draw() pushMatrix() pushStyle() -- Make a floor translate(0,-Size/2,0) rotate(Angle,0,1,0) rotate(90,1,0,0) sprite("SpaceCute:Background", 0, 0, 300, 300) -- render each block in turn for zi,zv in ipairs(self.scene) do for yi,yv in ipairs(zv) do for xi, xv in ipairs(yv) do -- apply each transform need - rotate, scale, translate to the correct place resetMatrix() rotate(Angle,0,1,0) local s = Size*0.25 scale(s,s,s) translate(xi-2, yi-2, zi-2) -- renders based on corner -- so -2 fudges it near center if xv > 0 then self.blocks[xv]:draw() end end end end popStyle() popMatrix() end function Test3:touched(touch) -- Codea does not automatically call this method end
  • Posts: 743

    Oh and you can change the numfaces parameter. The higher the number the smoother the cylinder. A value of 3 will give you a triangular column, 4 will give a rectangular one and so on. I've also left off the top and bottom of the cylinders

  • Posts: 1,595

    That worked like a charm, thanks @West, how would I go about adding the upper and lower faces

  • edited July 2013 Posts: 743

    @Luatee for the top, you need to add another vertex (in the vertices table) at the centre of your upper face then add the triangles to the cubeverts table, using this new point and two adjacent edges (the top face if you have set numfaces to 6 for a hexagon should look like this - http://3.bp.blogspot.com/_kl0df70KBgM/SnGWiF6Xw8I/AAAAAAAAADE/_-u4KE737i4/s1600-h/hexagon-with-lines.png)

    You then need to add the textures to the newly created triangles. This might get tricky depending upon your source image for the texture.

    The bottom is just the same process.

  • Posts: 1,595

    I did it another way, I created a 2d mesh the same shape and triangulated it then translated it to the bottom and top

Sign In or Register to comment.