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


Map by Andrew Davey
1
 
/***************************************************************************************
*    Everything below has been designed and created By Andrew Davey            *
*    They are all of the functions needed by the program to create a height map    *
*****************************************************************************************/


//============================================================
// v-- Loads the height data from a file.
//-------------------------------------------------
void map_loadHeightData(BYTE mapArray[MAP_WIDTH][MAP_HEIGHT]){
    
    unsigned int w =0, h=0, size = w*h;
    
    FILE *bmp = NULL;
        
    //read as binary
    bmp = fopen ("terrain.bmp","rb");
    
    //move to the width and height of the imag in the header
    fseek(bmp, 18, SEEK_SET);
    
    //set the width of the file to w
    fread(&w,4,1,bmp);

    //set the height of the file to h
    fread(&h,4,1,bmp);
    
    //set the size of the file
    size = w*h;
    
    //check to see if the file is right size
    if(w != MAP_WIDTH || h != MAP_HEIGHT){
        printf("file size incorrect, closing\n");
        fclose(bmp);
        exit(0);
    }//if
    
    //ignore the plane and stuff and skip to end of header
    fseek(bmp,28,SEEK_SET);

    //read in the pixel information
    fread(heightMapArray, size, 1, bmp);
    
    //create a border around the hieghtmap,
    //set all values on the first and last row to be 0 and the first and last olumn to be zero.
    for(n=0;n<(int)w;n++)
        for(i=0;i<(int)h;i++)
            if(n == 0||n == 1024||i == 0||i == 1024)
//                heightMapArray[n][i] = 0;
/*    
    //print array, check to see if the information is loading properly
    for(n=0;n<1024;n++){
        for(i=0;i<1024;i+=3) {
            printf("Values: %d\t%d\t%d\n",(int)heightMapArray[n][i], (int)heightMapArray[n][i+2], (int)heightMapArray[n][i+3]);    
        }
    }
*/

    //cloase the file
    fclose(bmp);    
    
    
}//map_loadHeightData(BYTE[][])

//--------------------------------------------------------
// ^-- Loads the height data from a raw file.  
// v-- draw the height map
//--------------------------------------------------------

void map_drawMap (){
    
    //set up some variables for rendering
    int row, column, x,y,z, switchSide;
    switchSide =1;
    
    //drawing the height map in quads.
    glBegin(GL_TRIANGLE_STRIP);
    
    for(row=0; row<=MAP_WIDTH; row+=QUAD_SIZE ){
        
        //checkto see if the oposite direction should get rendered.
        if(switchSide){
        
            for(column = MAP_HEIGHT; column >= 0; column -= QUAD_SIZE){
            
            //==============================================================
                        
                x = row;
                y = heightMapArray[row][column];
                z = column;

                map_setColour(heightMapArray[x][y]);
                                        
                glVertex3i( ( x * X_Z_MAG ), ( y * Y_MAG ), (z * X_Z_MAG) );
                        
            //==============================================================            
                
                x = row + QUAD_SIZE;
                y = heightMapArray[row + QUAD_SIZE][column];
                z = column;
                
                
                map_setColour(heightMapArray[x][y]);
                            
                glVertex3i( ( x * X_Z_MAG ), ( y * Y_MAG ), (z * X_Z_MAG) );
            
            //==============================================================
            }//for(column)        

        }else{//switchSide
            
            for(column =0; column <= MAP_HEIGHT; column += QUAD_SIZE){
            
            //==============================================================
                        
                
                x = row + QUAD_SIZE;
                y = heightMapArray[row+QUAD_SIZE][column];
                z = column;        


                map_setColour(heightMapArray[x][y]);
                                        
                glVertex3i( ( x * X_Z_MAG ), ( y * Y_MAG ), (z * X_Z_MAG) );
                
            //==============================================================            
                
                x = row;
                y = heightMapArray[row][column + QUAD_SIZE];
                z = column  + QUAD_SIZE;
                
                map_setColour(heightMapArray[x][y]);
                
                glVertex3i( ( x * X_Z_MAG ), ( y * Y_MAG ), (z * X_Z_MAG) );
            
            //==============================================================
            }//for(column)        
        
    }//switchSides

        //make render the other direction
        switchSide = !switchSide;
    }//for(row)

    glEnd();
        
}//map_drawMap()

//--------------------------------------------------------
// ^-- draw the height map
// v-- Calculate a colour for the vertex passed in
//--------------------------------------------------------

void map_setColour(BYTE height){
    
    unsigned char r,g,b;
    
    r= 0;
    g= 256 - height;
    b= 0;
    
    glColor3ub(r,g,b);

}//map_setColour(BYTE)


/****************************************************************************************
*    Everything above has been designed and created By Andrew Davey            *
*    They are all of the functions needed by the program to create a height map    *
*****************************************************************************************/