• Advertisement
Sign in to follow this  

Static Shadowmap from a HeightMap

This topic is 559 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

I try to implement this Algorithm in C#/XNA

http://www.tobias-franke.eu/download/articles/shadowmap/ Looks not complicated, but I get no result, so i decide to ask here on stackoverflow...

My Implementation looks like:


  int round(float n)
        {
            if (n - ((int)n) >= 0.5)
                return (int)n + 1;
            else
                return (int)n;
        }



 private void Shadowmap(Texture2D hMap)
        {
            float MapWidth = hMap.Width * hMap.Width;

            var tmpData = new Color[hMap.Width, hMap.Width];      
            
            // Here i grab the Texture Data into a 2D Array 
            var heightMapData = TextureTo2DArray(hMap);

            Vector3 CurrentPos = new Vector3();
            Vector3 LightDir = new Vector3();
            Vector3 Sun = new Vector3(128.0f, 512.0f, 256.0f);
            int LerpX = 0;
            int LerpZ = 0;


            for (int z = 0; z < hMap.Height; z++)
            {
                for (int x = 0; x < hMap.Width; x++)
                {
                    //Set current position in terrain
                    CurrentPos = new Vector3((float)x, heightMapData[x, z].R, (float)z);

                    //Calc new direction of lightray
                    LightDir = Sun - CurrentPos;
                    LightDir.Normalize();

                    tmpData[x, z].R = 255;
                    tmpData[x, z].A = 255;

                    while (CurrentPos.X >= 0 &&
                             CurrentPos.X < MapWidth &&
                             CurrentPos.Z >= 0 &&
                             CurrentPos.Z < MapWidth &&
                             CurrentPos != Sun && CurrentPos.Y < 255)
                    {
                        CurrentPos += LightDir;

                        LerpX = round(CurrentPos.X);
                        LerpZ = round(CurrentPos.Z);
                    }

                    if (CurrentPos.Y <= heightMapData[LerpX, LerpZ].R)
                    {
                        tmpData[x, z].R = 0;
                        break;
                    }
                }
            }

            SetTextureData2D(hMap.Width, tmpData);
        } 

What I'm doing wrong? the TextureTo2DArray() and SetTextureData2D() Function works perfectly because for testing I pass the original Data through and the output looks like the Input image!

Best regards, Alex! :)

Hope anybody can help me.

Share this post


Link to post
Share on other sites
Advertisement

Just some guesses/comments based on your code...

 

 

You at least seem to be using Width places where you should be using height.

 

float MapWidth = hMap.Width * hMap.Width;

should probably be MapSize? And using hMap.Width * hMap.Height.

 

tmpData also uses hMap.Width twice.

Check if indexing is correct for tmpData and heightMapData -- should it be [x, z] or [z, x]?

 

CurrentPos.X and CurrentPos.Z are compared with MapWidth (which I suggested you rename MapSize). X should probably compare against hMap.Width, and Z hMap.Height.

 

SetTextureData2D should probably use MapSize (previously named MapWidth) as the first parameter?

Share this post


Link to post
Share on other sites

thx for answer! i solve the problem.. i simply forgott to do the IF statement into the while loop! ;)

 

Anybody have a clue how i can port this into a shader(hlsl)? 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement