2024년 5월 29일 수요일

DirectX11 Tutorial - 2


저번에 세팅해놓았던 프로젝트 속성에 이어서

DirectX11 헤더를 포함시켜 클래스를 생성한다.


렌더링 하기 위해서 새로고침 빈도(프레임 속도), vsync, ...등 

여러가지 설정들을 해주어야 한다.



FeatureLevel : 기능 레벨 - 사용할 DirectX의 버전 

enum D3D_FEATURE_LEVEL_11_0 을 저장



SwapChain : 스왑 체인 - 실제로 그려지는 백 버퍼, 프론트 버퍼를 말한다.


C++, C# 으로 스네이크 게임을 만들어봤으면 알겠지만

화면을 지우고 처음부터 다시 그려야하는 시간 때매 화면이 자주 깜박인다. 


백 버퍼에 미리 그려놓고

프론트 버퍼와 번갈아가면서 화면에 띄워주는 원리로 이 문제를 해결한다.

그래서 이름이 Swap인 이유인듯 하다.




Depth Stencil - 깊이 & 스텐실 

깊이 버퍼(Z 버퍼)는 도형의 가려진 부분은 렌더링 되지 않고

보이는 부분만 렌더링하기 위한 버퍼이고


스텐실 버퍼는 화면에 특수 효과를 주기 위한 기능이라고 

생각하면 된다. (예: 모자이크, 블렌딩)



Rasterizer : 레스터라이저

폴리곤이 렌더링되는 방식을 결정한다.

삼각형 앞면만 그리지 않고 뒷면 또한 그린다거나

와이어 프레임 모드로 렌더링 시킬 수 있다.



+ D3D11_BLEND_DESC : 블랜드

이 친구는 알파 블랜딩을 사용하기 위한 것이라 알아만 두자

다른 여러 효과도 사용 가능


화면에 조준점을 띄울때 썼었음


소스 코드 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef _D3DCLASS_H_
#define _D3DCLASS_H_
 
 
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
 
#include <d3d11.h>
#include <directxmath.h>
using namespace DirectX;
 
 
class D3DClass
{
public:
    D3DClass();
    D3DClass(const D3DClass&);
    ~D3DClass();
 
    bool Initialize(bool, HWND, boolfloat screenDepth, float screenNear, float screenWidth, float screenHeight);
    void Shutdown();
 
    void BeginScene(floatfloatfloatfloat);
    void EndScene();
 
    ID3D11Device* GetDevice();
    ID3D11DeviceContext* GetDeviceContext();
    IDXGISwapChain* GetSwapChain();
 
    void GetProjectionMatrix(XMMATRIX&);
    void GetWorldMatrix(XMMATRIX&);
    void GetOrthoMatrix(XMMATRIX&);
 
    void GetVideoCardInfo(char*int&);
 
    void TurnZBufferOn();
    void TurnZBufferOff();
 
    void TurnOnCulling();
    void TurnOffCulling();
 
    void EnableAlphaBlending();
    void DisableAlphaBlending();
 
    void EnableWireframe();
    void DisableWireframe();
 
private:
    bool m_vsync_enabled;
    int m_videoCardMemory;
    char m_videoCardDescription[128];
    IDXGISwapChain* m_swapChain;
    ID3D11Device* m_device;
    ID3D11DeviceContext* m_deviceContext;
    ID3D11RenderTargetView* m_renderTargetView;
    ID3D11Texture2D* m_depthStencilBuffer;
    ID3D11DepthStencilState* m_depthStencilState;
    ID3D11DepthStencilView* m_depthStencilView;
    ID3D11RasterizerState* m_rasterState;
    ID3D11RasterizerState* m_rasterStateNoCulling;
    ID3D11RasterizerState* m_rasterStateWireframe;
    XMMATRIX m_projectionMatrix;
    XMMATRIX m_worldMatrix;
    XMMATRIX m_orthoMatrix;
    ID3D11DepthStencilState* m_depthDisabledStencilState;
    ID3D11BlendState* m_alphaEnableBlendingState;
    ID3D11BlendState* m_alphaDisableBlendingState;
};
 
