Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Storm Trooper on May 17, 2011

Title: How To Create A Segmented Cube
Post by: Storm Trooper on May 17, 2011
Does anyone know how to create a segmented cube, by telling it x y and z amounts, and it based on being centred

This source example is semi based on 3d Blitz, I am in the stages of making it for FB. I thought it may come handy for others as well.

Solving it would be amazing!

Code: [Select]
type vertex
    as single x, y, z, u, v
end type


type triangle
    as vertex ptr v0, v1, v2
    as integer tex_num
end type


function create_seg_cube( byval segs_x as integer, byval segs_y as integer, byval segs_z as integer )
   
    dim as integer total_verts=segs_x * segs_y * segs_z
   
    dim as vertex ptr vert_index( 0 to total_verts-1 )
   
    dim as integer v0, v1, v2, v3
   
    for z as integer=0 to segs_z-1
        for y as integer=0 to segs_y-1
            for z as integer=0 to segs_x-1
               
                vert_index( vert_num )=add_vertex( model, xx, yy, zz, u, v ) : vert_num+=1
               
            next
        next
    next
   
    for z as integer=0 to segs_z-1
        for y as integer=0 to segs_y-1
            for x as integer=0 to segs_x-1
               
                v0=vert_calc
                v1=vert_calc
                v2=vert_calc
                v3=vert_calc
               
                add_triangle( model, vert_index( v0 ), vert_index( v1 ), vert_index( v2 ), texture )
                add_triangle( model, vert_index( v0 ), vert_index( v2 ), vert_index( v3 ), texture )
           
            next
        next
    next
   
end function

Thanks in advane,
Storm Trooper.
Title: Re: How To Create A Segmented Cube
Post by: hellfire on May 17, 2011
What exactly does "segmented cube" mean?
- a single cube with subdivided sides, or
- many small cubes building a larger one

For the latter you probably want to create separate cubes (ie not using shared vertices), so you can eg make the whole thing fall apart:
Code: [Select]
for x.. for y.. for z.. createCube(at position x,y,z with size=1)Shared vertices don't make much sense with cubes anyways as each side will have separate texture-coordinates and normal-vectors.
Title: Re: How To Create A Segmented Cube
Post by: Storm Trooper on May 17, 2011
It's a single cube with subdivided sides. I read somewhere a while ago, that this is how you make the rectangular objects in demos that are twisted / deformed.
Title: Re: How To Create A Segmented Cube
Post by: hellfire on May 17, 2011
Ok, then you would just create each side of the cube as a 2d grid.
You don't need the vertices "inside" the cube.
Title: Re: How To Create A Segmented Cube
Post by: Xetick on May 17, 2011
Easiest would probably be to start with one side as just a subdivided plane and then repeat that for each of the other 5 sides.
Title: Re: How To Create A Segmented Cube
Post by: Storm Trooper on May 19, 2011
thank you for the pointers. I don't have to work Saturday, and will crack on with figuring it out then.