Windowsストアアプリ入門(DirectX) vol1.Direct3Dプロジェクトの概要その1

木曜日 , 14, 2月 2013 Leave a comment

 Windowsストアアプリの開発と普及に携わるなか情報の普及が遅れていると感じるジャンルがあります。それがDirectXです。

 DirectX11.1の情報はもちろんストアアプリでのDirecrXを用いた開発情報が不足していると日々感じています。

 

 というわけで、DirectX初心者ながら入門者の第一歩目ぐらいは支援すべく記事を書いていこうと思います。

 私自身がほとんど初心者なのでサンドバックになりながら成長していこうと思います。

(本筋はやはりC#+XAMLなので、更新速度は遅くなるかもしれませんが)

 

 というわけで第一回は初期Direct3D用の初期プロジェクトの概要から。

 

「Direct3Dアプリケーション」プロジェクト

 

  WindowsストアアプリでDirectX(特にDirect3D)を使う場合「Direct3Dアプリケーション」から始めることになると思います。

001

 今回はこの「Direct3Dアプリケーション」プロジェクトのコードを追ってみたいと思います。

 

プロジェクトの概要

 

 プロジェクトのファイル構成は以下です。

 002

 主なファイルを紹介します。

 

・Direct3DApp1.cpp & Direct3DApp1.h

 

 {プロジェクト名}.cppとそのヘッダーファイルです。

 cppファイルにはプロジェクトのエントリポイントになるmainメソッドが定義されています。

 

[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
	auto direct3DApplicationSource = ref new Direct3DApplicationSource();
	CoreApplication::Run(direct3DApplicationSource);
	return 0;
}

 

 プログラムの起点となるメソッドです。

 それ以外にもOnActivated、OnSuspending、OnResumingなどの重要なイベントハンドラーの記述部分がありますが、この辺は後の回で見ていこうと思います。

 

・pch.cpp & pch.h

 

 いくつかのライブラリがインクルードされています。

 

・CubeRenderer.cpp & CubeRenderer.h

 

 立方体サンプルを表示するためのクラスです。

 描画部分を確認したい場合はこの辺を読むと良いでしょう。

 

・SimplePixelShader.hlsl & SimpleVertexShader.hlsl

 

 シンプルなピクセルシェーダーと頂点シェーダーです。

 DirectX10以降はプログラマブルシェーダーしか利用できなくなりました。今は亡き固定シェーダーは初心者にとってDirectXの敷居を下げてくれるものでした。

 シェーダーまで説明できればいいなぁと思いますが、私の今のレベルでは遠い未来。

 

・Direct3DBase.cpp & Direct3DBase.h

 

 Direct3D描画用のベースクラスです。

 CubeRenderクラスはこのクラスを継承しています。

 

・外部依存関係

 

 利用している外部ライブラリーです。

 一度もビルドしていない状態では空ですが、ビルドすると表示されます。

 

・資産

 

 C#などのAssetsですね。

 C++の場合は資産ディレクトリとして表示されるようです。実際のディレクトリ名はAssetsなのでAssets表記でいいのに・・・。

 

プログラムの流れと主なクラス

 

 続いてプログラムの流れを追ってみましょう。

 まずは先述のDirect3DApp1.cppのmainメソッドが実行されます。

 

[Platform::MTAThread]
int main(Platform::Array<Platform::String^>^)
{
	auto direct3DApplicationSource = ref new Direct3DApplicationSource();
	CoreApplication::Run(direct3DApplicationSource);
	return 0;
}

 

 mainメソッドの中で生成しているDirect3DApplicationSourceクラスもDirect3DApp1.cppで定義されており、IFrameworkViewSourceクラスを継承しています。

 Direct3DApplicationSourceクラスはCoreApplicationクラスのRunメソッドに渡されCreateViewメソッドを通してビューを提供します。

 

IFrameworkView^ Direct3DApplicationSource::CreateView()
{
    return ref new Direct3DApp1();
}

 

 ここで返されるDirect3DApp1クラスはIFrameworkViewを継承しInitialize、SetWindow、Load、Runなどのメソッドを備えたビューです。

 Direct3DApp1は上記メソッドに加え、Initialize、SetWindowsの各メソッドで状態の変更などを受け取るイベントハンドラーを設定しています。

 

void Direct3DApp1::Initialize(CoreApplicationView^ applicationView)
{
	applicationView->Activated +=
        ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &Direct3DApp1::OnActivated);

	CoreApplication::Suspending +=
        ref new EventHandler<SuspendingEventArgs^>(this, &Direct3DApp1::OnSuspending);

	CoreApplication::Resuming +=
        ref new EventHandler<Platform::Object^>(this, &Direct3DApp1::OnResuming);

	m_renderer = ref new CubeRenderer();
}

void Direct3DApp1::SetWindow(CoreWindow^ window)
{
	window->SizeChanged += 
        ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &Direct3DApp1::OnWindowSizeChanged);

	window->VisibilityChanged +=
		ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &Direct3DApp1::OnVisibilityChanged);

	window->Closed += 
        ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &Direct3DApp1::OnWindowClosed);

	window->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0);

	window->PointerPressed +=
		ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &Direct3DApp1::OnPointerPressed);

	window->PointerMoved +=
		ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &Direct3DApp1::OnPointerMoved);

	m_renderer->Initialize(CoreWindow::GetForCurrentThread());
}

 

 Runメソッド内でキューブを描画する処理を行います。

 CubeRendererクラスのUpdate(更新)、Render(描画)がwhile文で繰り返し実行されています。

 

void Direct3DApp1::Run()
{
	BasicTimer^ timer = ref new BasicTimer();

	while (!m_windowClosed)
	{
		if (m_windowVisible)
		{
			timer->Update();
			CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
			m_renderer->Update(timer->Total, timer->Delta);
			m_renderer->Render();
			m_renderer->Present(); // この呼び出しは、表示フレーム レートに同期されます。
		}
		else
		{
			CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
		}
	}
}

 

 次回はCubeRendererクラスの中身を追いながら立方体を描画する処理を見ていきます。

 

(参考)MSDN:手順 1. ビューの作成と初期化 (Windows)

http://msdn.microsoft.com/ja-jp/library/windows/apps/hh465084.aspx

 


匿名 へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください