DEFINITION  
    output = "pix/colchain";

    xleft = 0; xright = 323;
    ylow  = 0; yhigh  = 242;

    aalevelmin  = 1;
    aalevelmax  = 4;
    aawidth     = 1;
    aathreshold = 40.0;

    up = yaxis;
    grid_cache = 4000;

	triple CameraView(0, 0, 0);
    scalar _print_ret_val(0);
    scalar a(0);
    scalar atn(0);

    scalar ax(0);
    scalar ay(0);
    scalar az(0);

    scalar b(0);
    scalar bend(0);
    scalar bend_damping(0);
    scalar bond(0);

    scalar d(0);
    scalar damping(0);
    scalar dt(0);
    scalar dt2(0);

    scalar dvx(0);
    scalar dvy(0);
    scalar dvz(0);

    scalar dx(0);
    scalar dy(0);
    scalar dz(0);

    scalar dxb(0);
    scalar dyb(0);
    scalar dzb(0);

    scalar elasticity(0);
    scalar f(0);
    scalar floor_damping(0);
    scalar floor_thickness(0);

    array forcex[64];
    array forcey[64];
    array forcez[64];

    scalar fram(0);
    scalar gravity(0);
    scalar last(0);
    scalar length(0);

    array mass[64];
    scalar mass_v(0);

    scalar next(0);
    scalar numint(0);
    scalar numpart(0);
    scalar other(0);
    scalar part(0);

    scalar r(0);
    scalar r2(0);
    scalar r3(0);
    scalar r5(0);
    scalar rad2(0);
    scalar radius(0);

    scalar scf(0);
    scalar sub(0);
    scalar timescale(0);
    scalar velocity(0);
    scalar vxmean(0);
    scalar vymean(0);
    scalar vzmean(0);

    array vx[64];
    array vy[64];
    array vz[64];

    scalar xmax(0);
    scalar xmin(0);
    scalar ymax(0);
    scalar ymin(0);

    scalar xmean(0);
    scalar ymean(0);
    scalar zmean(0);

    array x[64];
    array y[64];
    array z[64];

    scalar zangle(0);

startframe = 1.0;
endframe   = 300.0;
byframe    = 1.0;


shader Shader2 (
    model    = lambert,
    diffuse  = 0.7,
    color    = (25, 100, 30),
    transparency = 0.0,
    reflection_limit   = 0,
    refraction_limit   = 0,
    shadow_level_limit = -1,
    transparency_shade = 0.0
);

shader Shader1 (
    model    = lambert,
    diffuse  = 0.7,
    color    = (200, 200, 200),
    transparency = 0.0,
    reflection_limit   = 0,
    refraction_limit   = 0,
    shadow_level_limit = -1,
    transparency_shade = 0.0
);


ENVIRONMENT
#ifdef OLDSDL
    background = ramp(
        type = bg_linear, name = "bg_ramp",
            color_table = ( (0.0, (120, 120, 120) ),
                            (1.0, (40, 80, 140) )
        )
    );
#else
        background ( color = texture(
        procedure = Ramp,
        ramp_type = V_RAMP,
        interpolation = LINEAR_RAMP,
        u_wave = 0.0, v_wave = 0.0,
        noise = 0.0 ,
        noise_frequency = 0.5 ,
        hue_noise = 0.0 ,
        hue_noise_frequency = 0.5 ,
        sat_noise = 0.0 ,
        sat_noise_frequency = 0.5 ,
        val_noise = 0.0 ,
        val_noise_frequency = 0.5 ,
        ramp_color = ( 255.0, 0.0, 0.0),
        position = 0.0,
        ramp_color = ( 40.0, 80.0, 140.0),
        position = 1.0
        )
);
#endif

MODEL

{
    translate(0, 0, 16);
    light (model = ambient, color = (255, 255, 255),
        ambient_shade = 0);
}

{
    camera (
        eye   = (0, 0, 16),
        view  = CameraView,
        twist = 0,
        fov   = (100),
        viewport = (xleft, xright, ylow, yhigh),
        aspect   = 1.31627,
        near = 0.1,
        far = 10000
    );
}

