-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS580HWView.cpp
309 lines (246 loc) · 7.52 KB
/
CS580HWView.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
// 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 "rend.h"
#include "Application5.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView
IMPLEMENT_DYNCREATE(CCS580HWView, CView)
BEGIN_MESSAGE_MAP(CCS580HWView, CView)
//{{AFX_MSG_MAP(CCS580HWView)
ON_COMMAND(IDM_RENDER, OnRender)
ON_COMMAND(IDM_ROTATE, OnRotate)
ON_COMMAND(IDM_TRANSLATE, OnTranslate)
ON_COMMAND(IDM_SCALE, OnScale)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCS580HWView construction/destruction
CCS580HWView::CCS580HWView()
{
// TODO: add construction code here
m_pApplication = NULL;
}
CCS580HWView::~CCS580HWView()
{
if(m_pApplication != NULL)
{
delete m_pApplication;
}
}
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
if(m_pApplication != NULL)
DrawFrameBuffer(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// 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()
{
// TODO: Add your command handler code here
// Call renderer
// Application 5
if(m_pApplication != NULL)
((Application5 *)m_pApplication)->Render();
else
AfxMessageBox("Application was not allocated\n");
// 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, 0, 0, x_offset+m_pApplication->m_nWidth, y_offset+m_pApplication->m_nHeight, NULL/*,SWP_SHOWWINDOW*/);
Invalidate(true);
}
void CCS580HWView::DrawFrameBuffer(CDC *pDC)
{
if(m_pApplication->m_pFrameBuffer == NULL)
{
return;
}
HDC hdc;
hdc = ::CreateCompatibleDC(pDC->m_hDC);
HBITMAP m_bitmap;
// Display the current image
char buffer[sizeof(BITMAPINFO)];
BITMAPINFO* binfo = (BITMAPINFO*)buffer;
memset(binfo, 0, sizeof(BITMAPINFOHEADER));
binfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
// Create the bitmap
BITMAPINFOHEADER* bih = &binfo->bmiHeader;
bih->biBitCount = 3*8;
bih->biWidth = m_pApplication->m_nWidth;
bih->biHeight = m_pApplication->m_nHeight;
bih->biPlanes = 1;
bih->biCompression = BI_RGB;
bih->biSizeImage = 0;
m_bitmap = CreateDIBSection(hdc, binfo, 0, 0, 0, DIB_RGB_COLORS);
int colors = DIB_RGB_COLORS;
::SelectObject(hdc, m_bitmap);
binfo->bmiHeader.biBitCount = 0;
GetDIBits(hdc, m_bitmap, 0, 0, 0, binfo, colors);
binfo->bmiHeader.biBitCount = 24;
binfo->bmiHeader.biHeight = -abs(binfo->bmiHeader.biHeight);
SetDIBits(hdc, m_bitmap, 0, m_pApplication->m_nHeight, m_pApplication->m_pFrameBuffer, binfo, colors);
::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
CRect client;
GetClientRect(&client);
::BitBlt(pDC->m_hDC, 0, 0, m_pApplication->m_nWidth, m_pApplication->m_nHeight,
hdc, 0, 0, SRCCOPY);
::DeleteDC(hdc);
DeleteObject(m_bitmap);
}
void CCS580HWView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
// Assign Application 5
if(m_pApplication == NULL)
{
m_pApplication = new Application5;
}
// Initialize and begin renderer
((Application5 *)m_pApplication)->Initialize();
}
// Callback function for rotation
void CCS580HWView::OnRotate()
{
// TODO: Add your command handler code here
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
};
if(m_pApplication == NULL) return;
input = m_pApplication->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
m_pApplication->m_pRender->GzRotXMat(input->rotation[0], rotMat);
break;
case 1:
// Create matrix for Rot Y
m_pApplication->m_pRender->GzRotYMat(input->rotation[1], rotMat);
break;
case 2:
// Create matrix for Rot Z
m_pApplication->m_pRender->GzRotZMat(input->rotation[2], rotMat);
break;
}
// Accumulate matrix
m_pApplication->m_pRender->GzPushMatrix(rotMat);
}
}
// Callback function for Translation
void CCS580HWView::OnTranslate()
{
// TODO: Add your command handler code here
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
};
if(m_pApplication == NULL) return;
input = m_pApplication->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
m_pApplication->m_pRender->GzTrxMat(input->translation, trxMat);
// Accumulate matrix
m_pApplication->m_pRender->GzPushMatrix(trxMat);
}
}
// Callback function for Scaling
void CCS580HWView::OnScale()
{
// TODO: Add your command handler code here
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
};
if(m_pApplication == NULL) return;
input = m_pApplication->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
m_pApplication->m_pRender->GzScaleMat(input->scale, scaleMat);
// Accumulate matrix
m_pApplication->m_pRender->GzPushMatrix(scaleMat);
}
}