public static PackedGeometry Pack(LineSet lineSet)
        {
            PackedGeometry packed;
            Coordinate     coordinate;

            packed = new PackedGeometry();

            coordinate = (Coordinate)lineSet.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Coordinate));

            packed.RGBA             = false;
            packed.RGB              = false;
            packed.Coloring         = false;
            packed.generateColorMap = false;

            if (coordinate != null)
            {
                packed._coords = X3DTypeConverters.MFVec3f(coordinate.point);

                packed.restartIndex = null;
                packed.vertexStride = 2;
                packed.vertexCount  = lineSet.vertexCount;

                packed.Interleave();
            }

            return(packed);
        }
        public static PackedGeometry Pack(IndexedLineSet ils)
        {
            PackedGeometry packed;
            Coordinate     coordinate;

            packed = new PackedGeometry();
            //packed.Texturing = ifs.texCoordinate != null;// || parentShape.texturingEnabled;

            coordinate = (Coordinate)ils.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Coordinate));

            packed.RGBA             = false;
            packed.RGB              = false;
            packed.Coloring         = false;
            packed.generateColorMap = false;

            if (coordinate != null && !string.IsNullOrEmpty(ils.coordIndex))
            {
                packed._indices = X3DTypeConverters.ParseIndicies(ils.coordIndex);
                packed._coords  = X3DTypeConverters.MFVec3f(coordinate.point);

                if (ils.coordIndex.Contains(RESTART_INDEX.ToString()))
                {
                    packed.restartIndex = RESTART_INDEX;
                }

                packed.vertexStride = 2;

                packed.Interleave();
            }

            return(packed);
        }
        public static PackedGeometry Pack(IndexedFaceSet ifs)
        {
            PackedGeometry    packed;
            TextureCoordinate texCoordinate;
            Coordinate        coordinate;
            Color             colorNode;
            ColorRGBA         colorRGBANode;

            packed = new PackedGeometry();
            //packed.Texturing = ifs.texCoordinate != null;// || parentShape.texturingEnabled;

            texCoordinate = (TextureCoordinate)ifs.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(TextureCoordinate));
            coordinate    = (Coordinate)ifs.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Coordinate));
            colorNode     = (Color)ifs.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Color));
            colorRGBANode = (ColorRGBA)ifs.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(ColorRGBA));

            packed.RGBA             = colorRGBANode != null;
            packed.RGB              = colorNode != null;
            packed.Coloring         = packed.RGBA || packed.RGB;
            packed.generateColorMap = packed.Coloring;

            if (packed.RGB && !packed.RGBA)
            {
                packed.color = X3DTypeConverters.Floats(colorNode.color);
            }
            else if (packed.RGBA && !packed.RGB)
            {
                packed.color = X3DTypeConverters.Floats(colorRGBANode.color);
            }

            if (texCoordinate != null && !string.IsNullOrEmpty(ifs.texCoordIndex))
            {
                packed._texIndices = X3DTypeConverters.ParseIndicies(ifs.texCoordIndex);
                packed._texCoords  = X3DTypeConverters.MFVec2f(texCoordinate.point);
                packed.Texturing   = true;
            }

            if (coordinate != null && !string.IsNullOrEmpty(ifs.coordIndex))
            {
                packed._indices = X3DTypeConverters.ParseIndicies(ifs.coordIndex);
                packed._coords  = X3DTypeConverters.MFVec3f(coordinate.point);

                if (!string.IsNullOrEmpty(ifs.colorIndex))
                {
                    packed._colorIndicies = X3DTypeConverters.ParseIndicies(ifs.colorIndex);
                }

                if (ifs.coordIndex.Contains(RESTART_INDEX.ToString()))
                {
                    packed.restartIndex = RESTART_INDEX;
                }

                packed.Interleave();
            }

            return(packed);
        }
        public static PackedGeometry InterleavePacks(List <PackedGeometry> packs)
        {
            PackedGeometry p;

            p = new PackedGeometry();

            foreach (PackedGeometry g in packs)
            {
                p.interleaved3.AddRange(g.interleaved3);
                p.interleaved4.AddRange(g.interleaved4);
            }

            return(p);
        }
