Mango Teapot ② Teapot クラス

ソースコードは MangoTeapot.codeplex.com にあります。

  1. Silverlight と XNA によるグラフィックス デバイスの共有
  2. Teapot クラス
  3. 加速度センサーと モーション
  4. カメラ

以前 Silverlight 5 (beta) で Teapot を描画したとき、頂点バッファとインデックスバッファを基に Blinn-Phong シェーダーで描画する Teapot クラスを作成しました。そのときの Teapot クラスに以下の変更を加えました。おもに XNA 4.0 への対応とシェーダーから固定機能(BasicEffect)への移行です。

IVertexType

XNA 4.0 ではカスタム頂点フォーマットを定義する際に IVertexType を継承しなければならないので、それに対応しました。

private struct VertexPositionNormal : IVertexType

{

  private Vector3 _vertexPosition;

  private Vector3 _vertexNormal;

 

  public Vector3 Position

  {

    get { return _vertexPosition; }

    set { _vertexPosition = value; }

  }

  public Vector3 Normal

  {

    get { return _vertexNormal; }

    set { _vertexNormal = value; }

  }

  public VertexPositionNormal(
    float positionX, float positionY, float positionZ, 
    float normalX, float normalY, float normalZ)

  {

    _vertexPosition = new Vector3(positionX, positionY, positionZ);

    _vertexNormal = new Vector3(normalX, normalY, normalZ);

  }

  public static readonly VertexDeclaration VertexDeclaration =
    new VertexDeclaration(

      new VertexElement(0, VertexElementFormat.Vector3,
        VertexElementUsage.Position, 0),

      new VertexElement(12, VertexElementFormat.Vector3,
        VertexElementUsage.Normal, 0)
);

  VertexDeclaration IVertexType.VertexDeclaration

  {

    get { return VertexDeclaration; }

  }

}

 

BasicEffect

Silverlight 5 では BasicEffect が使えずカスタム シェーダー エフェクトしか使えなかったのですが、逆に Windows Phone ではカスタム シェーダー エフェクトが使えず BasicEffect しか使えません。BasicEffect のデフォルト設定に(EnableDefaultLighting)、ピクセル単位のシェーディングを有効にしました(PreferPerPixelLighting)。いちばんマンゴー色に似ていたので、ディフューズ色を Color.Goldenrod にしました。

private BasicEffect effect;

// BasicEffect

effect = new BasicEffect(device);

effect.EnableDefaultLighting();

effect.DiffuseColor = Color.Goldenrod.ToVector3();

effect.PreferPerPixelLighting = true;

もちろん World, View, Projection 行列はシェーダー レジスターではなく、BasicEffect オブジェクトの各プロパティに代入します。

// Set BasicEffect parameters

effect.World = this.World;

effect.View = this.View;

effect.Projection = this.Projection;

effect.SpecularPower = this.Shininess;

また、XNA 4.0 から描画時のエフェクトの適用は、Begin(), End() ではなく Apply() メソッドになりました。

foreach (EffectPass p in effect.CurrentTechnique.Passes)

{

  p.Apply();

  device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                               0, 0, numVertices, 0, numElements);

}

abstract:ソースコードは MangoTeapot.codeplex.com にあります。 Silverlight と XNA によるグラフィックス デバイスの共有 Teapot クラス 加速度センサーと モーション カメラ 以前 Silverlight 5 (be
 

转载于:https://www.cnblogs.com/gdhm2/archive/2011/10/25/2223957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值