オノデラの研究日記 in わんくま

思いついたネタを気ままに書いていくブログ

ホーム 連絡をする 同期する ( RSS 2.0 ) Login
投稿数  209  : 記事  5  : コメント  5960  : トラックバック  40

ニュース

プロフィール

  • ●おのでら
    宮城県在住
    主に業務向けソフトを製作

Twitter

ニュース

主なリンク

XNA 関連リンク

アイテム

ゲーマーカード

その他

記事カテゴリ

書庫

日記カテゴリ

 前回「WPF上でManaged DirectXを使用する」で WPF ウインドウに Managed DirectX で作成したビューを表示しました。今回は XNA のライブラリを使ってポリゴンを表示させたいと思います。以下がサンプル画面です。内容は前回とほとんど同じです。

WPF上でXNAを使用する

 今回 XNA のライブラリを使いますが、WindowsFormsHost コントロールを使うところは特に違いはなく、単純に Managed DirectX のコードを XNA Framework に置き換えたような感じになります。

 XNA Framework を使用するので、参照に「Microsoft.Xna.Framework」を追加してください。「Microsoft.Xna.Framework.Game」もありますが、こちらはゲーム専用プロジェクトで使うものなので入れる必要はありません。

Microsoft.Xna.Framework

 以下が、GtaphicsDeviceControl のコードです(デザイナ部分除く)。ファイルやコードの作り方とかは前回の「WPF上でManaged DirectXを使用する」を参照してください。コードも Managed DirectX のときと似ていると思います。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAOnWPF
{
    /// <summary>
    /// グラフィックデバイスコントロール
    /// </summary>
    public partial class GraphicsDeviceControl : Control
    {
        /// <summary>
        /// グラフィックデバイス
        /// </summary>
        private GraphicsDevice device = null;

        /// <summary>
        /// エフェクト
        /// </summary>
        private BasicEffect effect = null;

        /// <summary>
        /// 頂点データ
        /// </summary>
        private VertexPositionColor[] vertices = new VertexPositionColor[3];
        /// <summary>
        /// 頂点データ
        /// </summary>
        public VertexPositionColor[] Vertices
        {
            get { return this.vertices; }
        }


        /// <summary>
        /// コンストラクタ
        /// </summary>
        public GraphicsDeviceControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// コントロールが作成されるとき
        /// </summary>
        protected override void OnCreateControl()
        {
            if (this.DesignMode == false)
            {
                try
                {
                    // デバイス作成
                    PresentationParameters pp = new PresentationParameters();
                    pp.SwapEffect = SwapEffect.Discard;
                    pp.BackBufferWidth = 300;
                    pp.BackBufferHeight = 300;
                    pp.EnableAutoDepthStencil = true;
                    pp.AutoDepthStencilFormat = DepthFormat.Depth16;
                    this.device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
                        DeviceType.Hardware, this.Handle, pp);

                    // 頂点データの設定
                    this.vertices[0] = new VertexPositionColor(
                        new Vector3(0.0f, -2.0f + (float)Math.Sqrt(3) * 3.0f, 0.0f),
                        new Microsoft.Xna.Framework.Graphics.Color(255, 0, 0));
                    this.vertices[1] = new VertexPositionColor(
                        new Vector3(3.0f, -2.0f, 0.0f),
                        new Microsoft.Xna.Framework.Graphics.Color(0, 255, 0));
                    this.vertices[2] = new VertexPositionColor(
                        new Vector3(-3.0f, -2.0f, 0.0f),
                        new Microsoft.Xna.Framework.Graphics.Color(0, 0, 255));

                    // 頂点定義
                    this.device.VertexDeclaration =
                        new VertexDeclaration(this.device, VertexPositionColor.VertexElements);

                    // エフェクト
                    this.effect = new BasicEffect(this.device, null);
                    this.effect.VertexColorEnabled = true;

                    // ビュー変換行列を設定
                    this.effect.View = Matrix.CreateLookAt(
                        new Vector3(0.0f, 0.0f, -10.0f),
                        new Vector3(0.0f, 0.0f, 0.0f),
                        Vector3.Up);

                    // 射影変換を設定
                    this.effect.Projection = Matrix.CreatePerspectiveFieldOfView(
                        MathHelper.ToRadians(45.0f), 1.0f, 1.0f, 100.0f);

                    // レンダリングステート設定
                    this.device.RenderState.CullMode = CullMode.None;
                    this.device.RenderState.AlphaBlendEnable = true;
                    this.device.RenderState.SourceBlend = Blend.SourceAlpha;
                    this.device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                }
            }

            base.OnCreateControl();
        }

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }

            if (disposing)
            {
                if (this.device != null)
                {
                    this.device.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// 描画イベント
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            this.Draw();

            base.OnPaint(pe);
        }

        /// <summary>
        /// 描画
        /// </summary>
        private void Draw()
        {
            if (this.device == null)
            {
                return;
            }

            this.device.Clear(Microsoft.Xna.Framework.Graphics.Color.DarkBlue);

            // ポリゴンを描画する
            this.effect.Begin();
            this.effect.Techniques[0].Passes[0].Begin();

            this.effect.World = Matrix.CreateRotationY((float)Environment.TickCount / 1000.0f);
            this.device.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList, vertices, 0, 1);

            this.effect.Techniques[0].Passes[0].End();
            this.effect.End();

            this.device.Present();
        }

        /// <summary>
        /// タイマーイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.Draw();
        }
    }
}

 WPF の方(XAMLとプログラム)は前回とほぼ同じで、違うのは名前空間ぐらいです。詳しくはサンプルプロジェクトのほうを参照してください。

 以下、実行ファイルです。「.NET Framework 3.0」と「最新の DirectX ランタイム」「Microsoft XNA Framework Redistributable 2.0」が必要です。また、ハードウェア要件として「ピクセルシェーダ 1.1 以上に対応したグラフィックカード」が必要です(たぶん)。

 サンプルプロジェクト一式です。「Visual Studio 2008 Professional Edition」で作成しています。XNA は Visual Studio 2008 には対応していないと言われているので、ビルドコンテンツなどが使えない可能性があるかもしれません(このあたりはまだ試していません)。

