2016년 8월 31일 수요일

DirectX - 프레임 표시, 2DUI, 텍스쳐 가져오기, 마우스휠

// DirectX1.cpp : 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include "DirectX1.h"
#include <d3d9.h>
#include <d3dx9.h>
#include <Windows.h>
#include "Cuboid.h"
#include "Terrain.h"
#include "Sphere.h"
#include "Panel.h"

LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertextBuffer = NULL;
LPD3DXMESH g_pMesh = NULL;
D3DMATERIAL9 g_material;
CCuboid* g_pCube = NULL;
CTerrain* g_pTerrain = NULL;
CSphere* g_pSphere = NULL;
LPD3DXFONT g_pFont = NULL;
CPanel* g_pPanel = NULL;

float zoom = 0;
char text[256] = {};
DWORD dwTime = 0;
DWORD dwCount = 0;
DWORD dwPreCount = 0;
//BYTE, WORD, DWORD

struct MY_VERTEX
{
float x, y, z, rhw;
DWORD color;
};

#define D3DFVF_MYVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

HWND g_HWnd = NULL;

#define MAX_LOADSTRING 100

// 전역 변수:
HINSTANCE hInst;                                // 현재 인스턴스입니다.
WCHAR szTitle[MAX_LOADSTRING];                  // 제목 표시줄 텍스트입니다.
WCHAR szWindowClass[MAX_LOADSTRING];            // 기본 창 클래스 이름입니다.

// 이 코드 모듈에 들어 있는 함수의 정방향 선언입니다.
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);


HRESULT InitVertextBuffer()
{
g_pPanel = new CPanel(g_pD3DDevice, 256, 64, 800, 600);
g_pPanel->SetTexture("EnergyBar.bmp", D3DCOLOR_XRGB(0,0,0));
//HFONT hFont = CreateFont(FW_NORMAL, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, 0, 0, 0, 0, L"Arial");
D3DXCreateFont(g_pD3DDevice, 60, 0, FW_NORMAL, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, NONANTIALIASED_QUALITY, VARIABLE_PITCH, L"Arial", &g_pFont);
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(32, 32, 32));
D3DLIGHT9 light;
memset(&light, 0, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = 0.7f;
light.Diffuse.g = 0.7f;
light.Diffuse.b = 0.7f;
light.Diffuse.a = 0.7f;

light.Ambient.r = 0.2f;
light.Ambient.g = 0.2f;
light.Ambient.b = 0.2f;

light.Specular.r = 1.0f;
light.Specular.g = 0.0f;
light.Specular.b = 0.0f;

light.Direction.x = 0;
light.Direction.y = -0.8f;
light.Direction.z = 0.8f;

light.Position.x = 0;
light.Position.y = 10.0f;
light.Position.z = -10.0f;

light.Phi = D3DX_PI * 2.0f;
light.Theta = D3DX_PI / 4.0f;
light.Falloff = D3DX_PI;

light.Attenuation0 = 1.0f;

light.Range = 100.0f;
g_pD3DDevice->SetLight(0, &light);
g_pD3DDevice->LightEnable(0, TRUE);

D3DCOLORVALUE dif = { 1.0f,1.0f,1.0f,0.0f };
D3DCOLORVALUE amb = { 1.0f,1.0f,1.0f,0.0f };
D3DCOLORVALUE spe = { 1.0f,1.0f,1.0f,1.0f };
D3DCOLORVALUE emi = { 0,0,0,0 };
g_material.Diffuse = dif;
g_material.Ambient = amb;
g_material.Emissive = emi;
g_material.Specular = spe;
g_material.Power = 0;
//return D3DXCreateSphere(g_pD3DDevice, 2,100,100, &g_pMesh, NULL);
g_pCube = new CCuboid(g_pD3DDevice);
//g_pCube->SetSize(2, 2, 2);
g_pCube->SetTexture("1.bmp");

g_pTerrain = new CTerrain(g_pD3DDevice, 20, 20, 10.0f, 15.0f);
g_pTerrain->SetTexture("Grass.bmp");

g_pSphere = new CSphere(g_pD3DDevice, 20.0f, 20.0f);
g_pSphere->SetTexture("2.jpg");

return 1;
//return D3DXCreateTeapot(g_pD3DDevice, &g_pMesh, NULL);
}

void makeCamera()
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0, zoom, zoom),
&D3DXVECTOR3(0, 0, 0),
&D3DXVECTOR3(0, 1, 0));
if (zoom <= 0)
zoom = 10;
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);

D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f, 2.0f, 1.0f, 5000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}

void Setup2DCamera() {
D3DXMATRIX matOrtho;
D3DXMATRIX matIdentity;
D3DXMatrixOrthoLH(&matOrtho, 1024, 768, 0, 1.0f);
D3DXMatrixIdentity(&matIdentity);

g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matIdentity);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matIdentity);
g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}

void Render()
{
if (g_pD3DDevice == NULL)
return;

g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
g_pD3DDevice->BeginScene();
makeCamera();
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &mat);
g_pSphere->Render();
RECT rect;
rect.left = 100;
rect.top = 100;
rect.right = 1000;
rect.bottom = 1000;

DWORD dwNowTime = GetTickCount(); // 현재시간을 가져온다
float temp = (float)(dwNowTime - dwTime) / 1000.0f; // 1초마다 계산
temp = 1.0f / temp;
dwTime = GetTickCount();
char text[256] = {};
sprintf_s(text, "%.2f", temp);
g_pFont->DrawTextA(NULL, text, -1, &rect, DT_LEFT, D3DCOLOR_XRGB(255,255,255,255));
Setup2DCamera();
g_pPanel->MoveTo(600,50);
g_pPanel->Render();

g_pD3DDevice->EndScene();
g_pD3DDevice->Present(0, 0, 0, 0);
}


int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 여기에 코드를 입력합니다.

    // 전역 문자열을 초기화합니다.
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_DIRECTX1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 응용 프로그램 초기화를 수행합니다.
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

D3DDISPLAYMODE d3ddm;
g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));
//ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_HWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pD3DDevice);

InitVertextBuffer();

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DIRECTX1));

    MSG msg;
msg.message = 0;
BOOL fMessage = FALSE;
    // 기본 메시지 루프입니다.
    while (msg.message != WM_QUIT)
    {
fMessage = PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
        if (fMessage)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
else
{
Render();
}
    }

    return (int) msg.wParam;
}



//
//  함수: MyRegisterClass()
//
//  목적: 창 클래스를 등록합니다.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIRECTX1));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_DIRECTX1);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   함수: InitInstance(HINSTANCE, int)
//
//   목적: 인스턴스 핸들을 저장하고 주 창을 만듭니다.
//
//   설명:
//
//        이 함수를 통해 인스턴스 핸들을 전역 변수에 저장하고
//        주 프로그램 창을 만든 다음 표시합니다.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 인스턴스 핸들을 전역 변수에 저장합니다.

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   g_HWnd = hWnd;

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  함수: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  목적:  주 창의 메시지를 처리합니다.
//
//  WM_COMMAND  - 응용 프로그램 메뉴를 처리합니다.
//  WM_PAINT    - 주 창을 그립니다.
//  WM_DESTROY  - 종료 메시지를 게시하고 반환합니다.
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 메뉴 선택을 구문 분석합니다.
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 여기에 hdc를 사용하는 그리기 코드를 추가합니다.
            EndPaint(hWnd, &ps);
        }
        break;
case WM_MOUSEWHEEL: // 휠올리면 멀어지고 내리면 가까워지고
{
((short)HIWORD(wParam)<0) ? zoom -= 20 : zoom += 20;
}
break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
case WM_KEYUP:
{
switch (wParam)
{
case VK_ESCAPE:
DestroyWindow(hWnd);
return 0;
}
}
break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// 정보 대화 상자의 메시지 처리기입니다.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

2016년 8월 30일 화요일

DirectX - 태양계 만들기

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
// DirectX.cpp : 응용 프로그램에 대한 진입점을 정의합니다.
//
 
#include "stdafx.h"
#include "DirectX.h"
#include <d3d9.h>
#include <d3dx9.h>
#include "Cuboid.h"
 
