Sign in to follow this  

[MDX] First program trouble

This topic is 3740 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

Hello, I have just begun delving into MDX, Now I want to render a model in the center of the screen and try to rotate around it to get a grip of 3d coordinates. But I have a problem, I can't get the damn model to render, or well it is being rendered but I cant see it :) I have struggled with setting up my field of view and using look at in a good way, but I cant get it to work. It is surely really easy but I would apprishiate some help. Here is the relevant code:
public partial class Form1 : Form
    {
        Device device = null;

        Mesh dolphin = null;
        Material[] dolphin_materials = null;
        Texture[] dolphin_textures = null;

        public Form1()
        {
            InitializeComponent();
            InitializeGraphics();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
            this.Size = new Size(800, 600);

            LoadMesh("dolphin.x");

            //MessageBox.Show(dolphin.NumberFaces.ToString());
        }

        private void InitializeGraphics()
        {
            PresentParameters flags = new PresentParameters();
            flags.Windowed = true;
            flags.SwapEffect = SwapEffect.Discard;

            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, flags);
            device.DeviceReset += new EventHandler(device_DeviceReset);

            device.RenderState.Lighting = false;

            device.RenderState.CullMode = Cull.None;

            /*device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Position = new Vector3();
            device.Lights[0].Diffuse = Color.White;
            //device.Lights[0].Direction = new Vector3(0, 0, 0);
            device.Lights[0].Attenuation0 = 0.2f;
            device.Lights[0].Range = 10000f;
            device.Lights[0].Enabled = true;*/

            SetupCamera();
        }

        void device_DeviceReset(object sender, EventArgs e)
        {
            // Do something...
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.CornflowerBlue, 0, 0);

            device.BeginScene();
            DrawMesh();
            device.EndScene();
            device.Present();

            this.Invalidate();
        }

        private void SetupCamera()
        {
            device.Transform.World = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 10000f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 500f), new Vector3(), new Vector3(0, 1, 0));
        }

        private void LoadMesh(string file)
        {
            ExtendedMaterial[] mtrl = null;

            // Load the model
            dolphin = Mesh.FromFile(file, MeshFlags.Managed, device, out mtrl);

            // Store the materials
            if (mtrl != null && mtrl.Length > 0)
            {
                dolphin_materials = new Material[mtrl.Length];
                dolphin_textures = new Texture[mtrl.Length];

                for (int i = 0; i < mtrl.Length; i++)
                {
                    //MessageBox.Show("Material " +  (i+1) + " Loaded.");
                    dolphin_materials[i] = mtrl[i].Material3D;
                    if (mtrl[i].TextureFilename != null && mtrl[i].TextureFilename != string.Empty)
                    {
                       // MessageBox.Show("Texture " + (i + 1) + " Loaded.");
                        // Load the textrure
                        dolphin_textures[i] = TextureLoader.FromFile(device, mtrl[i].TextureFilename);
                    }

                    if ((dolphin.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal)
                    {
                        Mesh tmp = dolphin.Clone(dolphin.Options.Value, dolphin.VertexFormat | VertexFormats.Normal, device);

                        tmp.ComputeNormals();

                        dolphin.Dispose();
                        dolphin = tmp;
                        tmp.Dispose();
                    }
                }
            }
        }

        private void DrawMesh()
        {
            for (int i = 0; i < dolphin_materials.Length; i++)
            {
                device.Material = dolphin_materials[i];
                device.SetTexture(0, dolphin_textures[i]);
                dolphin.DrawSubset(i);
            }
        }
    }

I guess it is a bit of code, but hopefully someone can help me get the dolphin to display in the center of the screen so I can begin playing around and figuring out 3d coordinates. Thanks.

Share this post


Link to post
Share on other sites
1) You have no Z buffer

2) You should be rendering as fast as possible(use a "game loop", not the paint event)

3) device.Transform.World = Matrix.PerspectiveFovLH. Yeah this'll mess things up, try using device.Transform.Projection instead.

4) Your Z far value in PerspectiveFovLH might be too far, mine usually starts cutting the scene up if I push it past 4000. Try 0.1f for Z near and 1000.0f for Z far. If the mesh is really big use scaling.

5) I've never dealt with .X files but the statement "dolphin = tmp; tmp.Dispose();" seems strange.

6) Are your sure dolphin_materials.Length is over 0?

Share this post


Link to post
Share on other sites

This topic is 3740 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