-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS580HWView.cpp
More file actions
532 lines (457 loc) · 11.7 KB
/
CS580HWView.cpp
File metadata and controls
532 lines (457 loc) · 11.7 KB
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
// CS580HWView.cpp : implementation of the CCS580HWView class
//
#include "stdafx.h"
#include "CS580HW.h"
#include "CS580HWDoc.h"
#include "CS580HWView.h"
#include "RotateDlg.h"
#include "TranslateDlg.h"
#include "ScaleDlg.h"
#include "disp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView
IMPLEMENT_DYNCREATE(CCS580HWView, CView)
BEGIN_MESSAGE_MAP(CCS580HWView, CView)
ON_COMMAND(IDM_ROTATE, OnRotate)
ON_COMMAND(IDM_TRANSLATE, OnTranslate)
ON_COMMAND(IDM_SCALE, OnScale)
ON_COMMAND(ID_EDIT_POPMATRIX, &CCS580HWView::OnEditPopmatrix)
ON_COMMAND(ID_EDIT_ADD, &CCS580HWView::OnEditAdd)
ON_COMMAND(ID_EDIT_DELETELIGHT, &CCS580HWView::OnEditDeletelight)
ON_COMMAND(ID_MESH_SWITCHEFFECT, &CCS580HWView::OnMeshSwitcheffect)
ON_COMMAND(ID_MESH_TEXTURE, &CCS580HWView::OnMeshTexture)
ON_COMMAND(IDM_RENDER, OnRender)
ON_COMMAND(ID_ANIMATION, &CCS580HWView::OnAnimation)
ON_COMMAND(ID_EDIT_MESHDIV, &CCS580HWView::OnEditMeshdiv)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_DEMO_CAR, &CCS580HWView::OnDemoCar)
ON_COMMAND(ID_DEMO_F, &CCS580HWView::OnDemoF)
ON_COMMAND(ID_DEMO_PPOT, &CCS580HWView::OnDemoPpot)
ON_COMMAND(ID_DEMO_TORUS, &CCS580HWView::OnDemoTorus)
ON_COMMAND(ID_DEMO_TRICERATOPS, &CCS580HWView::OnDemoTriceratops)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView construction/destruction
CCS580HWView::CCS580HWView()
{
threadrun = false;
threadpause = false;
GzNewFrameBuffer(&screen, m_nWidth, m_nHeight);
req_draw = CreateSemaphore(NULL, 0, 1, NULL);
allow_draw = CreateSemaphore(NULL, 0, 1, NULL);
req_render = CreateSemaphore(NULL, 0, 1, NULL);
allow_render = CreateSemaphore(NULL, 1, 1, NULL);
}
CCS580HWView::~CCS580HWView()
{
}
BOOL CCS580HWView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView drawing
void CCS580HWView::OnDraw(CDC* pDC)
{
CCS580HWDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HDC memdc;
HBITMAP m_bitmap;
BITMAPINFO binfo;
BITMAPINFOHEADER &bih = binfo.bmiHeader;
// Create the bitmap
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biBitCount = 24;
bih.biWidth = m_nWidth;
bih.biHeight = -m_nHeight;
bih.biPlanes = 1;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
memdc = ::CreateCompatibleDC(pDC->m_hDC);
m_bitmap = CreateDIBSection(memdc, &binfo, 0, 0, 0, DIB_RGB_COLORS);
SelectObject(memdc, m_bitmap);
SetDIBits(memdc, m_bitmap, 0, m_nHeight, screen, &binfo, DIB_RGB_COLORS);
BitBlt(pDC->m_hDC, 0, 0, m_nWidth, m_nHeight, memdc, 0, 0, SRCCOPY);
DeleteDC(memdc);
DeleteObject(m_bitmap);
}
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView diagnostics
#ifdef _DEBUG
void CCS580HWView::AssertValid() const
{
CView::AssertValid();
}
void CCS580HWView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCS580HWDoc* CCS580HWView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCS580HWDoc)));
return (CCS580HWDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView message handlers
void CCS580HWView::OnRender()
{
if (threadrun)
{
if (threadpause)
{
AfxMessageBox(_T("Paused animation procedure occupies render\n"));
}
return;
}
// Call renderer
// Application
Application5::Render();
memcpy(screen, m_pFrameBuffer, _msize(screen));
// Set window size
CRect clientRect, windowRect;
int x_offset, y_offset;
GetClientRect(&clientRect);
AfxGetMainWnd()->GetWindowRect(&windowRect);
x_offset = windowRect.Width() - clientRect.Width();
y_offset = windowRect.Height() - clientRect.Height();
AfxGetMainWnd()->SetWindowPos(NULL,
windowRect.left,
windowRect.top,
x_offset + m_nWidth,
y_offset + m_nHeight,
NULL/*,SWP_SHOWWINDOW*/);
Invalidate(true);
}
// Callback function for rotation
void CCS580HWView::OnRotate()
{
CRotateDlg dlg;
GzInput* input;
GzMatrix rotMat =
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
input = m_pUserInput;
if (input == NULL) return;
// Initialize
input->rotation[0] = input->rotation[1] = input->rotation[2] = 0;
dlg.Initialize(input->rotation[0], input->rotation[1], input->rotation[2]);
if (dlg.DoModal() == IDOK)
{
// Update input rotation value
input->rotation[dlg.m_nAxis] = dlg.m_fRot;
// Create Rotation Matrix
switch (dlg.m_nAxis)
{
case 0:
// Create matrix for Rot X
GzRotXMat(input->rotation[0], rotMat);
break;
case 1:
// Create matrix for Rot Y
GzRotYMat(input->rotation[1], rotMat);
break;
case 2:
// Create matrix for Rot Z
GzRotZMat(input->rotation[2], rotMat);
break;
}
// Accumulate matrix
GzPushMatrix(m_pRender, rotMat);
}
}
// Callback function for Translation
void CCS580HWView::OnTranslate()
{
CTranslateDlg dlg;
GzInput* input;
GzMatrix trxMat =
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
input = m_pUserInput;
if (input == NULL) return;
// Initialize
input->translation[0] = input->translation[1] = input->translation[2] = 0;
dlg.Initialize(input->translation[0], input->translation[1], input->translation[2]);
if (dlg.DoModal() == IDOK)
{
// Update input translation value
input->translation[0] = dlg.m_fTx; input->translation[1] = dlg.m_fTy; input->translation[2] = dlg.m_fTz;
// Create Translation Matrix
GzTrxMat(input->translation, trxMat);
// Accumulate matrix
GzPushMatrix(m_pRender, trxMat);
}
}
// Callback function for Scaling
void CCS580HWView::OnScale()
{
CScaleDlg dlg;
GzInput* input;
GzMatrix scaleMat =
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
input = m_pUserInput;
if (input == NULL) return;
// Initialize
input->scale[0] = input->scale[1] = input->scale[2] = 1;
dlg.Initialize(input->scale[0], input->scale[1], input->scale[2]);
if (dlg.DoModal() == IDOK)
{
// Update input scale value
input->scale[0] = dlg.m_fSx; input->scale[1] = dlg.m_fSy; input->scale[2] = dlg.m_fSz;
// Create Scaling Matrix
GzScaleMat(input->scale, scaleMat);
// Accumulate matrix
GzPushMatrix(m_pRender, scaleMat);
}
}
void CCS580HWView::OnEditPopmatrix()
{
if (GzPopMatrix(m_pRender) != GZ_SUCCESS)
{
AfxMessageBox(_T("No matrix in stack\n"));
}
}
void CCS580HWView::OnEditAdd()
{
static bool sranded = false;
if (!sranded)
{
srand((unsigned int)time(NULL));
sranded = true;
}
const float d2pi = 0.01745329252f;
float c1 = (rand() & 0xFF) / (float)0xFF;
float angle1 = (rand() % 360) * d2pi;
float c2 = (rand() & 0xFF) / (float)0xFF;
float angle2 = (rand() % 180) * d2pi;
float c3 = (rand() & 0xFF) / (float)0xFF;
GzLight light =
{
{ sin(angle2) * cos(angle1), sin(angle2) * sin(angle1), cos(angle2) }, { c1, c2, c3 }
};
if (GzPushLight(m_pRender, light) != GZ_SUCCESS)
{
AfxMessageBox(_T("Too many lights\n"));
}
}
void CCS580HWView::OnEditDeletelight()
{
if (GzPopLight(m_pRender) != GZ_SUCCESS)
{
AfxMessageBox(_T("No light in environment\n"));
}
}
void CCS580HWView::OnMeshTexture()
{
static bool tex_off = (m_pRender->tex_fun == NULL);
if (tex_off)
{
m_pRender->tex_fun = m_pRender->tex_bak;
}
else
{
m_pRender->tex_bak = m_pRender->tex_fun;
m_pRender->tex_fun = NULL;
}
tex_off = !tex_off;
}
void CCS580HWView::OnMeshSwitcheffect()
{
static bool npr_off = (m_pRender->npr_fun == NULL);
if (npr_off)
{
m_pRender->npr_fun = m_pRender->npr_bak;
}
else
{
m_pRender->npr_bak = m_pRender->npr_fun;
m_pRender->npr_fun = NULL;
}
npr_off = !npr_off;
}
DWORD WINAPI ThreadProc1(CCS580HWView *This)
{
static int direction = 0;
GzMatrix rotMat =
{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
};
direction = (direction + 1) % 3;
for (int i = 1; i <= 90; i++)
{
while (This->threadpause) Sleep(40);
switch (direction)
{
case 0:
{
GzRotXMat((float)(i << 2), rotMat);
break;
}
case 1:
{
GzRotYMat((float)(i << 2), rotMat);
break;
}
default:
{
GzRotZMat((float)(i << 2), rotMat);
break;
}
}
GzPushMatrix(This->m_pRender, rotMat);
ReleaseSemaphore(This->req_render, 1, NULL);
WaitForSingleObject(This->allow_render, INFINITE);
This->fastRender();
WaitForSingleObject(This->req_draw, INFINITE);
ReleaseSemaphore(This->allow_draw, 1, NULL);
GzPopMatrix(This->m_pRender);
}
This->threadrun = false;
return 0;
}
DWORD WINAPI ThreadProc2(CCS580HWView *This)
{
while (This->threadrun)
{
ReleaseSemaphore(This->req_draw, 1, NULL);
WaitForSingleObject(This->allow_draw, INFINITE);
memcpy(This->screen, This->m_pFrameBuffer, _msize(This->screen));
WaitForSingleObject(This->req_render, INFINITE);
ReleaseSemaphore(This->allow_render, 1, NULL);
This->Invalidate(true);
Sleep(40);
}
return 0;
}
void CCS580HWView::OnAnimation()
{
if (threadrun)
{
threadpause = !threadpause;
return;
}
threadrun = true;
threadpause = false;
// Set window size
CRect clientRect, windowRect;
int x_offset, y_offset;
GetClientRect(&clientRect);
AfxGetMainWnd()->GetWindowRect(&windowRect);
x_offset = windowRect.Width() - clientRect.Width();
y_offset = windowRect.Height() - clientRect.Height();
AfxGetMainWnd()->SetWindowPos(NULL,
windowRect.left,
windowRect.top,
x_offset + m_nWidth,
y_offset + m_nHeight,
NULL/*,SWP_SHOWWINDOW*/);
CloseHandle(CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)ThreadProc1,
this, 0, NULL));
CloseHandle(CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)ThreadProc2,
this, 0, NULL));
}
void CCS580HWView::OnEditMeshdiv()
{
MeshCalculation(true);
}
/* Cancel flicker */
BOOL CCS580HWView::OnEraseBkgnd(CDC* pDC)
{
return true;
}
void CCS580HWView::OnDemoCar()
{
GzMatrix matrix;
m_pRender->matlevel = 4;
MeshFileInput(wstring(_T("./objects/car.obj")));
GzCoord cache0 = { 0.4f, 0.4f, 0.4f };
GzScaleMat(cache0, matrix);
GzPushMatrix(m_pRender, matrix);
GzRotZMat(90.f, matrix);
GzPushMatrix(m_pRender, matrix);
OnRender();
}
void CCS580HWView::OnDemoF()
{
GzMatrix matrix;
m_pRender->matlevel = 4;
MeshFileInput(wstring(_T("./objects/f-16.obj")));
GzCoord cache0 = { 4.f, 0.f, 6.f };
GzTrxMat(cache0, matrix);
GzPushMatrix(m_pRender, matrix);
GzRotZMat(90.f, matrix);
GzPushMatrix(m_pRender, matrix);
GzCoord cache1 = { 4.f, -2.f, 0.f };
GzTrxMat(cache1, matrix);
GzPushMatrix(m_pRender, matrix);
GzCoord cache2 = { 1.3f, 1.3f, 1.3f };
GzScaleMat(cache2, matrix);
GzPushMatrix(m_pRender, matrix);
OnRender();
}
void CCS580HWView::OnDemoPpot()
{
GzMatrix matrix;
m_pRender->matlevel = 4;
MeshFileInput(wstring(_T("./objects/ppot.asc")));
GzRotZMat(90.f, matrix);
GzPushMatrix(m_pRender, matrix);
GzCoord cache0 = { 0.f, -4.f, 0.f };
GzTrxMat(cache0, matrix);
GzPushMatrix(m_pRender, matrix);
OnRender();
}
void CCS580HWView::OnDemoTorus()
{
GzMatrix matrix;
m_pRender->matlevel = 4;
MeshFileInput(wstring(_T("./objects/torus.obj")));
GzRotXMat(45.f, matrix);
GzPushMatrix(m_pRender, matrix);
GzCoord cache0 = { 2.f, 2.f, 2.f };
GzScaleMat(cache0, matrix);
GzPushMatrix(m_pRender, matrix);
GzCoord cache1 = { 0.f, 0.f, 1.f };
GzTrxMat(cache1, matrix);
GzPushMatrix(m_pRender, matrix);
OnRender();
}
void CCS580HWView::OnDemoTriceratops()
{
GzMatrix matrix;
m_pRender->matlevel = 4;
MeshFileInput(wstring(_T("./objects/triceratops.obj")));
GzCoord cache0 = { 4.f, 2.f, 4.f };
GzTrxMat(cache0, matrix);
GzPushMatrix(m_pRender, matrix);
GzRotZMat(90.f, matrix);
GzPushMatrix(m_pRender, matrix);
GzRotYMat(90.f, matrix);
GzPushMatrix(m_pRender, matrix);
OnRender();
}