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:: No Line Numbers


normalization cubemap by mrd
1
 
void texture::gennormcubemap(unsigned int facesize,int * numbound)
{    
    cubeid=*numbound;
    
    glGenTextures(1, &tex[cubeid]);
    glBindTexture(GL_TEXTURE_CUBE_MAP, tex[cubeid]);

    if (cubemap) delete cubemap;
        cubemap = new float [facesize*facesize*3];
    
    float offset = 0.5f;
    float halfsize = facesize / 2.0f;
    unsigned int x,y;
    CVector3 temp;
    unsigned int index = 0;

    for (unsigned char face=0; face<=6; face++)
    {
        for (y=0; y<facesize; y++)
        {
            for (x=0; x<facesize; x++)
            {
                switch (face)
                {
                    case 0: temp = CVector3( halfsize,y+offset-halfsize,-(x+offset-halfsize));        break;
                    case 1: temp = CVector3(-halfsize,y+offset-halfsize,  x+offset-halfsize );        break;
                    case 2: temp = CVector3( x+offset-halfsize,-halfsize, y+offset-halfsize);        break;
                    case 3: temp = CVector3( x+offset-halfsize,halfsize,-(y+offset-halfsize));        break;
                    case 4: temp = CVector3( x+offset-halfsize,y+offset-halfsize,halfsize);            break;
                    case 5: temp = CVector3( -(x+offset-halfsize),(y+offset-halfsize),-halfsize);    break;
                }
                
                temp = Normalize(temp);

                cubemap[index++] = (float)(temp.x);
                cubemap[index++] = (float)(temp.y);
                cubemap[index++] = (float)(temp.z);
            }
        }

        glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X+face,0, GL_RGB, facesize, facesize, 0, GL_RGB, GL_UNSIGNED_BYTE, cubemap);

        index=0;
    }

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    delete cubemap;
}