Planet Terrain Generation

void Planet::genTerrain() {

float ** tmat = (float **)malloc(LAND_LENGTH * sizeof(float *));

for(int k = 0; k < LAND_LENGTH; k++)

tmat[k] = (float *)malloc(LAND_WIDTH * sizeof(float));

//Call the Perlin Noise Generator

Gen(tmat);

texture = -1;

tex = new textureImage;

tex->width = LAND_WIDTH;

tex->height = LAND_LENGTH;

tex->bpp = 24;

int imageSize = tex->width*tex->height*(tex->bpp/8);

tex->imageData = (GLubyte *)malloc(imageSize);

for(int r = 0; r<tex->height; r++) {

for(int c = 0; c<tex->width; c++) {

float red, g, b;

if(tmat[r][c] < 0.55f) {

red = 0.0f;

g = .2f;

b = 1.0f;

}

else if(tmat[r][c] < .75f){

red = (.75 – tmat[r][c])*4;

g = 1.0f;

b = 0.0f;

}

else {

red = 2.5*(tmat[r][c]-.3);

g = 1.0f;

b = 2.5*(tmat[r][c]-.3);

if(red > 1.0)

red = 1.0;

if(b > 1.0)

b = 1.0;

}

tex->imageData[r*tex->width*3 + c*3] = (GLubyte)(unsignedchar)(int)(red*255);

tex->imageData[r*tex->width*3 + c*3+1] = (GLubyte)(unsignedchar)(int)(g*255);

tex->imageData[r*tex->width*3 + c*3+2] = (GLubyte)(unsignedchar)(int)(b*255);

}

}

texfinished=true;

free(tmat);

//Since each planet’s terrain generation is run in a separate thread, allow the next planet to move forward

sem_post (&PLANET_GEN);

pthread_exit(NULL);

}

%d bloggers like this: