Exemplo n.º 1
0
        ModelMaterial ExtractMaterial(object diffuse)
        {
            ModelMaterial ret = new ModelMaterial();

            if (diffuse is common_color_or_texture_typeColor)
            {
                var col = (common_color_or_texture_typeColor)diffuse;
                ret.DiffuseColor = new Color4((float)col.Values[0], (float)col.Values[1], (float)col.Values[2], (float)col.Values[3]);
            }
            else if (diffuse is common_color_or_texture_typeTexture)
            {
                var tex = (common_color_or_texture_typeTexture)diffuse;
                ret.Texture = tex.texcoord;
            }
            return(ret);
        }
Exemplo n.º 2
0
        void AddFacesFromPolyList(polylist list, mesh mesh, ModelPrim prim, Matrix4 transform)
        {
            string material  = list.material;
            source posSrc    = null;
            source normalSrc = null;
            source uvSrc     = null;

            ulong stride    = 0;
            int   posOffset = -1;
            int   norOffset = -1;
            int   uvOffset  = -1;

            foreach (var inp in list.input)
            {
                stride = Math.Max(stride, inp.offset);

                switch (inp.semantic)
                {
                case "VERTEX":
                    posSrc    = FindSource(mesh.source, mesh.vertices.input[0].source);
                    posOffset = (int)inp.offset;
                    break;

                case "NORMAL":
                    normalSrc = FindSource(mesh.source, inp.source);
                    norOffset = (int)inp.offset;
                    break;

                case "TEXCOORD":
                    uvSrc    = FindSource(mesh.source, inp.source);
                    uvOffset = (int)inp.offset;
                    break;
                }
            }

            stride += 1;

            if (posSrc == null)
            {
                return;
            }

            var vcount = StrToArray(list.vcount);
            var idx    = StrToArray(list.p);

            Vector3[] normals = null;
            if (normalSrc != null)
            {
                var norVal = ((float_array)normalSrc.Item).Values;
                normals = new Vector3[norVal.Length / 3];

                for (int i = 0; i < normals.Length; i++)
                {
                    normals[i] = new Vector3((float)norVal[i * 3 + 0], (float)norVal[i * 3 + 1], (float)norVal[i * 3 + 2]);
                    normals[i] = Vector3.TransformNormal(normals[i], transform);
                    normals[i].Normalize();
                }
            }

            Vector2[] uvs = null;
            if (uvSrc != null)
            {
                var uvVal = ((float_array)uvSrc.Item).Values;
                uvs = new Vector2[uvVal.Length / 2];

                for (int i = 0; i < uvs.Length; i++)
                {
                    uvs[i] = new Vector2((float)uvVal[i * 2 + 0], (float)uvVal[i * 2 + 1]);
                }
            }

            ModelFace face = new ModelFace {
                MaterialID = list.material
            };

            if (face.MaterialID != null)
            {
                if (MatSymTarget.ContainsKey(list.material))
                {
                    ModelMaterial mat = Materials.Find(m => m.ID == MatSymTarget[list.material]);
                    if (mat != null)
                    {
                        face.Material = mat;
                    }
                }
            }

            int curIdx = 0;

            foreach (var nvert in vcount)
            {
                if (nvert < 3 || nvert > 4)
                {
                    throw new InvalidDataException("Only triangles and quads supported");
                }

                Vertex[] verts = new Vertex[nvert];
                for (int j = 0; j < nvert; j++)
                {
                    verts[j].Position = prim.Positions[idx[curIdx + posOffset + (int)stride * j]];

                    if (normals != null)
                    {
                        verts[j].Normal = normals[idx[curIdx + norOffset + (int)stride * j]];
                    }

                    if (uvs != null)
                    {
                        verts[j].TexCoord = uvs[idx[curIdx + uvOffset + (int)stride * j]];
                    }
                }


                switch (nvert)
                {
                case 3:
                    face.AddVertex(verts[0]);
                    face.AddVertex(verts[1]);
                    face.AddVertex(verts[2]);
                    break;

                case 4:
                    face.AddVertex(verts[0]);
                    face.AddVertex(verts[1]);
                    face.AddVertex(verts[2]);

                    face.AddVertex(verts[0]);
                    face.AddVertex(verts[2]);
                    face.AddVertex(verts[3]);
                    break;
                }

                curIdx += (int)stride * nvert;
            }

            prim.Faces.Add(face);
        }
