Esempio n. 1
0
        public static VertexsIndicesData Copy(VertexsIndicesData other)
        {
            VertexsIndicesData newVID = new VertexsIndicesData();

            newVID.VertexFormat = other.VertexFormat;
            newVID.VertexCount  = other.VertexCount;
            newVID.IndicesCount = other.IndicesCount;
            newVID.Vertexs      = new List <Vertex>();
            foreach (var item in other.Vertexs)
            {
                Vertex tempVertex = new Vertex();
                tempVertex.Color    = new Color4(item.Color.R, item.Color.G, item.Color.B, item.Color.A);
                tempVertex.Normal   = new Vector3(item.Normal.X, item.Normal.Y, item.Normal.Z);
                tempVertex.Position = new Vector3(item.Position.X, item.Position.Y, item.Position.Z);
                tempVertex.TexCoord = new Vector2(item.TexCoord.X, item.TexCoord.Y);
                newVID.Vertexs.Add(tempVertex);
            }

            newVID.Indices = new List <uint>();
            foreach (var item in other.Indices)
            {
                newVID.Indices.Add(item);
            }

            return(newVID);
        }
Esempio n. 2
0
        public TextRender(int width, int height, Vector2 position,
                          FontFamily fontFamily, float fontSize = 12, bool visible = true)
        {
            Width    = width;
            Height   = height;
            Position = position;

            Font = new System.Drawing.Font(fontFamily,
                                           fontSize,
                                           System.Drawing.FontStyle.Regular);
            TextBrush       = Brushes.White;
            BackgroundColor = Color.FromArgb(150, 0, 0, 0);
            FontStyle       = System.Drawing.FontStyle.Regular;

            Visible = visible;

            VertexsIndicesData =
                Tools.DeserializeModel(@"Models\Panel3D.xml");

            // initialize shaders
            string vs = File.ReadAllText("Shaders\\vShader_UV_Text.txt");
            string fs = File.ReadAllText("Shaders\\fShader_UV_Text.txt");

            Shader = new Shader(ref vs, ref fs, this);
            // initialize buffer
            VertexFormat = NRCGL.VertexFormat.XY_UV;
            VertexBuffer = new VertexFloatBuffer(VertexFormat, 512);
        }
Esempio n. 3
0
        public static VertexsIndicesData DeserializeModel(string path)
        {
            VertexsIndicesData vertexsIndicesData = new VertexsIndicesData();

            XmlSerializer xmlSerializer = new XmlSerializer(vertexsIndicesData.GetType());
            StreamReader  reader        = new StreamReader(path);

            vertexsIndicesData = (VertexsIndicesData)xmlSerializer.Deserialize(reader);

            reader.Close();

            return(vertexsIndicesData);
        }
Esempio n. 4
0
        public static VertexsIndicesData CopyPositionsAndNormals(VertexsIndicesData other)
        {
            VertexsIndicesData newVID = new VertexsIndicesData();

            newVID.VertexFormat = other.VertexFormat;
            newVID.VertexCount  = other.VertexCount;
            newVID.IndicesCount = other.IndicesCount;
            newVID.Vertexs      = new List <Vertex>();
            foreach (var item in other.Vertexs)
            {
                Vertex tempVertex = new Vertex();
                tempVertex.Color    = item.Color;
                tempVertex.Normal   = new Vector3(item.Normal.X, item.Normal.Y, item.Normal.Z);
                tempVertex.Position = new Vector3(item.Position.X, item.Position.Y, item.Position.Z);
                tempVertex.TexCoord = item.TexCoord;
                newVID.Vertexs.Add(tempVertex);
            }

            newVID.Indices = other.Indices;


            return(newVID);
        }
