• Advertisement
Sign in to follow this  

Terrain Texture Generation - lowest regions are black

This topic is 3577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = 0;
            }

            for (i = 0; i < num_textures; i++)
            {
                if (i == 0)
                    region += region_size;
                else
                {
                    region += 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 = TextureFactor(region, h, num_textures);
                        OldC = tex.GetPixel(x, y);

                        r += tex_fact * OldC.R;
                        g += tex_fact * OldC.G;
                        b += tex_fact * OldC.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
Advertisement
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
Sign in to follow this  

  • Advertisement