All pastes #82692 Raw Edit

Wiz

public text v1 · immutable
·published 2006-07-08 21:03 UTC
rendered paste body
using System;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Drawing;
using System.Drawing.Drawing2D;
using Helicopter3D.Core;
using System.Collections.Generic;

namespace Helicopter3D.Game
{
    class LevelBuilder
    {
        #region Variables

        private VertexBuffer vb;
        private int ringVerts;
        private int numRings;
        private float ringGap;
        private IndexBuffer ib;
        private List<short[]> indices;

        #endregion

        #region Constructor

        public LevelBuilder()
        {
            ringVerts = 32;
            numRings = 2;
            ringGap = 100.0f;
            BuildLevel();
            BuildIndices();
        }

        #endregion

        #region Render

        public void Render()
        {
            Engine.Direct3DDevice.SetStreamSource(0, vb, 0);
            ib.SetData(indices[0], 0, LockFlags.None);
            Engine.Direct3DDevice.Indices = ib;
            
            Engine.Direct3DDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, indices[0].Length, 0, indices[0].Length - 2);
        }

        #endregion

        #region BuildLevel

        private List<CustomVertex.PositionColored> BuildRing(float depth)
        {

            List<CustomVertex.PositionColored> verts = new List<CustomVertex.PositionColored>(ringVerts);
            Random c = new Random();
            for(float deg = 0.0f; deg < 360.0f; deg += 360.0f / (float)ringVerts)
            {
                float x = (float)Math.Cos(Geometry.DegreeToRadian(deg));
                float y = (float)Math.Sin(Geometry.DegreeToRadian(deg));
                verts.Add(new CustomVertex.PositionColored(x, y, depth, c.Next(-16777215, 0)));
            }

            return verts;

        }

        private void BuildIndices()
        {
            indices = new List<short[]>();

            for(int i = 0; i < numRings -1; i++)
            {
                short[] array = new short[ringVerts * 2];
                int k = 0;
                for(int j = 0; j < ringVerts * 2; j +=2)
                {
                    array[j] = (short)k;
                    array[j + 1] = (short)(k + (ringVerts));
                    k++;
                }
                indices.Add(array);
            }
            ib = new IndexBuffer(typeof(short), ringVerts  * 2, Engine.Direct3DDevice, Usage.WriteOnly, Pool.Default);
            
        }

        private void BuildLevel()
        {
            vb = new VertexBuffer(typeof(CustomVertex.PositionColored),
                                    ringVerts * numRings,
                                    Engine.Direct3DDevice, 
                                    Usage.Dynamic | Usage.WriteOnly,
                                    CustomVertex.PositionColored.Format,
                                    Pool.Default);
            List<CustomVertex.PositionColored> verts = new List<CustomVertex.PositionColored>(ringVerts*numRings);
            for(int i = 0; i < numRings; i++)
            {
                verts.AddRange(BuildRing(ringGap * i));
            }

            CustomVertex.PositionColored[] arrayVerts;
            arrayVerts = verts.ToArray();
            vb.SetData(arrayVerts, 0, LockFlags.None);

        }

        #endregion
    }
}