Sign in to follow this  
Nibbles

Terrain Texture Generation - lowest regions are black

Recommended Posts

Hi, I've been tampering with generating textures for my terrain from it's heightmap following the guide over on flipcode (Terrain Texture Generation. It's working the way it's supposed to from what I've gotten out of the site. However I dont like how the lowest regions (where the heightmap is black) turns out black as well on the generated texture. Examples (btw, whats a good site to just host pictures for scenarios like this?)
public float TextureFactor(float h1, float h2, int num_textures)
{
    float percent;
    float inc = 255 / (float)num_textures;

    percent = (inc - Math.Abs(h1 - h2)) / inc;

    if (percent < 0.0f)
        percent = 0.0f;
    else if (percent > 1.0f)
        percent = 1.0f;

    return percent;
}

public void GenerateTexture()
        {
            int i;
            int x, y;
            int num_textures = 4;

            float[] tex_fact = new float[num_textures];
            Color[] OldC = new Color[num_textures];;

            Bitmap[] tex = new Bitmap[4];
            tex[0] = new Bitmap(f.s_appPath + "/Data/Textures/sand.bmp");
            tex[1] = new Bitmap(f.s_appPath + "/Data/Textures/grass.bmp");
            tex[2] = new Bitmap(f.s_appPath + "/Data/Textures/rock.bmp");
            tex[3] = new Bitmap(f.s_appPath + "/Data/Textures/snow.bmp");

            bmp_heightmap = new Bitmap(i_size, i_size);

            float[] region = new float[num_textures];

            int region_size = 255 / num_textures;
            for (i = 0; i < num_textures; i++)
            {
                region[i] = 0;
            }

            for (i = 0; i < num_textures; i++)
            {
                if (i == 0)
                    region[i] += region_size;
                else
                {
                    region[i] += region[i - 1] + region_size;
                }
            }

            for (y = 0; y < i_size; y++)
            {
                for (x = 0; x < i_size; x++)
                {
                    float h = GetHeightAtPoint(x, y);
                    float r = 0;
                    float g = 0;
                    float b = 0;

                    // read colors from all tiles
                    for (i = 0; i < num_textures; i++)
                    {

                        tex_fact[i] = TextureFactor(region[i], h, num_textures);
                        OldC[i] = tex[i].GetPixel(x, y);

                        r += tex_fact[i] * OldC[i].R;
                        g += tex_fact[i] * OldC[i].G;
                        b += tex_fact[i] * OldC[i].B;
                    }

                    bmp_heightmap.SetPixel(x, y, Color.FromArgb((int)r, (int)g, (int)b));
                }
            }

            ConvertBitmapToTexture(bmp_heightmap, out i_texturemap);
            bmp_heightmap.Save("GeneratedTexture.bmp", ImageFormat.Bmp);
        }
I'm using visual c# 2008 by the way. So that's what i'm working with and it's really annoying me. Thanks, Scott

Share this post


Link to post
Share on other sites
assuming it works the way it's supposed to, couldn't you just change the TextureFactor function a bit, so instead of setting the minimum value to 0.0f you set it to a threshold? Like this:

#define min_lim 0.1f

public float TextureFactor(float h1, float h2, int num_textures)
{
float percent;
float inc = 255 / (float)num_textures;

percent = (inc - Math.Abs(h1 - h2)) / inc;

if (percent < min_lim)
percent = min_lim;
else if (percent > 1.0f)
percent = 1.0f;

return percent;
}

It can probably be done in a niftier way, but hey, I'm new at this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this