/// <summary>
        /// Set v1, v2, and v3 to be counterclockwise when viewed from the front of the face.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="v3"></param>
        /// <param name="normVector"></param>
        /// <param name="textureIndex"></param>
        /// <returns></returns>
        public static Face MakeFace(this Obj obj, Vertex v1, Vertex v2, Vertex v3, NormalVector normVector, int textureIndex)
        {
            //表からみて半時計まわりに頂点が回らないとダメ。

            var face = new Face(3);

            face.IsNormalEnable  = true;
            face.IsTextureEnable = true;

            face.VertexIndexList[0] = v1.Index;
            face.VertexIndexList[1] = v2.Index;
            face.VertexIndexList[2] = v3.Index;

            face.NormalVectorList[0] = normVector.Index;
            face.NormalVectorList[1] = normVector.Index;
            face.NormalVectorList[2] = normVector.Index;

            face.TextureVertexIndexList[0] = textureIndex;
            face.TextureVertexIndexList[1] = textureIndex;
            face.TextureVertexIndexList[2] = textureIndex;

            face.UseMtl = obj.CurrentMtl;

            obj.FaceList.Add(face);
            return(face);
        }
Exemplo n.º 2
0
        public XmlNode WriteXML(XmlDocument document)
        {
            // Create the element
            XmlNode node = document.CreateElement(ExportationConstants.XML_SLANT_IDENT);

            // ID
            XmlAttribute attr = document.CreateAttribute(Helper.Check(() => this.ID));

            attr.Value = this.ID.ToString(ExportationConstants.XML_PATTERN, ExportationConstants.XML_FORMATTER);
            node.Attributes.Append(attr);

            // Create position root
            XmlNode positionRoot = document.CreateElement(ExportationConstants.XML_SLANT_POSITION_IDENT);

            // Append position
            positionRoot.AppendChild(Position.WriteXML(document));
            // Attach slants
            node.AppendChild(positionRoot);

            // Create position root
            XmlNode normalRoot = document.CreateElement(ExportationConstants.XML_SLANT_NORMAL_VECTOR_IDENT);

            // Append position
            normalRoot.AppendChild(NormalVector.WriteXML(document));
            // Attach slants
            node.AppendChild(normalRoot);

            // Return it
            return(node);
        }
        public static NormalVector MakeNormalVector(this Obj obj, Vector3 v)
        {
            var normalVec = new NormalVector(v.X, v.Y, v.Z)
            {
                Index = obj.NormalVectorList.Count + 1,
            };

            obj.NormalVectorList.Add(normalVec);

            return(normalVec);
        }
        public static NormalVector MakeNormalVector(this Obj obj, float x, float y, float z)
        {
            var normalVec = new NormalVector(x, y, z)
            {
                Index = obj.NormalVectorList.Count + 1,
            };

            obj.NormalVectorList.Add(normalVec);

            return(normalVec);
        }
Exemplo n.º 5
0
        public HessePlane(ICoordinated a, ICoordinated b, ICoordinated c)
        {
            NormalVector = (b.Minus(a)).CrossProduct(c.Minus(a));
            NormalVector.Normalize();

            RootDistance = a.ScalarProduct(NormalVector);
            if (RootDistance < 0)
            {
                RootDistance   *= -1;
                NormalVector.X *= -1;
                NormalVector.Y *= -1;
                NormalVector.Z *= -1;
            }
        }
