AxisOfRotation.cpp
#include "StdAfx.h"
#include "AxisOfRotation.h"
using namespace Core;
using namespace Microsoft;
using namespace System;
using namespace System::Diagnostics;
// DirectXのライブラリによる const/volatile 修飾子の使用の警告を抑制する
#pragma warning( disable : 4400 )
/// [ Constructor ]
AxisOfRotation::AxisOfRotation( Microsoft::DirectX::Direct3D::Device^ device, float length ) :
m_length( length )
{
// ラインの 頂点バッファを生成する
m_line = gcnew DirectX::Direct3D::VertexBuffer(
DirectX::Direct3D::CustomVertex::PositionColored::typeid, // 頂点データを定義した構造体
2, // 頂点数
device, // デバイス
DirectX::Direct3D::Usage::Dynamic | // 使用法
DirectX::Direct3D::Usage::WriteOnly,
DirectX::Direct3D::CustomVertex::PositionColored::Format, // 頂点フォーマット
DirectX::Direct3D::Pool::Default
);
// 頂点の生成イベントに登録する
m_line->Created += gcnew System::EventHandler( this, &AxisOfRotation::SetLine );
// ... そのイベントを呼び出して 設定させる
SetLine( m_line, nullptr );
}
/// [ Finalizer ]
AxisOfRotation::!AxisOfRotation()
{
delete m_line;
}
/// ラインを設定する
void AxisOfRotation::SetLine( System::Object^ sender, System::EventArgs^ )
{
// 引数から目的とする型を取得する
DirectX::Direct3D::VertexBuffer^ vertexBuffer = safe_cast< DirectX::Direct3D::VertexBuffer^ >( sender );
// 頂点を格納する配列を生成する
array< DirectX::Direct3D::CustomVertex::PositionColored >^ vertices
= gcnew array< DirectX::Direct3D::CustomVertex::PositionColored >( 2 );
// ... 頂点の位置を設定する
vertices[ 0 ].Position = DirectX::Vector3( 0.0f, 0.0f, m_length / -2.0f );
vertices[ 1 ].Position = DirectX::Vector3( 0.0f, 0.0f, m_length / +2.0f );
// ... 頂点の色を設定する
int color = Drawing::Color::White.ToArgb();
vertices[ 0 ].Color = color;
vertices[ 1 ].Color = color;
// 頂点データの範囲をロックして、頂点バッファ メモリへのリファレンスを取得する
vertexBuffer->SetData( vertices, 0, DirectX::Direct3D::LockFlags::None );
// [ Reference ]「Managed DirectX9」Tom Miller(Sams) P40
}
/// 描画する
void AxisOfRotation::Render( Microsoft::DirectX::Direct3D::Device^ device, Robotics::AxisVector^ axis )
{
double rotationY = Math::Atan2( axis->X, axis->Z );
double rotationX = Math::Atan2( axis->Y, axis->Z );
// 回転軸の方向へ回転する
device->Transform->World
= DirectX::Matrix::RotationY( safe_cast< float >( rotationY ) )
* DirectX::Matrix::RotationX( safe_cast< float >( rotationX ) )
* device->Transform->World;
// ライトを無効にする
device->RenderState->Lighting = false;
// ラインの頂点バッファを デバイスのデータ ストリームにバインドする
device->SetStreamSource( 0, m_line, 0 );
device->VertexFormat = DirectX::Direct3D::CustomVertex::PositionColored::Format; // 頂点フォーマット
// ... レンダリングする
device->DrawPrimitives(
DirectX::Direct3D::PrimitiveType::LineList,
0, // 頂点の読み出し開始位置
1 // プリミティブの数
);
}