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


Proper blur by baldurk
void Init()
{
#define SIZE 128
    glGenTextures(2, texture);

    float texarray[3 * SIZE * SIZE];

    // generate a random texture
    for(int i=0; i < SIZE; i++)
    {
        for(int j=0; j < SIZE*3; j += 3)
        {
            texarray[j + (i*SIZE*3)] = float(rand()) / float(RAND_MAX);
            texarray[j + (i*SIZE*3) + 1] = float(rand()) / float(RAND_MAX);//texarray[j + (i*SIZE*3)];
            texarray[j + (i*SIZE*3) + 2] = float(rand()) / float(RAND_MAX);//texarray[j + (i*SIZE*3)];
        }
    }
    
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 0, GL_RGB, GL_FLOAT, texarray);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

    for(int q=0; q < reps; q++)
    {
         // set counter vars
        int i=0, j = 0;

        // do every pixel that has neighbours on all sides (N, NE, SW, etc)
        for(i=0; i < SIZE; i++)
        {
            for(j=0; j < (SIZE*3); j += 3)
            {
                if(j == (SIZE*3)-3 && i == SIZE-2)
                    continue;

                // RED
                float total = texarray[j + (i*SIZE*3)];
                float numpix = 1.0f;

                if(j <= (SIZE*3)-6)    { total += texarray[j+3 + (i*SIZE*3)];numpix += 1.0f; }
                if(j >= 3)          { total += texarray[j-3 + (i*SIZE*3)];numpix += 1.0f; }
                   if(i <= SIZE-2)        { total += texarray[j + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(i >= 2)          { total += texarray[j + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-6 && i <= SIZE-2) { total += texarray[j+3 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-6 && i >= 2) { total += texarray[j+3 + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 3 && i <= SIZE-2) { total += texarray[j-3 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 3 && i >= 2) { total += texarray[j-3 + ((i-1)*SIZE*3)];numpix += 1.0f; }

                texarray[j + (i*SIZE*3)] = total/numpix;


                // GREEN
                total = texarray[j+1 + (i*SIZE*3)];
                numpix = 1.0f;

                if(j <= (SIZE*3)-7) { total += texarray[j+4 + (i*SIZE*3)];numpix += 1.0f; }
                if(j >= 2)          { total += texarray[j-2 + (i*SIZE*3)];numpix += 1.0f; }
                if(i <= SIZE-2)     { total += texarray[j+1 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(i >= 2)             { total += texarray[j+1 + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-7 && i <= SIZE-2) { total += texarray[j+4 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-7 && i >= 2) { total += texarray[j+4 + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 2 && i <= SIZE-2) { total += texarray[j-2 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 2 && i >= 2) { total += texarray[j-2 + ((i-1)*SIZE*3)];numpix += 1.0f; }

                texarray[j+1 + (i*SIZE*3)] = total/numpix;

                // BLUE
                total = texarray[j+2 + (i*SIZE*3)];
                numpix = 1.0f;

                if(j <= (SIZE*3)-8) { total += texarray[j+5 + (i*SIZE*3)];numpix += 1.0f; }
                if(j >= 1)          { total += texarray[j-1 + (i*SIZE*3)];numpix += 1.0f; }
                if(i <= SIZE-2)        { total += texarray[j+2 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(i >= 2)          { total += texarray[j+2 + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-8 && i <= SIZE-2) { total += texarray[j+5 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j <= (SIZE*3)-8 && i >= 2) { total += texarray[j+5 + ((i-1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 1 && i <= SIZE-2) { total += texarray[j-1 + ((i+1)*SIZE*3)];numpix += 1.0f; }
                if(j >= 1 && i >= 2) { total += texarray[j-1 + ((i-1)*SIZE*3)];numpix += 1.0f; }

                texarray[j+2 + (i*SIZE*3)] = total/numpix;
            }
        }
    }

    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SIZE, SIZE, 0, GL_RGB, GL_FLOAT, texarray);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
}