Esempio n. 5
0
        public Sphere3D(Vector3 position, float r, Color4 color,
                        int textureId = 0, bool isSmoothShading = true) : base()
        {
            this.r          = r;
            IsSmoothShading = isSmoothShading;
            Bounding        = new Bounding(this, r);

            Model = @"Models\sphere3D.xml";

            if (isSmoothShading)
            {
                // smooth shading
                if (vid_smooth is VertexsIndicesData)
                {
                    VertexsIndicesData = vid_smooth;
                }
                else
                {
                    vid_smooth = Tools.DeserializeModel(@"Models\sphere3D_smooth.xml");

                    VertexsIndicesData = vid_smooth;
                }
            }
            else
            {
                // flat shading
                if (vid is VertexsIndicesData)
                {
                    VertexsIndicesData = vid;
                }
                else
                {
                    vid = Tools.DeserializeModel(Model);

                    VertexsIndicesData = vid;
                }
            }

            Scale(r);

            Position = position;

            FirstPosition = Position;

            ShapeVersorsUVW = Matrix4.Identity;

            TextureID = textureId;

            if (textureId == 0)
            {
                foreach (Vertex vertex in VertexsIndicesData.Vertexs)
                {
                    vertex.Color = color;
                }

                // initialize shaders
                string vs = File.ReadAllText("Shaders\\vShader_Color_Normal1.txt");
                string fs = File.ReadAllText("Shaders\\fShader_Color_Normal1.txt");
                Shader = new Shader(ref vs, ref fs, this);
                // initialize buffer
                VertexFormat = NRCGL.VertexFormat.XYZ_NORMAL_COLOR;
                VertexBuffer = new VertexFloatBuffer(VertexFormat, 97920, BeginMode.Triangles);
            }
            else
            {
                // initialize shaders
                string vs = File.ReadAllText("Shaders\\vShader_UV_Normal.txt");
                string fs = File.ReadAllText("Shaders\\fShader_UV_Normal_sphere.txt");
                Shader = new Shader(ref vs, ref fs, this);
                // initialize buffer
                VertexFormat = NRCGL.VertexFormat.XYZ_NORMAL_UV_COLOR;
                VertexBuffer = new VertexFloatBuffer(VertexFormat, 97920);
            }
        }
