d3module Z ソート

#include "d3m.hsp"

#module cube

#deffunc init_cube
    // 面の座標(4点)のパターン(4つ)
    dim pattern, 4, 4
    pattern.0.0 = 0, 0, 0, 0
    pattern.0.1 = 0, 1, 1, 0
    pattern.0.2 = 1, 1, 0, 0
    pattern.0.3 = 1, 1, 1, 1

    dim association, 3, 6
    // それぞれの面(6)のX,Y,Z(3)について、patternのインデックスを設定する
    // よい変数名がうかばない
    association.0.0 = 0, 1, 2
    association.0.1 = 3, 1, 2
    association.0.2 = 1, 0, 2
    association.0.3 = 1, 3, 2
    association.0.4 = 1, 2, 0
    association.0.5 = 1, 2, 3
    return

#deffunc cube_coordinate int face
    dup xs, pattern( 0, association( 0, face ) )
    dup ys, pattern( 0, association( 1, face ) )
    dup zs, pattern( 0, association( 2, face ) )
    return

#deffunc draw_cube int face
    cube_coordinate face
    d3square xs, ys, zs
    return

#defcfunc face_dist int face
    cube_coordinate face
    dx = 0.25 * ( xs.0 + xs.1 + xs.2 + xs.3 )
    dy = 0.25 * ( ys.0 + ys.1 + ys.2 + ys.3 )
    dz = 0.25 * ( zs.0 + zs.1 + zs.2 + zs.3 )

    // ダサい><
    dx += local_coordinate@.0 - camera@.0
    dy += local_coordinate@.1 - camera@.1
    dz += local_coordinate@.2 - camera@.2

    return d3dist( dx, dy, dz )

#deffunc cube_sorted_faces array faces
    dim faces, 6 // 面番号の配列
    ddim face_dists, 6 // 面のカメラとの距離
    repeat 6
        faces.cnt = cnt
        face_dists.cnt = face_dist( cnt )
    loop

    // バブルソート
    repeat 6
        i = cnt
        repeat 6 - i - 1
            j = cnt
            dup n, faces.j
            dup v, face_dists.j
            if( v < v.1 ) {
                tmp_n = n
                tmp_v = v
                n = n.1
                v = v.1
                n.1 = tmp_n
                v.1 = tmp_v
            }
        loop
    loop
    return

#global
init_cube

    gmode 3,,, 200
    repeat
        redraw 0
        camera = sin(0.01*cnt)*1.5, cos(0.01*cnt)*1.5, 1.0
        d3setcam camera.0, camera.1, camera.2
        local_coordinate = -0.5, -0.5, -0.5
        d3setlocal local_coordinate.0, local_coordinate.1, local_coordinate.2
        color : boxf
        cube_sorted_faces faces
        repeat 6
            i = faces.cnt
            hsvcolor i * 192 / 6, 255, 255
            draw_cube i
        loop
        redraw 1
        await 40
    loop

インフォメーション

公開日時
2008年2月7日 午前6時28分58秒
最終更新日時
2008年2月7日 午前7時45分52秒
カテゴリ
HSP