Exemplo n.º 3
0
        ModelMaterial ExtractMaterial(object diffuse)
        {
            ModelMaterial ret = new ModelMaterial();
            if (diffuse is common_color_or_texture_typeColor)
            {
                var col = (common_color_or_texture_typeColor)diffuse;
                ret.DiffuseColor = new Color4((float)col.Values[0], (float)col.Values[1], (float)col.Values[2], (float)col.Values[3]);
            }
            else if (diffuse is common_color_or_texture_typeTexture)
            {
                var tex = (common_color_or_texture_typeTexture)diffuse;
                ret.Texture = tex.texcoord;
            }
            return ret;

        }
Exemplo n.º 4
0
        void LoadImage(ModelMaterial material)
        {
            var fname = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(FileName), material.Texture);

            try
            {
                string ext = System.IO.Path.GetExtension(material.Texture).ToLower();

                Bitmap bitmap = null;

                switch (ext)
                {
                case ".jp2":
                case ".j2c":
                    material.TextureData = File.ReadAllBytes(fname);
                    return;

                case ".tga":
                    bitmap = LoadTGAClass.LoadTGA(fname);
                    break;

                default:
                    bitmap = (Bitmap)Image.FromFile(fname);
                    break;
                }

                int width  = bitmap.Width;
                int height = bitmap.Height;

                // Handle resizing to prevent excessively large images and irregular dimensions
                if (!IsPowerOfTwo((uint)width) || !IsPowerOfTwo((uint)height) || width > 1024 || height > 1024)
                {
                    var origWidth  = width;
                    var origHieght = height;

                    width  = ClosestPowerOwTwo(width);
                    height = ClosestPowerOwTwo(height);

                    width  = width > 1024 ? 1024 : width;
                    height = height > 1024 ? 1024 : height;

                    Logger.Log("Image has irregular dimensions " + origWidth + "x" + origHieght + ". Resizing to " + width + "x" + height, Helpers.LogLevel.Info);

                    Bitmap   resized  = new Bitmap(width, height, bitmap.PixelFormat);
                    Graphics graphics = Graphics.FromImage(resized);

                    graphics.SmoothingMode     = SmoothingMode.HighQuality;
                    graphics.InterpolationMode =
                        InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(bitmap, 0, 0, width, height);

                    bitmap.Dispose();
                    bitmap = resized;
                }

                using (var writer = new OpenJpegDotNet.IO.Writer(bitmap))
                {
                    material.TextureData = writer.Encode();
                }

                Logger.Log("Successfully encoded " + fname, Helpers.LogLevel.Info);
            }
            catch (Exception ex)
            {
                Logger.Log("Failed loading " + fname + ": " + ex.Message, Helpers.LogLevel.Warning);
            }
        }
Exemplo n.º 5
0
        void LoadImage(ModelMaterial material)
        {
            var fname = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(FileName), material.Texture);

            try
            {
                string ext = System.IO.Path.GetExtension(material.Texture).ToLower();

                Bitmap bitmap = null;

                if (ext == ".jp2" || ext == ".j2c")
                {
                    material.TextureData = File.ReadAllBytes(fname);
                    return;
                }

                if (ext == ".tga")
                {
                    bitmap = LoadTGAClass.LoadTGA(fname);
                }
                else
                {
                    bitmap = (Bitmap)Image.FromFile(fname);
                }

                int width = bitmap.Width;
                int height = bitmap.Height;

                // Handle resizing to prevent excessively large images and irregular dimensions
                if (!IsPowerOfTwo((uint)width) || !IsPowerOfTwo((uint)height) || width > 1024 || height > 1024)
                {
                    var origWidth = width;
                    var origHieght = height;

                    width = ClosestPowerOwTwo(width);
                    height = ClosestPowerOwTwo(height);

                    width = width > 1024 ? 1024 : width;
                    height = height > 1024 ? 1024 : height;

                    Logger.Log("Image has irregular dimensions " + origWidth + "x" + origHieght + ". Resizing to " + width + "x" + height, Helpers.LogLevel.Info);

                    Bitmap resized = new Bitmap(width, height, bitmap.PixelFormat);
                    Graphics graphics = Graphics.FromImage(resized);

                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphics.InterpolationMode =
                       System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphics.DrawImage(bitmap, 0, 0, width, height);

                    bitmap.Dispose();
                    bitmap = resized;
                }

                material.TextureData = OpenJPEG.EncodeFromImage(bitmap, false);

                Logger.Log("Successfully encoded " + fname, Helpers.LogLevel.Info);
            }
            catch (Exception ex)
            {
                Logger.Log("Failed loading " + fname + ": " + ex.Message, Helpers.LogLevel.Warning);
            }

        }