0
\$\begingroup\$

I've recently begun playing around with Monogame (OpenSource "remake" of XNA). My current task is to write a simple shadow mapping shader. After following Riemer's XNA Tutorial I got the shadows to show up. But interestingly only around where the "player" (camera) is. When I'm outside of the light's radius there's no light showing up around the camera (which is good), but the light from my spotlight should of course still be there. But it's not. The shadows and the bright lighting from my spotlight only appear around player.

As I said, my HLSL code is exactly the same as Riemer's (Except that I wrote SV_POSITION instead of POSITION0 at the VertexShaderInput struct because MonoGame only supports Pixel-/Vertexshader 4.0 and above)

I also have a feeling that my Light-View/-Projection Matrices might somehow be wrong:

Matrix lightsView = Matrix.CreateLookAt(lightPos, new Vector3(lightPos.X, lightPos.Y-1, lightPos.Z), new Vector3(0, 0, 1));
Matrix lightsProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45F), 1f, 1f, 10f);

(I know that my "up" vector is not pointing upwards; that was the only way I got the light to point downwards from it's position, otherwise it would always point to (0 -1 0) even if I wrote it so that the lights coordinates were used and then it would go y -1 from the light - really weird...)

I really hope someone knows what's up here, this issue prevented me from doing anything else in my code for a couple of days now...

PS: Here's my rendering code:

    ////In my Model-Class
    private void DrawModel(string tech)
    {
        Matrix[] transforms = new Matrix[model.Bones.Count];
        model.CopyAbsoluteBoneTransformsTo(transforms);

        foreach (var mesh in model.Meshes)
        {
            foreach (ModelMeshPart part in mesh.MeshParts)
            {
                part.Effect = Game1.effect;
                Matrix tempWorld = ((transforms[mesh.ParentBone.Index] * Matrix.CreateScale(scale)) * rotationMatrix) * Matrix.CreateTranslation(pos);
                part.Effect.CurrentTechnique = part.Effect.Techniques[tech];
                part.Effect.Parameters["xWorldViewProjection"].SetValue(tempWorld * Game1.camera.View * Game1.camera.Projektion);
                part.Effect.Parameters["xTexture"].SetValue(textures[part]);
                part.Effect.Parameters["xWorld"].SetValue(tempWorld);
                part.Effect.Parameters["xLightPos"].SetValue(Game1.currentLevel.lightPos);
                part.Effect.Parameters["xLightPower"].SetValue(Game1.currentLevel.lightPower);
                part.Effect.Parameters["xAmbient"].SetValue(Game1.currentLevel.ambientPower);
                part.Effect.Parameters["xLightsWorldViewProjection"].SetValue(tempWorld * Game1.currentLevel.lightsViewProjectionMatrix);
                part.Effect.Parameters["xShadowMap"].SetValue(Game1.currentLevel.shadowMap);
            }

            mesh.Draw();
        }
    }

    ////In my Level-Class
    public void Render()
    {
        var device = Game1.graphics.GraphicsDevice;

        device.SetRenderTarget(renderTarget);
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

        DrawScene("ShadowMap");

        device.SetRenderTarget(null);
        shadowMap = (Texture2D)renderTarget;

        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
        DrawScene("ShadowedScene");
    }

    private void DrawScene(string tech)
    {
        foreach (var model in models)
        {
            model.Render(tech);
        }
    }

and the light-update code:

private void UpdateLightData()
    {
        ambientPower = 0.2f;

        lightPos = new Vector3(-0.2F, 1.2F, 1.1F);

        lightPower = 1.8f;

        Matrix lightsView = Matrix.CreateLookAt(lightPos, new Vector3(lightPos.X, lightPos.Y-1, lightPos.Z), new Vector3(0, 0, 1));
        Matrix lightsProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45F), 1f, 1f, 10f);

        lightsViewProjectionMatrix = lightsView * lightsProjection;
    }
\$\endgroup\$
0
\$\begingroup\$

Alright, so I tried pretty much everything. It didn't work. Then I resignated and tried putting everything into an XNA project instead of using MonoGame... And it worked without modification. Maybe it's because it's now compiled with PS/VS 3.0 as supposed to 4.0. Maybe because of magic. I dont know.

\$\endgroup\$
0
\$\begingroup\$

It looks like you have a view projection specifically for your lighting ... so my guess is that the GPU is culling any math outside your view frustrum ...

Maybe try placing the "camera" for building your light map in a more "overview" type position or something?

but surely you only want to do light math in front of your camera not for your whole scene?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.