Esempio n. 1
0
        private Tuple <DynamicVao, DynamicVao> TryBuildVaoForLight(Light light)
        {
            _hullVertices.Clear();
            _shadowVertices.Clear();
            _shadowIndices.Clear();
            _hullIndices.Clear();

            int numSegments       = 0;
            int shadowIndexOffset = 0;
            int hullIndexOffset   = 0;
            int hullCount         = _engine.Hulls.Count;

            for (int i = 0; i < hullCount; i++)
            {
                Hull hull = _engine.Hulls[i];
                if (!hull.Enabled ||
                    !hull.Valid ||
                    light.IgnoredHulls.Contains(hull) ||
                    !light.Intersects(hull))
                {
                    continue;
                }

                Polygon points = hull.WorldPoints;

                Vector2 prevPoint = points[points.Count - 1];

                int pointCount = points.Count;
                numSegments += pointCount;
                for (int j = 0; j < pointCount; j++)
                {
                    Vector2 currentPoint = points[j];

                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(0.0f, 0.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(1.0f, 0.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(0.0f, 1.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(1.0f, 1.0f)));

                    _shadowIndices.Add(shadowIndexOffset * 4 + 0);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 1);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 2);

                    _shadowIndices.Add(shadowIndexOffset * 4 + 1);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 3);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 2);

                    prevPoint = currentPoint;
                    shadowIndexOffset++;
                }

                _hullVertices.AddRange(hull.WorldPoints);

                int indexCount = hull.Indices.Count;
                for (int j = 0; j < indexCount; j++)
                {
                    _hullIndices.Add(hull.Indices[j] + hullIndexOffset);
                }
                hullIndexOffset += pointCount;
            }

            if (numSegments == 0)
            {
                return(null);
            }

            Tuple <DynamicVao, DynamicVao> lightVaos;

            if (!_lightsVaos.TryGetValue(light, out lightVaos))
            {
                lightVaos = Tuple.Create(
                    DynamicVao.New(_engine.Device, VertexShadow.Layout, PrimitiveType.TriangleList, _shadowVertices.Count, _shadowIndices.Count, useIndices: true),
                    DynamicVao.New(_engine.Device, VertexPosition2.Layout, PrimitiveType.TriangleList, _hullVertices.Count, _hullIndices.Count, useIndices: true));
                _lightsVaos.Add(light, lightVaos);
            }

            lightVaos.Item1.SetVertices(_shadowVertices);
            lightVaos.Item1.SetIndices(_shadowIndices);
            lightVaos.Item2.SetVertices(_hullVertices);
            lightVaos.Item2.SetIndices(_hullIndices);

            return(lightVaos);
        }
Esempio n. 2
0
        private void BuildVaosForLight(Light light, LightVaos lightVaos)
        {
            _hullVertices.Clear();
            _shadowVertices.Clear();
            _shadowIndices.Clear();
            _hullIndices.Clear();

            int numSegments       = 0;
            int shadowIndexOffset = 0;
            int hullIndexOffset   = 0;
            int hullCount         = _engine.Hulls.Count;

            for (int i = 0; i < hullCount; i++)
            {
                Hull hull = _engine.Hulls[i];
                if (!hull.Enabled ||
                    !hull.Valid ||
                    light.IgnoredHulls.Contains(hull) ||
                    !light.Intersects(hull))
                {
                    continue;
                }

                Polygon points = hull.WorldPoints;

                Vector2 prevPoint = points.Items[points.Count - 1];

                int pointCount = points.Count;
                numSegments += pointCount;
                for (int j = 0; j < pointCount; j++)
                {
                    Vector2 currentPoint = points.Items[j];

                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(0.0f, 0.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(1.0f, 0.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(0.0f, 1.0f)));
                    _shadowVertices.Add(new VertexShadow(prevPoint, currentPoint, new Vector2(1.0f, 1.0f)));

                    _shadowIndices.Add(shadowIndexOffset * 4 + 0);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 1);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 2);

                    _shadowIndices.Add(shadowIndexOffset * 4 + 1);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 3);
                    _shadowIndices.Add(shadowIndexOffset * 4 + 2);

                    prevPoint = currentPoint;
                    shadowIndexOffset++;
                }

                _hullVertices.AddRange(hull.WorldPoints);

                int indexCount = hull.Indices.Count;
                for (int j = 0; j < indexCount; j++)
                {
                    _hullIndices.Add(hull.Indices.Items[j] + hullIndexOffset);
                }
                hullIndexOffset += pointCount;
            }

            lightVaos.Enabled = numSegments > 0;
            if (!lightVaos.Enabled)
            {
                return;
            }

            if (lightVaos.ShadowVao == null)
            {
                lightVaos.ShadowVao = DynamicVao.New(
                    _engine.Device,
                    VertexShadow.Layout,
                    PrimitiveType.TriangleList,
                    _shadowVertices.Count,
                    _shadowIndices.Count,
                    useIndices: true);
                lightVaos.HullVao = DynamicVao.New(
                    _engine.Device,
                    VertexPosition2.Layout,
                    PrimitiveType.TriangleList,
                    _hullVertices.Count,
                    _hullIndices.Count,
                    useIndices: true);
            }

            lightVaos.ShadowVao.SetVertices(_shadowVertices);
            lightVaos.ShadowVao.SetIndices(_shadowIndices);
            lightVaos.HullVao.SetVertices(_hullVertices);
            lightVaos.HullVao.SetIndices(_hullIndices);
        }