LPDIRECT3D9 g_pD3D = NULL// 장치 정보를 가져온다.
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL;
//하드웨어를 제어하려면 다이렉트를 통해 디바이스의 메모리를 가져와야한다. ( 그러기 위해 사용하는 두가지의 변수 )
LPDIRECT3DVERTEXBUFFER9  g_pVertexBuffer = NULL// 점을 배열에 넣고, 배열을 이용해서 그린다.
LPD3DXMESH g_pSunMesh = NULL;
LPD3DXMESH g_pEarthMesh = NULL;
LPD3DXMESH g_pMoonMesh = NULL;
LPD3DXMESH g_pSaturnMesh = NULL;
LPD3DXMESH g_pSaturnTorusMesh = NULL;
LPD3DXMESH g_pOthersMesh = NULL;
D3DMATERIAL9 g_pMaterial;
CCuboid*    g_pCube = NULL;
                                                 // BYTE 1, WORD 2 , DWORD 4 byte
struct MY_VERTEX
{
    float x, y, z, rhw;
    DWORD color;
};
 
#define D3DFVF_MYVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
// FVF : flexible vertex format (유연한 버텍스 포맷 : 내가 지정해주는 대로 됨)
 
 
HWND g_HWnd = NULL// 우리가 만들 윈도우 창의 메모리 주소
 
 
#define MAX_LOADSTRING 100
 
                    // 전역 변수:
int distance = 12;
float speed = 400.0f;
HINSTANCE hInst;                                // 현재 인스턴스입니다.
WCHAR szTitle[MAX_LOADSTRING];                  // 제목 표시줄 텍스트입니다.
WCHAR szWindowClass[MAX_LOADSTRING];            // 기본 창 클래스 이름입니다.
 
 
                                                // 이 코드 모듈에 들어 있는 함수의 정방향 선언입니다.
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
 
HRESULT InitVertextBuffer() {
    //VOID* pVertices;
    //MY_VERTEX ver[] =
    //{
    //    { 100.0f, 100.0f, 0.5, 1.0, D3DCOLOR_XRGB(255,0,0), },
    //    { 400.0f, 100.0f, 0.5, 1.0, D3DCOLOR_XRGB(0,255,0), },
    //    { 100.0f, 350.0f, 0.5, 1.0, D3DCOLOR_XRGB(0,0,255), },
    //    { 400.0f, 350.0f, 0.5, 1.0, D3DCOLOR_XRGB(0,255,255), },
    //};
    //g_pD3DDevice->CreateVertexBuffer(sizeof(MY_VERTEX) * 3, 0, D3DFVF_MYVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuffer, NULL);
    ////vertexbuffer 메모리에 버텍스 값 저장
    //g_pVertexBuffer->Lock(0, sizeof(ver), &pVertices, 0); //하드웨어에 직접 접근하는데, 메모리를 내가 사용 하는 동안 다른 데서 접근하지 못하도록 하기 위해 락을 걸어준다.
    //memcpy(pVertices, ver, sizeof(ver));
    //g_pVertexBuffer->Unlock();
    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, TRUE); // 렌더링 환경 설정
    g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
    g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(10,10,10)); // xrgb - rgb만 argb - a값도 넣는다 
    D3DLIGHT9 light;
    memset(&light, 0sizeof(light)); // memset 0값으로 구조체 초기화
    light.Type = D3DLIGHT_SPOT; // directional + point = spot
    light.Diffuse.r = 0.7f; // 직사광선 받앗을때 반사하는 색
    light.Diffuse.g = 0.7f;
    light.Diffuse.b = 0.7f;
    light.Diffuse.a = 0.7f;
 
    light.Ambient.r = 0.2f; // 주변광을 반사
    light.Ambient.g = 0.2f;
    light.Ambient.b = 0.2f;
 
    light.Specular.r = 1.0f; // 제일 밝은곳에 반사
    light.Specular.g = 1.0f;
    light.Specular.b = 1.0f;
 
    light.Direction.x = 0;
    light.Direction.y = -0.8f;
    light.Direction.z = 0.8f;
 
    light.Position.x = 0
    light.Position.y = 10.0f;
    light.Position.z = 0;
 
    light.Attenuation0 = 1.0f;
    light.Range = 100.0f;
 
    g_pD3DDevice->SetLight(0&light); // 라이트 넣고
    g_pD3DDevice->LightEnable(0, TRUE); // 라이트 켜라
 
    D3DCOLORVALUE dif = { 1.0f, 1.0f, 1.0f, 0 };
    D3DCOLORVALUE amb = { 1.0f, 1.0f, 1.0f, 0 };
    D3DCOLORVALUE spe = { 1.0f, 1.0f, 0.0f, 1.0f };
    D3DCOLORVALUE ami = { 0000 };
    g_pMaterial.Diffuse = dif;
    g_pMaterial.Ambient = amb;
    g_pMaterial.Emissive = ami;
    g_pMaterial.Specular = spe;
    g_pMaterial.Power = 0;
 
    g_pCube = new CCuboid(g_pD3DDevice);
    g_pCube->SetSize(222);
    g_pCube->SetTexture("1.bmp");
 
    D3DXCreateBox(g_pD3DDevice, 2.0f, 2.0f, 2.0f, &g_pSunMesh, NULL);
    D3DXCreateBox(g_pD3DDevice, 1.0f, 1.0f, 1.0f, &g_pEarthMesh, NULL);
    D3DXCreateBox(g_pD3DDevice, 1.0f, 1.0f, 1.0f, &g_pMoonMesh, NULL);
    D3DXCreateBox(g_pD3DDevice, 2.0f, 2.5f, 2.0f, &g_pSaturnMesh, NULL);
    D3DXCreateTorus(g_pD3DDevice, 0.1f, 2.0f, 20.0f, 20.0f, &g_pSaturnTorusMesh, NULL);
    D3DXCreateSphere(g_pD3DDevice, 1.5f, 5050&g_pOthersMesh, NULL); // 주전자 그리기
    return S_OK;
}
 
