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 Black
void Terrain::createOverlayTexture(unsigned char *pOverlayData,Vector3 *pNormals,int size)
{
    //Terrain textures are generated real time
    unsigned char *tmp;
    tmp = new unsigned char[size*size*3];
    if(!tmp)
        return;
    if(!pNormals)
        return;
    int var = 0;
    for(int i=0;i<size;i++)
        for(int k=0;k<size;k++){
            if(m_pData->m_pVerts[(k)+(i)*m_nSizeX].y>240){
                tmp[(k+i*size)*3]    = 245;    
                tmp[(k+i*size)*3+1]    = 245;
                tmp[(k+i*size)*3+2]    = 245;
            }
            ///Should be water take water level into account and if there is water O_O
            else if(m_pData->m_pVerts[(k)+(i)*m_nSizeX].y<150){
                tmp[(k+i*size)*3]    = 0;    
                tmp[(k+i*size)*3+1]    = 0;
                tmp[(k+i*size)*3+2]    = 245;
            }
            else if(pNormals[(k)+(i)*m_nSizeX].y<0.975f){
                var = rand()%100+150;
                tmp[(k+i*size)*3]    = var;
                tmp[(k+i*size)*3+1]    = var;
                tmp[(k+i*size)*3+2]    = var;
            }else{
                var = 190+rand()%40;
                int choice = rand()%100;
                if(choice>30){
                    tmp[(k+i*size)*3]    = 0;    
                    tmp[(k+i*size)*3+1]    = var;
                    tmp[(k+i*size)*3+2]    = 0;
                }else if(choice<31){
                    
                    tmp[(k+i*size)*3]    = 171;    
                    tmp[(k+i*size)*3+1]    = 210;
                    tmp[(k+i*size)*3+2]    = 70;
                }
            }
        }
        
    if(!pOverlayData)
        return;
    memcpy(pOverlayData,tmp,sizeof(unsigned char)*size*size*3);
        
    int sumR,sumG,sumB;
    for(int i=1;i<size-1;i++)
        for(int k=1;k<size-1;k++){
            sumR = 0;
            sumG = 0;
            sumB = 0;
            
            sumR =    tmp[(k-1+(i-1)*size)*3] 
                +    tmp[(k+(i-1)*size)*3]                                                          
                +    tmp[(k+1+(i-1)*size)*3]    
                +    tmp[(k-1+i*size)*3]    
                +    tmp[(k+i*size)*3]
                +    tmp[(k+1+i*size)*3]
                +    tmp[(k-1+(i+1)*size)*3]
                +    tmp[(k+(i+1)*size)*3]
                +    tmp[(k+1+(i+1)*size)*3];
            
            sumG =    tmp[(k-1+(i-1)*size)*3+1]
                +    tmp[(k+(i-1)*size)*3+1]    
                +    tmp[(k+1+(i-1)*size)*3+1]
                +    tmp[(k-1+i*size)*3+1]
                +    tmp[(k+i*size)*3+1]    
                +    tmp[(k+1+i*size)*3+1]
                +    tmp[(k-1+(i+1)*size)*3+1]
                +    tmp[(k+(i+1)*size)*3+1]
                +    tmp[(k+1+(i+1)*size)*3+1];
            
            sumB =    tmp[(k-1+(i-1)*size)*3+2]
                +    tmp[(k+(i-1)*size)*3+2]
                +    tmp[(k+1+(i-1)*size)*3+2]
                +    tmp[(k-1+i*size)*3+2]
                +    tmp[(k+i*size)*3+2]
                +    tmp[(k+1+i*size)*3+2]
                +    tmp[(k-1+(i+1)*size)*3+2]
                +    tmp[(k+(i+1)*size)*3+2]
                +    tmp[(k+1+(i+1)*size)*3+2];
        
            if(sumR<sumB) sumR=sumB;

            pOverlayData[(k+i*size)*3]    = (int)(sumR /9);
            pOverlayData[(k+i*size)*3+1]= (int)(sumG /9);
            pOverlayData[(k+i*size)*3+2]= (int)(sumB /9);
    
        }
    g_TerrainTextures[0].createFromData(pOverlayData,size,3,GL_RGB);    
    delete []tmp;
}