Exemplo n.º 6
0
            private NormalVector ConvertNormalMapToNormalVector(int x, int y)
            {
                Color c;

                lock (normalMap)
                {
                    c = normalMap.GetPixel(x, y);
                }
                NormalVector N = new NormalVector();

                N.X = c.R / 127.0 - 1.0;
                N.Y = c.G / 127.0 - 1.0;
                N.Z = c.B / 255.0;

                return(N);
            }
        public void UpdateCollisionWithWall(XNACS1Rectangle wall)
        {
            Vector2 mVectorV;
            Vector2 TangentVector;

            float   DistOnTangent;
            Vector2 PtOnTangentLine;
            Vector2 NormalVector;
            float   DistOnNormal;
            Vector2 mPtOnNormal;
            float   mDistOnTangern;

            if (Collided(wall))
            {
                mVectorV = Center - wall.Center;                    // V vector(vector from hero center to wall center)

                TangentVector = wall.FrontDirection;                //  tangent vector
                TangentVector.Normalize();
                DistOnTangent   = Vector2.Dot(mVectorV, TangentVector);
                PtOnTangentLine = wall.Center + (DistOnTangent * TangentVector);
                NormalVector    = mVectorV - (DistOnTangent * TangentVector);
                NormalVector.Normalize();
                DistOnNormal = Vector2.Dot(mVectorV, NormalVector);


                mPtOnNormal    = wall.Center + (DistOnNormal * NormalVector);
                mDistOnTangern = Vector2.Dot(mVectorV, TangentVector);

                if (Math.Abs(DistOnTangent) < wall.Width / 2f)                 // collided with top and bottom
                {
                    float dOnN = Height / 2 + (wall.Height / 2f);
                    if (Math.Abs(DistOnNormal) < dOnN)
                    {
                        Center = PtOnTangentLine + Math.Sign(DistOnNormal) * (dOnN * NormalVector);
                    }
                }
                else                  // collide with left and right

                {
                    if (Math.Abs(mDistOnTangern) < Width / 2 + (wall.Width / 2f))
                    {
                        Center = mPtOnNormal + Math.Sign(mDistOnTangern) * (Width / 2 + wall.Width / 2f) * TangentVector;
                    }
                }
            }
        }
        /// <summary>
        /// Set v1, v2, and v3 to be counterclockwise when viewed from the front of the face.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <param name="v3"></param>
        /// <param name="normVector"></param>
        /// <returns></returns>
        public static Face MakeFace(this Obj obj, Vertex v1, Vertex v2, Vertex v3, NormalVector normVector)
        {
            var face = new Face(3);

            face.VertexIndexList[0] = v1.Index;
            face.VertexIndexList[1] = v2.Index;
            face.VertexIndexList[2] = v3.Index;

            face.NormalVectorList[0] = normVector.Index;
            face.NormalVectorList[1] = normVector.Index;
            face.NormalVectorList[2] = normVector.Index;

            face.UseMtl = obj.CurrentMtl;

            face.IsNormalEnable = true;

            obj.FaceList.Add(face);
            return(face);
        }
Exemplo n.º 9
0
        internal void Refresh(bool switchBackFront)
        {
            UpdateNormalVector();

            if (mIsTransparent || Edges.Any <Edge>(e => e.OtherFace == null))
            {
                mIsFrontFacing = true;
            }
            else
            {
                //the face may no longer be front-facing (or no longer back-facing)
                double dotProduct = NormalVector.DotProduct(new Coord(0, 0, 1));
                int    dotSign    = Math.Sign(dotProduct);
                mIsFrontFacing = switchBackFront ? (dotSign == -1) : (dotSign == 1);
            }
            //update the bounding box
            mBoundingBox = Global.GetRectangleWithGivenCorners(Vertices[0].ViewCoord.ToPointD(), Vertices[1].ViewCoord.ToPointD());
            for (int i = 1; i < Vertices.Count; i++)
            {
                mBoundingBox = Rectangle.Union(mBoundingBox, Global.GetRectangleWithGivenCorners(Vertices[i - 1].ViewCoord.ToPointD(), Vertices[i].ViewCoord.ToPointD()));
            }
        }
Exemplo n.º 10
0
 public override string ToString()
 {
     return("Slant" + ID.ToString() + "-" + Position.ToString() + "-" + NormalVector.ToString());
 }
