protected void DrawElement(RenderElement currElem)
        {
            if (currElem.VertexDec != GFX.Device.VertexDeclaration)
                GFX.Device.VertexDeclaration = currElem.VertexDec;
            GFX.Device.Indices = currElem.IndexBuffer;
            GFX.Device.Vertices[0].SetSource(currElem.VertexBuffer, 0, currElem.VertexStride);
            for (int j = 0; j < currElem.Transform.Length; j += GFXShaderConstants.NUM_INSTANCES)
            {

                int binLength = currElem.Transform.Length - j;

                if (binLength > GFXShaderConstants.NUM_INSTANCES)
                    binLength = GFXShaderConstants.NUM_INSTANCES;

                if (currElem.IsAnimated)
                    binLength = 1;

                if (currElem.Transform.Length > 1 && !currElem.IsAnimated)
                {
                    // Upload transform matrices as shader constants.
                    Array.Copy(currElem.Transform, j, tempTransforms, 0, binLength);
                    GFX.Device.SetVertexShaderConstant(GFXShaderConstants.VC_WORLD, tempTransforms);
                }
                else
                {
                    GFX.Device.SetVertexShaderConstant(GFXShaderConstants.VC_WORLD, currElem.Transform);
                }
                GFX.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, currElem.StartVertex, currElem.VertexCount * binLength, 0, currElem.PrimitiveCount * binLength);
            }
        }