void makeCamera() {
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(020.0f, -50.0f), &D3DXVECTOR3(000), &D3DXVECTOR3(010)); // camera lookat
    g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4.0f, 2.0f, 1.0f, 500.0f); // 화면비 다음인수 시야각
    g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
 
void SetPosition() {
    // 태양
    D3DXMATRIX matSun, matSunRotationX, matSunRotationY;
    D3DXMatrixTranslation(&matSun, 000);
    D3DXMatrixRotationY(&matSunRotationY, GetTickCount() / 400.0f);
    D3DXMatrixMultiply(&matSun, &matSunRotationY, &matSun);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matSun);
    g_pD3DDevice->SetMaterial(&g_pMaterial);
    g_pSunMesh->DrawSubset(0);
 
    // 지구
    D3DXMATRIX matEarth, matEarthRotationX, matEarthRotationY;
    D3DXMatrixTranslation(&matEarth, 500);
    D3DXMatrixRotationY(&matEarthRotationY, GetTickCount() / 400.0f);
    D3DXMatrixMultiply(&matEarth, &matEarthRotationY, &matEarth);
    D3DXMatrixMultiply(&matEarth, &matEarth, &matEarthRotationY);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matEarth);
    g_pCube->Render();
 
    // 달
    D3DXMATRIX matMoon, matMoonRotationX, matMoonRotationY, matMoonScale;
    D3DXMatrixTranslation(&matMoon, 200);
    D3DXMatrixRotationY(&matMoonRotationY, GetTickCount() / 200.0f);
    D3DXMatrixScaling(&matMoonScale, 0.5f, 1.0f, 0.5f);
    D3DXMatrixMultiply(&matMoon, &matMoon, &matMoonScale);
    D3DXMatrixMultiply(&matMoon, &matMoonRotationY, &matMoon);
    D3DXMatrixMultiply(&matMoon, &matMoon, &(matMoonRotationY * matEarth));
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matMoon);
    g_pCube->Render();
 
    // 토성
    D3DXMATRIX matSaturn, matSaturnRotationX, matSaturnRotationY;
    D3DXMatrixTranslation(&matSaturn, 800);
    D3DXMatrixRotationY(&matSaturnRotationY, GetTickCount() / 300.0f);
    D3DXMatrixMultiply(&matSaturn, &matSaturnRotationY, &matSaturn);
    D3DXMatrixMultiply(&matSaturn, &matSaturn, &matSaturnRotationY);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matSaturn);
    g_pSaturnMesh->DrawSubset(0);
    // 고리
    D3DXMATRIX matSaturnTorus, matSaturnTorusRotationX, matSaturnTorusRotationY;
    D3DXMatrixTranslation(&matSaturnTorus, 800);
    D3DXMatrixRotationX(&matSaturnTorusRotationX, GetTickCount() / 300.0f);
    D3DXMatrixRotationY(&matSaturnTorusRotationY, GetTickCount() / 300.0f);
    D3DXMatrixMultiply(&matSaturnTorus, &matSaturnTorusRotationY, &matSaturnTorus);
    D3DXMatrixMultiply(&matSaturnTorus, &matSaturnTorus, &matSaturnTorusRotationX);
    D3DXMatrixMultiply(&matSaturnTorus, &matSaturnTorus, &matSaturnTorusRotationY);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matSaturnTorus);
    g_pSaturnTorusMesh->DrawSubset(0);
 
}
 
