Sign in to follow this  
Wicken

Lighting problem

Recommended Posts

I'm trying to get the lighting to work on my single triangle and I can't find out why its not working heres the code:
namespace game
{
    public class Form1 : Form
    {
        D3D.Device device = null;
        D3D.VertexBuffer vb = null;                
        protected D3D.CustomVertex.PositionNormalColored[] data;
        public Form1()
        {
            InitD3D();
            this.Size = new Size(800, 600);
            this.Text = "GAME";
        }
        public void InitD3D()
        {
            D3D.PresentParameters param = new D3D.PresentParameters();
            param.Windowed = true;            
            param.SwapEffect = D3D.SwapEffect.Discard;
            param.AutoDepthStencilFormat = D3D.DepthFormat.D16;            
            data = new D3D.CustomVertex.PositionNormalColored[3];
            device = new D3D.Device(0, D3D.DeviceType.Hardware, this, D3D.CreateFlags.SoftwareVertexProcessing, param);            
            this.Data();
            //this.SetupMaterials();
            this.Lights();
            this.Camera();
        }
        public void Camera()
        {          
            float angle = Environment.TickCount / 500.0F;
            device.Transform.World = Matrix.RotationY(angle);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0.5F, -3), 
            new Vector3(0, 0.5F, 0), new Vector3(0, 1, 0));
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4.0F, 1.0F, 1.0F, 3.25F);
        }
        protected void SetupMaterials()
        {
            D3D.Material mat = new D3D.Material();                      
            mat.Diffuse = Color.Blue;            
            mat.Specular = Color.LightGray;
            mat.SpecularSharpness = 15.0F;
            device.Material = mat;            
            device.RenderState.SpecularEnable = true;
        }
        public void Lights()
        {           
                device.Lights[0].Diffuse = Color.White;
                device.Lights[0].Type = D3D.LightType.Directional;
                device.Lights[0].Direction = new Vector3(-1, -1, -3);
                device.Lights[0].Update();
                device.Lights[0].Enabled = true;
                device.RenderState.Ambient = Color.White;                                        
        }       
        public void Data()
        {
            vb = new D3D.VertexBuffer(typeof(D3D.CustomVertex.PositionNormalColored), data.Length, device, D3D.Usage.WriteOnly, D3D.CustomVertex.PositionNormalColored.Format, D3D.Pool.Managed);
            data[0] = new D3D.CustomVertex.PositionNormalColored(0, 1, 0, 0, 0, 1, Color.Purple.ToArgb());
            data[1] = new D3D.CustomVertex.PositionNormalColored(-0.5f, 0, 0, 0, 0, 1, Color.Red.ToArgb());
            data[2] = new D3D.CustomVertex.PositionNormalColored(0.5f, 0, 0, 0, 0, 1, Color.Green.ToArgb());
            vb.SetData(data, 0, D3D.LockFlags.None);
        }                
        public void Render()
        {
            device.VertexFormat = D3D.CustomVertex.PositionNormalColored.Format;
            device.Clear(D3D.ClearFlags.Target, Color.Orange, 1.0f, 0);
            //device.RenderState.Lighting = false;
            device.RenderState.CullMode = D3D.Cull.None;
            device.BeginScene();
            this.Camera();
            device.SetStreamSource(0, vb, 0);
            device.DrawPrimitives(D3D.PrimitiveType.TriangleList, 0, 1);
            device.EndScene();
            device.Present();
        }        
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                this.Close();
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
        }        
        public static void Main()
        {
            Form1 form = new Form1();
            form.Show();
            while (form.Created)
            {
                form.Render();                
                Application.DoEvents();
            }
        }
    }
}

any help would be greatly appreciated. [Edited by - Wicken on February 16, 2005 4:26:54 PM]

Share this post


Link to post
Share on other sites
I can't spot any obvious errors so...

Q. What does happen: Error? No Error?
- If an error, what error? details - line reference, message etc..
- No error - what, if anything, can you see on the screen?

A couple of observations on your code:

This line:
device.RenderState.Ambient = Color.White;

Effectively cancels out this line:
device.Lights[0].Diffuse = Color.White;

Why? well, the general "Ambient lighting value" can be thought of as a minimum colour - everything will be at least this bright. You've set it to Color.White which is the brightest possible; effectively ensuring that whatever is rendered will be rendered as the brightest possible colour.

This line:
mat.Diffuse = Color.Blue;
Confuses issues somewhat, because it would mean that any outputted colour will only display its blue component. However, from looking at your code, you don't call SetupMaterials() so it's a bit of a moot point.

Also:
device.Lights[0].Direction = new Vector3(-1, -1, -3)
I'm not sure how nice MDX will be to you, but as a general rule of thumb you want to set a lights direction to be a unit vector. Might be worth normalizing this value.

In answer to my above question: If you're not getting an error, are you just seeing a single white triangle on a bright orange background?

Thats all I can see for now,
hth
Jack

Share this post


Link to post
Share on other sites
lol I guess I should have told you what the problem is eh? Well its not lighting anything, all you see is a Black triangle on the orange background.

Also I know that I dont call the SetupMaterials() function, I went back to get the lighting working before I finished the materials part.

And I guess it does make sense that the direction is made a unit vector, thanks for the tip.

As for the device.RenderState.Ambient = Color.White; line I forgot to comment that out. I really need to get used to Direct X.

Thanks for the help (still rendering a black triangle though).

Share this post


Link to post
Share on other sites
Quote:
all you see is a Black triangle on the orange background.

This is a big clue. When you're dealing with lighting and you get a completely black set of triangles, 1 of 2 things (usually!) are culprits:

Firstly, you haven't enabled lighting or you've left out a crucial setting.
- I don't see a device.RenderState.Lighting = true; call, so from what I see of things - you're never turning lighting on!
- You require a material of some description to be configured for lighting to work. Maybe SetupMaterials() is worth enabling again. If you set the material to be Color.White it shouldn't affect the lighting at all (leave specular alone for now).

Secondly, you have set up everything, but the configuration you've used doesn't affect the geometry you're looking at.
- The material * texture * light colour cancels each other out. [red material]*[green texture]*[blue light] = black triangle
- The light you are using is not within range of your geometry (not true in this case!)
- The normals for the triangle you're creating are pointing away from the light (don't think this is the case with yours).

I can't think of anything else right now, see if any of the above points you in the right direction...

Quote:
I really need to get used to Direct X.

Don't put yourself down, it takes YEARS to fully get to grips with DX. Give it time and practice and you'll be fine [smile]

hth
Jack

Share this post


Link to post
Share on other sites
You're being quite helpfull thank you. I enabled lighting during the D3D initialization just as you told me and uncommented my SetupMaterials without specular. I also changed the colour of the texture and material several times to make sure they weren't canceling each other out.

I'm still getting used to debugging i guess, cause I still can't seem to find the problem. It's still just rendering a black triangle.


Is there any other reason that the light wouldn't be affecing the triangle, because when I set the ambiant light to white, thelightest possible, it still doesn't affect the triangle, its black. There must be something else that I have to do to initialize the light that im not aware of.

[Edited by - Wicken on February 16, 2005 6:25:00 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this