/*
* Renderable.cs
* Authors: August Zinsser
*
* Copyright August Zinsser 2007
* This program is distributed under the terms of the GNU General Public License
*/
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace Pina3D
{
///
/// A PinaObject that can be rendered
///
public class Renderable : PinaObject
{
protected IRenderableSourceData _Type; // Enforces rendering, but allows each sourceData type to implement it differently
public Renderable(PinaState spawnState, IRenderableSourceData sourceData)
{
mState = new RenderableState(spawnState, sourceData);
_Type = sourceData;
}
///
/// This Material will override the model's default material
///
public Material Material
{
set { ((RenderableState)mState).Material = value; }
get { return ((RenderableState)mState).Material; }
}
internal IRenderableSourceData SourceData
{
set { ((RenderableState)mState).SourceData = value; }
get { return ((RenderableState)mState).SourceData; }
}
///
/// Draws to the backbuffer
///
internal virtual void Render()
{
// TODO: Sort all of the renderables by sourceData and then call render on that sourcedata once with multiple PinaStates to optimize rendering
if (((RenderableState)mState).Shader != null)
_Type.Render(((RenderableState)mState).SourceData, ((RenderableState)mState).Shader, mState);
}
protected class RenderableState : PinaState
{
///
/// Only children will know how to interpret this
///
public IRenderableSourceData SourceData;
///
/// If assigned, use this material when rendering instead of the Model's default material
///
public Material Material
{
set
{
mMaterial = value;
mShader = new Shader(ref mMaterial);
}
get
{
return mMaterial;
}
}
///
/// Used only for rendering, but is based on the Material
///
public Shader Shader
{
get { return mShader; }
}
protected Material mMaterial;
protected Shader mShader;
public RenderableState(PinaState pinaState, IRenderableSourceData sourceData)
: base(pinaState.Matrix)
{
this.SourceData = sourceData;
this.mMaterial = null;
this.mShader = null;
}
}
}
public interface IRenderableSourceData
{
void Render(IRenderableSourceData sourceData, Shader shader, PinaState state);
}
}