Exemplo n.º 5
0
        public static GeometryHandle BufferShaderGeometry(PackedGeometry packSet)
        {
            GeometryHandle handle;

            handle = new GeometryHandle();

            if (packSet.interleaved3.Count > 0)
            {
                Buffering.BufferShaderGeometry(packSet.interleaved3, out handle.vbo3, out handle.NumVerticies3);
            }

            if (packSet.interleaved4.Count > 0)
            {
                Buffering.BufferShaderGeometry(packSet.interleaved4, out handle.vbo4, out handle.NumVerticies4);
            }

            return(handle);
        }
        public static PackedGeometry Pack(PointSet pointSet)
        {
            PackedGeometry packed;
            Coordinate     coordinate;
            Color          colorNode;
            ColorRGBA      colorRGBANode;

            packed = new PackedGeometry();

            coordinate    = (Coordinate)pointSet.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Coordinate));
            colorNode     = (Color)pointSet.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(Color));
            colorRGBANode = (ColorRGBA)pointSet.ChildrenWithAppliedReferences.FirstOrDefault(n => n.GetType() == typeof(ColorRGBA));

            packed.RGBA             = colorRGBANode != null;
            packed.RGB              = colorNode != null;
            packed.Coloring         = packed.RGBA || packed.RGB;
            packed.generateColorMap = packed.Coloring;

            if (packed.RGB && !packed.RGBA)
            {
                packed.color = X3DTypeConverters.Floats(colorNode.color);
            }
            else if (packed.RGBA && !packed.RGB)
            {
                packed.color = X3DTypeConverters.Floats(colorRGBANode.color);
            }

            if (coordinate != null)
            {
                packed._coords = X3DTypeConverters.MFVec3f(coordinate.point);

                packed.restartIndex = null;
                packed.vertexStride = 1;

                packed.Interleave();
            }

            return(packed);
        }
        public void Interleave(bool calcBounds = true)
        {
            PackedGeometry pack = this;

            Buffering.Interleave(ref pack, genTexCoordPerVertex: false, calcBounds: calcBounds);
        }
Exemplo n.º 8
0
        private static void interleaveVertex(out Vertex v, ref PackedGeometry pack, int faceType, int k)
        {
            v = Vertex.Zero;

            if (pack._coords != null)
            {
                v.Position = pack._coords[pack.faceset[k]];
            }

            pack.maximum = MathHelpers.Max(v.Position, pack.maximum);
            pack.minimum = MathHelpers.Min(v.Position, pack.minimum);

            // Flip Z and Y
            //tmp = v.Position.Z;
            //v.Position.Z = -v.Position.Y;
            //v.Position.Y = tmp;

            if (pack.texset != null && pack.texset.Count > 0 && pack._texCoords != null && pack._texCoords.Length > 0)
            {
                v.TexCoord = pack._texCoords[pack.texset[k]];
            }
            //else if (genTexCoordPerVertex && texturing && _bbox != null)
            //{
            //    //v.TexCoord = MathHelpers.uv(v.Position.x);
            //    v = MathHelpers.uv(_bbox, new Vertex[] { v }, at_origin: false)[0];
            //}

            if (pack.Coloring)
            {
                if (pack.colorPerVertex)
                {
                    if (pack.colset != null && pack.colset.Count > 0 && pack.color != null && pack.color.Length > 0)
                    {
                        if (pack.colset.Count == 3)
                        {
                            v.Color = new Vector4(
                                pack.color[pack.colset[k]],
                                pack.color[pack.colset[k] + 1],
                                pack.color[pack.colset[k] + 2],
                                1.0f
                                );
                        }
                        else if (pack.colset.Count == 4)
                        {
                            v.Color = new Vector4(
                                pack.color[pack.colset[k]],
                                pack.color[pack.colset[k] + 1],
                                pack.color[pack.colset[k] + 2],
                                pack.color[pack.colset[k] + 3]
                                );
                        }
                        else if (pack.colset.Count == 1)
                        {
                            v.Color = new Vector4(
                                pack.color[pack.colset[k]],
                                pack.color[pack.colset[k] + 1],
                                pack.color[pack.colset[k] + 2],
                                1.0f
                                );
                        }
                    }
                }
                else
                {
                    // color per face
                }
            }

            if (pack.normals != null && pack.normals.Length > 0)
            {
                //TODO: interleave normals

                //v.Normal = pack.normals[faceset[k]];
            }

            if (pack.vertexStride.HasValue)
            {
                if (pack.vertexStride == 1 || pack.vertexStride == 2 || pack.vertexStride == 3)
                {
                    pack.interleaved3.Add(v);
                }

                if (pack.vertexStride == 4)
                {
                    pack.interleaved4.Add(v);
                }
            }
            else
            {
                switch (faceType)
                {
                case 3:
                    pack.interleaved3.Add(v);
                    break;

                case 4:
                    pack.interleaved4.Add(v);
                    break;

                case 2:
                    break;
                }
            }
        }
