4

I've recently started learning HLSL after deciding that I wanted better lighting than what BasicEffect offered. After going through many tutorials, I found this and decided to learn from it: http://brooknovak.wordpress.com/2008/11/13/hlsl-per-pixel-point-light-using-phong-blinn-lighting-model/

It seems that the shader above doesn't work very well in my game though, because my game uses a tile based approach, which means multiple models in a grid-like formation.

What happens is that each of my tiles gets shaded separately from the others. Please see this image for a visual reference: https://i.stack.imgur.com/hGDv1.png I understand that this is because each tile has it's own model and the shader doesn't take into account other models as it's executing on the meshes of a model.

Now, for the question. How does one go about to shade all the tiles together? I understand that I may have to write a shader from scratch to accomplish this, but if anyone could give me some tips on how to achieve the effect I want, I'd really appreciate it.

It's late so there's a possibility that I've forgotten something. If you need more information, please tell me and I'll add it.

Thanks, Merigrim

EDIT:

Here is my code for drawing a model:

public void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms, Vector3 color, float alpha = 1.0f, Texture2D texture = null)
{
    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (ModelMeshPart part in mesh.MeshParts)
            {
                part.Effect = effect;
                Matrix world = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
                effect.Parameters["World"].SetValue(absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform);
                effect.Parameters["View"].SetValue(camera.view);
                effect.Parameters["Projection"].SetValue(camera.projection);
                effect.Parameters["CameraPos"].SetValue(camera.cameraPosition);
                Vector3 lookAt = camera.cameraPosition + camera.cameraDirection;
                effect.Parameters["LightPosition"].SetValue(new Vector3(lookAt.X, 1.0f, lookAt.Z - 5.0f));
                effect.Parameters["LightDiffuseColor"].SetValue(new Vector3(0.45f, 0.45f, 0.45f));
                effect.Parameters["LightSpecularColor"].SetValue(new Vector3(0.45f, 0.45f, 0.45f));
                effect.Parameters["LightDistanceSquared"].SetValue(40.0f);
                effect.Parameters["DiffuseColor"].SetValue(color);
                effect.Parameters["AmbientLightColor"].SetValue(Color.Black.ToVector3());
                effect.Parameters["EmissiveColor"].SetValue(Color.White.ToVector3());
                effect.Parameters["SpecularColor"].SetValue(Color.White.ToVector3());
                effect.Parameters["SpecularPower"].SetValue(10.0f);
                if (texture != null)
                {
                    effect.Parameters["DiffuseTexture"].SetValue(texture);
                }
                mesh.Draw();
            }
        }
        pass.Apply();
    }
}
Merigrim
  • 846
  • 10
  • 18
  • It doesn't look like your problem is caused by the tiles being seperate since the lighting calculations should take that into account and the resultant lighting would blend smoothly across all the tiles. Can you post your draw code? – meds Jul 29 '11 at 00:39
  • 1
    OK, I added the code to my post. – Merigrim Jul 29 '11 at 01:04
  • Just a quick suggest, try adding 'pass.Apply()' on top of mesh.Draw(). – meds Jul 29 '11 at 01:17
  • Unfortunately, that didn't seem to affect anything. Thanks for trying to help me with this by the way. :) – Merigrim Jul 29 '11 at 01:23
  • 1
    Looks like it could be something to do with your normals. Do tiles that share vertices also share normals? – Andrew Russell Jul 29 '11 at 01:37
  • I don't have much experience in 3D graphics so I'm not sure about this. But all tiles of a certain shape shares the same model, exported from Blender. If they share the same model, they should also share normals, right? – Merigrim Jul 29 '11 at 01:52
  • 1
    You can test to check if your normals are all correct if in your pixel shader the first thing you do is return float4(input.Normal, 1). The colour of all the tiles should be the same when you run it, and in your vertex shader instead of doing this: output.Normal = mul(input.Normal, (float3x3)World); you do output.Normal = input.Normal. – meds Jul 29 '11 at 01:55
  • This is the result of what you suggested: http://i.imgur.com/wbcvH.png Does this mean something is wrong with the normals? Or is this what you meant by the same? (It looks wrong to me, but then again, I'm no expert) – Merigrim Jul 29 '11 at 02:00

1 Answers1

1

It seems that the normals were the villain this time around. After correcting the normals in Blender, everything seems to work now.

I want to thank meds and Andrew Russell. Without your help I wouldn't have figured it out!

So now I know, when you have problems with your lighting, always check the normals first.

Merigrim
  • 846
  • 10
  • 18