Loading objects

Started by
6 comments, last by Kenny77 16 years, 11 months ago

        private void LoadMesh(string path)
        {
            ExtendedMaterial[] exMaterials;

            
            pond = Mesh.FromFile(
                path,       
                MeshFlags.SystemMemory,
                device,
                out exMaterials);

            
            materials = new Material[exMaterials.Length];

            
            for (int i = 0; i < exMaterials.Length; ++i)
            {
                materials = exMaterials.Material3D;
                materials.Ambient = materials.Diffuse;
            }
        }

When I run this code through the debugger, it highlights the "duck = M..." part and I can't figure out why. I have the file in the right directory, and I call the LoadMesh() method with this line: window.LoadMesh("pond.x");
Advertisement
If anyone has a better way of loading meshes I'd be thankful for that too.
It highlights which line?

The file may be in the application/project's directory, but that's not necessarily the 'right' directory. I can't say for sure, 'cause I'm not too familiar with VC#, but in VC++, unqualified file-names are assumed to be in the executable's directory (which is often a '/debug/' from where you'd like it to be). Try specifying the full file path instead of just the name.

As a rule, your IDE will give you an error whenever it encounters a problem, so there's probably a message that goes with the highlighting. Find it and tell us what it is.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Microsoft.DirectX.Direct3D.Direct3DXException was unhandled  Message="Error in the application."  Source="Microsoft.DirectX.Direct3DX"  ErrorCode=-2147467259  ErrorString="E_FAIL"  StackTrace:       at Microsoft.DirectX.Direct3D.Mesh.FromFile(String filename, MeshFlags options, Device device, GraphicsStream& adjacency, ExtendedMaterial[]& materials, EffectInstance[]& effects)       at Microsoft.DirectX.Direct3D.Mesh.FromFile(String filename, MeshFlags options, Device device, ExtendedMaterial[]& materials)       at Main.WinForm.LoadMesh(String path) in C:\Documents and Settings\family\My Documents\Visual Studio 2005\Projects\DirectX Practice - the real deal!\DirectX Practice - the real deal!\Program.cs:line 163       at Main.WinForm.Main() in C:\Documents and Settings\family\My Documents\Visual Studio 2005\Projects\DirectX Practice - the real deal!\DirectX Practice - the real deal!\Program.cs:line 31       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)       at System.Threading.ThreadHelper.ThreadStart()


This is the error I get.
Okay, so Mesh.FromFile is failing. This could be for any of a few reasons.

Like I said before, try passing the absolute file path. If it can't find the mesh, it will never be able to load it. The mesh will need to be valid, too. I guess the only way to test this would be to take a reliable mesh (say, one from the SDK samples) and try loading that the same way. Further, check that all the parameters are valid. I'd guess that E_INVALIDARG would be more in-keeping, but if you pass it a null or invalidated device, set of options, or other, things will go awry.

My best advice is to enable the Direct3D debug runtimes from your control panel. This way, Direct3D will tell you everything it can regarding the source of the error, rather than a fairly useless E_FAIL.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
using System;using System.Drawing;using System.Windows.Forms;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;using Microsoft.DirectX.DirectInput;using System.IO;namespace Main{    public class WinForm : System.Windows.Forms.Form    {        private Microsoft.DirectX.Direct3D.Device device;        private Microsoft.DirectX.DirectInput.Device keyb;        private Mesh pond;        private Material[] materials;                private System.ComponentModel.Container components = null;        static void Main()        {            WinForm window = new WinForm();            window.InitializeDevice();            window.InitializeKeyboard();            window.LoadMesh("C:/Documents and Settings/Kenny/My Documents/Visual Studio 2005/Projects/DirectX Practice/DirectX Practice/bin/Debug");            Application.Run(window);            //Free the window            window.Dispose();        }        public WinForm()        {            InitializeComponent();        }        public void InitializeDevice()        {            PresentParameters presentParams = new PresentParameters();            presentParams.Windowed = true;            presentParams.SwapEffect = SwapEffect.Discard;                        //Create the device            device = new Microsoft.DirectX.Direct3D.Device(                0,                                      //Select the first graphical adapter                Microsoft.DirectX.Direct3D.DeviceType.Hardware,//Hardware rendering                this,                                   //Bind the window to the device                CreateFlags.SoftwareVertexProcessing,   //CPU vertex processing                presentParams);                         //Pass the parameters        }        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            device.Clear(                ClearFlags.Target,      //Clear the target, the window                Color.DarkSlateBlue,    //Fill the screen this color                1.0f,                   //!!!!!!!!!!!!!!!!!!!!!!!!                0);                     //!!!!!!!!!!!!!!!!!!!!!!!!            //!!!!!!!!!!!!!            device.BeginScene();            RenderMesh();            //!!!!!!!!!!!!!            device.EndScene();            //!!!!!!!!!!!!!            device.Present();            ReadKeyboard();        }        private void InitializeComponent()        {            //!!!!!!!!!!!!!!!!            this.components = new System.ComponentModel.Container();            this.Size = new Size(500, 500);            this.Text = "App title";        }        public void InitializeKeyboard()        {            //Set the device to use the default keyboard            keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);            //If the window loses focus, ignore the keyboard            keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);            //!!!!!!!!!!!!!!!!!            keyb.Acquire();        }        private void ReadKeyboard()        {            //Set a variable to read the keys            KeyboardState keys = keyb.GetCurrentKeyboardState();                        //Read the keys            if (keys[Key.UpArrow])            {                ///////////////            }            if (keys[Key.DownArrow])            {                ///////////////            }        }        private void CameraPositioning()        {            device.Transform.Projection =                Matrix.PerspectiveFovLH(                (float)Math.PI / 4,         //View angle                this.Width / this.Height,   //Aspect ratio                1f,                         //Near clip plane                50f);                       //Far clip plane            device.Transform.View = Matrix.LookAtLH(                new Vector3(0, 0, 30),  //Camera position                new Vector3(0, 0, 0),   //Where to point the camera                new Vector3(0, 1, 0));  //What to be 'up'            //Set lighting on            device.RenderState.Lighting = true;            //Set culling            device.RenderState.CullMode = Cull.None;            //Set light type            device.Lights[0].Type = LightType.Directional;            //Set light colour            device.Lights[0].Diffuse = Color.White;            //Light direction            device.Lights[0].Direction = new Vector3(0.8f, 0, -1);            //Enable the lights            device.Lights[0].Enabled = true;        }        private void LoadMesh(string path)        {            //Used for loading the meshes information            ExtendedMaterial[] exMaterials;            pond = Mesh.FromFile(                path,                   //File path                MeshFlags.SystemMemory, //                device,                 //Device reference                out exMaterials);       //Hold the information            materials = new Material[exMaterials.Length];            for (int i = 0; i < exMaterials.Length; ++i)            {                materials = exMaterials.Material3D;                materials.Ambient = materials.Diffuse;            }        }        private void RenderMesh()        {            for (int i = 0; i < materials.Length; ++i)            {                device.Material = materials;                pond.DrawSubset(i);            }        }    }}


I went into the SDK control panel and some of the options were unavailable. I put the exact file path, and I've tried different models, and I always get the same thing.
window.LoadMesh("C:/Documents and Settings/Kenny/My Documents/Visual Studio 2005/Projects/DirectX Practice/DirectX Practice/bin/Debug");


Where's the filename? Surely it ought to be ".../Debug/pond.x" or something like that? I'm not familiar with C# SDK and it seems to differ from C++ one, so I cannot give help beyond that.
Lol, thanks for pointing that out, it's fine now. I think I'll take a break now.

This topic is closed to new replies.

Advertisement