Exemplo n.º 9
0
        public static void Interleave(ref PackedGeometry pack, bool genTexCoordPerVertex = true, bool calcBounds = true)
        {
            int FACE_RESTART_INDEX = 2;

            pack.colorPerVertex = true;

            int coordIndex = 0;
            int faceSetIndex = 0;
            int faceSetValue, texSetValue = -1, colSetValue = -1;
            int faceType = 0;
            int i;

            pack.faceset = new List <int>();
            pack.texset  = new List <int>();
            pack.colset  = new List <int>();
            List <Vertex> verticies2 = new List <Vertex>();

            pack.interleaved3 = new List <Vertex>(); // buffer verticies of different face types separatly
            pack.interleaved4 = new List <Vertex>();
            Vertex v;

            //Vector4 c;
            //float tmp;

            pack.maximum = new Vector3(float.MinValue, float.MinValue, float.MinValue);
            pack.minimum = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);

            // MESH INTERLEAVING ALGORITHM
            if (pack._indices == null && pack.vertexStride.HasValue)
            {
                i             = -1;
                pack._indices = pack._coords.Select(c => ++ i).ToArray();

                Console.WriteLine("Interleaving {0} verticies", pack._coords.Length / pack.vertexStride.Value);

                if (pack.vertexStride > 4)
                {
                    Console.WriteLine("vertexStride must be <= 4");

                    return;
                }

                if (pack._colorIndicies == null && pack.Coloring)
                {
                    i = -1;
                    if (pack.RGB)
                    {
                        pack._colorIndicies = pack.color.Take(pack.color.Length / 3).Select(c => ++ i).ToArray();
                    }
                    else if (pack.RGBA)
                    {
                        pack._colorIndicies = pack.color.Take(pack.color.Length / 4).Select(c => ++ i).ToArray();
                    }
                }

                for (coordIndex = 0; coordIndex < pack._coords.Length; coordIndex++)
                {
                    faceSetValue = pack._indices[coordIndex];

                    if (pack._texIndices != null && pack._texIndices.Length > 0)
                    {
                        texSetValue = pack._texIndices[coordIndex];
                    }

                    if (pack._colorIndicies != null && pack._colorIndicies.Length > 0)
                    {
                        colSetValue = pack._colorIndicies[coordIndex];
                    }

                    if (pack.vertexStride.Value == 1 || coordIndex % pack.vertexStride.Value == 1)
                    {
                        if (pack.vertexStride.Value == 1)
                        {
                            faceType = 1;
                            pack.faceset.Add(faceSetValue);

                            if (pack._texIndices != null && pack._texIndices.Length > 0)
                            {
                                pack.texset.Add(texSetValue);
                            }

                            if (pack._colorIndicies != null && pack._colorIndicies.Length > 0)
                            {
                                pack.colset.Add(colSetValue);
                            }
                        }

                        for (int k = 0; k < faceType; k++)
                        {
                            interleaveVertex(out v, ref pack, faceType, k);
                        }

                        faceSetIndex++;
                        faceType = 0;
                        pack.faceset.Clear();
                        pack.texset.Clear();
                        pack.colset.Clear();
                    }
                    else
                    {
                        faceType++;
                        pack.faceset.Add(faceSetValue);

                        if (pack._texIndices != null && pack._texIndices.Length > 0)
                        {
                            pack.texset.Add(texSetValue);
                        }

                        if (pack._colorIndicies != null && pack._colorIndicies.Length > 0)
                        {
                            pack.colset.Add(colSetValue);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Interleaving {0} indicies", pack._indices.Length);

                if (pack.restartIndex.HasValue)
                {
                    for (coordIndex = 0; coordIndex < pack._indices.Length; coordIndex++)
                    {
                        faceSetValue = pack._indices[coordIndex];

                        if (pack._texIndices != null && pack._texIndices.Length > 0)
                        {
                            texSetValue = pack._texIndices[coordIndex];
                        }

                        if (pack._colorIndicies != null && pack._colorIndicies.Length > 0)
                        {
                            colSetValue = pack._colorIndicies[coordIndex];
                        }

                        if (faceSetValue == pack.restartIndex.Value)
                        {
                            for (int k = 0; k < faceType; k++)
                            {
                                interleaveVertex(out v, ref pack, faceType, k);
                            }

                            faceSetIndex++;
                            faceType = 0;
                            pack.faceset.Clear();
                            pack.texset.Clear();
                            pack.colset.Clear();
                        }
                        else
                        {
                            faceType++;
                            pack.faceset.Add(faceSetValue);

                            if (pack._texIndices != null && pack._texIndices.Length > 0)
                            {
                                pack.texset.Add(texSetValue);
                            }

                            if (pack._colorIndicies != null && pack._colorIndicies.Length > 0)
                            {
                                pack.colset.Add(colSetValue);
                            }
                        }
                    }
                }
                else
                {
                    // NO RESTART INDEX, assume new face is at every 3rd value / i = 2

                    if (pack._indices.Length == 4)
                    {
                        FACE_RESTART_INDEX = 4; // 0-3 Quad
                    }
                    else if (pack._indices.Length == 3)
                    {
                        FACE_RESTART_INDEX = 3; // 0-3 Triangle
                    }
                    else
                    {
                        FACE_RESTART_INDEX = 3;
                    }

                    for (coordIndex = 0; coordIndex < pack._indices.Length; coordIndex++)
                    {
                        faceSetValue = pack._indices[coordIndex];
                        pack.faceset.Add(faceSetValue);
                        faceType++;

                        if (pack._texIndices != null)
                        {
                            texSetValue = pack._texIndices[coordIndex];
                            pack.texset.Add(texSetValue);
                        }

                        if (coordIndex > 0 && (coordIndex + 1) % FACE_RESTART_INDEX == 0)
                        {
                            for (int k = 0; k < faceType; k++)
                            {
                                interleaveVertex(out v, ref pack, faceType, k);
                            }

                            faceSetIndex++;
                            faceType = 0;
                            pack.faceset.Clear();
                            pack.texset.Clear();
                        }
                    }
                }
            }

            if (calcBounds)
            {
                Vector3 x3dScale = new Vector3(0.06f, 0.06f, 0.06f);

                pack.bbox = new BoundingBox()
                {
                    Width  = (pack.maximum.X - pack.minimum.X),
                    Height = (pack.maximum.Y - pack.minimum.Y),
                    Depth  = (pack.maximum.Z - pack.minimum.Z),

                    Maximum = pack.maximum,
                    Minimum = pack.minimum
                };

                // corners
                // (min_x,min_y), (min_x,max_y), (max_x,max_y), (max_x,min_y)
            }
            else
            {
                pack.bbox = null;
            }

            pack.faceset = null;
            pack.texset  = null;
            pack.colset  = null;

            //Dont buffer geometry here, just interleave
        }