前田稔(Maeda Minoru)の超初心者のプログラム入門
ファイル | 説明 |
---|---|
Content\MyCamera.h | MyCamera の Header File |
Content\MyCamera.cpp | MyCamera の Program File |
#include "MyCamera.h" MyCamera *my_camera = nullptr; |
Sample3DSceneRenderer::Sample3DSceneRenderer(const std::shared_ptr<DX::DeviceResources>& deviceResources) : m_loadingComplete(false), m_degreesPerSecond(45), m_indexCount(0), m_tracking(false), m_deviceResources(deviceResources) { CreateDeviceDependentResources(); CreateWindowSizeDependentResources(); my_camera = new MyCamera(m_deviceResources); } |
void Sample3DSceneRenderer::CreateWindowSizeDependentResources() { if (my_camera!=nullptr) my_camera->Projection(); } |
void Sample3DSceneRenderer::Rotate(float radians) { XMStoreFloat4x4(&my_camera->m_model, XMMatrixTranspose(XMMatrixRotationY(radians))); } |
m_constantBufferData.projection = my_camera->m_projection; m_constantBufferData.view = my_camera->m_view; m_constantBufferData.model = my_camera->m_model; |
context->DrawIndexed( m_indexCount, 0, 0); |
void Sample3DSceneRenderer::Render() { // 読み込みは非同期です。読み込みが完了した後にのみ描画してください。 if (!m_loadingComplete) { return; } auto context = m_deviceResources->GetD3DDeviceContext(); m_constantBufferData.projection = my_camera->m_projection; m_constantBufferData.view = my_camera->m_view; m_constantBufferData.model = my_camera->m_model; // 定数バッファーを準備して、グラフィックス デバイスに送信します。 context->UpdateSubresource( m_constantBuffer.Get(), 0, NULL, &m_constantBufferData, 0, 0); // 各頂点は、VertexPositionColor 構造体の 1 つのインスタンスです。 UINT stride = sizeof(VertexPositionColor); UINT offset = 0; context->IASetVertexBuffers( 0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset); context->IASetIndexBuffer( m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, //各インデックスは、1 つの 16 ビット符号なし整数 (short) です。 0); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); context->IASetInputLayout(m_inputLayout.Get()); // 頂点シェーダーをアタッチします。 context->VSSetShader( m_vertexShader.Get(), nullptr, 0); // 定数バッファーをグラフィックス デバイスに送信します。 context->VSSetConstantBuffers( 0, 1, m_constantBuffer.GetAddressOf()); // ピクセル シェーダーをアタッチします。 context->PSSetShader( m_pixelShader.Get(), nullptr, 0); // オブジェクトを描画します。 context->DrawIndexed( m_indexCount, 0, 0); } |
// MyCamera Header File Ver-1 前田 稔 #pragma once #include "..\Common\DeviceResources.h" using namespace DirectX; class MyCamera { public: MyCamera(std::shared_ptr<DX::DeviceResources>& deviceResources); void Projection(); XMVECTOR m_eye = { 0.0f, 0.7f, 1.5f, 0.0f }; XMVECTOR m_at = { 0.0f, -0.1f, 0.0f, 0.0f }; XMVECTOR m_up = { 0.0f, 1.0f, 0.0f, 0.0f }; XMFLOAT4X4 m_projection; XMFLOAT4X4 m_view; XMFLOAT4X4 m_model; private: std::shared_ptr<DX::DeviceResources> m_deviceResources; }; |
/*****************************************/ /* MyCamera Program File Ver-1 前田 稔 */ /*****************************************/ #pragma once #include "pch.h" #include "..\Common\DirectXHelper.h" #include "MyCamera.h" using namespace Windows::Foundation; MyCamera::MyCamera(std::shared_ptr<DX::DeviceResources>& deviceResources) { m_deviceResources = deviceResources; Projection(); XMStoreFloat4x4(&m_view, XMMatrixTranspose(XMMatrixLookAtRH(m_eye, m_at, m_up))); XMStoreFloat4x4(&m_model, XMMatrixIdentity()); } void MyCamera::Projection() { Size outputSize = m_deviceResources->GetOutputSize(); float aspectRatio = outputSize.Width / outputSize.Height; float fovAngleY = 70.0f * XM_PI / 180.0f; if (aspectRatio < 1.0f) { fovAngleY *= 2.0f; } XMMATRIX perspectiveMatrix = XMMatrixPerspectiveFovRH( fovAngleY, aspectRatio, 0.01f, 100.0f); XMFLOAT4X4 orientation = m_deviceResources->GetOrientationTransform3D(); XMMATRIX orientationMatrix = XMLoadFloat4x4(&orientation); XMStoreFloat4x4(&m_projection, XMMatrixTranspose(perspectiveMatrix * orientationMatrix)); } |