Sign in to follow this  

Loading objects

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

        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[i] = exMaterials[i].Material3D;
                materials[i].Ambient = materials[i].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");

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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[i] = exMaterials[i].Material3D;
materials[i].Ambient = materials[i].Diffuse;
}
}

private void RenderMesh()
{
for (int i = 0; i < materials.Length; ++i)
{
device.Material = materials[i];

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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