Convertion of C++ Vector products to Blitz

Blitz3D Forums/Blitz3D Programming/Convertion of C++ Vector products to Blitz

TartanTangerine (was Indiepath)(Posted 2003) [#1]
Could someone please help with the converstion of the Vector and ray commands in to blitz3d.

Here is the C Code.

int intersect_map(const vector3& iv,const ray& r,Image* hm,float fHeightScale){
    int w,hits;
    float d,h,D;
    vector3 v,dir;

    v = iv + r.direction;
    w = hm->w;

    hits = 0;

    while (!(( v.x >= w-1 ) || ( v.x <= 0 ) || ( v.z >= w-1 ) || ( v.z <= 0 ))){
        D = Magnitude(vector3(v.x,0,v.z)-vector3(r.origin.x,0,r.origin.z)); // length of lightdir's projection
        d = Magnitude(iv-v); // light direction
        h = iv.y + (d*r.origin.y) / D; // X(P) point
        if (hm->data[ifloor(v.z)* w + ifloor(v.x)] * fHeightScale > h){ // check if height in point P is bigger than point X's height
            hits++; // if so, mark as hit, and skip this work point.
            break;
        };
        dir = r.direction;
        dir.y = 0;
        v += Normalize(dir); // fetch new working point
    };
    return hits;
};

Image* genLightmap(char* normal,Image* hm,vector3 fSunDir,int w,float fAmbient){
    int i,j,hits;
    float f,dot;
    vector3 n,fVertex;
    Image* lmap;
    ray r;

    float fHeightScale = 10.0f / 255.0f;
    lmap = new Image(w,w,1);
    if (!lmap){printf("(!) Error: cannot alloc lightmap!\n");return 0;};

    for (j=0; j<w; j++){
        for (i=0; i<w; i++){
            fVertex.x = i;
            fVertex.y = hm->data[j*w+i] * fHeightScale;
            fVertex.z = j;

            f = fAmbient ;

            r.origin = fVertex + fSunDir * 2000.0f;
            r.direction = fSunDir;

            if (!intersect_map(fVertex,r,hm,fHeightScale)){ // checks current working point for intersection
                // computing the lighting equation
                n.x = (float)(normal[3*(j*w+i)+0]); 
                n.y = (float)(normal[3*(j*w+i)+1]);
                n.z = (float)(normal[3*(j*w+i)+2]);
                f += 0.5f*(1.0f+DotProduct(Normalize(n),Normalize(fSunDir)));
                if (f>1.0f) f = 1.0f;
            };

            dot = f * 255.0f;
            lmap->data[j*w+i] = (unsigned char)dot;
        };
    };
    return lmap;
};




Miracle(Posted 2003) [#2]
I won't just do this for you :), but I will point you to the Code Archives for Chroma's Vector3D Library (Updated). That'll give you all the functions necessary to do the vector math your code chunk requires.

A Ray, incidentally, is a construct containing two Vectors named "origin" and "direction."


TartanTangerine (was Indiepath)(Posted 2003) [#3]
Thankyou Miracle. :)


TartanTangerine (was Indiepath)(Posted 2003) [#4]
Hey I worked it out without the use of the Vector lib.

Thanks anyway