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


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);

    // set counter vars
    int i=0, j = 0;

    // do the top row (or the bottom, depending which way you bind it), except the corners
    // you could do the corners if you want, but it looks fine without them
    for(j=3; j < (SIZE*3)-3; j += 3)
    {
        texarray[j + (i*SIZE*3)] = (texarray[j + (i*SIZE*3)] + 
                                    texarray[j+3 + (i*SIZE*3)] + texarray[j-3 + (i*SIZE*3)] + 
                                    texarray[j + ((i+1)*SIZE*3)] +
                                    texarray[j+3 + ((i+1)*SIZE*3)] +
                                    texarray[j-3 + ((i+1)*SIZE*3)]) / 6.0f;
        
        texarray[j+1 + (i*SIZE*3)] = (texarray[j+1 + (i*SIZE*3)] + 
                                      texarray[j+4 + (i*SIZE*3)] + texarray[j-2 + (i*SIZE*3)] + 
                                      texarray[j+1 + ((i+1)*SIZE*3)] +
                                      texarray[j+4 + ((i+1)*SIZE*3)] +
                                      texarray[j-2 + ((i+1)*SIZE*3)]) / 6.0f;

        texarray[j+2 + (i*SIZE*3)] = (texarray[j+2 + (i*SIZE*3)] + 
                                      texarray[j+5 + (i*SIZE*3)] + texarray[j-1 + (i*SIZE*3)] + 
                                      texarray[j+2 + ((i+1)*SIZE*3)] +
                                      texarray[j+5 + ((i+1)*SIZE*3)] +
                                      texarray[j-1 + ((i+1)*SIZE*3)]) / 6.0f;
    }

    i=SIZE-1;

    // do the bottom row (or the top, depending which way you bind it), except the corners
    // you could do the corners if you want, but it looks fine without them
    for(j=3; j < (SIZE*3)-3; j += 3)
    {
        texarray[j + (i*SIZE*3)] = (texarray[j + (i*SIZE*3)] + 
                                    texarray[j+3 + (i*SIZE*3)] + texarray[j-3 + (i*SIZE*3)] + 
                                    texarray[j + ((i-1)*SIZE*3)] + 
                                    texarray[j+3 + ((i-1)*SIZE*3)] +
                                    texarray[j-3 + ((i-1)*SIZE*3)]) / 6.0f;

        texarray[j+1 + (i*SIZE*3)] = (texarray[j+1 + (i*SIZE*3)] + 
                                      texarray[j+4 + (i*SIZE*3)] + texarray[j-2 + (i*SIZE*3)] + 
                                      texarray[j+1 + ((i-1)*SIZE*3)] + 
                                      texarray[j+4 + ((i-1)*SIZE*3)] +
                                      texarray[j-2 + ((i-1)*SIZE*3)]) / 6.0f;

        texarray[j+2 + (i*SIZE*3)] = (texarray[j+2 + (i*SIZE*3)] + 
                                      texarray[j+5 + (i*SIZE*3)] + texarray[j-1 + (i*SIZE*3)] + 
                                      texarray[j+2 + ((i-1)*SIZE*3)] + 
                                      texarray[j+5 + ((i-1)*SIZE*3)] +
                                      texarray[j-1 + ((i-1)*SIZE*3)]) / 6.0f;
    }

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

            texarray[j + (i*SIZE*3)] = (texarray[j + (i*SIZE*3)] + 
                                        texarray[j+3 + (i*SIZE*3)] + texarray[j-3 + (i*SIZE*3)] + 
                                        texarray[j + ((i+1)*SIZE*3)] + texarray[j + ((i-1)*SIZE*3)] + 
                                        texarray[j+3 + ((i+1)*SIZE*3)] + texarray[j+3 + ((i-1)*SIZE*3)] +
                                        texarray[j-3 + ((i+1)*SIZE*3)] + texarray[j-3 + ((i-1)*SIZE*3)]) / 9.0f;

            texarray[j+1 + (i*SIZE*3)] = (texarray[j+1 + (i*SIZE*3)] + 
                                          texarray[j+4 + (i*SIZE*3)] + texarray[j-2 + (i*SIZE*3)] + 
                                          texarray[j+1 + ((i+1)*SIZE*3)] + texarray[j+1 + ((i-1)*SIZE*3)] + 
                                          texarray[j+4 + ((i+1)*SIZE*3)] +  texarray[j+4 + ((i-1)*SIZE*3)] +
                                          texarray[j-2 + ((i+1)*SIZE*3)] +  texarray[j-2 + ((i-1)*SIZE*3)]) / 9.0f;

            texarray[j+2 + (i*SIZE*3)] = (texarray[j+2 + (i*SIZE*3)] + 
                                          texarray[j+5 + (i*SIZE*3)] + texarray[j-1 + (i*SIZE*3)] + 
                                          texarray[j+2 + ((i+1)*SIZE*3)] + texarray[j+2 + ((i-1)*SIZE*3)] + 
                                          texarray[j+5 + ((i+1)*SIZE*3)] + texarray[j+5 + ((i-1)*SIZE*3)] +
                                          texarray[j-1 + ((i+1)*SIZE*3)] + texarray[j-1 + ((i-1)*SIZE*3)]) / 9.0f;
        }
    }


    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);
}