Lighting Question/Problem

I am attempting to light a square.  The square is made up of 4 vertices having a position and a normal.  The normal for all the vertices is pointing towards the user(0.0f, 0.0f, -1.0f).  I have applied a material with the diffuse and ambient color set to Red.

I am using a directional light which has a direction of (0.0f, 0.0f, 1.0f).  If I set the diffuse color to White, the square shows up Red as I would expect.  If I change the color to something else, like Blue, the square appears black where I was expecting it to be purple.

The reason for this line "d3dDevice.Lights[0].LightType = (LightType)3;" is that the CLR 2.0 version of MDX does not have LightType enumeration defines with Directional, Spot, or Point.



private void SetupLights()
{
 ColorValue val = new ColorValue(Color.Red.R, Color.Red.G, Color.Red.B);

 Material m = new Material();
 m.AmbientColor = val;
 m.DiffuseColor = val;
 d3dDevice.Material = m;

 d3dDevice.Lights[0].LightType = (LightType)3;
 d3dDevice.Lights[0].Diffuse = Color.Blue;
 d3dDevice.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f);
 d3dDevice.Lights[0].Enabled = true;
}

 



Answer this question

Lighting Question/Problem

  • LucyS

    There is nothing wrong with your code, nor with Direct3D here - this is fundamental graphics theory Smile

    Setting your material's diffuse to be RED is stating that it will *reflect* the red wavelengths and *absorb* the others. Setting your light to be BLUE is stating that it is contributing blue wavelengths from its source.

    BLUE wavelengths being (0,0,1) do not contain any red (1,0,0) - thus the material, by definition, is absorbing the incoming light energy and reflects nothing - which is black (0,0,0). Setting the diffuse to WHITE (1,1,1) says it'll reflect all incoming wavelengths, hence you get the colour you're expecting.

    In the real-world there aren't many occurences of "pure" colour (such as appears in computer graphics) - which is why you might be expecting to see a combination of blue and red being purple (which, in CG is an additive operation not muliplicative).

    Might be worth picking up a graphics text on the subject - I recently read Advanced Lighting and Materials with Shaders that covers this sort of thing reasonably well. I'd imagine many other books do a good job.

    hth
    Jack


  • Lighting Question/Problem