GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


sinedistort by _MrDutchy
rgbacol texture::getpix_bilfiltered(float xcoord, float ycoord)
{
    int ul_x = (int) floor(xcoord);
    int ul_y = (int) floor(ycoord);

    float fracU = xcoord-ul_x;
    float fracV = ycoord-ul_y;

    float   ul = (1.0f - fracU) * (1.0f - fracV);
    float   ll = (1.0f - fracU) * fracV;
    float   ur = fracU * (1.0f - fracV);
    float   lr = fracU * fracV;

    rgbacol returncol;

    returncol.r =    (unsigned char)
                    (ul * colarr[getpixeladress(TC_RGB,ul_x  ,ul_y)+0] + ur * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y)+0] +
                     ll * colarr[getpixeladress(TC_RGB,ul_x,ul_y-1)+0] + lr * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y-1)+0] * 255.0f);

    returncol.g =    (unsigned char)
                    (ul * colarr[getpixeladress(TC_RGB,ul_x  ,ul_y)+1] + ur * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y)+1] +
                     ll * colarr[getpixeladress(TC_RGB,ul_x,ul_y-1)+1] + lr * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y-1)+1] * 255.0f);

    returncol.b =    (unsigned char)
                    (ul * colarr[getpixeladress(TC_RGB,ul_x  ,ul_y)+2] + ur * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y)+2] +
                     ll * colarr[getpixeladress(TC_RGB,ul_x,ul_y-1)+2] + lr * colarr[getpixeladress(TC_RGB,ul_x+1,ul_y-1)+2] * 255.0f);

    return returncol;
}

void texture::sinedistort(texture * dest, float sin_xscale, float sin_yscale)
{
    for (unsigned int i=0; i<size_sqr; i++)
    {
        unsigned int x = i % size; // this is the dest pixel's x
        unsigned int y = i / size; // this is the dest pixel's y

        float x2; float y2;
        rgbacol tmpcol;

        x2 = x + (sin_xscale * sin((float) x));
        y2 = y + (sin_yscale * sin((float) y));

        tmpcol = getpix_bilfiltered(x2,y2);

        dest->colarr[dest->getpixeladress(TC_RGB,x,y)+0] = tmpcol.r;
        dest->colarr[dest->getpixeladress(TC_RGB,x,y)+1] = tmpcol.g;
        dest->colarr[dest->getpixeladress(TC_RGB,x,y)+2] = tmpcol.b;
    }
}