Exemple #2
0
        public Projectile(Actor sender, string tracerEffectName, string explosionEffectName)
        {
            this.sender = sender;
            renderElement = new RenderElement();
            renderElement.IndexBuffer = GFXPrimitives.SphereGeometry.Geometry.renderElement.IndexBuffer;
            renderElement.VertexBuffer = GFXPrimitives.SphereGeometry.Geometry.renderElement.VertexBuffer;
            renderElement.VertexCount = GFXPrimitives.SphereGeometry.Geometry.renderElement.VertexCount;
            renderElement.VertexDec = GFXPrimitives.SphereGeometry.Geometry.renderElement.VertexDec;
            renderElement.VertexStride = GFXPrimitives.SphereGeometry.Geometry.renderElement.VertexStride;
            renderElement.StartVertex = GFXPrimitives.SphereGeometry.Geometry.renderElement.StartVertex;
            renderElement.PrimitiveCount = GFXPrimitives.SphereGeometry.Geometry.renderElement.PrimitiveCount;

            projectileMaterial = ResourceManager.Inst.GetMaterial("GateMaterial");

            //tracerEffect = ResourceManager.Inst.GetParticleEffect(tracerEffectName);
            explosionEffect = ResourceManager.Inst.GetParticleEffect(explosionEffectName);
        }
 public void AddElement(Material material, RenderElement element)
 {
     if (!Elements.ContainsKey(material))
         Elements.Add(material, new Queue<RenderElement>());
     Elements[material].Enqueue(element);
 }
        void InitializeVoxels()
        {
            int voxelCountX = (DensityFieldWidth - 1) / VoxelGridSize;
            int voxelCountY = (DensityFieldHeight - 1) / VoxelGridSize;
            int voxelCountZ = (DensityFieldDepth - 1) / VoxelGridSize;
            Voxels = new VoxelGeometry[voxelCountX * voxelCountY * voxelCountZ];
            VoxelBounds = new BoundingBox[Voxels.Length];
            voxelKDTree = new KDTree<VoxelElement>(VoxelCompareFunction, VoxelBoundsFunction, false, true);
            Vector3 ratio = Vector3.One * 2.0f * (float)VoxelGridSize / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1);

            for (int z = 0; z < voxelCountZ; z++)
            {
                int zOff = voxelCountX * voxelCountY * z;
                for (int y = 0; y < voxelCountY; y++)
                {
                    int yOff = voxelCountX * y;

                    for (int x = 0; x < voxelCountX; x++)
                    {
                        int idx = x + yOff + zOff;
                        /*
                        VoxelBounds[idx] = new BoundingBox(new Vector3(x, y, z) * ratio - Vector3.One, new Vector3(x + 1, y + 1, z + 1) * ratio - Vector3.One);
                        VoxelBounds[idx].Min = Vector3.Transform(VoxelBounds[idx].Min, Transformation.GetTransform());
                        VoxelBounds[idx].Max = Vector3.Transform(VoxelBounds[idx].Max, Transformation.GetTransform());
                        */
                        Voxels[idx] = new VoxelGeometry((ushort)idx);
                        Voxels[idx].renderElement.Transform = new Matrix[1] { Transformation.GetTransform() };
                        Vector3 geometryRatio = 2.0f*Vector3.One / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1);
                        Voxels[idx].GenerateGeometry(ref DensityField, IsoValue, DensityFieldWidth, DensityFieldHeight, DensityFieldDepth, VoxelGridSize, VoxelGridSize, VoxelGridSize, x * VoxelGridSize, y * VoxelGridSize, z * VoxelGridSize, geometryRatio, this.Transformation.GetTransform());

                        VoxelBounds[idx] = MathUtils.TransformBounds(Voxels[idx].GetBounds(), Transformation.GetTransform());
                        if(Voxels[idx].CanRender)
                            voxelKDTree.AddElement(new VoxelElement(Voxels[idx], VoxelBounds[idx]), false);
                    }
                }
            }
            voxelKDTree.BuildTree();
            RecursivelyBuildBounds(voxelKDTree.GetRoot());

            terrainQuadMaterial = ResourceManager.Inst.GetMaterial("TerrainQuadMaterial");
            giantQuadElement = GFXPrimitives.Decal.GetRenderElement();
            Matrix quadMatrix = Matrix.CreateScale(1.5f) * Matrix.CreateRotationX(MathHelper.Pi) * this.Transformation.GetTransform();
            quadMatrix.Translation = Vector3.Up * this.Transformation.GetBounds().Min.Y;
            giantQuadElement.Transform = new Matrix[1] { quadMatrix };
        }
 public virtual void AddElement(Material material, RenderElement element)
 {
     if (!Elements.ContainsKey(material))
         Elements.Add(material, new CustomList<RenderElement>());
     Elements[material].Add(element);
 }
 void InitializeRenderElement()
 {
     renderElement = new RenderElement();
     renderElement.VertexDec = GFXVertexDeclarations.PNDec;
     renderElement.VertexStride = VertexPN.SizeInBytes;
     renderElement.StartVertex = 0;
 }
        public ScreenAlignedQuad(bool faceUp)
        {
            VertexPositionTexture[] verts;
            if(faceUp)
            {
                verts = new VertexPositionTexture[]
                        {
                            new VertexPositionTexture(
                                new Vector3(1,0,-1),
                                new Vector2(1,1)),
                            new VertexPositionTexture(
                                new Vector3(-1,0,-1),
                                new Vector2(0,1)),
                            new VertexPositionTexture(
                                new Vector3(-1,0,1),
                                new Vector2(0,0)),
                            new VertexPositionTexture(
                                new Vector3(1,0,1),
                                new Vector2(1,0))
                        };
            }
            else
            {
                verts = new VertexPositionTexture[]
                        {
                            new VertexPositionTexture(
                                new Vector3(1,-1,0),
                                new Vector2(1,1)),
                            new VertexPositionTexture(
                                new Vector3(-1,-1,0),
                                new Vector2(0,1)),
                            new VertexPositionTexture(
                                new Vector3(-1,1,0),
                                new Vector2(0,0)),
                            new VertexPositionTexture(
                                new Vector3(1,1,0),
                                new Vector2(1,0))
                        };
            }

            vertexBuffer = new VertexBuffer(GFX.Device, verts.Length * VertexPositionTexture.SizeInBytes, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionTexture>(verts);

            short[] ib = new short[] { 0, 1, 2, 2, 3, 0 };

            indexBuffer = new IndexBuffer(GFX.Device, sizeof(short) * ib.Length, BufferUsage.WriteOnly, IndexElementSize.SixteenBits);
            indexBuffer.SetData<short>(ib);

            renderElement = new RenderElement();
            renderElement.IndexBuffer = indexBuffer;
            renderElement.VertexBuffer = vertexBuffer;
            renderElement.StartVertex = 0;
            renderElement.PrimitiveCount = 2;
            renderElement.IsAnimated = false;
            renderElement.VertexCount = 4;
            renderElement.VertexDec = GFXVertexDeclarations.PTDec;
            renderElement.VertexStride = VertexPositionTexture.SizeInBytes;

            CreateInstancedBuffers(verts, ib);
        }
 public static RenderElement CreateBillboardElement()
 {
     RenderElement element;
     element = new RenderElement();
     element.IndexBuffer = GFXPrimitives.Quad.GetInstanceIndexBuffer();
     element.VertexBuffer = GFXPrimitives.Quad.GetInstanceVertexBuffer();
     element.VertexCount = 4;
     element.VertexDec = GFXVertexDeclarations.PTIDec;
     element.VertexStride = VertexPTI.SizeInBytes;
     element.StartVertex = 0;
     element.IsAnimated = false;
     element.PrimitiveCount = 2;
     return element;
 }
 void CreateRenderElements()
 {
     renderElements = new RenderElement[materials.Count];
     for (int i = 0; i < renderElements.Length; i++)
     {
         renderElements[i] = new RenderElement();
         renderElements[i].VertexCount = 4;
         renderElements[i].PrimitiveCount = 4;
         renderElements[i].StartVertex = 0;
         renderElements[i].VertexStride = VertexPTI.SizeInBytes;
         renderElements[i].VertexDec = GFXVertexDeclarations.PTIDec;
         renderElements[i].VertexBuffer = GFXPrimitives.Quad.GetInstanceVertexBuffer();
         renderElements[i].IndexBuffer = GFXPrimitives.Quad.GetInstanceIndexBufferDoubleSided();
     }
 }
Exemple #10
0
        public List<RenderElement> generateGeometry(Vector3 position, int varyTreeNum)
        {
            cylinderTransforms = new List<Matrix>();
            leafTransforms = new List<Matrix>();
            Vector3 offset = Vector3.Zero;
            offset.Y = -2.0f * forwardLength;
            initPosition = position + offset;
            maxPos = initPosition;
            minPos = initPosition;

            RenderElement cylinderMesh = new RenderElement();
            cylinderMesh.VertexBuffer = GFXPrimitives.CylinderGeometry.GetVertexBufferInstanced();
            cylinderMesh.IndexBuffer = GFXPrimitives.CylinderGeometry.GetIndexBufferInstanced();
            cylinderMesh.StartVertex = 0;
            cylinderMesh.VertexDec = GFXVertexDeclarations.PNTTIDec;
            cylinderMesh.VertexStride = VertexPNTTI.SizeInBytes;
            cylinderMesh.VertexCount = GFXPrimitives.CylinderGeometry.GetVertexCount();
            cylinderMesh.PrimitiveCount = GFXPrimitives.CylinderGeometry.GetPrimitiveCount();

            if (dirty)
            {
                result = generateResult(axiom, 0);
                dirty = false;
            }

            // For testing:
            //result = "G[+&F][-%F[+&F]]GFF@";

            initRotationStack();
            initTranslationStack();

            for (int i = 0; i < result.Length; i++)
            {

                float v;
                switch (result[i])
                {
                    case SYMBOL_FORWARD_F:
                        drawLine();
                        translate();
                        break;
                    case SYMBOL_FORWARD_G:
                        drawLine();
                        translate();
                        break;
                    case SYMBOL_FORWARD_NO_DRAW_F:
                        translate();
                        break;
                    case SYMBOL_FORWARD_NO_DRAW_G:
                        translate();
                        break;
                    case SYMBOL_PITCH_DOWN:
                        v = vary(turnValue);
                        rotate(v, 0.0f, 1.0f, 0.0f);
                        break;
                    case SYMBOL_PITCH_UP:
                        v = vary(turnValue);
                        rotate(-v, 0.0f, 1.0f, 0.0f);
                        break;
                    case SYMBOL_POP_MATRIX:
                        popMatrix();
                        forwardLength = forwardLength / 0.7f;
                        break;
                    case SYMBOL_PUSH_MATRIX:
                        pushMatrix();
                        forwardLength = forwardLength * 0.7f;
                        break;
                    case SYMBOL_ROLL_LEFT:
                        rotate(turnValue, 1.0f, 0.0f, 0.0f);
                        break;
                    case SYMBOL_ROLL_RIGHT:
                        rotate(-turnValue, 1.0f, 0.0f, 0.0f);
                        break;
                    case SYMBOL_CLUSTER:
                        drawLeafCluster(sphereRadius);
                        break;
                    case SYMBOL_TURN_AROUND:
                        rotate(180.0f, 0.0f, 0.0f, 0.0f);
                        break;
                    case SYMBOL_TURN_LEFT:
                        rotate(-turnValue, 0.0f, 0.0f, 1.0f);
                        break;
                    case SYMBOL_TURN_RIGHT:
                        rotate(turnValue, 0.0f, 0.0f, 1.0f);
                        break;
                    case SYMBOL_TURN_RAND_LEFT:
                        v = vary(turnValue + (lineCount*40 % 360));
                     //   float leftRotAngle = (1 + lineCount);

                        rotate(-v, 0.0f, 1.0f, 0.0f);
                        break;
                    case SYMBOL_TURN_RAND_RIGHT:
                        v = vary(turnValue + (lineCount * 40 % 360));
                     //   float rightRotAngle = (1+ lineCount) * 10;
                        rotate(v, 0.0f, 1.0f, 0.0f);
                        break;
                }

            }

            RenderElement leaves = new RenderElement();
            leaves.StartVertex = 0;
            leaves.VertexCount = 4;
            leaves.PrimitiveCount = 4;
            leaves.VertexDec = GFXVertexDeclarations.PTIDec;
            leaves.VertexStride = VertexPTI.SizeInBytes;
            leaves.VertexBuffer = GFXPrimitives.Quad.GetInstanceVertexBuffer();
            leaves.IndexBuffer = GFXPrimitives.Quad.GetInstanceIndexBufferDoubleSided();
            leaves.Transform = leafTransforms.ToArray();

            cylinderMesh.Transform = cylinderTransforms.ToArray();
            List<RenderElement> elements = new List<RenderElement>();
            elements.Add(cylinderMesh);
            elements.Add(leaves);

            return elements;
        }
Exemple #11
0
 public Imposter(Mesh mesh)
 {
     Element = new RenderElement();
     Element.IndexBuffer = GFXPrimitives.ImposterGeometry.GetInstanceIndexBuffer();
     Element.IsAnimated = false;
     Element.PrimitiveCount = 8;
     Element.StartVertex = 0;
     Element.VertexBuffer = GFXPrimitives.ImposterGeometry.GetInstanceVertexBuffer();
     Element.VertexCount = 16;
     Element.VertexDec = GFXVertexDeclarations.PTIDec;
     Element.VertexStride = VertexPTI.SizeInBytes;
     //GFXPrimitives.CreateBillboardElement();
     ImposterMaterial = new Material();
     Vector3 scale = (mesh.GetBounds().Max - mesh.GetBounds().Min);
     Scale = Matrix.CreateScale(scale);
 }