Előadást letölteni
Az előadás letöltése folymat van. Kérjük, várjon
1
GraphGame gg001-Triangle
Visual Studio, háromszög rajzolása Szécsi László
2
Direct3D11 SDK Win10 SDK része Visual Studio 2015
3
Könyvtár-hierarchia GraphGame – solution folder
Projects – ide gyűjtjük a projecteket (csak forrás) gg001-Triangle – ez például egy project folder lesz Build – ide dolgozik majd a Visual Studio Debug – ide ha debugba fordítunk gg001-Triangle – minden projectnek lesz alkönyvtára Release – ide ha releasebe gg001-Triangle – itt is Bin – ide kerülnek a futtathatók (ugyanaz a hierarchia)
4
Könyvtár-hierarchia II.
GraphGame – solution folder Media – ide kerülnek a projectek közös modell, textúra, stb filejai shaders – ide kerülnek a közös shaderek
5
Solution és project gyártása
Visual Studio/Project/New Project… Templates/Visual C++/Win32/Win32 project project name: gg001-Triangle solution name: GraphGame Application settings empty project
6
Project áthelyezése solution explorerben jobb klikk a projectre
remove filerendszerben új folder létrehozása: a solution folderben neve: Projects ide mozgassuk be a projectfolderünket solution explorer add new solution folder: Projects jobb klikk az új folderen: add existing project
7
64-bites platform jobb klikk, project properties
Configuration manager... Active solution platform és project platform megváltoztatása x64-re Poject properties/Linker/Advances/Target machine beállítás ellenőrizhető
8
Folderek A GraphGame folderben hozzuk létre a következő foldereket
Projects (már megcsináltuk) Media (projectek közös modelljei, textúrái) Shaders (projectek közös shader kódja) Bin (bináris exe, lib) Build (ideiglenes fileok)
9
Könyvtárbeállítások solution explorer/jobbklikk a projecten/project properties/general output directory $(ProjectDir.Replace($(SolutionDir),$(SolutionDir)Bin\$(Configuration)\)) intermediate directory $(ProjectDir.Replace($(SolutionDir),$(SolutionDir)Build\$(Configuration)\)) project properties/debugging/working dir $(SolutionDir)
10
Fordítás (majd ha lesz mit)
A megfelelő könyvtárakba szemetel Így könnyű pl. csak a forrást összezippelni DE: duplaklikkel megnyitott solutionre nem működik!! helyette: studio indítása, solution drag&drop ha elfelejtjük, nincs nagy baj, csak esetleg újra lefordít már kész dolgokat, meg sokat szemetel
11
stdafx.h lérehozása header files/jobb klikk/add new item/C++ header file neve: stdafx.h
12
stdafx.h #pragma once #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #define NOMINMAX #include <wrl.h>
13
main.cpp lérehozása source files/jobb klikk/add new item/C++ file
neve: main.cpp
14
main.cpp #include "stdafx.h"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
15
main.cpp int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) { // Register the window class. const wchar_t CLASS_NAME[] = L"D3D11 Window"; WNDCLASS wc = {}; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc);
16
main.cpp // Create the window. HWND hwnd = CreateWindowEx(
0, // Optional window styles. CLASS_NAME, // Window class L"GraphGame17",// Window text WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data );
17
main.cpp if (hwnd == NULL) { return 0; } ShowWindow(hwnd, nCmdShow);
18
main.cpp - hurok MSG msg = { 0 }; while (WM_QUIT != msg.message) {
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) TranslateMessage(&msg); DispatchMessage(&msg); } else // itt fogunk rajzolni return 0;
19
main.cpp LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam);
20
Fordítás, futtatás: üres ablak
21
Precompiled header main.cpp properties
C/C++/precompiled headers/Precomiled header: Create később az összes cpp-ben az első sor az stdafx.h includeolása mindegyikre C/C++/precompiled headers/Precomiled header: Use
22
stdafx.h – D3D include #include <d3d11_2.h>
#include <D3DCompiler.h>
23
main.cpp LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, uMsg, wParam, lParam);
24
SDK könyvtárak $(WindowsSDK_IncludePath);
25
Háromszög rajzolása erőforrás létrehozás/felszabadítás (vertex buffer), render target törlés, rajzolás
26
Ezekre lesz szükség vertex buffer = [x, y, z] [x, y, z] [x, y, z, 1]
IA VS IA RS PS OM [x, y, z, 1] [x, y, z, 1] input layout vertex shader primitive topology marad default pixel shader marad default
27
main.cpp ShowWindow(hwnd, nCmdShow);
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; #if defined(_DEBUG) creationFlags |= D3D11_CREATE_DEVICE_DEBUG; #endif D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0 };
28
main.cpp using namespace Microsoft::WRL;
// Create the Direct3D 11 API device object and a corresponding context. ComPtr<ID3D11Device> device; ComPtr<ID3D11DeviceContext> context; D3D_FEATURE_LEVEL d3dFeatureLevel;
29
ThrowOnFail.h class ThrowOnFail { std::string errorMessage;
std::string filename; int lineNumber; public: ThrowOnFail(std::string errorMessage, std::string filename, int lineNumber) :errorMessage(errorMessage), filename(filename), lineNumber(lineNumber) { }
30
ThrowOnFail.h void operator^(HRESULT hr){ if (FAILED(hr))
std::stringstream errorMessage; errorMessage << this->filename << "(" << this->lineNumber << "): " << this->errorMessage; MessageBoxA(NULL, errorMessage.str().c_str(), "Error!", MB_ICONSTOP | MB_OK); exit(-1); } };
31
main.cpp ThrowOnFail("Failed to create D3D device.", __FILE__, __LINE__) ^ D3D11CreateDevice( nullptr,// Specify nullptr to use the default adapter. D3D_DRIVER_TYPE_HARDWARE,// Create a device using the hardware graphics driver. 0,// Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE. creationFlags,// Set debug and Direct2D compatibility flags. featureLevels,// List of feature levels this app can support. ARRAYSIZE(featureLevels),// Size of the list above. D3D11_SDK_VERSION,// Always set this to D3D11_SDK_VERSION for Windows Store apps. device.GetAddressOf(),// Returns the Direct3D device created. &d3dFeatureLevel,// Returns feature level of device created. context.GetAddressOf()// Returns the device immediate context. );
32
main.cpp ComPtr<IDXGIDevice3> dxgiDevice;
ThrowOnFail("The device is not a DXGI device.", __FILE__, __LINE__) ^ device.As(&dxgiDevice); ComPtr<IDXGIAdapter> dxgiAdapter; ThrowOnFail("Failed to get adapter from device.", __FILE__, __LINE__) ^ dxgiDevice->GetAdapter(dxgiAdapter.GetAddressOf()); ComPtr<IDXGIFactory2> dxgiFactory; ThrowOnFail("Failed to get factory from adapter.", __FILE__, __LINE__) ^ dxgiAdapter->GetParent(IID_PPV_ARGS(dxgiFactory.GetAddressOf()));
33
main.cpp ComPtr<IDXGISwapChain1> swapChain; RECT r;
GetWindowRect(hwnd, &r); DXGI_SWAP_CHAIN_DESC1 swapChainDesc = { 0 }; swapChainDesc.Width = r.right - r.left; // Match the size of the window. swapChainDesc.Height = r.bottom - r.top; swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format. swapChainDesc.Stereo = false; swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling. swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect. swapChainDesc.Flags = 0; swapChainDesc.Scaling = DXGI_SCALING_NONE; swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
34
main.cpp ThrowOnFail("Failed to create swap chain.", __FILE__, __LINE__) ^ dxgiFactory->CreateSwapChainForHwnd( device.Get(), hwnd, &swapChainDesc, nullptr, &swapChain );
35
main.cpp ComPtr<ID3D11Texture2D> backBuffer;
ThrowOnFail("Failed to get swapchain backbuffer.", __FILE__, __LINE__) ^ swapChain->GetBuffer(0, IID_PPV_ARGS(backBuffer.GetAddressOf())); ComPtr<ID3D11RenderTargetView> renderTargetView; ThrowOnFail("Failed to create render target view.", __FILE__, __LINE__) ^ device->CreateRenderTargetView( backBuffer.Get(), nullptr, renderTargetView.GetAddressOf() );
36
main.cpp // Create a depth stencil view for use with 3D rendering if needed. CD3D11_TEXTURE2D_DESC depthStencilDesc( DXGI_FORMAT_D24_UNORM_S8_UINT, static_cast<UINT>(swapChainDesc.Width), static_cast<UINT>(swapChainDesc.Height), 1, // This depth stencil view has only one texture. 1, // Use a single mipmap level. D3D11_BIND_DEPTH_STENCIL );
37
main.cpp ComPtr<ID3D11Texture2D> depthStencil;
GraphGame::ThrowOnFail("Failed to create depth stencil texture.", __FILE__, __LINE__) ^ device->CreateTexture2D( &depthStencilDesc, nullptr, &depthStencil );
38
main.cpp ComPtr<ID3D11DepthStencilView> depthStencilView;
CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D); GraphGame::ThrowOnFail("Failed to create depth stencil view.", __FILE__, __LINE__) ^ device->CreateDepthStencilView( depthStencil.Get(), &depthStencilViewDesc, depthStencilView.GetAddressOf() );
39
main.cpp // Set the 3D rendering viewport to target the entire window.
CD3D11_VIEWPORT screenViewport( 0.0f, swapChainDesc.Width, swapChainDesc.Height ); context->RSSetViewports(1, &screenViewport);
40
Keret kész jöhet a háromszög erőforrások rajzolás vertex buffer
input layout vertex shader pixel shader rajzolás erőforrások bekötése draw
41
main.cpp ComPtr<ID3D11Buffer> vertexBuffer;
ComPtr<ID3D11InputLayout> inputLayout; ComPtr<ID3D11VertexShader> vertexShader; ComPtr<ID3D11PixelShader> pixelShader;
42
main.cpp struct float3 { float x, y, z;
float3(float x, float y, float z) :x(x), y(y), z(z) {} };
43
main.cpp D3D11_BUFFER_DESC desc;
desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; desc.ByteWidth = sizeof(float3) * 3; desc.CPUAccessFlags = 0; desc.MiscFlags = 0; desc.StructureByteStride = sizeof(float3); desc.Usage = D3D11_USAGE_IMMUTABLE;
44
main.cpp float3 vertexPositionArray[3] = { float3(0, 0, 0.5),
D3D11_SUBRESOURCE_DATA initData; initData.pSysMem = vertexPositionArray; initData.SysMemPitch = 0; initData.SysMemSlicePitch = 0;
45
main.cpp ThrowOnFail("Failed to create vertex buffer.", __FILE__, __LINE__) ^ device->CreateBuffer(&desc, &initData, vertexBuffer.GetAddressOf());
46
main.cpp const char* vertexShaderCode =
"float4 vsIdle(float4 pos :POSITION ) :SV_Position {return pos;}"; ID3DBlob* vertexShaderByteCode; ThrowOnFail("Failed to compile vertex shader source.", __FILE__, __LINE__) ^ D3DCompile(vertexShaderCode, strlen(vertexShaderCode), NULL, NULL, NULL, "vsIdle", "vs_5_0", 0, 0, &vertexShaderByteCode, NULL);
47
main.cpp GraphGame::ThrowOnFail("Failed to create vertex shader.", __FILE__, __LINE__) ^ device->CreateVertexShader( vertexShaderByteCode->GetBufferPointer(), vertexShaderByteCode->GetBufferSize(), NULL, vertexShader.GetAddressOf());
48
main.cpp D3D11_INPUT_ELEMENT_DESC positionElement;
positionElement.AlignedByteOffset = 0; positionElement.Format = DXGI_FORMAT_R32G32B32_FLOAT; positionElement.InputSlot = 0; positionElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; positionElement.InstanceDataStepRate = 0; positionElement.SemanticIndex = 0; positionElement.SemanticName = "POSITION";
49
main.cpp ThrowOnFail("Failed to create input layout.", __FILE__, __LINE__) ^ device->CreateInputLayout( &positionElement, 1, vertexShaderByteCode ->GetBufferPointer(), vertexShaderByteCode ->GetBufferSize(), inputLayout.GetAddressOf());
50
main.cpp const char* pixelShaderCode = "float4 psIdle() : SV_Target {return float4(1, 0, 0, 1);}"; A PIXEL SHADER LEFORDíTÁSA, OBJEKTUM LÉTREHOZÁSA ÖNÁLLÓ FELADAT – VS ALAPJÁN
51
rajzolás a hurokban context->OMSetRenderTargets(1, renderTargetView.GetAddressOf(), depthStencilView.Get()); // idle: update, render float clearColor[4] = { 0.9f, 0.7f, 0.1f, 0.0f }; context->ClearRenderTargetView( renderTargetView.Get(), clearColor); context->ClearDepthStencilView(depthStencilView.Get(), D3D11_CLEAR_DEPTH, 1.0, 0);
52
rajzolás a hurokban unsigned int stride = sizeof(float3);
unsigned int offset = 0; context->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset); context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); context->IASetInputLayout(inputLayout.Get()); context->VSSetShader(vertexShader.Get(), NULL, 0); context->PSSetShader(pixelShader.Get(), NULL, 0);
53
rajzolás a hurokban context->Draw(3, 0);
ThrowOnFail("Failed to present swap chain.", __FILE__, __LINE__) ^ swapChain->Present(0, 0);
54
Háromszög kész
55
Honnan tudjuk mit kell csinálni?
MSDN Direct3D 11 Graphics MSDN Direct3D 11 Reference
56
Visual Studio 2015 tippek bepastelt kódhoz: általában kódoláshoz
formáz: kijelöl, Ctrl+K, Ctrl+F általában kódoláshoz kódieg.: eleje begépel, aztán Ctrl+space metódus paraméterlistája kurzor a zárójelek közé v. nyitó zárójel után ctrl+alt+space
Hasonló előadás
© 2024 SlidePlayer.hu Inc.
All rights reserved.