Esempio n. 6
0
        public static void GenerateModelFrom3DS(string path)
        {
            var x3d = new X3D();

            var xmlSerializer = new XmlSerializer(x3d.GetType());
            var reader        = new StreamReader(path);

            x3d = (X3D)xmlSerializer.Deserialize(reader);

            reader.Close();

            var scene = new X3DScene();

            scene = x3d.Scene[0];

            var transf0 = new Transform();

            transf0 = scene.Transform[0];

            var transf1 = new Transform();

            transf1 = transf0.Transform1[0];

            var transfGroup = new TransformGroup();

            transfGroup = transf1.Group[0];

            var transfGroupShape = new TransformGroupShape();

            transfGroupShape = transfGroup.Shape[0];

            var shapeIndexedFaceSet
                = new TransformGroupShapeIndexedFaceSet();

            // indices
            shapeIndexedFaceSet = transfGroupShape.IndexedFaceSet[0];

            // position coords
            TransformGroupShapeIndexedFaceSetCoordinate positionCoords
                = new TransformGroupShapeIndexedFaceSetCoordinate();

            positionCoords = shapeIndexedFaceSet.Coordinate[0];

            // texture coords
            TransformGroupShapeIndexedFaceSetTextureCoordinate texCoords
                = new TransformGroupShapeIndexedFaceSetTextureCoordinate();

            texCoords = shapeIndexedFaceSet.TextureCoordinate[0];

            /*
             * string output = shapeIndexedFaceSet.coordIndex + "\n";
             * output += shapeIndexedFaceSet.texCoordIndex + "\n";
             * output += positionCoords.point + "\n";
             * output += texCoords.point + "\n";
             */


            var positionsList = new List <Vector3>();

            string[] positionCoordsStr = positionCoords.point.Split(' ');

            for (int i = 0; i < positionCoordsStr.Length - 1;)
            {
                var vector3 = new Vector3(
                    Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture),
                    Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture),
                    Convert.ToSingle(positionCoordsStr[i++], CultureInfo.InvariantCulture));

                positionsList.Add(vector3);
            }

            var texCoordsList = new List <Vector2>();

            string[] texCoordsStr = texCoords.point.Split(' ');

            // OpenTK flips V texCoord. Used (U, 1-V) to convert from X3D to OpenTK
            for (int i = 0; i < texCoordsStr.Length - 1;)
            {
                var vector2 = new Vector2(
                    Convert.ToSingle(texCoordsStr[i++], CultureInfo.InvariantCulture),
                    1 - Convert.ToSingle(texCoordsStr[i++], CultureInfo.InvariantCulture));


                texCoordsList.Add(vector2);
            }



            // generate model xml

            var vertexsIndicesData = new VertexsIndicesData();

            vertexsIndicesData.VertexFormat = VertexFormat.XYZ_NORMAL_UV_COLOR;

            var vertexs = new List <Vertex>();
            int idx     = 0;


            var texCoordIndexs = new List <uint>();
            var coordIndexs    = new List <uint>();

            string[] coordsIndexStr    = shapeIndexedFaceSet.coordIndex.Split(' ');
            string[] texCoordsIndexStr = shapeIndexedFaceSet.texCoordIndex.Split(' ');

            int vertCount = 0; // check if is 4 or 3 when -1 arrives
            int v         = 0;

            for (int ic = 0; ic < coordsIndexStr.Length; ic++)
            {
                if (coordsIndexStr[ic] == "-1")
                {
                    switch (vertCount)
                    {
                    case 4:
                        #region quad
                        var vertex0 = new Vertex();
                        vertex0.Color    = Color4.Chocolate;
                        vertex0.Normal   = Vector3.Zero;
                        vertex0.Position = new Vector3(positionsList[(int)coordIndexs[idx]]);
                        vertex0.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]);
                        vertexs.Add(vertex0);
                        v++;
                        var vertex1 = new Vertex();
                        vertex1.Color    = Color4.Chocolate;
                        vertex1.Normal   = Vector3.Zero;
                        vertex1.Position = new Vector3(positionsList[(int)coordIndexs[idx + 1]]);
                        vertex1.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 1]]);
                        vertexs.Add(vertex1);
                        v++;
                        var vertex2 = new Vertex();
                        vertex2.Color    = Color4.Chocolate;
                        vertex2.Normal   = Vector3.Zero;
                        vertex2.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]);
                        vertex2.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]);
                        vertexs.Add(vertex2);
                        v++;
                        // normal
                        Vector3 edge1 = Vector3.Subtract(vertex1.Position, vertex0.Position);
                        Vector3 edge2 = Vector3.Subtract(vertex2.Position, vertex0.Position);
                        vertex0.Normal = Vector3.Cross(edge1, edge2);
                        vertex1.Normal = vertex0.Normal;
                        vertex2.Normal = vertex0.Normal;

                        Vertex vertex3 = new Vertex();
                        vertex3.Color    = Color4.Chocolate;
                        vertex3.Normal   = Vector3.Zero;
                        vertex3.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]);
                        vertex3.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]);
                        vertexs.Add(vertex3);
                        v++;
                        Vertex vertex4 = new Vertex();
                        vertex4.Color    = Color4.Chocolate;
                        vertex4.Normal   = Vector3.Zero;
                        vertex4.Position = new Vector3(positionsList[(int)coordIndexs[idx + 3]]);
                        vertex4.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 3]]);
                        vertexs.Add(vertex4);
                        v++;
                        Vertex vertex5 = new Vertex();
                        vertex5.Color    = Color4.Chocolate;
                        vertex5.Normal   = Vector3.Zero;
                        vertex5.Position = new Vector3(positionsList[(int)coordIndexs[idx]]);
                        vertex5.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]);
                        vertexs.Add(vertex5);
                        v++;
                        idx += 4;

                        // normal
                        Vector3 edge3 = Vector3.Subtract(vertex4.Position, vertex3.Position);
                        Vector3 edge4 = Vector3.Subtract(vertex5.Position, vertex3.Position);
                        vertex3.Normal = Vector3.Cross(edge3, edge4);
                        vertex4.Normal = vertex3.Normal;
                        vertex5.Normal = vertex3.Normal;
                        #endregion
                        break;

                    case 3:
                        #region triangle
                        Vertex vertex6 = new Vertex();
                        vertex6.Color    = Color4.Chocolate;
                        vertex6.Normal   = Vector3.Zero;
                        vertex6.Position = new Vector3(positionsList[(int)coordIndexs[idx]]);
                        vertex6.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx]]);
                        vertexs.Add(vertex6);
                        v++;
                        Vertex vertex7 = new Vertex();
                        vertex7.Color    = Color4.Chocolate;
                        vertex7.Normal   = Vector3.Zero;
                        vertex7.Position = new Vector3(positionsList[(int)coordIndexs[idx + 1]]);
                        vertex7.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 1]]);
                        vertexs.Add(vertex7);
                        v++;
                        Vertex vertex8 = new Vertex();
                        vertex8.Color    = Color4.Chocolate;
                        vertex8.Normal   = Vector3.Zero;
                        vertex8.Position = new Vector3(positionsList[(int)coordIndexs[idx + 2]]);
                        vertex8.TexCoord = new Vector2(texCoordsList[(int)texCoordIndexs[idx + 2]]);
                        vertexs.Add(vertex8);
                        v++;
                        idx += 3;
                        // normal
                        Vector3 edge5 = Vector3.Subtract(vertex7.Position, vertex6.Position);
                        Vector3 edge6 = Vector3.Subtract(vertex8.Position, vertex6.Position);
                        vertex6.Normal = Vector3.Cross(edge5, edge6);
                        vertex7.Normal = vertex6.Normal;
                        vertex8.Normal = vertex6.Normal;
                        #endregion
                        break;

                    default:
                        break;
                    }
                    vertCount = 0;
                    continue;
                }

                if (coordsIndexStr[ic] != String.Empty)
                {
                    coordIndexs.Add(Convert.ToUInt16(coordsIndexStr[ic]));
                }

                if (texCoordsIndexStr[ic] != String.Empty)
                {
                    texCoordIndexs.Add(Convert.ToUInt16(texCoordsIndexStr[ic]));
                }

                vertCount++;
            }


            // bug! there are triangles also
            vertexsIndicesData.VertexCount  = (coordIndexs.Count / 4) * 6;
            vertexsIndicesData.IndicesCount = (coordIndexs.Count / 4) * 6;

            var indices = new List <uint>();
            for (int i = 0; i < vertexsIndicesData.IndicesCount; i++)
            {
                indices.Add((uint)i);
            }

            vertexsIndicesData.Vertexs = vertexs;
            vertexsIndicesData.Indices = indices;

            var xmlSerializerModel = new XmlSerializer(typeof(VertexsIndicesData));
            var textWriter         = new StringWriter();
            xmlSerializerModel.Serialize(textWriter, vertexsIndicesData);

            File.WriteAllText(path + "_out", textWriter.ToString());

            //File.WriteAllText(@"Models\test.txt", output);

            // smooth shadow

            var solvedVertexesPositions = new List <Vector3>();

            for (int i = 0; i < vertexsIndicesData.Vertexs.Count; i++)
            {
                if (solvedVertexesPositions.Contains(
                        vertexsIndicesData.Vertexs[i].Position))
                {
                    continue;
                }

                solvedVertexesPositions.
                Add(vertexsIndicesData.Vertexs[i].Position);

                var normal = new Vector3();

                normal = vertexsIndicesData.Vertexs[i].Normal;

                var vertexes2Solve = new Stack <Vertex>();
                vertexes2Solve.Push(vertexsIndicesData.Vertexs[i]);

                for (int j = 0; j < vertexsIndicesData.Vertexs.Count; j++)
                {
                    if (vertexsIndicesData.Vertexs[i].Position ==
                        vertexsIndicesData.Vertexs[j].Position &&
                        i != j)
                    {
                        normal += vertexsIndicesData.Vertexs[j].Normal;
                        vertexes2Solve.Push(vertexsIndicesData.Vertexs[j]);
                    }
                }

                normal.Normalize();

                while (vertexes2Solve.Count > 0)
                {
                    vertexes2Solve.Pop().Normal = normal;
                }
            }

            var xmlSerializerModelSmooth = new XmlSerializer(typeof(VertexsIndicesData));
            var textWriter1 = new StringWriter();
            xmlSerializerModelSmooth.Serialize(textWriter1, vertexsIndicesData);

            File.WriteAllText(path + "_out_smooth", textWriter1.ToString());
        }
