Sign in to follow this  
hetrobezz

Lighting a Mesh

Recommended Posts

Iv been tinkering with Direct3D for a while now and have run across a problem applying Light to a Mesh. The Mesh does respond to the Ambient light that is setup but does nothing with the Directional light. Thanks in advance Curtis
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using System.ComponentModel;
using System.IO;


namespace Practice
{

    public class CreateDevice : Form
    {
        Device theDevice = null;
        PresentParameters presentParams = new PresentParameters();

        Mesh theMesh = null;
        Material[] meshMaterials;
        Texture[] meshTextures;

        bool pause = false;

        public CreateDevice()
        {
            this.ClientSize = new System.Drawing.Size(640, 480);
            this.Text = "Mesh - Without Light...";
        }


        public void InitGraphics()
        {
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            presentParams.EnableAutoDepthStencil = true;
            presentParams.AutoDepthStencilFormat = DepthFormat.D16;

            theDevice = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
            theDevice.DeviceReset += new System.EventHandler(this.OnResetDevice);

            this.OnResetDevice(theDevice, null);

            pause = false;
        }


        public void OnResetDevice(object sender, EventArgs e)
        {
            ExtendedMaterial[] materials = null;

            Device theDevice = (Device)sender;

            theDevice.RenderState.ZBufferEnable = true;
            theDevice.RenderState.Lighting = true;

            theMesh = Mesh.FromFile("teapot.x", MeshFlags.SystemMemory, theDevice, out materials);

            if (meshTextures == null)
            {
                meshTextures = new Texture[materials.Length];
                meshMaterials = new Material[materials.Length];

                for (int i = 0; i < materials.Length; ++i)
                {
                    meshMaterials[i] = materials[i].Material3D;
                    meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
                    meshTextures[i] = TextureLoader.FromFile(theDevice, materials[i].TextureFilename);
                }
            }

        }


        private void Render()
        {
            if (theDevice == null)
                return;

            if (pause)
                return;

            theDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
            theDevice.BeginScene();

            SetupMatrix();
            SetupLights();
            RenderMesh();

            theDevice.EndScene();
            theDevice.Present();
        }


        private void RenderMesh()
        {
            for (int i = 0; i < meshMaterials.Length; ++i)
            {

                theDevice.SetTexture(0, meshTextures[i]);
                theDevice.Material = meshMaterials[i];
                theMesh.DrawSubset(i);

            }
            return;
        }


        private void SetupMatrix()
        {
            int theTime = Environment.TickCount % 10000;
            float rotationAngle = theTime * (2.0f * (float)Math.PI) / 10000.0f;

            Matrix transformationMatrix = Matrix.Scaling(0.3f, 0.3f, 0.3f);

            transformationMatrix.Multiply(Matrix.Translation(-1.0f, -1.0f, 2.0f));
            transformationMatrix.Multiply(Matrix.RotationAxis(new Vector3(2.0f, 11.0f, 2.0f), rotationAngle));

            theDevice.Transform.World = transformationMatrix;

            theDevice.Transform.View = Matrix.LookAtLH(new Vector3( 0.0f,  3.0f, -5.0f),
                                                       new Vector3( 0.0f,  0.0f,  0.0f),
                                                       new Vector3( 0.0f,  0.1f,  0.0f));

            theDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
                                                                       1.0f,
                                                                       1.0f,
                                                                       100.0f);
        }

        private void SetupLights()
        {

            theDevice.Lights[0].Type = LightType.Point;
            theDevice.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
            theDevice.Lights[0].Direction = new Vector3(-1, -1, 3);
            theDevice.Lights[0].Enabled = true;
            theDevice.Lights[0].Update();

            theDevice.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);

        }


        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            this.Render(); // Render on painting
        }

        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
                this.Close(); // Esc was pressed
        }

        protected override void OnResize(System.EventArgs e)
        {
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
        }

        static void Main()
        {

            using (CreateDevice theWindow = new CreateDevice())
            {
                theWindow.InitGraphics();
                theWindow.Show();

                while (theWindow.Created)
                {
                    theWindow.Render();
                    Application.DoEvents();
                }
            }
        
        }
    }
}

edit: added [source] tags -SiCrane [Edited by - SiCrane on December 4, 2004 2:18:33 AM]

Share this post


Link to post
Share on other sites
Check the materials for your mesh (in mesh editor). The material for diffuse light may not be affecting the result, which means light/no light doesn't make a difference. Refer to SDK for more details.

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