Author Topic: How To Create A Segmented Cube  (Read 4336 times)

0 Members and 1 Guest are viewing this topic.

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
How To Create A Segmented Cube
« 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.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: How To Create A Segmented Cube
« Reply #1 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.
Challenge Trophies Won:

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: How To Create A Segmented Cube
« Reply #2 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.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: How To Create A Segmented Cube
« Reply #3 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.
Challenge Trophies Won:

Offline Xetick

  • Atari ST
  • ***
  • Posts: 132
  • Karma: 80
    • View Profile
    • Plane9
Re: How To Create A Segmented Cube
« Reply #4 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.
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won:

Offline Storm Trooper

  • C= 64
  • **
  • Posts: 45
  • Karma: 2
    • View Profile
Re: How To Create A Segmented Cube
« Reply #5 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.