Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Some more lighting problems.

Started by Wicken Feb 17, 2005 at 7:50 PM 2 replies 900+ views
Original Post
Wicken
Wicken
Well here I am with some more lighitng problems, 2 more of them. The first one is for some reason when i turn on ambient lighting, it doesnt light anythign up, everything remains black. Even when I set the colour to white. The next im not sure if its a problem or just me doing something wrong. Im using a point light and trying to light up a wall of a bunch of polygons but the light seems wrong. I'll put up a screen shot after the code here:
using System;
using System.Drawing;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.DirectX;
using D3D = Microsoft.DirectX.Direct3D;

namespace game
{
    public class Form1 : Form
    {
        D3D.Device device = null;
        D3D.VertexBuffer vb = null;
        private int[] indices;
        private D3D.IndexBuffer ib;
        float angle;
        bool xprod = true;
        int width=6, height=6;
        float lx = -0.25f, ly = 0.25f, lz = -0.25f;
        protected D3D.CustomVertex.PositionNormalColored[] data;
        public Form1()
        {
            InitD3D();
            this.Size = new Size(800, 600);
            this.Text = "GAME";
        }
        public void InitD3D()
        {
            try
            {
                D3D.PresentParameters param = new D3D.PresentParameters();
                param.Windowed = true;
                param.SwapEffect = D3D.SwapEffect.Discard;
                param.AutoDepthStencilFormat = D3D.DepthFormat.D16;
                param.EnableAutoDepthStencil = true;
                param.AutoDepthStencilFormat = D3D.DepthFormat.D16;
                data = new D3D.CustomVertex.PositionNormalColored[width*height];
                device = new D3D.Device(0, D3D.DeviceType.Hardware, this, D3D.CreateFlags.SoftwareVertexProcessing, param);                
                device.RenderState.Lighting = true;
                device.RenderState.Ambient = Color.Gray;
                this.Data();

            }
            catch (Microsoft.DirectX.DirectXException)
            {
                MessageBox.Show("Could not initialize Direct X");
                this.Close();
            }
            this.Data();                      
            this.Camera();            
        }
        public void Camera()
        {          
            //angle = Environment.TickCount / 500.0F;
            //device.Transform.World = Matrix.RotationY(angle);                                    
            device.Transform.View = Matrix.LookAtLH(new Vector3(10, 3, -4), 
            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, 30f);
        }
        protected void SetupMaterials()
        {
            D3D.Material mat = new D3D.Material();                      
            mat.Diffuse = Color.White;
            mat.Ambient = Color.Gray;
            device.Material = mat;            
        }
        public void Lights()
        {
                device.RenderState.Lighting = true;
                device.Lights[0].Diffuse = Color.White;                
                device.Lights[0].Type = D3D.LightType.Point;
                device.Lights[0].Position = new Vector3(lx, ly, lz);
                device.Lights[0].Range = 1f;
                device.Lights[0].Update();
                device.Lights[0].Enabled = true;                
        }       
        public void Data()
        {
            
            vb = new D3D.VertexBuffer(typeof(D3D.CustomVertex.PositionNormalColored), height*width, device, D3D.Usage.WriteOnly, D3D.CustomVertex.PositionNormalColored.Format, D3D.Pool.Managed);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    data[x + y * width] = new D3D.CustomVertex.PositionNormalColored(x, y, 0, 0, 0, -1, Color.Blue.ToArgb());
                    if(((x+y*width)+1)%3==0 && xprod)
                    {
                        
                        Vector3 temp =Cross_Product(new Vector3(data[(x + y * width) - 2].X, data[(x + y * width) - 2].Y, data[(x + y * width) - 2].Z),
                                      new Vector3(data[(x + y * width) - 1].X, data[(x + y * width) - 1].Y, data[(x + y * width) - 1].Z));
                        data[(x + y * width) - 2].Normal = temp;
                        data[(x + y * width) - 1].Normal = temp;
                        data[(x + y * width)].Normal = temp;                        
                    }
                }
            }            
            vb.SetData(data, 0, D3D.LockFlags.None);
        }
        public void Indices()
        {
            ib = new D3D.IndexBuffer(typeof(int), (width - 1) * (height - 1) * 6, device, D3D.Usage.WriteOnly, D3D.Pool.Default);
            indices = new int[(width - 1) * (height - 1) * 6];
            for (int x = 0; x < width - 1; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    indices[(x + y * (width - 1)) * 6] = (x + 1) + (y + 1) * width;
                    indices[(x + y * (width - 1)) * 6 + 1] = (x + 1) + y * width;
                    indices[(x + y * (width - 1)) * 6 + 2] = x + y * width;
                    indices[(x + y * (width - 1)) * 6 + 3] = (x + 1) + (y + 1) * width;
                    indices[(x + y * (width - 1)) * 6 + 4] = x + y * width;
                    indices[(x + y * (width - 1)) * 6 + 5] = x + (y + 1) * width;
                }
            }
            ib.SetData(indices, 0, D3D.LockFlags.None);
        }

        public Vector3 Cross_Product(Vector3 v1, Vector3 v2)
        {            
            Vector3 nABC;
            nABC.X = ( v1.Y * v2.Z ) - ( v1.Z * v2.Y );
            nABC.Y = ( v1.Z * v2.X ) - ( v1.X * v2.Z );
            nABC.Z = ( v1.X * v2.Y ) - ( v1.Y * v2.X );
            return nABC ;



        }
        public void Render()
        {
            device.VertexFormat = D3D.CustomVertex.PositionNormalColored.Format;
            device.Clear(D3D.ClearFlags.Target | D3D.ClearFlags.ZBuffer, Color.Orange, 1.0f, 0);            
            
            this.Indices();
            //device.RenderState.ShadeMode = D3D.ShadeMode.Gouraud;
            //device.RenderState.CullMode = D3D.Cull.None;
            //device.RenderState.FillMode = D3D.FillMode.WireFrame;
            device.RenderState.Lighting = true;
            this.SetupMaterials();
            this.Lights();            
            device.BeginScene();
            this.Camera();            
            device.SetStreamSource(0, vb, 0);
            device.Indices = ib;
            device.DrawIndexedPrimitives(D3D.PrimitiveType.TriangleList, 0, 0, height * width, 0, indices.Length/3);            
            device.EndScene();
            device.Present();
        }        
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                Console.WriteLine(angle);
            }
            if (e.KeyChar == (char)Keys.Escape)
            {
                this.Close();
            }
            //
            if (e.KeyChar == (char)Keys.A)
            {
                lx -= 0.01f;
            }
            if (e.KeyChar == (char)Keys.S)
            {
                lx += 0.01f;
            }
            //
            if (e.KeyChar == (char)Keys.D)
            {
                ly -= 0.01f;
            }
            if (e.KeyChar == (char)Keys.F)
            {
                ly += 0.01f;
            }
            //
            if (e.KeyChar == (char)Keys.G)
            {
                lz -= 0.01f;
            }
            if (e.KeyChar == (char)Keys.H)
            {
                lz += 0.01f;
            }
        }
        public static void Main()
        {
            Form1 form = new Form1();
            if (form == null)
            {
                MessageBox.Show("WTF");
            }
            form.Show();
            while (form.Created)
            {
                form.Render();                
                Application.DoEvents();
            }
        }
    }
heres the picture of the app http://www.geocities.com/edf3000/Lights.JPG
lenny4422
lenny4422
About the ambient lighting, this is kinda the same problem you had the last time. I dont know why your render states are being changed from your init. If you set the device.RenderState.Ambient in your Lights method, i could get the ambient light to work.

Edit: someone can correct me if i'm wrong, but the point light seems to be correct. The reason why it looks so odd is that the plane you have has so little vertices, so it comes out very rough (it calculates per vertex).
Aldenar
Aldenar
Lenny is right, for vertex lighting to look right, you need to have a high tessellation (i.e. lots of triangles) since the light intensity is computed on the vertices and linearly interpolated between them.
Wicken
Wicken
nice thanks. You see i had thought that it was because I had too few vertices but I had asked a friend who knows more than me in this topic and he had said that it shouldn't be doing that anyway. I'm going to have to point him to this post. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.