void SetPosition2() {
    D3DXMATRIX matOthers, matOthersRotationX, matOthersRotationY;
    D3DXMatrixTranslation(&matOthers, 1400);
    D3DXMatrixRotationY(&matOthersRotationY, GetTickCount() / 300.0f);
    D3DXMatrixMultiply(&matOthers, &matOthersRotationY, &matOthers);
    D3DXMatrixMultiply(&matOthers, &matOthers, &matOthersRotationY);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matOthers);
}
 
void Render() {
    if (g_pD3DDevice == NULL)
        return;
 
    g_pD3DDevice->Clear(0NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(00255), 1.0f, 0); // z버퍼도 초기화를 해줘야 나온다.
    g_pD3DDevice->BeginScene();
 
    //g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, 0, sizeof(MY_VERTEX));
    //g_pD3DDevice->SetFVF(D3DFVF_MYVERTEX); // 내가 정해준 포맷으로
    //g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // 화면에 그려라
    makeCamera();
    SetPosition();
    SetPosition2();
    g_pOthersMesh->DrawSubset(0);
 
    // 주전자 태양
    D3DXMATRIX matSSun;
    D3DXMatrixRotationY(&matSSun, GetTickCount() / 400.0f);
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matSSun);
    g_pOthersMesh->DrawSubset(0);
 
    // 주전자 지구
    D3DXMATRIX matEarth2, matEarth2RotationY1, matEarth2RotationY2, matEarthScale;
    D3DXMatrixIdentity(&matEarth2);
    D3DXMatrixScaling(&matEarthScale, 0.5f, 1.0f, 0.5f);
    D3DXMatrixRotationY(&matEarth2, GetTickCount() / 400.0f);
    D3DXMatrixTranslation(&matEarth2RotationY1, 1600);
    D3DXMatrixRotationY(&matEarth2RotationY2, GetTickCount() / 1000.0f);
    matEarthScale = matEarth2 * matEarthScale;
    matEarth2RotationY1 = matEarth2RotationY1 * matEarth2RotationY2;
    matEarth2 = matEarth2 * matEarthScale * matEarth2RotationY1;
 
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matEarth2);
    g_pCube->Render();
 
    // 주전자 달
    D3DXMATRIX matMoon2, matMoon2RotationY1, matMoon2RotationY2;
    D3DXMatrixIdentity(&matMoon2);
    D3DXMatrixRotationY(&matMoon2, GetTickCount() / 300.0f);
    D3DXMatrixTranslation(&matMoon2RotationY1, 300);
    D3DXMatrixRotationY(&matMoon2RotationY2, GetTickCount() / 500.0f);
    matMoon2RotationY1 = matMoon2RotationY1 * matMoon2RotationY2;
    matMoon2 = matMoon2 * matMoon2RotationY1 * matEarth2RotationY1;
 
    g_pD3DDevice->SetTransform(D3DTS_WORLD, &matMoon2);
    g_pCube->Render();
 
    g_pD3DDevice->EndScene();
    g_pD3DDevice->Present(0000);
}
 
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
 
    // TODO: 여기에 코드를 입력합니다.
 
    // 전역 문자열을 초기화합니다.
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_DIRECTX, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
 
    // 응용 프로그램 초기화를 수행합니다.
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }
    g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
 
    D3DDISPLAYMODE d3ddm;
    g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm); // DEVICE 그릴 환경을 어떻게 만들건가
    D3DPRESENT_PARAMETERS d3dpp;
    memset(&d3dpp, 0sizeof(d3dpp)); // 메모리를 0 으로 초기화해준다. 같은의미 = ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE; // FALSE 하면 전체화면이 된다.
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //화면 갱신니 어떻게 할 것인가, 메모리를 복사할것인가? (DISCAR, COPY, FLIP)
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE; // z버퍼 자동 생성
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_HWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice);
    // (HAL부터 하드웨어 엑셀레이터, 윈도우 포인터, 버텍스(3차원 상의 점) 위주로 소프트웨어로 돌린다. (하드웨어로 돌리는 기술도 있지만, 일반컴은 안댐), ,디바이스 포인터);
 
    InitVertextBuffer();
 
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DIRECTX));
 
    MSG msg;
    msg.message = 0;
    BOOL fMessage = FALSE;
 
    // 기본 메시지 루프입니다.
    while (msg.message != WM_QUIT) // GetMessage  - 메세지가 없으면 대기한다.  msg.message!=WM_QUIT - 유저가 종료하지 않으면 루프해라.(메세지가 없더라도 계속 루프) CPU 점유율이 달라진다.
    {
        fMessage = PeekMessage(&msg, nullptr, 00, PM_REMOVE);
        if (fMessage) // 메세지가 있을 때
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else // 메세지가 없을때
        {
            Render();
        }
    }
 
    return (int)msg.wParam;
}
 
 
 
