MaterialBucket.cs :  » Game » RealmForge » Axiom » Core » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » Core » MaterialBucket.cs
using System;
using System.Collections;
using System.Text;
using System.IO;

using Axiom.Graphics;
using Axiom.MathLib;
using Axiom.Collections;

namespace Axiom.Core{
  
  public class MaterialBucket
  {
    #region Fields and Properties
    protected LODBucket parent;
    protected string materialName;
    protected Material material;
    protected Technique technique;

    protected GeometryBucketList geometryBucketList;
    protected CurrentGeometryMap currentGeometryMap;
    public string MaterialName
    {
      get 
      {
        return materialName;
      }
    }

    public LODBucket Parent
    {
      get 
      {
        return parent;
      }
    }

    public Material Material
    {
      get 
      {
        return material;
      }
    }

    public Technique CurrentTechnique
    {
      get 
      {
        return technique;
      }
    }

    #endregion

    #region Constructors
      
    public MaterialBucket(LODBucket parent, string materialName)
    {
      this.parent = parent;
      this.materialName = materialName;
    }

    #endregion

    #region Proteced Methods
    protected string GetGeometryFormatString(SubMeshLodGeometryLink geom)
    {
      // Formulate an identifying string for the geometry format
      // Must take into account the vertex declaration and the index type
      // Format is (all lines separated by '|'):
      // Index type
      // Vertex element (repeating)
      //   source
      //   semantic
      //   type
      string str = string.Format("{0}|", geom.indexData.indexBuffer.Type);

      for(int i = 0; i < geom.vertexData.vertexDeclaration.ElementCount; ++i)
      {
        VertexElement elem = geom.vertexData.vertexDeclaration.GetElement(i);
        str += string.Format("{0}|{0}|{1}|{2}|", elem.Source, elem.Semantic, elem.Type);
      }
      return str;
    }

    #endregion

    #region Public Methods
    public void Assign(QueuedGeometry qgeom)
    {
      // Look up any current geometry
      string formatString = GetGeometryFormatString(qgeom.geometry);
      bool newBucket = true;
      if(currentGeometryMap.ContainsKey(formatString))
      {
        GeometryBucket gbucket = (GeometryBucket)currentGeometryMap[formatString];
        // Found existing geometry, try to assign
        newBucket = !gbucket.Assign(qgeom);
        // Note that this bucket will be replaced as the 'current'
        // for this format string below since it's out of space
      }
      // Do we need to create a new one?
      if(newBucket)
      {
        GeometryBucket gbucket = new GeometryBucket(this, formatString, qgeom.geometry.vertexData, qgeom.geometry.indexData);
        // Add to main list
        geometryBucketList.Add(gbucket);
        // Also index in 'current' list
        currentGeometryMap[formatString] = gbucket;
        if(!gbucket.Assign(qgeom))
        {
          throw new AxiomException("Somehow we couldn't fit the requested geometry even in a " +
            "brand new GeometryBucket!! Must be a bug, please report.");
        }
      }
    }

    public void Build(bool stencilShadows)
    {
      material = MaterialManager.Instance.GetByName(materialName);
      if(null == material)
      {
        throw new AxiomException("Material '{0}' not found.", materialName);
      }
      material.Load();
      // tell the geometry buckets to build
      IEnumerator iter = geometryBucketList.GetEnumerator();
      while(iter.MoveNext())
      {
        GeometryBucket gbucket = (GeometryBucket)iter.Current;
        gbucket.Build(stencilShadows);
      }
    }

    public void AddRenderables(RenderQueue queue, RenderQueueGroupID group, float camDistanceSquared)
    {
      // determine the current material technique
      technique = material.GetTechnique(material.GetLodIndexSquaredDepth(camDistanceSquared));

      IEnumerator iter = geometryBucketList.GetEnumerator();
      while(iter.MoveNext())
      {
        GeometryBucket gbucket = (GeometryBucket)iter.Current;
        queue.AddRenderable(gbucket, RenderQueue.DEFAULT_PRIORITY, group);
      }
    }

    public IEnumerator GetGeometryEnumerator()
    {
      return geometryBucketList.GetEnumerator();
    }

    public void Dump(TextWriter output)
    {
      output.WriteLine("Material Bucket {0}", materialName);
      output.WriteLine("--------------------------------------------------");
      output.WriteLine("Geometry buckets: {0}", geometryBucketList.Count);
      for(int i = 0; i < geometryBucketList.Count; ++i)
      {
        GeometryBucket gbucket = (GeometryBucket)geometryBucketList[i];
        gbucket.Dump(output);
      }
      output.WriteLine("--------------------------------------------------");
    }
    #endregion
  }

}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.