Ejemplo n.º 1
0
void RandomizeBodiesSplitData(NBodyConfig config, float* position_x, float *position_y, float *position_z, float *mass,
                         float* velocity_x, float *velocity_y, float *velocity_z, float* color, float cluster_scale, float velocity_scale, int body_count)
{
    if (color)
    {
        int v = 0;
        for (int i = 0; i < body_count; i++)
        {
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = (float) rand() / (float) RAND_MAX;
            color[v++] = 1.0f;
        }
    }
    switch (config)
    {
    default:
    case NBODY_CONFIG_RANDOM:
    {
        float scale = cluster_scale * std::max(1.0f, body_count / (1024.f));
        float vscale = velocity_scale * scale;

        int p = 0, v = 0;
        int i = 0;
        while (i < body_count)
        {
            float3 point;
            point[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            float lenSqr = dot3f(point, point);
            if (lenSqr > 1)
                continue;
            float3 velocity;
            velocity[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            velocity[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            velocity[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            lenSqr = dot3f(velocity, velocity);
            if (lenSqr > 1)
                continue;

            position_x[p] = point[0] * scale; // pos.x
            position_y[p] = point[1] * scale; // pos.y
            position_z[p] = point[2] * scale; // pos.z
            mass[p] = 1.0f; // mass

            velocity_x[v] = velocity[0] * vscale; // pos.x
            velocity_y[v] = velocity[1] * vscale; // pos.x
            velocity_z[v] = velocity[2] * vscale; // pos.x

            p++;
            v++;
            i++;
        }
    }
    break;
    case NBODY_CONFIG_SHELL:
    {
        float scale = cluster_scale;
        float vscale = scale * velocity_scale;
        float inner = 2.5f * scale;
        float outer = 4.0f * scale;

        int p = 0, v = 0;
        int i = 0;
        while (i < body_count)
        {
            float x, y, z;
            x = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            y = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            z = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;

            float3 point = { {x, y, z} };
            float len = normalize3f(point);
            if (len > 1)
                continue;

            position_x[p] =  point[0] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            position_y[p] =  point[1] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            position_z[p] =  point[2] * (inner + (outer - inner) * rand() / (float) RAND_MAX);
            mass[p] = 1.0f;

            x = 0.0f;
            y = 0.0f;
            z = 1.0f;
            float3 axis = { {x, y, z} };
            normalize3f(axis);

            if (1 - dot3f(point, axis) < 1e-6)
            {
                axis[0] = point[1];
                axis[1] = point[0];
                normalize3f(axis);
            }

            float3 vv = { {position_x[i], position_y[i], position_z[i]} };
            vv = cross3f(vv, axis);
            velocity_x[v] = vv[0] * vscale;
            velocity_y[v] = vv[1] * vscale;
            velocity_z[v] = vv[2] * vscale;

            p++;
            v++;
            i++;
        }
    }
    break;
    case NBODY_CONFIG_MWM31:       //////////////     Galaxy collision ////////////////////////////
    {
        float scale = cluster_scale;
        float vscale = scale * velocity_scale;
        float mscale = scale * scale * scale;

        std::ifstream *infile;

        switch (body_count)
        {
        case 16384:
            infile = new std::ifstream(GalaxyDataFiles[0]);
            break;
        case 24576:
            infile = new std::ifstream(GalaxyDataFiles[1]);
            break;
        case 32768:
            infile = new std::ifstream(GalaxyDataFiles[2]);
            break;
        case 65536:
            infile = new std::ifstream(GalaxyDataFiles[3]);
            break;
        case 81920:
            infile = new std::ifstream(GalaxyDataFiles[4]);
            break;
        default:
            printf("Numbodies must be one of 16384, 24576, 32768, 65536 or 81920.\n");
            exit(1);
            break;
        }

        int numPoints = 0;

        int p = 0;

        float pX, pY, pZ, vX, vY, vZ, bMass, bIDf;
        int bID;
        if (!infile->fail())
        {

            while (!(infile->eof()) && numPoints < body_count)
            {

                numPoints++;

                *infile >> bMass >> pX >> pY >> pZ >> vX >> vY >> vZ >> bIDf;

                bID = (int)bIDf;

                bMass *= mscale;

                position_x[p] = scale * pX;
                position_y[p] = scale * pY;
                position_z[p] = scale * pZ;
                mass[p] = bMass;
                velocity_x[p] = vscale * vX;
                velocity_y[p] = vscale * vY;
                velocity_z[p] = vscale * vZ;
                SetupGalaxyColor(p, color, bID);
                p++;
            }
        }
        delete infile;
    }
    break;
    case NBODY_CONFIG_EXPAND:
    {
        float scale = cluster_scale * std::max(1.0f, body_count / (1024.f));
        float vscale = scale * velocity_scale;

        int p = 0, v = 0;
        for (int i = 0; i < body_count;)
        {
            float3 point;

            point[0] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[1] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;
            point[2] = (float) rand() / (float) RAND_MAX * 2.0f - 1.0f;

            float lenSqr = dot3f(point, point);
            if (lenSqr > 1)
                continue;

            position_x[p] = point[0] * scale; // pos.x
            position_y[p] = point[1] * scale; // pos.y
            position_z[p] = point[2] * scale; // pos.z
            mass[p] = 1.0f; // mass
            
            velocity_x[v] = point[0] * vscale; // pos.x
            velocity_y[v] = point[1] * vscale; // pos.x
            velocity_z[v] = point[2] * vscale; // pos.x

            p++;
            v++;
            i++;
        }
    }
    break;
    }
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: damora/ivr
void initcamera()
{

    // initialize view (u,v,n) in world coordinates
    vrp.x = vrp.y = vrp.z = 0.0f;
    dir.x = 0.0f;
    dir.y = 0.0f;
    dir.z = -1.0f;
    up.x = 0.0f;
    up.y = 1.0f;
    up.z = 0.0f;

    // initialize viewvolume in image plane (assumes that imageplane reference point is 0,0,0)
    // adjust so we have same aspect ratio as window
    float fwidth = FRUSTUMWIDTH;
    float fheight = FRUSTUMHEIGHT;
    float aspect;
    if (viewport.w > viewport.h) {
        aspect = (float) viewport.w/viewport.h;
        fwidth = fwidth * aspect;
    }
    else {
        aspect = (float) viewport.h/viewport.w;
        fheight = fheight * aspect;
    }
    frustum.bottom = -fheight/2.0f;
    frustum.top = -frustum.bottom;
    frustum.left = -fwidth/2.0f;
    frustum.right = -frustum.left;

    // initialize eyepos origin
#ifdef ORTHO
    e.u = 0.0f;
    e.v = 0.0f;
    e.n = -10000.0f; 	//ORTHO
    frustum.near = -2.0f;
    frustum.far = 2.0f;
#else
    e.u = 0.0f;
    e.v = 0.0f;
    e.n = -5.0f;		// PERSPECTIVE
    frustum.near = 0.1f;
    frustum.far = 100.0f;
#endif

    // initialize viewvolume in image plane (assumes that imageplane reference point is 0,0,0)
    viewvolume.bottom = frustum.bottom;
    viewvolume.left = frustum.left;
    viewvolume.top = frustum.top;
    viewvolume.right = frustum.right;
    viewvolume.far = frustum.far;
    viewvolume.near = frustum.near;


    // set projection matrix
#ifdef ORTHO
    setortho(scale, viewvolume);
#else
    float fovy = 2.0f * atan(frustum.top/-e.n);
    fovy = fovy * DEGREES;

    setperspective(fovy, 1.0f, 0.1f, 100.0f);
#endif

    // initialize view (u,v,n) in world coordinates
    camera.r.x = vrp.x;
    camera.r.y = vrp.y;
    camera.r.z = vrp.z;
    camera.n.x = dir.x;
    camera.n.y = dir.y;
    camera.n.z = dir.z;
    camera.v.x = up.x;
    camera.v.y = up.y;
    camera.v.z = up.z;
    camera.v = normalize3f(camera.v);
    camera.u = cross3f(camera.n, camera.v);
    camera.e.u = e.u;
    camera.e.v = e.v;
    camera.e.n = e.n;

    // set view to world transform matrix
    setvwm(vwm, camera);

    // set world to view transform matrix
    setwvm(wvm, camera);

    // initialize eyepos origin
    eyepos.u = e.u;
    eyepos.v = e.v;
    eyepos.n = e.n;


    return;
}