Lights and .X files in c# using dx9

This is a discussion on Lights and .X files in c# using dx9 within the Game Programming forums, part of the General Programming Boards category; hey, ok every time i try to get mesh loaded from .x files to work with a light i cant ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Angry Lights and .X files in c# using dx9

    hey, ok every time i try to get mesh loaded from .x files to work with a light i cant wether it was dx8 or 9, c++ or c# but atm i am using c# with dx9 sdk and vs6 .net 2003 but ive been through the tutorials on msdn from creating a device to loading .x files but when i try to use a light with a model it compiles fine and the app works but there is no light except ambient .. and just using ambient light kicks lighting effects out of the window tbh so the app loads u can see the model but no light ive tried different light types with different params but just the same

    ive included my project so i one of u nice people was too take a quick look at it and maybe tell me what i am doing wrong

    much appreciated if u do !

    here is the .cs file incase u cba d/l the project :P

    if u need any more info ill tell u

    thanks

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
    
    
    namespace WindowsApplication1
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		private System.ComponentModel.Container components = null;
    
    		Device device = null;
    		VertexBuffer vertexBuffer = null;
    		Texture texture = null;
    		bool pause = false;
    		Mesh mesh = null;
    		Microsoft.DirectX.Direct3D.Material[] meshMaterials;
    		Texture[] meshTextures;
    
    		public bool Init_3d()
    		{
    			try
    			{
    				PresentParameters presentParams = new PresentParameters();
    				presentParams.Windowed = true;
    				presentParams.SwapEffect = SwapEffect.Discard;
    				presentParams.EnableAutoDepthStencil = true;
    				presentParams.AutoDepthStencilFormat = DepthFormat.D16;
    
    				device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
    
    				this.OnResetDevice(device, null);
    				this.OnCreateDevice(device, null);
    
    				pause = false;
    
    				return true;
    			}
    			catch
    			{
    				return false;
    			}
    		}
    
    		public void OnCreateDevice(object sender, EventArgs e)
    		{
    			Device dev = (Device)sender;
    			vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormal), 100, dev, Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Default);
    			vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer);
    			this.OnCreateVertexBuffer(vertexBuffer, null);
    		}
    
    		private void SetupMatrices()
    		{
    
    			device.Transform.View = Matrix.LookAtLH( 
    				new Vector3( 0.0f, 0.0f,-5.0f ),
    				new Vector3( 0.0f, 0.0f, 0.0f ),
    				new Vector3( 0.0f, 1.0f, 0.0f ) );
    
    			device.Transform.Projection = Matrix.PerspectiveFovLH( 
    				(float)Math.PI / 4, 1.0f, 1.0f, 100.0f );
    		}
    
    		private void SetupLights()
    		{
    			System.Drawing.Color col = System.Drawing.Color.White;
    			Microsoft.DirectX.Direct3D.Material mtrl = new Microsoft.DirectX.Direct3D.Material();
    			mtrl.Diffuse = col;
    			mtrl.Ambient = col;
    			device.Material = mtrl;
    
    			device.Lights[0].Type = LightType.Directional;
    			device.Lights[0].Diffuse = col;
    			device.Lights[0].Direction = new Vector3(-1.0f,0.0f,0.0f);
    			device.Lights[0].Position = new Vector3(0.0f, -5.0f, 0.0f);
    			device.Lights[0].Range = 1000.0f;
    			device.Lights[0].Commit();
    			device.Lights[0].Enabled = true;
    			device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
    			
    		}
    
    		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    		{
    			this.Render();
    		}
    
    		public void OnResetDevice(object sender, EventArgs e)
    		{
    			Device dev = (Device)sender;
    			dev.RenderState.CullMode = Cull.None;
    			dev.RenderState.ZBufferEnable = true;
    			dev.RenderState.Lighting = true;
    			texture = TextureLoader.FromFile(dev,"me.bmp");
    			ExtendedMaterial[] materials = null;
    
    			mesh = Mesh.FromFile("tiger.x",
    				MeshFlags.SystemMemory,
    				dev,
    				out materials);
    			if (meshTextures == null)
    			{
    				// Extract the material properties and texture names.
    				meshTextures  = new Texture[materials.Length];
    				meshMaterials = new Microsoft.DirectX.Direct3D.Material[materials.Length];
    				for( int i=0; i<materials.Length; i++ )
    				{
    					meshMaterials[i] = materials[i].Material3D;
                
    					// Set the ambient color for the material. Direct3D
    					// does not do this by default.
    					meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
                
    					// Create the texture.
    					meshTextures[i] = TextureLoader.FromFile(dev,
    						materials[i].TextureFilename);
    				}
    			}
    
    		}
    
    		public void OnCreateVertexBuffer(object sender, EventArgs e)
    		{
    			VertexBuffer vb = (VertexBuffer)sender;
    			// Create a vertex buffer (100 customervertex)
    			CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vb.Lock(0,0); // Lock the buffer (which will return our structs)
    			for (int i = 0; i < 50; i++)
    			{
    				// Fill up our structs
    				float theta = (float)(2 * Math.PI * i) / 49;
    				verts[2 * i].SetPosition(new Vector3((float)Math.Sin(theta), -1, (float)Math.Cos(theta)));
    				verts[2 * i].SetNormal(new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta)));
    				verts[2 * i + 1].SetPosition(new Vector3((float)Math.Sin(theta), 1, (float)Math.Cos(theta)));
    				verts[2 * i + 1].SetNormal(new Vector3((float)Math.Sin(theta), 0, (float)Math.Cos(theta)));
    			}
    			// Unlock (and copy) the data
    			vb.Unlock();
    		}
    
    		private void Render()
    		{
    			if (pause)
    				return;
    
    			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
    			device.BeginScene();
    			SetupLights();
    			SetupMatrices();
    	
    			for( int i=0; i<meshMaterials.Length; i++ )
    			{
    				// Set the material and texture for this subset.
    				device.Material = meshMaterials[i];
    				device.SetTexture(0, meshTextures[i]);
            
    				// Draw the mesh subset.
    				mesh.DrawSubset(i);
    			}
    
    			device.EndScene();
    			device.Present();
    		}
    
    		public Form1()
    		{
    			//
    			// Required for Windows Form Designer support
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add any constructor code after InitializeComponent call
    			//
    		}
    
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(292, 266);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.Resize += new System.EventHandler(this.Form1_Resize);
    
    		}
    		#endregion
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			using(Form1 form = new Form1())
    			{
    				if(!form.Init_3d())
    				{
    					MessageBox.Show("Init_3d Failed");
    					return;
    				}
    
    				form.Show();
    				while(form.Created)
    				{
    					form.Render();
    					Application.DoEvents();
    				}
    			}
    		}
    
    		private void Form1_Resize(object sender, System.EventArgs e)
    		{
    			pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);
    		}
    	}
    }
    Attached Files Attached Files
    Theres a sucker born every minute, but a swallower is harder to find

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    omg C# is uglyyyyyyyyyyyyyyyyyyyyy.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    35
    That was helpful in everyway
    Theres a sucker born every minute, but a swallower is harder to find

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .x file format
    By fighter92 in forum Game Programming
    Replies: 7
    Last Post: 06-11-2008, 05:37 AM
  2. Saving and Loading Files
    By renanmzmendes in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 08:46 AM
  3. 3D Studio Max with .X files
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-12-2005, 04:51 PM
  4. Making .X files in 3D Studio Max 7
    By Muphin in forum Game Programming
    Replies: 6
    Last Post: 07-16-2005, 01:26 AM
  5. Converting blender files to .X
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 06-28-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21