Esempio n. 7
0
        public void SerializeBufer(string path)
        {
            VertexsIndicesData vertexsIndicesData = new VertexsIndicesData();

            vertexsIndicesData.VertexFormat = Format;
            vertexsIndicesData.VertexCount  = (vertex_position / Stride) * 4;
            vertexsIndicesData.IndicesCount = index_position;
            List <Vertex> vertexs = new List <Vertex>();

            for (int v = 0; v < vertexsIndicesData.VertexCount * (Stride / 4);)
            {
                Vertex vertex = new Vertex();
                switch (Format)
                {
                case VertexFormat.XY:
                    break;

                case VertexFormat.XY_COLOR:
                    break;

                case VertexFormat.XY_UV:
                    break;

                case VertexFormat.XY_UV_COLOR:
                    break;

                case VertexFormat.XYZ:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    break;

                case VertexFormat.XYZ_COLOR:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.Color    = new Color4(vertex_data[v++], vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    break;

                case VertexFormat.XYZ_UV:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.TexCoord = new Vector2(vertex_data[v++], vertex_data[v++]);
                    break;

                case VertexFormat.XYZ_UV_COLOR:
                    break;

                case VertexFormat.XYZ_NORMAL_COLOR:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.Normal   = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.Color    = new Color4(vertex_data[v++], vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    break;

                case VertexFormat.XYZ_NORMAL_UV:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.Normal   = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.TexCoord = new Vector2(vertex_data[v++], vertex_data[v++]);
                    break;

                case VertexFormat.XYZ_NORMAL_UV_COLOR:
                    vertex.Position = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.Normal   = new Vector3(vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    vertex.TexCoord = new Vector2(vertex_data[v++], vertex_data[v++]);
                    vertex.Color    = new Color4(vertex_data[v++], vertex_data[v++], vertex_data[v++], vertex_data[v++]);
                    break;

                default:
                    break;
                }
                vertexs.Add(vertex);
            }
            List <uint> indices = new List <uint>();

            for (int i = 0; i < vertexsIndicesData.IndicesCount; i++)
            {
                indices.Add(index_data[i]);
            }

            vertexsIndicesData.Vertexs = vertexs;
            vertexsIndicesData.Indices = indices;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(VertexsIndicesData));
            TextWriter    textWriter    = new StringWriter();

            xmlSerializer.Serialize(textWriter, vertexsIndicesData);

            File.WriteAllText(path, textWriter.ToString());
        }