/* Define the control parameters for the particles and the springs */
timescale = 2.2;
mass_v    = 1.0;
gravity   = (0.125 / (30.0));
velocity  = 0.005;
ymax      = 60.0;
ymin      = -5.4;
xmax      = 60.0;
xmin      = -60.0;
elasticity = 0.95;
radius    = 1.0;
rad2      = (radius * radius);
floor_damping = 0.1;
floor_thickness = (radius / 2);
bond      = 4.0;
damping   = 4.0;
bend      = 0.15;
bend_damping = 0.15;

a = bond;
b = (bond / (radius * radius));

numpart = 20;
numint  = 12;

dt = ((timescale * 1) / numint);
dt2 = ((0.5 * dt) * dt);

if ((frame == startframe))
    fram = 1.0;
else
    fram = frame;
for ( ; (fram < (frame + 1));fram = (fram + 1); )
{
    /* Define the initial conditions */
    if ((fram == 1.0))
    {
        _print_ret_val = srand( 0 );
        for (part = 0; (part < numpart); part = (part + 1); )
        {
            x[part] = (-0.0 + (radius * part));
            y[part] = 9.0;
            z[part] = 0.0;
            vx[part] = (velocity * rand());
            vy[part] = (velocity * rand());
            vz[part] = 0.0;
            mass[part] = mass_v;
            mass[part] = mass_v;
        }
    }

/* Loop for each subinterval of time */
    for (sub = 0; (sub < numint); sub = (sub + 1); )
    {
        for (part = 0; (part < numpart); part = (part + 1); )
        {
            forcex[part] = 0.0;
            forcey[part] = 0.0;
            forcez[part] = 0.0;
        }

/* Loop for each part to compute the effect of the collision forces */ 
        for (part = 0; (part < (numpart - 1)); part = (part + 1); )
        {
            for (other = (part + 1); (other < numpart); other = (other + 1); )
            {
                dx = (x[part] - x[other]);
                dy = (y[part] - y[other]);
                dz = (z[part] - z[other]);

                r2 = (((dx * dx) + (dy * dy)) + (dz * dz));
                if ((r2 < rad2))
                {
                    r = sqrt( r2 );
                    if ((r < 0.0001))
                        r = 0.0001;

                    r3 = (r * r2);
                    r5 = (r3 * r2);

                    scf = ((radius - r) / radius);

                    dvx = (vx[part] - vx[other]);
                    dvy = (vy[part] - vy[other]);
                    dvz = (vz[part] - vz[other]);

                    f = (((a / r5) - ((damping
                        * (((dvx * dx) + (dvy * dy)) + (dvz * dz))) / r2))
                        * scf);

                    forcex[part] = (forcex[part] + (f * dx));
                    forcey[part] = (forcey[part] + (f * dy));
                    forcez[part] = (forcez[part] + (f * dz));

                    forcex[other] = (forcex[other] - (f * dx));
                    forcey[other] = (forcey[other] - (f * dy));
                    forcez[other] = (forcez[other] - (f * dz));
                }
            }
        }

/* Loop for each part to compute the effect of the neighbour forces */
        for (part = 0; (part < (numpart - 1)); part = (part + 1); )
        {
            other = (part + 1);

            /* Compute the interparticle attraction */
            dx = (x[part] - x[other]);
            dy = (y[part] - y[other]);
            dz = (z[part] - z[other]);

            r2 = (((dx * dx) + (dy * dy)) + (dz * dz));
            if ((r2 < (4 * rad2)))
            {
                r = sqrt( r2 );
                if ((r < 0.0001))
                    r = 0.0001;

                r3 = (r * r2);
                r5 = (r3 * r2);

                scf = ((radius - (0.5 * r)) / radius);

                dvx = (vx[part] - vx[other]);
                dvy = (vy[part] - vy[other]);
                dvz = (vz[part] - vz[other]);

                f = ((((a / r5) - (b / r3)) - ((damping
                    * (((dvx * dx) + (dvy * dy)) + (dvz * dz))) / r2)) * scf);

                forcex[part] = (forcex[part] + (f * dx));
                forcey[part] = (forcey[part] + (f * dy));
                forcez[part] = (forcez[part] + (f * dz));

                forcex[other] = (forcex[other] - (f * dx));
                forcey[other] = (forcey[other] - (f * dy));
                forcez[other] = (forcez[other] - (f * dz));

                /* Compute the floor drag-factor term */
                if ((y[part] < (ymin + floor_thickness)))
                {
                    atn = ((floor_damping * ((ymin + floor_thickness)
                        - y[part])) / floor_thickness);
                    forcex[part] = (forcex[part] - (atn * vx[part]));
                    forcey[part] = (forcey[part] - (atn * vy[part]));
                    forcez[part] = (forcez[part] - (atn * vz[part]));
                }
            }
        }

        /* Loop for each particle computing the torques */
        for (part = 1; (part < (numpart - 1)); part = (part + 1); )
        {
            last = (part - 1);
            next = (part + 1);
            xmean = (((x[((last))] + x[part]) + x[((next))]) / 3);
            ymean = (((y[((last))] + y[part]) + y[((next))]) / 3);
            zmean = (((z[((last))] + z[part]) + z[((next))]) / 3);
            dx = (x[part] - xmean);
            dy = (y[part] - ymean);
            dz = (z[part] - zmean);
            d = sqrt( (((dx * dx) + (dy * dy)) + (dz * dz)) );
            dxb = (dx * bend);
            dyb = (dy * bend);
            dzb = (dz * bend);

            vxmean = (((vx[((last))] + vx[part]) + vx[((next))]) / 3);
            vymean = (((vy[((last))] + vy[part]) + vy[((next))]) / 3);
            vzmean = (((vz[((last))] + vz[part]) + vz[((next))]) / 3);
            dvx = (vx[part] - vxmean);
            dvy = (vy[part] - vymean);
            dvz = (vz[part] - vzmean);

            dxb = (dxb + (bend_damping * dvx));
            dyb = (dyb + (bend_damping * dvy));
            dzb = (dzb + (bend_damping * dvz));

            forcex[part] = (forcex[part] - dxb);
            forcey[part] = (forcey[part] - dyb);
            forcez[part] = (forcez[part] - dzb);

            dxb = (dxb * 0.5);
            dyb = (dyb * 0.5);
            dzb = (dzb * 0.5);

            forcex[((last))] = (forcex[((last))] + dxb);
            forcey[((last))] = (forcey[((last))] + dyb);
            forcez[((last))] = (forcez[((last))] + dzb);

            forcex[((next))] = (forcex[((next))] + dxb);
            forcey[((next))] = (forcey[((next))] + dyb);
            forcez[((next))] = (forcez[((next))] + dzb);
        }

        /* Loop for each particle applying the forces to the position */
        for (part = 0; (part < numpart); part = (part + 1); )
        {
            ax = (forcex[part] / mass[part]);
            ay = (-gravity + (forcey[part] / mass[part]));
            az = (forcez[part] / mass[part]);
            if (((part > 0) || (fram > 900)))
            {
                x[part] = ((x[part]+(vx[part] * dt)) + (ax * dt2));
                y[part] = ((y[part]+(vy[part] * dt)) + (ay * dt2));
                z[part] = ((z[part]+(vz[part] * dt)) + (az * dt2));

                vx[part] = (vx[part] + (ax * dt));
                vy[part] = (vy[part] + (ay * dt));
                vz[part] = (vz[part] + (az * dt));
            }

            /* Bounce off the floor and ceiling */
            if ((y[part] > ymax))
            {
                y[part] = (ymax + ((ymax - y[part]) * elasticity));
                vy[part] = (-vy[part] * elasticity);
            }

            if ((y[part] < ymin))
            {
                y[part] = (ymin + ((ymin - y[part]) * elasticity));
                vy[part] = (-vy[part] * elasticity);
            }

            if ((x[part] > xmax))
            {
                x[part] = (xmax + ((xmax - x[part]) * elasticity));
                vx[part] = (-vx[part] * elasticity);
            }

            if ((x[part] < xmin))
            {
                x[part] = (xmin + ((xmin - x[part]) * elasticity));
                vx[part] = (-vx[part] * elasticity);
            }
        }
    }
}