Exemplo n.º 11
0
        public void loadFile(String fileName)
        {
            StreamReader objReader  = new StreamReader(fileName);
            ArrayList    al         = new ArrayList();
            string       texLineTem = "";

            while (objReader.Peek() != -1)
            {
                texLineTem = objReader.ReadLine();
                if (texLineTem.Length < 2)
                {
                    continue;
                }
                if (texLineTem.IndexOf("v") == 0)
                {
                    if (texLineTem.IndexOf("t") == 1)
                    {
                        string[]           tempArray = texLineTem.Split(' ');
                        TextureCoordinates vt        = new TextureCoordinates();
                        vt.TU = double.Parse(tempArray[1]);
                        vt.TV = double.Parse(tempArray[2]);
                        mesh.VT.Add(vt);
                    }
                    else if (texLineTem.IndexOf("n") == 1)
                    {
                        string[]     tempArray = texLineTem.Split(new char[] { '/', ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
                        NormalVector vn        = new NormalVector();
                        vn.NX = double.Parse(tempArray[1]);
                        vn.NY = double.Parse(tempArray[2]);
                        if (tempArray[3] == "\\")
                        {
                            texLineTem = objReader.ReadLine();
                            vn.NZ      = double.Parse(texLineTem);
                        }
                        else
                        {
                            vn.NZ = double.Parse(tempArray[3]);
                        }

                        mesh.VN.Add(vn);
                    }
                    else
                    {
                        string[] tempArray = texLineTem.Split(' ');
                        POINT3   v         = new POINT3();
                        v.X = double.Parse(tempArray[1]);
                        v.Y = double.Parse(tempArray[2]);
                        v.Z = double.Parse(tempArray[3]);
                        mesh.V.Add(v);
                    }
                }
                else if (texLineTem.IndexOf("f") == 0)
                {
                    string[] tempArray = texLineTem.Split(new char[] { '/', ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
                    Surface  f         = new Surface();
                    int      i         = 0;
                    int      k         = 1;
                    while (i < 3)
                    {
                        if (mesh.V.Count != 0)
                        {
                            f.V[i] = int.Parse(tempArray[k]) - 1;
                            k++;
                        }
                        if (mesh.VT.Count != 0)
                        {
                            f.T[i] = int.Parse(tempArray[k]) - 1;
                            k++;
                        }
                        if (mesh.VN.Count != 0)
                        {
                            f.N[i] = int.Parse(tempArray[k]) - 1;
                            k++;
                        }
                        i++;
                    }
                    mesh.F.Add(f);
                }
            }
        }
Exemplo n.º 12
0
 public static extern int HIMC_CircAbs(int nCtrlID, int nGroupID, CenterPosition center_pos, NormalVector normal_vector, int turns, CoordPosition target_pos, MotionProfile motion_profile, CoordSystem coord_sys, MotionBufferMode buf_mode, MotionTransitionMode trans_mode, double dTransPar);
 public static Vector3 AsVector3(this NormalVector obj)
 {
     return(new Vector3(obj.X, obj.Y, obj.Z));
 }
Exemplo n.º 14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        /// <param name="dirPath">マテリアルを読み込むために必要。</param>
        /// <returns></returns>
        public static Obj LoadObjWithMaterial(string[] data, string dirPath)
        {
            var obj = new Obj(data.Length / 3);

            foreach (var line in data)
            {
                string[] parts = line.Split(_separator, StringSplitOptions.RemoveEmptyEntries);

                if (parts.Length > 0)
                {
                    switch (parts[0])
                    {
                    case "usemtl":
                        obj.CurrentMtl = parts[1];
                        break;

                    case "mtllib":
                        obj.MtlFileList.Add(parts[1]);
                        break;

                    case "v":
                        var v = Vertex.LoadFromStringArray(parts);
                        obj.VertexList.Add(v);
                        v.Index = obj.VertexList.Count;

                        if (parts.Length == 7)
                        {
                            var vc = VertexColor.LoadFromStringArray(parts);
                            obj.VertexColorList.Add(vc);
                            vc.Index = obj.VertexColorList.Count;
                        }

                        break;

                    case "f":
                        var f = Face.LoadFromStringArray(parts);
                        f.UseMtl = obj.CurrentMtl;
                        obj.FaceList.Add(f);
                        break;

                    case "vt":
                        var uv = Uv.LoadFromStringArray(parts);
                        obj.UvList.Add(uv);
                        uv.Index = obj.UvList.Count;
                        break;

                    case "vn":
                        var vn = NormalVector.LoadFromStringArray(parts);
                        obj.NormalVectorList.Add(vn);
                        vn.Index = obj.NormalVectorList.Count;
                        break;
                    }
                }
            }

            if (obj.VertexColorList.Count != 0)
            {
                obj.ColorType = ColorType.VertexColor;
            }

            var hasTexture = obj.FaceList.FirstOrDefault()?.IsTextureEnable ?? false;

            if (hasTexture)
            {
                obj.ColorType = ColorType.TextureColor;
            }

            foreach (var mtlFile in obj.MtlFileList)
            {
                var lines = File.ReadAllLines(Path.Combine(dirPath, mtlFile));
                var mtl   = MtlLoader.LoadMtl(lines);
                obj.MtlList.Add(mtl);
            }

            return(obj);
        }
Exemplo n.º 15
0
            private Color CalculateColorForXandY(int x, int y)
            {
                Color objCol;

                if (constObjectColor)
                {
                    objCol = objectColor;
                }
                else
                {
                    lock (texture)
                    {
                        objCol = texture.GetPixel(x, y);
                    }
                }

                //Vector N
                NormalVector N = new NormalVector();

                if (constNormalVector)
                {
                    N.X = 0;
                    N.Y = 0;
                    N.Z = 1;
                }
                else
                {
                    N = ConvertNormalMapToNormalVector(x, y);
                }

                if (lightStationary == false)
                {
                    double d = Mesh.PointDistances(new Point(x, y), lightPos);
                    double k = d / 50;
                    if (d < 50)
                    {
                        L.Z = 1 - k * 3 * (d / 707) + dLz;
                    }
                    else
                    {
                        L.Z = 1 - k * 5 * (d / 707) + dLz;
                    }
                    if (L.Z > 1)
                    {
                        L.Z = 1;
                    }
                    if (L.Z < 0)
                    {
                        L.Z = 0;
                    }
                }
                else
                {
                    L.Z = 1;
                }

                double mdx = Math.Max(500 - lightPos.X, lightPos.X);
                double mdy = Math.Max(500 - lightPos.Y, lightPos.Y);

                L.X = (lightPos.X - x) / mdx;
                L.Y = (lightPos.Y - y) / mdy;

                //CosNL
                double cosNL = (N.X * L.X + N.Y * L.Y + N.Z * L.Z);

                //R
                NormalVector R = new NormalVector();

                R.X = cosNL * N.X - L.X;
                R.Y = cosNL * N.Y - L.Y;
                R.Z = cosNL * N.Z - L.Z;

                double cosVR = (V.X * R.X + V.Y * R.Y + V.Z * R.Z);

                double X;

                X = Kd * lightColor.R / 255 * objCol.R / 255 * cosNL + Ks * lightColor.R / 255 * objCol.R / 255 * Math.Pow(cosVR, m);
                if (X > 1)
                {
                    X = 1;
                }
                if (X < 0)
                {
                    X = 0;
                }
                double Y;

                Y = Kd * lightColor.G / 255 * objCol.G / 255 * cosNL + Ks * lightColor.G / 255 * objCol.G / 255 * Math.Pow(cosVR, m);
                if (Y > 1)
                {
                    Y = 1;
                }
                if (Y < 0)
                {
                    Y = 0;
                }
                double Z;

                Z = Kd * lightColor.B / 255 * objCol.B / 255 * cosNL + Ks * lightColor.B / 255 * objCol.B / 255 * Math.Pow(cosVR, m);
                if (Z > 1)
                {
                    Z = 1;
                }
                if (Z < 0)
                {
                    Z = 0;
                }

                Color col = Color.FromArgb((int)(X * 255), (int)(Y * 255), (int)(Z * 255));

                return(col);
            }