How to load a Model from a .OBJ file:

            Stream objStream = Application.GetResourceStream(new Uri("/Babylon.Toolbox.Sample;component/Models/temp.obj", UriKind.Relative)).Stream;
            ObjImporter importer = new ObjImporter();

            importer.OnImportCompleted += m =>
            {
                ...
            };

            // Report loading progress
            importer.OnImportProgressChanged += p =>
            {
                ...
            };

            importer.ImportAsync(objStream, GetMaterialStream, device, ImportationOptions.Optimize);

How to render a model:
            e.GraphicsDevice.BlendState = BlendState.AlphaBlend;
            e.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            e.GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            e.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Color(0, 0, 0, 0), 1.0f, 0);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect basicEffect in mesh.Effects)
                {
                    basicEffect.World = Matrix.Identity;
                    basicEffect.View = camera.View;
                    basicEffect.Projection = camera.Projection;
                    basicEffect.LightPosition = camera.Position;
                    basicEffect.CameraPosition = camera.Position;
                }
            }

            model.Draw();

How to use the Effect class :
(Remember to install the custom build task : ($ProjectPath)\Libs\ShaderBuildTaskSetup.msi)
        public BasicEffect(GraphicsDevice device)
            : base(device, "Babylon.Toolbox", "BasicEffect/BasicEffect")
        {
            worldViewProjectionParameter = GetParameter("WorldViewProjection");
            worldParameter = GetParameter("World");
            diffuseColorParameter = GetParameter("DiffuseColor");
            emissiveColorParameter = GetParameter("EmissiveColor");
        }

How to use a camera:
        private void drawingSurface_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!mouseLeftDown)
                return;

            Point currentPosition = e.GetPosition(drawingSurface);
            camera.InertialAlpha += (float)(currentPosition.X - startPosition.X) * camera.AngularSpeed;
            camera.InertialBeta -= (float)(currentPosition.Y - startPosition.Y) * camera.AngularSpeed;


            startPosition = currentPosition;
        }

        private void LayoutRoot_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mouseLeftDown = true;
            startPosition = e.GetPosition(drawingSurface);
        }

        private void LayoutRoot_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            mouseLeftDown = false;
        }

        private void LayoutRoot_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            mouseLeftDown = false;
        }

        private void LayoutRoot_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            camera.Radius -= e.Delta * camera.Radius / 1000.0f;
        }

        private void DrawingSurface_Draw(object sender, DrawEventArgs e)
        {
            ....
            camera.ApplyInertia();

            e.InvalidateSurface();
        }

Last edited May 9, 2011 at 7:17 PM by Deltakosh, version 2

Comments

ice037 May 15, 2011 at 2:55 AM 
Hey David I love your stuff. I'm attempting to Load a .obj of a model that I've been working on and am having some issues. Maybe you could help me. importer.ImportAsync(objStream, GetMaterialStream, GraphicsDeviceManager.Current.GraphicsDevice, ImportationOptions.Optimize); I call this method in the load of the drawing surface but when it returns in the handler the Model is always null. I think its because I'm not sure what to put in for the Func<string, Stream> parameter of this method. The parameters I am using are as follows. objStream: Stream objStream = Application.GetResourceStream(new Uri("/FootBallViz;component/SoccerBallsmall.obj", UriKind.Relative)).Stream; GetMaterialStream ??? -- Don't know what to put for this? -- so I put null... Device: GraphicsDeviceManager.Current.GraphicsDevice ImportationOptions: ImportationOptions.Optimize So when the handler gets called it always returns null. (I know its not due to my model because it is actually being loaded into the stream. void importer_OnImportCompleted(Model obj) { model = obj; //model always == null } Also, is there some kind of "getting started" documentation? I would love to read through it. Thanks! And great work!! - David :)