Kamera, 3D, transzformációk Szécsi László GraphGame ggl005-Cam Kamera, 3D, transzformációk Szécsi László
Egg bővítése Math.zip kibontása az Egg projectkönyvtárba float2, foat3, float4 típusok, HLSL-ben megszokott műveletekkel float4x4 típus Cam.zip kibontása az Egg projectkönyvtárba Egg::Cam::Base – kamera interface Egg::Cam::FirstPerson – WASD + egér Math, Cam filterek létrehozása, forrásfileok hozzáadása az Egg projecthez
Min, max A windows.h-ben definiált makrók nem kellenek ezért van az stdafx.h-ban az #include <windows.h> (vagy wrl.h) előtt #define NOMINMAX és nem akad össze a Math::min, Math::max-xal
gg005-Cam project copy-paste-rename gg004-Mesh folder vcxproj, filters átnevezés solution/add existing project build, run
#6.0 Game.cpp ComPtr<ID3DBlob> vertexShaderByteCode = loadShaderCode("vsIdleTrafo.cso");
Shaders/vsTrafo.hlsl #5.0 cbuffer perObject{ float4x4 modelMatrix; float4x4 modelMatrixInverse; float4x4 modelViewProjMatrix; };
Shaders/vsTrafo.hlsl #5.1 struct IaosTrafo { float4 pos : POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD; }; struct VsosTrafo float4 pos : SV_POSITION; float4 worldPos : WORLDPOS; float3 normal : NORMAL; float2 tex : TEXCOORD;
Shaders/vsTrafo.hlsl #5.2 VsosTrafo vsTrafo(IaosTrafo input) { VsosTrafo output = (VsosTrafo)0; output.pos = mul(input.pos, modelViewProjMatrix); output.worldPos = mul(input.pos, modelMatrix); output.normal = mul(modelMatrixInverse, float4(input.normal.xyz, 0.0)); output.tex = input.tex; return output; }
#6.0 Game.h Microsoft::WRL::ComPtr<ID3D11Buffer> perObjectConstantBuffer;
#6.0 Game.cpp using namespace Egg::Math; struct float3
Game.cpp #6.0 D3D11_BUFFER_DESC constantBufferDesc; constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDesc.ByteWidth = sizeof(Egg::Math::float4x4) * 3; constantBufferDesc.CPUAccessFlags = 0; constantBufferDesc.MiscFlags = 0; constantBufferDesc.StructureByteStride = 0; constantBufferDesc.Usage = D3D11_USAGE_DEFAULT; Egg::ThrowOnFail("Failed to create constant buffer.", __FILE__, __LINE__) ^ device->CreateBuffer(&constantBufferDesc, nullptr, perObjectConstantBuffer.GetAddressOf());
#6.0 Game.cpp material->setCb(Egg::Mesh::ShaderStageFlag::Vertex, 0, perObjectConstantBuffer);
Game.cpp - render #6.0 float4x4 perObjectMatrices[3]; perObjectMatrices[2] = float4x4::scaling( float3(0.5, 0.5, 1)); perObjectMatrices[1] = float4x4::identity; perObjectMatrices[0] = float4x4::identity; context->UpdateSubresource(perObjectConstantBuffer.Get(), 0, nullptr, perObjectMatrices, 0, 0);
main.cpp if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { app->processMessage(hwnd, msg.message, msg.wParam, msg.lParam); TranslateMessage(&msg); DispatchMessage(&msg); } else { auto currentTime = std::chrono::system_clock::now(); std::chrono::duration<double> timeStep = currentTime - time; std::chrono::duration<double> timeSinceStart = currentTime - startTime; app->animate(timeStep.count(), timeSinceStart.count()); app->render(context); Egg::ThrowOnFail("Failed to present swap chain.", __FILE__, __LINE__) ^ swapChain->Present(0, 0); }
Game osztályba #include "Cam/FirstPerson.h" Egg::Cam::FirstPerson::P firstPersonCam; firstPersonCam = Egg::Cam::FirstPerson::create(); void animate(double dt, double t); bool processMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Game.cpp void Game::animate(double dt, double t) { if(!firstPersonCam) return; firstPersonCam->animate(dt); } bool Game::processMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if(!firstPersonCam) return false; firstPersonCam->processMessage(hWnd, uMsg, wParam, lParam); return false;
#6.0 Game.cpp - render // perObjectMatrices beállítása kamera alapján
Itt tartunk, kamera mozgatható