//
//  함수: MyRegisterClass()
//
//  목적: 창 클래스를 등록합니다.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;
 
    wcex.cbSize = sizeof(WNDCLASSEX);
 
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc; // 윈도우 명령어를 받아준다.
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIRECTX));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_DIRECTX);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
 
    return RegisterClassExW(&wcex);
}
 
//
//   함수: InitInstance(HINSTANCE, int)
//
//   목적: 인스턴스 핸들을 저장하고 주 창을 만듭니다.
//
//   설명:
//
//        이 함수를 통해 인스턴스 핸들을 전역 변수에 저장하고
//        주 프로그램 창을 만든 다음 표시합니다.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // 인스턴스 핸들을 전역 변수에 저장합니다.
 
    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
    // 윈도우를 처음 만들 때 쓰이는 것 CreateWindow
 
    if (!hWnd)
    {
        return FALSE;
    }
 
    g_HWnd = hWnd;
 
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
 
    return TRUE;
}
 
//
//  함수: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  목적:  주 창의 메시지를 처리합니다.
//
//  WM_COMMAND  - 응용 프로그램 메뉴를 처리합니다.
//  WM_PAINT    - 주 창을 그립니다.
//  WM_DESTROY  - 종료 메시지를 게시하고 반환합니다.
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND: // 윈도우 도구 선택줄에서 오는 명령어를 확인한다.
    {
        int wmId = LOWORD(wParam);
        // 메뉴 선택을 구문 분석합니다.
        switch (wmId)
        {
        case IDM_ABOUT: // 도움말
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT: // 끝내기
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps); // 윈도우에서 그려주는 공간을 얻기 위한 변수.
                                         // TODO: 여기에 hdc(device context) 를 사용하는 그리기 코드를 추가합니다.
        EndPaint(hWnd, &ps);
    }
    break;
    case WM_DESTROY: // 종료메세지를 받을때 (알트 + f4/x버튼) 없으면 프로그램이 안죽는다.
        PostQuitMessage(0);
        break;
 
    case WM_KEYUP: // 마우스나 키의 명령어를 받을 수 있다.
    {
        switch (wParam) // 어떤 키를 받았는가
        {
        case VK_ESCAPE: // 가상 키보드의 ESC 를 받아온다.(키보드의 ESC를 누르면 프로그램이 종료된다.)
            DestroyWindow(hWnd);
            return 0;
        }
        break;
    }
    break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
 
// 정보 대화 상자의 메시지 처리기입니다.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;
 
    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
cs