Beispiel #1
0
        public void SetSystem(LSystems.System system)
        {
            this.system = system;

            // Rebuild system.
            Clear(ref this.vertexesPtr);
            Clear(ref this.colorsPtr);
            Clear(ref this.normalsPtr);
            Clear(ref this.triVertexesPtr);
            Clear(ref this.triColorsPtr);
            Clear(ref this.triNormalsPtr);

            this.numQuadVertexes = 0;
            this.numTriVertexes  = 0;

            this.vertexes.Clear();
            this.normals.Clear();
            this.colors.Clear();
            this.triVertexes.Clear();
            this.triNormals.Clear();
            this.triColors.Clear();

            this.helperLines.Clear();

            ResetState();

            this.Push();

            if (this.system != null)
            {
                //Debug.WriteLine("---------------------");
                //foreach (object obj in (IEnumerable)this.system.String)
                //{
                //    Debug.WriteLine(string.Format("{0}", obj.GetType()));
                //}

                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition) this.system.Definition).Turtle = this;
                }

                var interpreter = new LSystems.Turtle.Interpreter();
                interpreter.RegisterExternalFunctions(this.system.Definition);
                interpreter.Interpret(this, this.system.String as IEnumerable);
                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition) this.system.Definition).Turtle = null;
                }
            }

            this.Pop();

            // Generate normals.
            for (int i = 0; i < vertexes.Count; i += 12)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(vertexes[i], vertexes[i + 1], vertexes[i + 2]),
                    new OpenTK.Vector3(vertexes[i + 3], vertexes[i + 4], vertexes[i + 5]),
                    new OpenTK.Vector3(vertexes[i + 6], vertexes[i + 7], vertexes[i + 8]));

                for (int j = 0; j < 4; ++j)
                {
                    Add(normals, normal);
                }
            }

            for (int i = 0; i < triVertexes.Count; i += 9)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(triVertexes[i], triVertexes[i + 1], triVertexes[i + 2]),
                    new OpenTK.Vector3(triVertexes[i + 3], triVertexes[i + 4], triVertexes[i + 5]),
                    new OpenTK.Vector3(triVertexes[i + 6], triVertexes[i + 7], triVertexes[i + 8]));

                for (int j = 0; j < 3; ++j)
                {
                    Add(triNormals, normal);
                }
            }

            // Copy arrays in non-managed memory.
            Create(this.vertexes, ref this.vertexesPtr);
            Create(this.colors, ref this.colorsPtr);
            Create(this.normals, ref this.normalsPtr);

            this.numQuadVertexes = this.vertexes.Count / 3;

            Create(this.triVertexes, ref this.triVertexesPtr);
            Create(this.triColors, ref this.triColorsPtr);
            Create(this.triNormals, ref this.triNormalsPtr);

            this.numTriVertexes = this.triVertexes.Count / 3;

            this.toolStripLabelStatistics.Text =
                string.Format("{0} quads, {1} triangles.", this.numQuadVertexes / 4, this.numTriVertexes / 3);

            // Force redraw.
            glControl1.Invalidate();
        }
        public void SetSystem(LSystems.System system)
        {
            this.system = system;

            // Rebuild system.
            Clear(ref this.vertexesPtr);
            Clear(ref this.colorsPtr);
            Clear(ref this.normalsPtr);
            Clear(ref this.triVertexesPtr);
            Clear(ref this.triColorsPtr);
            Clear(ref this.triNormalsPtr);

            this.numQuadVertexes = 0;
            this.numTriVertexes = 0;

            this.vertexes.Clear();
            this.normals.Clear();
            this.colors.Clear();
            this.triVertexes.Clear();
            this.triNormals.Clear();
            this.triColors.Clear();

            this.helperLines.Clear();

            ResetState();

            this.Push();

            if (this.system != null)
            {
                //Debug.WriteLine("---------------------");
                //foreach (object obj in (IEnumerable)this.system.String)
                //{
                //    Debug.WriteLine(string.Format("{0}", obj.GetType()));
                //}

                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition)this.system.Definition).Turtle = this;
                }

                var interpreter = new LSystems.Turtle.Interpreter();
                interpreter.RegisterExternalFunctions(this.system.Definition);
                interpreter.Interpret(this, this.system.String as IEnumerable);
                if (this.system.Definition is LSystems.Turtle.SystemDefinition)
                {
                    ((LSystems.Turtle.SystemDefinition)this.system.Definition).Turtle = null;
                }
            }

            this.Pop();

            // Generate normals.
            for (int i = 0; i < vertexes.Count; i += 12)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(vertexes[i], vertexes[i + 1], vertexes[i + 2]),
                    new OpenTK.Vector3(vertexes[i + 3], vertexes[i + 4], vertexes[i + 5]),
                    new OpenTK.Vector3(vertexes[i + 6], vertexes[i + 7], vertexes[i + 8]));

                for (int j = 0; j < 4; ++j) { Add(normals, normal); }
            }

            for (int i = 0; i < triVertexes.Count; i += 9)
            {
                OpenTK.Vector3 normal = CalcNormal(
                    new OpenTK.Vector3(triVertexes[i], triVertexes[i + 1], triVertexes[i + 2]),
                    new OpenTK.Vector3(triVertexes[i + 3], triVertexes[i + 4], triVertexes[i + 5]),
                    new OpenTK.Vector3(triVertexes[i + 6], triVertexes[i + 7], triVertexes[i + 8]));

                for (int j = 0; j < 3; ++j) { Add(triNormals, normal); }
            }

            // Copy arrays in non-managed memory.
            Create(this.vertexes, ref this.vertexesPtr);
            Create(this.colors, ref this.colorsPtr);
            Create(this.normals, ref this.normalsPtr);

            this.numQuadVertexes = this.vertexes.Count / 3;

            Create(this.triVertexes, ref this.triVertexesPtr);
            Create(this.triColors, ref this.triColorsPtr);
            Create(this.triNormals, ref this.triNormalsPtr);

            this.numTriVertexes = this.triVertexes.Count / 3;

            this.toolStripLabelStatistics.Text =
                string.Format("{0} quads, {1} triangles.", this.numQuadVertexes / 4, this.numTriVertexes / 3);

            // Force redraw.
            glControl1.Invalidate();
        }