/* Loop for each object outputting the geometry */
for (part = 0; (part < (numpart - 1)); part = (part + 1); )
{
    dx = (x[(((part + 1)))] - x[part]);
    dy = (y[(((part + 1)))] - y[part]);
    length = sqrt( ((dx * dx) + (dy * dy)) );
    zangle = atan2d( dy, dx );
    translate (x[part], y[part], z[part]);
    rotate    ( zaxis, zangle);
    translate ((-0.5 * radius), 0, 0);
    scale     ((radius + length), radius, 1);
    {
        translate (0.5, 0, 0);
        scale     (5, 5, 5);
        patch (
	    doublesided = TRUE,
            shader = ( Shader1 ),
            subdivide = uniform,
            divisions = (1, 1),
            udegree = 3, vdegree = 3,
            uknots = (0, 0, 0, 1, 1,
                     1),
            vknots = (0, 0, 0, 1, 1,
                     1),
            cvs = ((cv( (-0.1,  -0.1, 0.0), 1.0),
                    cv( (-0.03, -0.1, 0.0), 1.0),
                    cv( (0.03,  -0.1, 0.0), 1.0),
                    cv( (0.1,   -0.1, 0.0), 1.0)
                ),
                (   cv( (-0.1,  -0.03, 0.0), 1.0),
                    cv( (-0.03, -0.03, 0.0), 1.0),
                    cv( (0.03,  -0.03, 0.0), 1.0),
                    cv( (0.1,   -0.03, 0.0), 1.0)
                ),
                (   cv( (-0.1,  0.03, 0.0), 1.0),
                    cv( (-0.03, 0.03, 0.0), 1.0),
                    cv( (0.03,  0.03, 0.0), 1.0),
                    cv( (0.1,   0.03, 0.0), 1.0)
                ),
                (   cv( (-0.1,  0.1, 0.0), 1.0),
                    cv( (-0.03, 0.1, 0.0), 1.0),
                    cv( (0.03,  0.1, 0.0), 1.0),
                    cv( (0.1,   0.1, 0.0), 1.0)
                )
            )
        ); /* end patch */
    }
}
{
    translate (0, (ymin - (0.5 * radius)), -0.0001);
    scale     (1000, 1000, 1000);
    translate (0, -0.1, 0);
    patch (
	doublesided = TRUE,
        shader = ( Shader2 ),
        subdivide = uniform,
        divisions = (1, 1),
        udegree = 3, vdegree = 3,
        uknots = (0.0, 0.0, 0.0, 1.0, 1.0,
                 1.0),
        vknots = (0.0, 0.0, 0.0, 1.0, 1.0,
                 1.0),

        cvs = ((cv( (-0.1,  -0.1, 0.0), 1.0),
                cv( (-0.03, -0.1, 0.0), 1.0),
                cv( (0.03,  -0.1, 0.0), 1.0),
                cv( (0.1,   -0.1, 0.0), 1.0)
            ),
            (   cv( (-0.1,  -0.03, 0.0), 1.0),
                cv( (-0.03, -0.03, 0.0), 1.0),
                cv( (0.03,  -0.03, 0.0), 1.0),
                cv( (0.1,   -0.03, 0.0), 1.0)
            ),
            (   cv( (-0.1,  0.03, 0.0), 1.0),
                cv( (-0.03, 0.03, 0.0), 1.0),
                cv( (0.03,  0.03, 0.0), 1.0),
                cv( (0.1,   0.03, 0.0), 1.0)
            ),
            (   cv( (-0.1,  0.1, 0.0), 1.0),
                cv( (-0.03, 0.1, 0.0), 1.0),
                cv( (0.03,  0.1, 0.0), 1.0),
                cv( (0.1,   0.1, 0.0), 1.0)
            )
        )
    ); /* end patch */
}