投稿日時 : 2008年1月29日 8:50

コメント

# WPFウインドウ上にXNAのビューを表示する 2008/01/30 12:48 オノデラの研究日記 in わんくま
WPFウインドウ上にXNAのビューを表示する

# dsnxbzAtqlorpWdST 2022/04/19 10:59 johnansog
http://imrdsoacha.gov.co/silvitra-120mg-qrms

# prednisone generic brand name https://deltasone.icu/
generic prednisone for sale 2022/08/22 17:35 Prednisone
prednisone generic brand name https://deltasone.icu/
generic prednisone for sale

# best ed treatment pills https://ed-pills.xyz/
best pills for ed 2022/09/16 2:34 EdPills
best ed treatment pills https://ed-pills.xyz/
best pills for ed

# order doxycycline 100mg without prescription https://antibiotic.best/ 2022/10/08 8:53 Antibiotic
order doxycycline 100mg without prescription https://antibiotic.best/

# ed pills comparison https://cheapestedpills.com/
non prescription ed pills 2022/12/10 22:15 CheapPills
ed pills comparison https://cheapestedpills.com/
non prescription ed pills

# the best ed pill https://edpills.science/
best treatment for ed 2023/01/07 14:00 Edpills
the best ed pill https://edpills.science/
best treatment for ed

# Read now. Comprehensive side effect and adverse reaction information.
https://canadianfast.com/
Cautions. Get information now. 2023/02/20 11:52 CanadaBest
Read now. Comprehensive side effect and adverse reaction information.
https://canadianfast.com/
Cautions. Get information now.

# doxycycline mono - https://doxycyclinesale.pro/# 2023/04/21 23:22 Doxycycline
doxycycline mono - https://doxycyclinesale.pro/#

# buy cytotec online - https://cytotecsale.pro/# 2023/04/29 6:30 Cytotec
buy cytotec online - https://cytotecsale.pro/#

# meds online without doctor prescription https://pillswithoutprescription.pro/# 2023/05/16 11:33 PillsPro
meds online without doctor prescription https://pillswithoutprescription.pro/#

# paxlovid price https://paxlovid.pro/# - buy paxlovid online 2023/07/02 23:13 Paxlovid
paxlovid price https://paxlovid.pro/# - buy paxlovid online

# paxlovid generic https://paxlovid.store/
paxlovid covid 2023/07/13 17:49 Paxlovid
paxlovid generic https://paxlovid.store/
paxlovid covid

# Paxlovid buy online https://paxlovid.life/# paxlovid covid 2023/07/26 1:57 Paxlovid
Paxlovid buy online https://paxlovid.life/# paxlovid covid

# top sites dating 2023/08/09 16:09 WayneGurry
tinder online: http://datingtopreview.com/# - all free dating site

# indian pharmacies safe 2023/08/20 18:34 Jeffreybloft
https://stromectolonline.pro/# ivermectin ebay

# п»їcytotec pills online 2023/08/26 1:58 Georgejep
https://misoprostol.guru/# Misoprostol 200 mg buy online

# purchase cytotec 2023/08/27 2:53 Georgejep
http://misoprostol.guru/# cytotec buy online usa

# farmacia online senza ricetta 2023/09/24 23:05 Archieonelf
https://pharmacieenligne.icu/# Pharmacie en ligne livraison 24h

# migliori farmacie online 2023 2023/09/26 0:12 Archieonelf
https://farmaciaonline.men/# migliori farmacie online 2023

# farmacie online sicure 2023/09/27 0:38 Archieonelf
https://farmaciaonline.men/# comprare farmaci online all'estero

# men's ed pills https://edpillsotc.store/# - over the counter erectile dysfunction pills 2023/10/08 2:51 EdPills
men's ed pills https://edpillsotc.store/# - over the counter erectile dysfunction pills

# doxycycline gel in india 2023/10/09 0:41 GaylordPah
Their digital prescription service is innovative and efficient. https://edpillsotc.store/# ed pills for sale

# how to get valtrex prescription https://valtrex.auction/ valtrex tablets for sale 2023/10/24 23:44 Valtrex
how to get valtrex prescription https://valtrex.auction/ valtrex tablets for sale

# generic prednisone 10mg https://prednisonepharm.store/ prednisone 10 mg online 2024/01/20 13:16 Prednisone
generic prednisone 10mg https://prednisonepharm.store/ prednisone 10 mg online

# pin up aviator https://pinupcassino.pro/ aviator oficial pin up
2024/03/12 2:51 Cassino
pin up aviator https://pinupcassino.pro/ aviator oficial pin up


Post Feedback

タイトル
名前
Url:
コメント