public bool LoadModel(string file)
 {
     if (contentType == ContentTypes.SurfacePlot)
     {
         cachedSurfaceScene = abScene;
         abScene = null;
     }
     bool result = LoadABSupportedScene(file);
     if (result)
     {
         contentType = ContentTypes.Avalon;
         SetupView();
         Invalidate();
     }
     else
     {
         abScene = cachedSurfaceScene;
     }
     return result;
 }
        public ABScene3D LoadScene(string file)
        {
            uint failId;
            int failPos;
            lwObject lwobj = lwObject.GetObject5(file, out failId, out failPos);
            if (lwobj != null)
            {
                // convert to AB
                ABScene3D scene = new ABScene3D();

                ABMaterial[] materials = null;
                int[] clipIndices = null;
                if (lwobj.surf != null && lwobj.surf.Count > 0)
                {
                    materials = new ABMaterial[lwobj.surf.Count];
                    clipIndices = new int[lwobj.surf.Count];
                    LinkedList<lwSurface>.Enumerator matIter = lwobj.surf.GetEnumerator();
                    int matIdx = 0;
                    while (matIter.MoveNext())
                    {
                        lwSurface surface = matIter.Current;
                        ABMaterial mat = materials[matIdx] = new ABMaterial();

                        mat.Name = surface.name;
                        mat.Ambient = new ABColorARGB();
                        mat.Ambient.A = 0xFF;
                        mat.Ambient.R = (byte)(surface.color.rgb[0] / 255f);
                        mat.Ambient.G = (byte)(surface.color.rgb[1] / 255f);
                        mat.Ambient.B = (byte)(surface.color.rgb[2] / 255f);
                        mat.Diffuse = new ABColorARGB();
                        mat.Diffuse.A = 0xFF;
                        mat.Diffuse.R = (byte)((surface.diffuse.val * surface.color.rgb[0]) / 255f);
                        mat.Diffuse.G = (byte)((surface.diffuse.val * surface.color.rgb[1]) / 255f);
                        mat.Diffuse.B = (byte)((surface.diffuse.val * surface.color.rgb[2]) / 255f);

                        if (surface.color.tex != null)
                        {
                            // resolve texture
                            //mat.TextureName
                            clipIndices[matIdx] = surface.color.tex.First.Value.imap.cindex;
                            mat.TextureName = surface.color.tex.First.Value.imap.vmap_name;
                        }
                        else
                            clipIndices[matIdx] = -1;

                        matIdx++;
                    }
                }

                LinkedList<lwLayer>.Enumerator layIter = lwobj.layer.GetEnumerator();
                scene.Models = new ABModel3D[lwobj.layer.Count];
                int mdlIdx = 0;
                while (layIter.MoveNext())
                {
                    lwLayer layer = layIter.Current;
                    ABModel3D model = scene.Models[mdlIdx] = new ABModel3D();

                    model.Geometry = new ABGeometry3D();
                    model.Geometry.Vertices = new Vector3[layer.point.count];

                    // break up polygons by surface
                    Dictionary<lwSurface, int> surfGroupCounts = new Dictionary<lwSurface, int>();
                    for (int i = 0; i < layer.polygon.pol.Length; i++)
                    {
                        lwPolygon polygon = layer.polygon.pol[i];
                        if (surfGroupCounts.ContainsKey(polygon.surf))
                            surfGroupCounts[polygon.surf]++;
                        else
                            surfGroupCounts[polygon.surf] = 1;
                    }
                    int[] groupIndices = new int[surfGroupCounts.Count];
                    model.MaterialIndices = new ABMaterialIndex[surfGroupCounts.Count];
                    for (int i = 0; i < layer.polygon.pol.Length; i++)
                    {
                        lwPolygon polygon = layer.polygon.pol[i];
                        if (surfGroupCounts.ContainsKey(polygon.surf))
                            surfGroupCounts[polygon.surf]++;
                        else
                            surfGroupCounts[polygon.surf] = 1;
                    }
                }

                // match up clips to materials

            }
            return null;
        }
        public ABScene3D LoadScene(string file)
        {
            string path = file.Substring(0, file.Length - Path.GetFileName(file).Length);
            // parse file into 3ds structures
            ThreeDSFileData data = ThreeDSParser.ParseFromFile(file);

            // convert to AB
            ABScene3D scene = new ABScene3D();
            ABMaterial[] materials = null;
            if (data.materials != null && data.materials.Count > 0)
            {
                materials = new ABMaterial[data.materials.Count];
                for (int mIdx = 0; mIdx < data.materials.Count; mIdx++)
                {
                    ABMaterial material = materials[mIdx] = new ABMaterial();
                    material.Name = data.materials[mIdx].name;

                    material.Ambient = new ABColorARGB();
                    material.Ambient.A = 255;
                    material.Ambient.R = data.materials[mIdx].ambient.r;
                    material.Ambient.G = data.materials[mIdx].ambient.g;
                    material.Ambient.B = data.materials[mIdx].ambient.b;

                    material.Diffuse = new ABColorARGB();
                    material.Diffuse.A = 255;
                    material.Diffuse.R = data.materials[mIdx].diffuse.r;
                    material.Diffuse.G = data.materials[mIdx].diffuse.g;
                    material.Diffuse.B = data.materials[mIdx].diffuse.b;

                    // convert to absolute path
                    if (data.materials[mIdx].texture == null || Path.IsPathRooted(data.materials[mIdx].texture))
                        material.TextureName = data.materials[mIdx].texture;
                    else
                        material.TextureName = path + data.materials[mIdx].texture;

                    // match file
                    if (material.TextureName != null)
                    {
                        string filename = Path.GetFileNameWithoutExtension(material.TextureName).ToLower();
                        string ext = Path.GetExtension(material.TextureName).ToLower();
                        string[] files = Directory.GetFiles(path);
                        foreach (string fn in files)
                        {
                            string fno = Path.GetFileNameWithoutExtension(fn).ToLower();
                            if (fno.StartsWith(filename) && Path.GetExtension(fn).ToLower() == ext)
                            {
                                material.TextureName = fn;
                                break;
                            }
                        }
                    }
                }
            }

            int numMeshes = data.GetNumFaceGroups();
            if (numMeshes > 0)
            {
                scene.Models = new ABModel3D[numMeshes];
                int meshIdx = 0;
                foreach (ThreeDSObject obj in data.objects)
                {
                    foreach (ThreeDSMesh mesh in obj.meshes)
                    {
                        scene.Models[meshIdx++] = ConvertMesh(mesh, materials);
                    }
                }
            }
            return scene;
        }
        private void ReBuildSceneBuffers()
        {
            if (device != null && contentType == ContentTypes.SurfacePlot)
            {
                if (abScene != null)
                    abScene.Dispose();

                abScene = new ABScene3D();
                
                // Geometry
                abScene.Models = softRender.ToAvalonBridgeModel();
                abScene.Models[1].Geometry.Clr = softRender.PenColor.ToArgb();
                abScene.Models[0].BuildBuffers(device);
                abScene.Models[1].BuildBuffers(device);
                abScene.CalcBounds();

                // Cameras
                abScene.Cameras = new ABCamera[1];
                abScene.Cameras[0] = new ABCameraSpherical(abScene.BoundingSphere * 2f, abScene.Origin);
                abScene.Cameras[0].Scroll(new Vector3());
                abScene.CurrentCamera = 0;
            }
        }