-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsClass.cpp
627 lines (499 loc) · 16.2 KB
/
GraphicsClass.cpp
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
#include "stdafx.h"
#include "d3dclass.h"
#include "cameraclass.h"
#include "modelclass.h"
#include "lightshaderclass.h"
#include "depthShaderClass.h"
#include "shadowShaderClass.h"
#include "EnvironmentShaderClass.h"
#include "lightclass.h"
#include "rendertextureclass.h"
#include "debugwindowclass.h"
#include "textureshaderclass.h"
#include "graphicsclass.h"
GraphicsClass::GraphicsClass()
{
LVP = XMMatrixIdentity();
NDCToWolrdMatrix = XMMatrixIdentity();
}
GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
LVP = XMMatrixIdentity();
NDCToWolrdMatrix = XMMatrixIdentity();
}
GraphicsClass::~GraphicsClass()
{
}
void GraphicsClass::CameraMove(float dx, float dy, float dz)
{
XMFLOAT3 pos = m_Camera->GetPosition();
m_Camera->SetPosition(dx + pos.x, dy + pos.y, dz + pos.z);
}
void GraphicsClass::CameraRotate(float dx, float dy, float dz)
{
XMFLOAT3 pos = m_Camera->GetRotation();
m_Camera->SetRotation(dx + pos.x, dy + pos.y, dz + pos.z);
}
bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
// Direct3D 객체 생성
m_Direct3D = new D3DClass;
if (!m_Direct3D)
{
return false;
}
// Direct3D 객체 초기화
if (!m_Direct3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR))
{
MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
return false;
}
// m_Camera 객체 생성
m_Camera = new CameraClass;
if (!m_Camera)
{
return false;
}
// 모델 객체 생성
m_Model = new ModelClass;
if (!m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), "./data/stone03.tga", "./data/Ai.obj"))
{
MessageBox(hwnd, L"Could not initialize the m_Model model object.", L"Error", MB_OK);
return false;
}
m_Plane = new ModelClass;
if (!m_Plane->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), "./data/desk.tga", "./data/Plane.obj"))
{
MessageBox(hwnd, L"Could not initialize the m_Plane model object.", L"Error", MB_OK);
return false;
}
m_sphere = new ModelClass;
if (!m_sphere->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), "./data/stone03.tga", "./data/bigTeapot.obj"))
{
MessageBox(hwnd, L"Could not initialize the m_sphere model object.", L"Error", MB_OK);
return false;
}
m_underPlane = new ModelClass;
if (!m_underPlane->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), "./data/stone03.tga", "./data/plane1.obj"))
{
MessageBox(hwnd, L"Could not initialize the m_underPlane model object.", L"Error", MB_OK);
return false;
}
m_sphereMap = new ModelClass;
if (!m_sphereMap->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), "./data/spheremap2.tga", "./data/smallsphere.obj"))
{
MessageBox(hwnd, L"Could not initialize the m_underPlane model object.", L"Error", MB_OK);
return false;
}
// 라이트 쉐이더 객체를 만듭니다.
m_LightShader = new LightShaderClass;
if (!m_LightShader)
{
return false;
}
// 라이트 쉐이더 객체를 초기화합니다.
if (!m_LightShader->Initialize(m_Direct3D->GetDevice(), hwnd))
{
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
return false;
}
m_depthShader = new depthShaderClass;
if (!m_depthShader)
{
return false;
}
if (!m_depthShader->Initialize(m_Direct3D->GetDevice(), hwnd))
{
MessageBox(hwnd, L"Could not initialize the depth shader object.", L"Error", MB_OK);
return false;
}
m_shadowShader = new shadowShaderClass;
if (!m_shadowShader)
{
return false;
}
if (!m_shadowShader->Initialize(m_Direct3D->GetDevice(), hwnd))
{
MessageBox(hwnd, L"Could not initialize the shadow shader object.", L"Error", MB_OK);
return false;
}
m_EnvironmentShader = new EnvironmentShaderClass;
if (!m_EnvironmentShader)
{
return false;
}
if (!m_EnvironmentShader->Initialize(m_Direct3D->GetDevice(), hwnd))
{
MessageBox(hwnd, L"Could not initialize the shadow shader object.", L"Error", MB_OK);
return false;
}
// 조명 객체를 만듭니다.
m_Light = new LightClass;
if (!m_Light)
{
return false;
}
m_Camera->SetPosition(0.f, 1.f, -5.f);
m_Light->SetAmbientColor(1.f, 1.f, 1.f, 1.f); //주변광
m_Light->SetDiffuseColor(1.f, 1.f, 1.f, 1.f); //색상
m_Light->SetPosition(0.f, 3.f, -5.f); //빛의 방향
// 렌더링 텍스처 객체를 생성한다.
m_RenderTexture = new RenderTextureClass;
if (!m_RenderTexture)
{
return false;
}
// 렌더링 텍스처 객체를 초기화한다.
if (!m_RenderTexture->Initialize(m_Direct3D->GetDevice(), screenWidth, screenHeight))
{
return false;
}
// 디버그 창 객체를 만듭니다.
m_DebugWindow = new DebugWindowClass;
if (!m_DebugWindow)
{
return false;
}
// 디버그 창 객체를 초기화 합니다.
if (!m_DebugWindow->Initialize(m_Direct3D->GetDevice(), screenWidth, screenHeight, 128 * 2, 128 * 2))
{
MessageBox(hwnd, L"Could not initialize the debug window object.", L"Error", MB_OK);
return false;
}
// 텍스처 쉐이더 객체를 생성한다.
m_TextureShader = new TextureShaderClass;
if (!m_TextureShader)
{
return false;
}
// 텍스처 쉐이더 객체를 초기화한다.
if (!m_TextureShader->Initialize(m_Direct3D->GetDevice(), hwnd))
{
MessageBox(hwnd, L"Could not initialize the texture shader object.", L"Error", MB_OK);
return false;
}
return true;
}
void GraphicsClass::Shutdown()
{
// 텍스처 쉐이더 객체를 해제한다.
if (m_TextureShader)
{
m_TextureShader->Shutdown();
delete m_TextureShader;
m_TextureShader = 0;
}
// 디버그 창 객체를 해제합니다.
if (m_DebugWindow)
{
m_DebugWindow->Shutdown();
delete m_DebugWindow;
m_DebugWindow = 0;
}
// 렌더를 텍스쳐 객체로 릴리즈한다.
if (m_RenderTexture)
{
m_RenderTexture->Shutdown();
delete m_RenderTexture;
m_RenderTexture = 0;
}
// 조명 객체를 해제한다.
if (m_Light)
{
delete m_Light;
m_Light = 0;
}
// 라이트 쉐이더 객체를 해제합니다.
if (m_LightShader)
{
m_LightShader->Shutdown();
delete m_LightShader;
m_LightShader = 0;
}
if (m_depthShader)
{
m_depthShader->Shutdown();
delete m_depthShader;
m_depthShader = 0;
}
if (m_shadowShader)
{
m_shadowShader->Shutdown();
delete m_shadowShader;
m_shadowShader = 0;
}
if (m_EnvironmentShader)
{
m_EnvironmentShader->Shutdown();
delete m_EnvironmentShader;
m_EnvironmentShader = 0;
}
// 모델 객체 반환
if (m_Model)
{
m_Model->Shutdown();
delete m_Model;
m_Model = 0;
}
if (m_Plane)
{
m_Plane->Shutdown();
delete m_Plane;
m_Plane = 0;
}
if (m_sphere)
{
m_sphere->Shutdown();
delete m_sphere;
m_sphere = 0;
}
if (m_underPlane)
{
m_underPlane->Shutdown();
delete m_underPlane;
m_underPlane = 0;
}
if (m_sphereMap)
{
m_sphereMap->Shutdown();
delete m_sphereMap;
m_sphereMap = 0;
}
// m_Camera 객체 반환
if (m_Camera)
{
delete m_Camera;
m_Camera = 0;
}
// Direct3D 객체 반환
if (m_Direct3D)
{
m_Direct3D->Shutdown();
delete m_Direct3D;
m_Direct3D = 0;
}
}
bool GraphicsClass::RenderToTexture()
{
// 렌더링 대상을 렌더링에 맞게 설정합니다.
m_RenderTexture->SetRenderTarget(m_Direct3D->GetDeviceContext(), m_Direct3D->GetDepthStencilView());
// 렌더링을 텍스처에 지웁니다.
m_RenderTexture->ClearRenderTarget(m_Direct3D->GetDeviceContext(), m_Direct3D->GetDepthStencilView(), 1.0f, 1.0f, 1.0f, 1.0f);
// 이제 장면을 렌더링하면 백 버퍼 대신 텍스처로 렌더링됩니다.
if (!RenderScene())
{
return false;
}
// 렌더링 대상을 원래의 백 버퍼로 다시 설정하고 렌더링에 대한 렌더링을 더 이상 다시 설정하지 않습니다.
m_Direct3D->SetBackBufferRenderTarget();
return true;
}
bool GraphicsClass::RenderScene()
{
// 카메라 및 d3d 객체에서 월드, 뷰 및 투영 행렬을 가져옵니다
XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
// 각 프레임의 rotation 변수를 업데이트합니다.
static float rotation = 0.0f;
rotation += (float)XM_PI * 0.0025f;
// 회전 값으로 월드 행렬을 회전합니다.
worldMatrix = XMMatrixRotationY(rotation);
// 모델 버텍스와 인덱스 버퍼를 그래픽 파이프 라인에 배치하여 렌더링 합니다.
m_Model->Render(m_Direct3D->GetDeviceContext());
// 라이트 쉐이더를 사용하여 모델을 렌더링합니다.
m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(), m_Light->GetPosition(), m_Light->GetDiffuseColor());
return true;
}
bool GraphicsClass::TextureToRender()
{
// 카메라 및 d3d 객체에서 월드, 뷰 및 투영 행렬을 가져옵니다
XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
// 각 프레임의 rotation 변수를 업데이트합니다.
static float rotation = 0.0f;
rotation += (float)XM_PI * 0.0025f;
// 회전 값으로 월드 행렬을 회전합니다.
worldMatrix = XMMatrixRotationY(rotation);
// 모델 버텍스와 인덱스 버퍼를 그래픽 파이프 라인에 배치하여 렌더링 합니다.
m_Model->Render(m_Direct3D->GetDeviceContext());
// 라이트 쉐이더를 사용하여 모델을 렌더링합니다.
m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(), m_Light->GetPosition(), m_Light->GetDiffuseColor());
static float rotation1 = 110;
static float rotation2 = 3.141592653f;
worldMatrix = XMMatrixRotationY(rotation1) * XMMatrixRotationZ(rotation2);
// 모델 버텍스와 인덱스 버퍼를 그래픽 파이프 라인에 배치하여 렌더링 합니다.
m_Plane->Render(m_Direct3D->GetDeviceContext());
// 라이트 쉐이더를 사용하여 모델을 렌더링합니다.
m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Plane->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetDiffuseColor());
return true;
}
bool GraphicsClass::debugWindowRender(ID3D11ShaderResourceView* texture, int x, int y) {
// 모든 2D 렌더링을 시작하려면 Z 버퍼를 끕니다.
m_Direct3D->TurnZBufferOff();
// 카메라 및 d3d 객체에서 월드, 뷰 및 투영 행렬을 가져옵니다
XMMATRIX worldMatrix, viewMatrix, orthoMatrix;
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetOrthoMatrix(orthoMatrix);
worldMatrix = XMMatrixIdentity();
// 디버그 윈도우 버텍스와 인덱스 버퍼를 그래픽 파이프 라인에 배치하여 그리기를 준비합니다.
if (!m_DebugWindow->Render(m_Direct3D->GetDeviceContext(), x, y))
{
return false;
}
//텍스처 셰이더를 사용해 디버그 윈도우를 렌더링한다.
if (!m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_DebugWindow->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, texture))
{
return false;
}
// 모든 2D 렌더링이 완료되었으므로 Z 버퍼를 다시 켜십시오.
m_Direct3D->TurnZBufferOn();
return true;
}
bool GraphicsClass::depthMapRender() {
XMMATRIX worldMatrix, lightViewMatrix, projectionMatrix, MVP;
m_RenderTexture->SetRenderTarget(m_Direct3D->GetDeviceContext(), m_Direct3D->GetDepthStencilView());
m_RenderTexture->ClearRenderTarget(m_Direct3D->GetDeviceContext(), m_Direct3D->GetDepthStencilView(), 1.0f, 1.0f, 1.0f, 1.0f);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
lightViewMatrix = m_Light->lightViewMatrix(m_Light->GetPosition(), m_Light->at);
MVP = XMMatrixMultiply(worldMatrix, lightViewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
m_underPlane->Render(m_Direct3D->GetDeviceContext());
if (!m_depthShader->depthRender(m_Direct3D->GetDeviceContext(), m_underPlane->GetIndexCount(), MVP, m_underPlane->GetTexture())) {
return false;
}
static float rotation = 0.0f;
rotation += (float)XM_PI * 0.0025f;
worldMatrix = XMMatrixRotationY(rotation);
MVP = XMMatrixMultiply(worldMatrix, lightViewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
m_sphere->Render(m_Direct3D->GetDeviceContext());
if (!m_depthShader->depthRender(m_Direct3D->GetDeviceContext(), m_sphere->GetIndexCount(), MVP, m_sphere->GetTexture())) {
return false;
}
m_Direct3D->SetBackBufferRenderTarget();
return true;
}
bool GraphicsClass::shadowRender() {
XMMATRIX worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, MVP;
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
worldMatrix = XMMatrixMultiply(worldMatrix, XMMatrixScaling(100.f, 100.f, 100.f));
MVP = XMMatrixMultiply(worldMatrix, viewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
NDCToWolrdMatrix = XMMatrixMultiply(viewMatrix, projectionMatrix);
NDCToWolrdMatrix = XMMatrixInverse(nullptr, NDCToWolrdMatrix);
lightViewMatrix = m_Light->lightViewMatrix(m_Light->GetPosition(), m_Light->at);
LVP = XMMatrixMultiply(lightViewMatrix, projectionMatrix);
m_underPlane->Render(m_Direct3D->GetDeviceContext());
if (!m_shadowShader->shadowRender(m_Direct3D->GetDeviceContext(), m_underPlane->GetIndexCount(), MVP, worldMatrix, LVP, NDCToWolrdMatrix,
m_underPlane->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetDiffuseColor())) {
return false;
}
static float rotation = 0.0f;
rotation += (float)XM_PI * 0.0025f;
worldMatrix = XMMatrixRotationY(rotation);
MVP = XMMatrixMultiply(worldMatrix, viewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
m_sphere->Render(m_Direct3D->GetDeviceContext());
if (!m_shadowShader->shadowRender(m_Direct3D->GetDeviceContext(), m_sphere->GetIndexCount(), MVP, worldMatrix, LVP, NDCToWolrdMatrix,
m_sphere->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetDiffuseColor())) {
return false;
}
return true;
}
bool GraphicsClass::EnvironmentRender() {
XMMATRIX worldMatrix, viewMatrix, projectionMatrix, lightViewMatrix, MVP;
m_Camera->GetViewMatrix(viewMatrix);
m_Direct3D->GetWorldMatrix(worldMatrix);
m_Direct3D->GetProjectionMatrix(projectionMatrix);
MVP = XMMatrixMultiply(worldMatrix, viewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
NDCToWolrdMatrix = XMMatrixMultiply(viewMatrix, projectionMatrix);
NDCToWolrdMatrix = XMMatrixInverse(nullptr, NDCToWolrdMatrix);
lightViewMatrix = m_Light->lightViewMatrix(m_Light->GetPosition(), m_Light->at);
LVP = XMMatrixMultiply(lightViewMatrix, projectionMatrix);
m_underPlane->Render(m_Direct3D->GetDeviceContext());
if (!m_shadowShader->shadowRender(m_Direct3D->GetDeviceContext(), m_underPlane->GetIndexCount(), MVP, worldMatrix, LVP, NDCToWolrdMatrix,
m_underPlane->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetDiffuseColor())) {
return false;
}
static float rotation = 0.0f;
rotation += (float)XM_PI * 0.0025f;
worldMatrix = XMMatrixRotationY(rotation);
MVP = XMMatrixMultiply(worldMatrix, viewMatrix);
MVP = XMMatrixMultiply(MVP, projectionMatrix);
m_sphere->Render(m_Direct3D->GetDeviceContext());
if (!m_EnvironmentShader->EnvironmentRender(m_Direct3D->GetDeviceContext(), m_sphere->GetIndexCount(), MVP, worldMatrix, LVP, NDCToWolrdMatrix,
m_sphere->GetTexture(), m_sphereMap->GetTexture(), m_RenderTexture->GetShaderResourceView(), m_Light->GetPosition(), m_Light->GetDiffuseColor(), m_Camera->GetPosition())) {
return false;
}
return true;
}
bool GraphicsClass::RTT() {
m_Camera->GetPosition();
m_Camera->viewMatrix();
if (!RenderToTexture())
{
return false;
}
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
if (!TextureToRender())
{
return false;
}
//debugWindowRender(m_RenderTexture->GetShaderResourceView(), 50, 50);
m_Direct3D->EndScene();
return true;
}
bool GraphicsClass::shadowMapping() {
m_Camera->GetPosition();
m_Camera->viewMatrix();
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
if (!depthMapRender()) {
return false;
}
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
if (!shadowRender()) {
return false;
}
//debugWindowRender(m_RenderTexture->GetShaderResourceView(), 50, 50);
m_Direct3D->EndScene();
return true;
}
bool GraphicsClass::EnvironmentMappingRender() {
m_Camera->GetPosition();
m_Camera->viewMatrix();
if (!depthMapRender()) {
return false;
}
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
if (!EnvironmentRender()) {
return false;
}
debugWindowRender(m_sphereMap->GetTexture(), 50, 50);
m_Direct3D->EndScene();
return true;
}
bool GraphicsClass::Render()
{
if (!EnvironmentMappingRender()) {
return false;
}
/*if (!shadowMapping()) {
return false;
}*/
/*if (!RTT()) {
return false;
}*/
return true;
}