single_threaded_resizeable_old.c
// return lamberitan colour of sphere intersection
return lamberitan colour of sphere intersection
return lamberitan colour of sphere intersection
Uint32 lambert_trace(const vec rayorig, const vec raydir)
{
    float tnear = FLOAT_MAX;
    sphere* sphr = NULL;
    for(uint i = 0; i < MAX_SPHERES; i++)
    {
        float t0 = FLOAT_MAX, t1 = FLOAT_MAX;
        if(intersect(rayorig, raydir, &t0, &t1, &sphere_array[i]))
        {
            if(t0 < 0){t0 = t1;}
            if(t0 < tnear)
            {
                tnear = t0;
                sphr = &sphere_array[i];
            }
        }
    }
    if(sphr == NULL)
        return 0xFF00FF00;
    vec phit;
    vAdd(&phit, rayorig, raydir);
    vMulS(&phit, phit, tnear);
    vec nhit;
    vSub(&nhit, phit, (vec){sphr->x, sphr->y, sphr->z});
    vNorm(&nhit);
    vec lightpos;
    vAdd(&lightpos, rayorig, (vec){0.0f, 0.0f, 130.f});
    vec lightDirection;
    vSub(&lightDirection, lightpos, phit);
    vNorm(&lightDirection);
    const float angle = fMax(vDot(nhit, lightDirection), 0.f);
    return SDL_MapRGB(bb->format, (Uint8)(sphr->r*angle*255.f), (Uint8)(sphr->g*angle*255.f), (Uint8)(sphr->b*angle*255.f));
}
Uint32 lambert_trace ( const vec rayorig , const vec raydir ) { float tnear = FLOAT_MAX ; sphere * sphr = NULL ; for ( uint i = 0 ; i < MAX_SPHERES ; i ++ ) { float t0 = FLOAT_MAX , t1 = FLOAT_MAX ; if ( intersect ( rayorig , raydir , & t0 , & t1 , & sphere_array [ i ] ) ) { if ( t0 < 0 ) { t0 = t1 ; } if ( t0 < tnear ) { tnear = t0 ; sphr = & sphere_array [ i ] ; } } } if ( sphr == NULL ) return 0xFF00FF00 ; vec phit ; vAdd ( & phit , rayorig , raydir ) ; vMulS ( & phit , phit , tnear ) ; vec nhit ; vSub ( & nhit , phit , ( vec ) { sphr -> x , sphr -> y , sphr -> z } ) ; vNorm ( & nhit ) ; vec lightpos ; vAdd ( & lightpos , rayorig , ( vec ) { 0.0f , 0.0f , 130.f } ) ; vec lightDirection ; vSub ( & lightDirection , lightpos , phit ) ; vNorm ( & lightDirection ) ; const float angle = fMax ( vDot ( nhit , lightDirection ) , 0.f ) ; return SDL_MapRGB ( bb -> format , ( Uint8 ) ( sphr -> r * angle * 255.f ) , ( Uint8 ) ( sphr -> g * angle * 255.f ) , ( Uint8 ) ( sphr -> b * angle * 255.f ) ) ; }
return lamberitan colour of sphere intersection
return lamberitan colour of sphere intersection
// find intersection with a sphere // if there's no intersection return black // hit position // hit normal vector // light is always behind ray origin like a flashlight //const vec lightpos = (vec){0.0f, 0.0f, 130.f}; // return surface colour lambertian