Terrain* loadTerrain(const char* filename, float height) {
	Image* image = loadBMP(filename);
	Terrain* t = new Terrain(image->width, image->height);
	for (int y = 0; y < image->height; y++) {
		for (int x = 0; x < image->width; x++) {
			unsigned char color =
				(unsigned char)image->pixels[3 * (y * image->width + x)];
			float h = height * ((color / 255.0f) - 0.5f);
			t->setHeight(x, y, h);
		}
	}
	delete image;
	t->computeNormals();
	return t;
}
Esempio n. 2
0
//Loads a terrain from a heightmap.  The heights of the terrain range from
//-height / 2 to height / 2.
Terrain* loadTerrain(float width, float length) {
    //Build basic terrain
    Terrain* t = new Terrain(width, length);
    for(int y = 0; y < length; y++) {
        for(int x = 0; x < width; x++) {
            float min = -2.0f;
            float max =  3.0f;
            float h = 1.0f;//min + (float)rand()/((float)RAND_MAX/(max-min));
            t->setHeight(x, y, h);
        }
    }

    //squareStep(Terrain* t, int xStart, int yStart, int d, float height)
    int d = 4;
    int h = 3;
    for(int x = 0; x < t->length(); x += d) {
        for(int y = 0; y < t->width(); y += d) {
            //printf("Starting Squares\n");
            squareStep( t, x, y, d, h);
            //printf("Starting Diamonds\n");
            diamondStep(t, x, y, d, h);
            //d--;
            //h--;
        }
    }

    int numMount = rand() % 20 + 1;
    printf ("Making %d mountains.\n", numMount);
    for(int i = 0; i < numMount; i++) {
        //buildMountain(t);
    }

    int numRivers = rand() % 3 + 1;
    printf ("Making %d rivers.\n", numRivers);
    for(int i = 0; i < numRivers; i++) {
        //buildRiver(t);
    }

    int terPasses = 20;
    printf ("Generating terrain.\n");
    for (int i = 0; i < terPasses; i++) {
        //buildTerrain(t);
    }

    t->computeNormals();
    return t;
}
Esempio n. 3
0
//Loads a terrain from a heightmap.  The heights of the terrain range from
//-height / 2 to height / 2.
Terrain* loadTerrain(const char* filename, float height) {
    Image* image = loadBMP(filename);
    int  span=2;
    Terrain* t = new Terrain(image->width*span, image->height*span);
    for(int y = 0; y < image->height*span; y++) {
        for(int x = 0; x < image->width*span; x++) {
            unsigned char color =
                (unsigned char)image->pixels[3 * ((y%image->height) * image->width + x%image->width)];
            float h = height * ((color / 255.0f) - 0.5f);
            if(h<0)
                t->setHeight(x, y, 0);
            else
                t->setHeight(x, y, h);

        }
    }

    delete image;
    t->computeNormals();
    return t;
}