#endif
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include "d3dclass.h"
 
 
D3DClass::D3DClass()
{
    m_swapChain = 0;
    m_device = 0;
    m_deviceContext = 0;
    m_renderTargetView = 0;
    m_depthStencilBuffer = 0;
    m_depthStencilState = 0;
    m_depthStencilView = 0;
    m_rasterState = 0;
    m_rasterStateNoCulling = 0;
    m_rasterStateWireframe = 0;
    m_depthDisabledStencilState = 0;
    m_alphaEnableBlendingState = 0;
    m_alphaDisableBlendingState = 0;
}
 
 
D3DClass::D3DClass(const D3DClass& other)
{
}
 
 
D3DClass::~D3DClass()
{
}
 
 
bool D3DClass::Initialize(bool vsync, HWND hwnd, bool fullscreen, float screenDepth, float screenNear)
{
    HRESULT result;
    IDXGIFactory* factory;
    IDXGIAdapter* adapter;
    IDXGIOutput* adapterOutput;
    unsigned int numModes, i, numerator, denominator;
    size_t stringLength;
    DXGI_MODE_DESC* displayModeList;
    DXGI_ADAPTER_DESC adapterDesc;
    int error;
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    D3D_FEATURE_LEVEL featureLevel;
    ID3D11Texture2D* backBufferPtr;
    D3D11_TEXTURE2D_DESC depthBufferDesc;
    D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
    D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
    D3D11_RASTERIZER_DESC rasterDesc;
    D3D11_VIEWPORT viewport;
    float fieldOfView, screenAspect;
    D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
    D3D11_BLEND_DESC blendStateDescription;
    ID3D11Device* pDevice = 0;
 
    //VSync 여부 
    m_vsync_enabled = vsync;
 
    //새로고침 빈도
    numerator = 60;        //분자
    denominator = 1;    //분모
 
 
    //윈도우 창 크기를 구함
    RECT rect;
    FLOAT screenWidth, screenHeight;
 
    GetClientRect(hwnd, &rect);
    screenWidth = (float)rect.right - (float)rect.left;
    screenHeight = (float)rect.bottom - (float)rect.top;
 
    //그래픽 인터페이스 팩토리 생성
    result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
    if (FAILED(result))
    {
        return false;
    }
 
    //그래픽 인터페이스용 어댑터 생성
    result = factory->EnumAdapters(0&adapter);
    if (FAILED(result))
    {
        return false;
    }
 
    //어댑터 출력(Output)을 열거(Enumerate)함(나열하다)
    result = adapter->EnumOutputs(0&adapterOutput);
    if (FAILED(result))
    {
        return false;
    }
 
    //어댑터 출력에서 DXGI_FORMAT_..._UNORM 형식에 맞는 디스플레이 모드 갯수만 가져옴
    result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
    if (FAILED(result))
    {
        return false;
    }
 
    //현 컴퓨터의 모든 디스플레이 모드 리스트를 생성
    displayModeList = new DXGI_MODE_DESC[numModes];
    if (!displayModeList)
    {
        return false;
    }
 
    //아까의 작업을 반복하여 이번엔 디스플레이 모드 리스트의 Structure를 채워줌
    result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
    if (FAILED(result))
    {
        return false;
    }
 
    //모든 디스플레이 모드를 확인하여 현재 화면 크기에 맞는 모드를 찾아, 해당 디스플레이의 새로고침 빈도를 가져옴
    for (i = 0; i < numModes; i++)
    {
        if (displayModeList[i].Width == (unsigned int)screenWidth)
        {
            if (displayModeList[i].Height == (unsigned int)screenHeight)
            {
                numerator = displayModeList[i].RefreshRate.Numerator;
                denominator = displayModeList[i].RefreshRate.Denominator;
            }
        }
    }
 
 
    //그래픽 카드 정보를 가져옴
    result = adapter->GetDesc(&adapterDesc);
    if (FAILED(result))
    {
        return false;
    }
 
    //그래픽 카드 메모리를 저장함
    m_videoCardMemory = (int)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);
 
    //그래픽 카드 이름 저장
    error = wcstombs_s(&stringLength, m_videoCardDescription, 128, adapterDesc.Description, 128);
    if (error != 0)
    {
        return false;
    }
 
    //그래픽카드에서 필요한 정보는 다 얻었으니 자원 반환해줌
    delete[] displayModeList;
    displayModeList = 0;
 
    adapterOutput->Release();
    adapterOutput = 0;
 
    adapter->Release();
    adapter = 0;
 
    factory->Release();
    factory = 0;
 
 
    //변수 초기화
    ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
 
    //스왑 체인 설정
    swapChainDesc.BufferCount = 1;                        //백 버퍼 갯수
    swapChainDesc.BufferDesc.Width = (unsigned int)screenWidth;        //백 버퍼 너비
    swapChainDesc.BufferDesc.Height = (unsigned int)screenHeight;        //백 버퍼 높이
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    //백 버퍼 형식
 
    //새로고침 빈도 설정
    if (m_vsync_enabled)
    {
        swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
        swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
    }
    else
    {
        swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
        swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
    }
 
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    //백 버퍼 사용 방식 설정
    swapChainDesc.OutputWindow = hwnd;    //출력할 창의 Hwnd 핸들 
    swapChainDesc.SampleDesc.Count = 1;    //샘플 갯수 설정(1은 다중 샘플링을 끔) ex)슈퍼 샘플링, 멀티 샘플링
    swapChainDesc.SampleDesc.Quality = 0;//품질 레벨 설정(0은 다중 샘플링을 끈 것)
 
    //전체화면 설정
    if (fullscreen)
    {
        swapChainDesc.Windowed = false;
    }
    else
    {
        swapChainDesc.Windowed = true;
    }
 
    swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
    swapChainDesc.Flags = 0;
 
    featureLevel = D3D_FEATURE_LEVEL_11_0;
 
    //Direct Device 생성
    result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT, &featureLevel, 1,
        D3D11_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device, NULL&m_deviceContext);
    if (FAILED(result))
    {
        return false;
    }
 
    //백 버퍼 포인터 가져오기
    result = m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
    if (FAILED(result))
    {
        return false;
    }
 
    //랜더 타겟 뷰 생성
    result = m_device->CreateRenderTargetView(backBufferPtr, NULL&m_renderTargetView);
    if (FAILED(result))
    {
        return false;
    }
 
    //백 버퍼 포인터 해제
    backBufferPtr->Release();
    backBufferPtr = 0;
 
    //변수 초기화
    ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
 
    //깊이 버퍼 설정
    depthBufferDesc.Width = (unsigned int)screenWidth;    //너비
    depthBufferDesc.Height = (unsigned int)screenHeight;    //높이
    depthBufferDesc.MipLevels = 1;
    depthBufferDesc.ArraySize = 1;
    depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthBufferDesc.SampleDesc.Count = 1;
    depthBufferDesc.SampleDesc.Quality = 0;
    depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    depthBufferDesc.CPUAccessFlags = 0;
    depthBufferDesc.MiscFlags = 0;
 
    //깊이 버퍼 생성
    result = m_device->CreateTexture2D(&depthBufferDesc, NULL&m_depthStencilBuffer);
    if (FAILED(result))
    {
        return false;
    }
 
    //변수 초기화
    ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
 
    //깊이, 스텐실 설정
    depthStencilDesc.DepthEnable = true;
    depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
 
    depthStencilDesc.StencilEnable = true;
    depthStencilDesc.StencilReadMask = 0xFF;
    depthStencilDesc.StencilWriteMask = 0xFF;
 
    //앞면 픽셀의 경우 작업할 스텐실
    depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
    depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
 
    //뒷면 픽셀의 경우 작업할 스텐실
    depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
    depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
 
    //깊이 스텐실 State 생성
    result = m_device->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState);
    if (FAILED(result))
    {
        return false;
    }
 
    //바로 생성한 State를 적용
    m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1);
 
    //변수 초기화
    ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
 
    //깊이 스텐실 뷰 설정
    depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    depthStencilViewDesc.Texture2D.MipSlice = 0;
 
    //깊이 스텐실 뷰 생성
    result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView);
    if (FAILED(result))
    {
        return false;
    }
 
    //렌더 타겟 뷰와 깊이 스텐실 뷰를 출력 렌더 파이프 라인에 바인딩
    m_deviceContext->OMSetRenderTargets(1&m_renderTargetView, m_depthStencilView);
    
    //기본 레스터라이저 설정 
    rasterDesc.AntialiasedLineEnable = false;
    rasterDesc.CullMode = D3D11_CULL_BACK;
    rasterDesc.DepthBias = 0;
    rasterDesc.DepthBiasClamp = 0.0f;
    rasterDesc.DepthClipEnable = true;
    rasterDesc.FillMode = D3D11_FILL_SOLID;
    rasterDesc.FrontCounterClockwise = false;
    rasterDesc.MultisampleEnable = false;
    rasterDesc.ScissorEnable = false;
    rasterDesc.SlopeScaledDepthBias = 0.0f;
 
    //레스터라이저 상태 생성
    result = m_device->CreateRasterizerState(&rasterDesc, &m_rasterState);
    if (FAILED(result))
    {
        return false;
    }
 
    //설정한 레스터 상태 적용
    m_deviceContext->RSSetState(m_rasterState);
 
    //컬링 모드만 해제한 레스터를 생성하기 위한 것
    rasterDesc.CullMode = D3D11_CULL_NONE;
 
    //노 컬링 레스터라이저 상태 생성
    result = m_device->CreateRasterizerState(&rasterDesc, &m_rasterStateNoCulling);
    if (FAILED(result))
    {
        return false;
    }
 
    //와이어 프레임 모드 설정
    rasterDesc.AntialiasedLineEnable = false;
    rasterDesc.CullMode = D3D11_CULL_BACK;
    rasterDesc.DepthBias = 0;
    rasterDesc.DepthBiasClamp = 0.0f;
    rasterDesc.DepthClipEnable = true;
    rasterDesc.FillMode = D3D11_FILL_WIREFRAME;
    rasterDesc.FrontCounterClockwise = false;
    rasterDesc.MultisampleEnable = false;
    rasterDesc.ScissorEnable = false;
    rasterDesc.SlopeScaledDepthBias = 0.0f;
 
    //와이어 프레임 모드 상태 생성
    result = m_device->CreateRasterizerState(&rasterDesc, &m_rasterStateWireframe);
    if (FAILED(result))
    {
        return false;
    }
 
    //DirectX가 클립 공간 좌표를 렌더링 대상 공간에 매핑할 수 있도록 뷰포트를 설정함
    viewport.Width = screenWidth;
    viewport.Height = screenHeight;
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    viewport.TopLeftX = 0.0f;
    viewport.TopLeftY = 0.0f;
 
    //뷰포트 생성
    m_deviceContext->RSSetViewports(1&viewport);
 
    //Projection 행렬 설정
    fieldOfView = 3.141592654f / 4.0f;
    screenAspect = screenWidth / screenHeight;
 
    //렌더링 하기 위한 Perspective Projection 행렬 생성
    m_projectionMatrix = XMMatrixPerspectiveFovLH(fieldOfView, screenAspect, screenNear, screenDepth);
 
    //월드 행렬 초기화
    m_worldMatrix = XMMatrixIdentity();
 
    //2D 렌더링을 위해 원근감을 없앤 Orthographic Projection 행렬 생성
    m_orthoMatrix = XMMatrixOrthographicLH(screenWidth, screenHeight, screenNear, screenDepth);
 
    //2D 렌더링을 위한 깊이 스텐실 설정
    depthDisabledStencilDesc.DepthEnable = false;
    depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
    depthDisabledStencilDesc.StencilEnable = true;
    depthDisabledStencilDesc.StencilReadMask = 0xFF;
    depthDisabledStencilDesc.StencilWriteMask = 0xFF;
    depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
    depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
    depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
    depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
    depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
    depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
 
    //2D용 깊이 스텐실 상태 생성
    result = m_device->CreateDepthStencilState(&depthDisabledStencilDesc, &m_depthDisabledStencilState);
    if (FAILED(result))
    {
        return false;
    }
 
    //변수 초기화
    ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC));
 
    //블랜드 상태 설정(알파 블랜딩)
    blendStateDescription.AlphaToCoverageEnable = false;
    blendStateDescription.IndependentBlendEnable = false;
    blendStateDescription.RenderTarget[0].BlendEnable = true;
    blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
    blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
    blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
    blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
    blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
    blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
    blendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN | D3D11_COLOR_WRITE_ENABLE_BLUE;
    
 
 
    //알파 블랜딩 상태 생성
    result = m_device->CreateBlendState(&blendStateDescription, &m_alphaEnableBlendingState);
    if (FAILED(result))
    {
        return false;
    }
 
    //알파 블랜딩 끈 상태 설정
    blendStateDescription.RenderTarget[0].BlendEnable = false;
    blendStateDescription.AlphaToCoverageEnable = false;
 
    //블랜딩 상태 생성
    result = m_device->CreateBlendState(&blendStateDescription, &m_alphaDisableBlendingState);
    if (FAILED(result))
    {
        return false;
    }
 
    return true;
}
 
 
void D3DClass::Shutdown()
{
    //종료하기 전 창모드로 설정하거나 스왑 체인을 해제하면 예외 발생
    if (m_swapChain)
    {
        m_swapChain->SetFullscreenState(falseNULL);
    }
 
 
    if (m_alphaDisableBlendingState)
    {
        m_alphaDisableBlendingState->Release();
        m_alphaDisableBlendingState = 0;
    }
 
    if (m_alphaEnableBlendingState)
    {
        m_alphaEnableBlendingState->Release();
        m_alphaEnableBlendingState = 0;
    }
 
    if (m_depthDisabledStencilState)
    {
        m_depthDisabledStencilState->Release();
        m_depthDisabledStencilState = 0;
    }
 
    if (m_rasterStateWireframe)
    {
        m_rasterStateWireframe->Release();
        m_rasterStateWireframe = 0;
    }
 
    if (m_rasterStateNoCulling)
    {
        m_rasterStateNoCulling->Release();
        m_rasterStateNoCulling = 0;
    }
 
    if (m_rasterState)
    {
        m_rasterState->Release();
        m_rasterState = 0;
    }
 
    if (m_depthStencilView)
    {
        m_depthStencilView->Release();
        m_depthStencilView = 0;
    }
 
    if (m_depthStencilState)
    {
        m_depthStencilState->Release();
        m_depthStencilState = 0;
    }
 
    if (m_depthStencilBuffer)
    {
        m_depthStencilBuffer->Release();
        m_depthStencilBuffer = 0;
    }
 
    if (m_renderTargetView)
    {
        m_renderTargetView->Release();
        m_renderTargetView = 0;
    }
 
    if (m_deviceContext)
    {
        m_deviceContext->Release();
        m_deviceContext = 0;
    }
 
    if (m_device)
    {
        m_device->Release();
        m_device = 0;
    }
 
    if (m_swapChain)
    {
        m_swapChain->Release();
        m_swapChain = 0;
    }
 
    return;
}
 
 
void D3DClass::BeginScene(float red, float green, float blue, float alpha)
{
    float color[4];
 
 
    //버퍼를 채울 색상을 설정
    color[0= red;
    color[1= green;
    color[2= blue;
    color[3= alpha;
 
    //백 버퍼 닦기
    m_deviceContext->ClearRenderTargetView(m_renderTargetView, color);
 
    //깊이 버퍼 닦기
    m_deviceContext->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
 
    return;
}
 
 
void D3DClass::EndScene()
{
    //백 버퍼를 화면에 표시
    if (m_vsync_enabled)
    {
        //화면 새로 고침 빈도 고정
        m_swapChain->Present(10);
    }
    else
    {
        //준비 되는대로 화면에 최대한 빨리 표시
        m_swapChain->Present(00);
    }
 
    return;
}
 
 
ID3D11Device* D3DClass::GetDevice()
{
    return m_device;
}
 
 
ID3D11DeviceContext* D3DClass::GetDeviceContext()
{
    return m_deviceContext;
}
 
IDXGISwapChain* D3DClass::GetSwapChain()
{
    return m_swapChain;
}
 
void D3DClass::GetProjectionMatrix(XMMATRIX& projectionMatrix)
{
    projectionMatrix = m_projectionMatrix;
    return;
}
 
 
void D3DClass::GetWorldMatrix(XMMATRIX& worldMatrix)
{
    worldMatrix = m_worldMatrix;
    return;
}
 
 
void D3DClass::GetOrthoMatrix(XMMATRIX& orthoMatrix)
{
    orthoMatrix = m_orthoMatrix;
    return;
}
 
 
void D3DClass::GetVideoCardInfo(char* cardName, int& memory)
{
    strcpy_s(cardName, 128, m_videoCardDescription);
    memory = m_videoCardMemory;
    return;
}
 
 
void D3DClass::TurnZBufferOn()
{
    m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1);
    return;
}
 
 
void D3DClass::TurnZBufferOff()
{
    m_deviceContext->OMSetDepthStencilState(m_depthDisabledStencilState, 1);
    return;
}
 
 
void D3DClass::EnableAlphaBlending()
{
    float blendFactor[4];
 
    blendFactor[0= 0.0f;
    blendFactor[1= 0.0f;
    blendFactor[2= 0.0f;
    blendFactor[3= 0.0f;
 
    m_deviceContext->OMSetBlendState(m_alphaEnableBlendingState, blendFactor, 0xffffffff);
 
    return;
}
 
 
void D3DClass::DisableAlphaBlending()
{
    float blendFactor[4];
 
    blendFactor[0= 0.0f;
    blendFactor[1= 0.0f;
    blendFactor[2= 0.0f;
    blendFactor[3= 0.0f;
 
    m_deviceContext->OMSetBlendState(m_alphaDisableBlendingState, blendFactor, 0xffffffff);
 
    return;
}
 
 
void D3DClass::TurnOnCulling()
{
    //컬링 모드 적용
    m_deviceContext->RSSetState(m_rasterState);
 
    return;
}
 
 
void D3DClass::TurnOffCulling()
{
    //컬링 모드 해제
    m_deviceContext->RSSetState(m_rasterStateNoCulling);
 
    return;
}
 
 
void D3DClass::EnableWireframe()
{
    //와이어 프레임 모드 적용
    m_deviceContext->RSSetState(m_rasterStateWireframe);
 
    return;
}
 
 
void D3DClass::DisableWireframe()
{
    //와이어 프레임 모드 해제
    m_deviceContext->RSSetState(m_rasterState);
 
    return;
}
cs


깃 링크

h117562/Tutorial4: Directx11 Tutorial (github.com)






c++ thread.h

 c++에서 쓰레드 돌릴려면 thread.h 헤더를 쓰면 되는데 이 친구는 쓰레드가 아직 실행 중인지, 아니면 강제 종료하거나 하는 함수가 없어서 조금 아쉬운 애다. std::thread 는 로컬 변수로 선언하든 new 동적 할당을 하든 start 함...