Ejemplo n.º 1
0
        private Dictionary <IVertexArrayObject, int> PrepareVAOs(IEnumerable <Entity> entities)
        {
            Dictionary <IVertexArrayObject, int> simulatorVAOs = new Dictionary <IVertexArrayObject, int>();

            Dictionary <Enums.EntityType, IVertexArrayObject> simVAOs         = new Dictionary <Enums.EntityType, IVertexArrayObject>();
            Dictionary <Enums.EntityType, List <Matrix4x4> >  transformations = new Dictionary <Enums.EntityType, List <Matrix4x4> >();
            Dictionary <Enums.EntityType, List <Vector4> >    colors          = new Dictionary <Enums.EntityType, List <Vector4> >();
            Dictionary <Enums.EntityType, int> instanceCounts = new Dictionary <Enums.EntityType, int>();

            foreach (var entity in entities)
            {
                if (!simVAOs.ContainsKey(entity.Type))
                {
                    Mesh mesh = null;
                    switch (entity.Type)
                    {
                    case Enums.EntityType.Triangle:
                        mesh = MeshCreator.CreateTriangle();
                        break;

                    case Enums.EntityType.Tetrahedron:
                        mesh = MeshCreator.CreateteTrahedron();
                        break;
                    }

                    if (mesh != null)
                    {
                        switch (_wrapper)
                        {
                        case RenderSimulator _:
                            simVAOs.Add(entity.Type, VAOLoader.FromMesh <SimulatorVAO>(mesh, new Tuple <VertexShader, FragmentShader>(_vertex, _fragment), new object[] { _wrapper }));
                            break;

                        case RenderTranslator _:
                            simVAOs.Add(entity.Type, VAOLoader.FromMesh <TranslatorVAO>(mesh, new Tuple <VertexShader, FragmentShader>(_vertex, _fragment), new object[] { _wrapper }));
                            break;
                        }
                        transformations.Add(entity.Type, new List <Matrix4x4>());
                        colors.Add(entity.Type, new List <Vector4>());
                        instanceCounts.Add(entity.Type, 0);
                    }
                }
                if (transformations.ContainsKey(entity.Type))
                {
                    transformations[entity.Type].Add(entity.Transformation);
                    colors[entity.Type].Add(entity.Color);
                    instanceCounts[entity.Type]++;
                }
            }

            foreach (var key in simVAOs.Keys)
            {
                simVAOs[key].SetAttribute("InstanceTransformation", new Tuple <VertexShader, FragmentShader>(_vertex, _fragment), transformations[key], true);
                simVAOs[key].SetAttribute("Color", new Tuple <VertexShader, FragmentShader>(_vertex, _fragment), colors[key], true);
                simulatorVAOs.Add(simVAOs[key], instanceCounts[key]);
            }

            return(simulatorVAOs);
        }