| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #include "stdafx.h" |
| 13 | #include "cube.h" |
| 14 | |
| 15 | #include "cubedoc.h" |
| 16 | #include "cubeview.h" |
| 17 | |
| 18 | #include "gl/gl.h" |
| 19 | #include "gl/glu.h" |
| 20 | |
| 21 | #ifdef _DEBUG |
| 22 | #undef THIS_FILE |
| 23 | static char BASED_CODE THIS_FILE[] = __FILE__; |
| 24 | #endif |
| 25 | |
| 26 | unsigned char threeto8[8] = |
| 27 | { |
| 28 | 0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377 |
| 29 | }; |
| 30 | |
| 31 | unsigned char twoto8[4] = |
| 32 | { |
| 33 | 0, 0x55, 0xaa, 0xff |
| 34 | }; |
| 35 | |
| 36 | unsigned char oneto8[2] = |
| 37 | { |
| 38 | 0, 255 |
| 39 | }; |
| 40 | |
| 41 | static int defaultOverride[13] = |
| 42 | { |
| 43 | 0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91 |
| 44 | }; |
| 45 | |
| 46 | static PALETTEENTRY defaultPalEntry[20] = |
| 47 | { |
| 48 | { 0, 0, 0, 0 }, |
| 49 | { 0x80,0, 0, 0 }, |
| 50 | { 0, 0x80,0, 0 }, |
| 51 | { 0x80,0x80,0, 0 }, |
| 52 | { 0, 0, 0x80, 0 }, |
| 53 | { 0x80,0, 0x80, 0 }, |
| 54 | { 0, 0x80,0x80, 0 }, |
| 55 | { 0xC0,0xC0,0xC0, 0 }, |
| 56 | |
| 57 | { 192, 220, 192, 0 }, |
| 58 | { 166, 202, 240, 0 }, |
| 59 | { 255, 251, 240, 0 }, |
| 60 | { 160, 160, 164, 0 }, |
| 61 | |
| 62 | { 0x80,0x80,0x80, 0 }, |
| 63 | { 0xFF,0, 0, 0 }, |
| 64 | { 0, 0xFF,0, 0 }, |
| 65 | { 0xFF,0xFF,0, 0 }, |
| 66 | { 0, 0, 0xFF, 0 }, |
| 67 | { 0xFF,0, 0xFF, 0 }, |
| 68 | { 0, 0xFF,0xFF, 0 }, |
| 69 | { 0xFF,0xFF,0xFF, 0 } |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
Top |
1 | | 75 | IMPLEMENT_DYNCREATE(CCubeView, CView) |
1 | | 75 | return new CCubeView |
| 75 | } |
Top |
5576 | | 75 | FUNCTION CCubeView::_GetBaseClass() |
5576 | | 75 | return ( CView::GetThisClass ( ) ) |
| 75 | } |
Top |
1 | | 75 | FUNCTION CCubeView::GetThisClass() |
1 | | 75 | return ( ( CRuntimeClass * ) ( & CCubeView::classCCubeView ) ) |
| 75 | } |
Top |
13732 | | 75 | FUNCTION CCubeView::GetRuntimeClass() |
13732 | | 75 | return ( ( CRuntimeClass * ) ( & CCubeView::classCCubeView ) ) |
| 75 | } |
| 76 | |
Top |
2696 | | 77 | BEGIN_MESSAGE_MAP(CCubeView, CView) |
2696 | | 77 | return GetThisMessageMap ( ) |
| 77 | } |
Top |
2696 | | 77 | FUNCTION CCubeView::GetThisMessageMap() |
| 78 | |
| 79 | ON_COMMAND(ID_FILE_PLAY, OnFilePlay) |
| 80 | ON_UPDATE_COMMAND_UI(ID_FILE_PLAY, OnUpdateFilePlay) |
| 81 | ON_WM_CREATE() |
| 82 | ON_WM_DESTROY() |
| 83 | ON_WM_SIZE() |
| 84 | ON_WM_TIMER() |
| 85 | ON_WM_ERASEBKGND() |
| 86 | |
2696 | | 87 | END_MESSAGE_MAP() |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
Top |
1 | | 92 | CCubeView::CCubeView() |
| 93 | { |
| 94 | m_pDC = NULL; |
| 95 | m_pOldPalette = NULL; |
| 96 | m_play = FALSE; |
1 | | 97 | } |
| 98 | |
Top |
1 | | 99 | CCubeView::~CCubeView() |
| 100 | { |
1 | | 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
Top |
1 | | 106 | void CCubeView::OnDraw(CDC* ) |
| 107 | { |
| 108 | CCubeDoc* pDoc = GetDocument(); |
| 109 | ASSERT_VALID(pDoc); |
| 110 | |
| 111 | DrawScene(); |
1 | | 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | #ifdef _DEBUG |
Top |
7756 | | 118 | void CCubeView::AssertValid() const |
| 119 | { |
| 120 | CView::AssertValid(); |
7756 | | 121 | } |
| 122 | |
Top |
0 | | 123 | void CCubeView::Dump(CDumpContext& dc) const |
| 124 | { |
| 125 | CView::Dump(dc); |
0 | | 126 | } |
| 127 | |
Top |
1 | | 128 | CCubeDoc* CCubeView::GetDocument() |
| 129 | { |
1 | | 130 | return STATIC_DOWNCAST(CCubeDoc, m_pDocument); |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
Top |
2 | | 137 | void CCubeView::OnFilePlay() |
| 138 | { |
1 | 1 | 139 | m_play = m_play ? FALSE : TRUE; |
1 | 1 | 140 | if (m_play) |
| 141 | SetTimer(1, 15, NULL); |
| 142 | else |
| 143 | KillTimer(1); |
2 | | 144 | } |
| 145 | |
Top |
399 | | 146 | void CCubeView::OnUpdateFilePlay(CCmdUI* pCmdUI) |
| 147 | { |
| 148 | pCmdUI->SetCheck(m_play); |
399 | | 149 | } |
| 150 | |
Top |
1 | | 151 | BOOL CCubeView::PreCreateWindow(CREATESTRUCT& cs) |
| 152 | { |
| 153 | |
| 154 | |
| 155 | |
| 156 | cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; |
| 157 | |
1 | | 158 | return CView::PreCreateWindow(cs); |
| 159 | } |
| 160 | |
Top |
1 | | 161 | int CCubeView::OnCreate(LPCREATESTRUCT lpCreateStruct) |
| 162 | { |
0 | 1 | 163 | if (CView::OnCreate(lpCreateStruct) == -1) |
0 | | 164 | return -1; |
| 165 | |
| 166 | Init(); |
| 167 | |
1 | | 168 | return 0; |
| 169 | } |
| 170 | |
Top |
1 | | 171 | void CCubeView::OnDestroy() |
| 172 | { |
| 173 | HGLRC hrc; |
| 174 | |
| 175 | KillTimer(1); |
| 176 | |
| 177 | hrc = ::wglGetCurrentContext(); |
| 178 | |
| 179 | ::wglMakeCurrent(NULL, NULL); |
| 180 | |
1 | 0 | 181 | if (hrc) |
| 182 | ::wglDeleteContext(hrc); |
| 183 | |
0 | 1 | 184 | if (m_pOldPalette) |
| 185 | m_pDC->SelectPalette(m_pOldPalette, FALSE); |
| 186 | |
1 | 0 | 187 | if (m_pDC) |
| 188 | delete m_pDC; |
| 189 | |
| 190 | CView::OnDestroy(); |
1 | | 191 | } |
| 192 | |
Top |
3 | | 193 | void CCubeView::OnSize(UINT nType, int cx, int cy) |
| 194 | { |
| 195 | CView::OnSize(nType, cx, cy); |
| 196 | |
2 | 1 | 197 | if(cy > 0) |
| 198 | { |
| 199 | glViewport(0, 0, cx, cy); |
| 200 | |
1 | 1 | 201 | if((m_oldRect.right > cx) || (m_oldRect.bottom > cy)) |
0 | | 201 | 1: (T) || (_) |
1 | | 201 | 2: (F) || (T) |
| 1 | 201 | 3: (F) || (F) |
- | 201 | MC/DC (cond 1): 1 - 3 |
+ | 201 | MC/DC (cond 2): 2 + 3 |
| 202 | RedrawWindow(); |
| 203 | |
| 204 | m_oldRect.right = cx; |
| 205 | m_oldRect.bottom = cy; |
| 206 | |
| 207 | glMatrixMode(GL_PROJECTION); |
| 208 | glLoadIdentity(); |
| 209 | gluPerspective(45.0f, (GLdouble)cx/cy, 3.0f, 7.0f); |
| 210 | glMatrixMode(GL_MODELVIEW); |
| 211 | } |
3 | | 212 | } |
| 213 | |
Top |
84 | | 214 | void CCubeView::OnTimer(UINT_PTR nIDEvent) |
| 215 | { |
| 216 | DrawScene(); |
| 217 | |
| 218 | CView::OnTimer(nIDEvent); |
| 219 | |
| 220 | |
| 221 | MSG msg; |
0 | 84 | 222 | while(::PeekMessage(&msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE)); |
84 | | 223 | } |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
Top |
1 | | 228 | void CCubeView::Init() |
| 229 | { |
| 230 | PIXELFORMATDESCRIPTOR pfd; |
| 231 | int n; |
| 232 | HGLRC hrc; |
| 233 | GLfloat fMaxObjSize, fAspect; |
| 234 | GLfloat fNearPlane, fFarPlane; |
| 235 | |
| 236 | m_pDC = new CClientDC(this); |
| 237 | |
| 238 | ASSERT(m_pDC != NULL); |
| 239 | |
0 | 1 | 240 | if (!bSetupPixelFormat()) |
0 | | 241 | return; |
| 242 | |
| 243 | n = ::GetPixelFormat(m_pDC->GetSafeHdc()); |
| 244 | ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); |
| 245 | |
| 246 | CreateRGBPalette(); |
| 247 | |
| 248 | hrc = wglCreateContext(m_pDC->GetSafeHdc()); |
| 249 | wglMakeCurrent(m_pDC->GetSafeHdc(), hrc); |
| 250 | |
| 251 | GetClientRect(&m_oldRect); |
| 252 | glClearDepth(1.0f); |
| 253 | glEnable(GL_DEPTH_TEST); |
| 254 | |
0 | 1 | 255 | if (m_oldRect.bottom) |
| 256 | fAspect = (GLfloat)m_oldRect.right/m_oldRect.bottom; |
| 257 | else |
| 258 | fAspect = 1.0f; |
| 259 | fNearPlane = 3.0f; |
| 260 | fFarPlane = 7.0f; |
| 261 | fMaxObjSize = 3.0f; |
| 262 | m_fRadius = fNearPlane + fMaxObjSize / 2.0f; |
| 263 | |
| 264 | glMatrixMode(GL_PROJECTION); |
| 265 | glLoadIdentity(); |
| 266 | gluPerspective(45.0f, fAspect, fNearPlane, fFarPlane); |
| 267 | glMatrixMode(GL_MODELVIEW); |
1 | | 268 | } |
| 269 | |
Top |
1 | | 270 | BOOL CCubeView::bSetupPixelFormat() |
| 271 | { |
| 272 | static PIXELFORMATDESCRIPTOR pfd = |
| 273 | { |
| 274 | sizeof(PIXELFORMATDESCRIPTOR), |
| 275 | 1, |
| 276 | PFD_DRAW_TO_WINDOW | |
| 277 | PFD_SUPPORT_OPENGL | |
| 278 | PFD_DOUBLEBUFFER, |
| 279 | PFD_TYPE_RGBA, |
| 280 | 24, |
| 281 | 0, 0, 0, 0, 0, 0, |
| 282 | 0, |
| 283 | 0, |
| 284 | 0, |
| 285 | 0, 0, 0, 0, |
| 286 | 32, |
| 287 | 0, |
| 288 | 0, |
| 289 | PFD_MAIN_PLANE, |
| 290 | 0, |
| 291 | 0, 0, 0 |
| 292 | }; |
| 293 | int pixelformat; |
| 294 | |
0 | 1 | 295 | if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 ) |
| 296 | { |
| 297 | MessageBox("ChoosePixelFormat failed"); |
0 | | 298 | return FALSE; |
| 299 | } |
| 300 | |
0 | 1 | 301 | if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE) |
| 302 | { |
| 303 | MessageBox("SetPixelFormat failed"); |
0 | | 304 | return FALSE; |
| 305 | } |
| 306 | |
1 | | 307 | return TRUE; |
| 308 | } |
| 309 | |
Top |
0 | | 310 | unsigned char CCubeView::ComponentFromIndex(int i, UINT nbits, UINT shift) |
| 311 | { |
| 312 | unsigned char val; |
| 313 | |
| 314 | val = (unsigned char) (i >> shift); |
| 315 | switch (nbits) |
| 316 | { |
| 317 | |
0 | | 318 | case 1: |
| 319 | val &= 0x1; |
0 | | 320 | return oneto8[val]; |
0 | | 321 | case 2: |
| 322 | val &= 0x3; |
0 | | 323 | return twoto8[val]; |
0 | | 324 | case 3: |
| 325 | val &= 0x7; |
0 | | 326 | return threeto8[val]; |
| 327 | |
0 | | 328 | default: |
0 | | 329 | return 0; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | #pragma warning(disable : 4244) |
Top |
1 | | 334 | void CCubeView::CreateRGBPalette() |
| 335 | { |
| 336 | PIXELFORMATDESCRIPTOR pfd; |
| 337 | LOGPALETTE *pPal; |
| 338 | int n, i; |
| 339 | |
| 340 | n = ::GetPixelFormat(m_pDC->GetSafeHdc()); |
| 341 | ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); |
| 342 | |
0 | 1 | 343 | if (pfd.dwFlags & PFD_NEED_PALETTE) |
| 344 | { |
| 345 | n = 1 << pfd.cColorBits; |
| 346 | pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)]; |
| 347 | |
| 348 | ASSERT(pPal != NULL); |
| 349 | |
| 350 | pPal->palVersion = 0x300; |
| 351 | pPal->palNumEntries = n; |
0 | 0 | 352 | for (i=0; i<n; i++) |
| 353 | { |
| 354 | pPal->palPalEntry[i].peRed = |
| 355 | ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift); |
| 356 | pPal->palPalEntry[i].peGreen = |
| 357 | ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift); |
| 358 | pPal->palPalEntry[i].peBlue = |
| 359 | ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift); |
| 360 | pPal->palPalEntry[i].peFlags = 0; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | if ((pfd.cColorBits == 8) && |
| 365 | (pfd.cRedBits == 3) && (pfd.cRedShift == 0) && |
| 366 | (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) && |
0 | 0 | 367 | (pfd.cBlueBits == 2) && (pfd.cBlueShift == 6) |
0 | | 367 | 1: (T) && (T) && (T) && (T) && (T) && (T) && (T) |
| 0 | 367 | 2: (T) && (T) && (T) && (T) && (T) && (T) && (F) |
| 0 | 367 | 3: (T) && (T) && (T) && (T) && (T) && (F) && (_) |
| 0 | 367 | 4: (T) && (T) && (T) && (T) && (F) && (_) && (_) |
| 0 | 367 | 5: (T) && (T) && (T) && (F) && (_) && (_) && (_) |
| 0 | 367 | 6: (T) && (T) && (F) && (_) && (_) && (_) && (_) |
| 0 | 367 | 7: (T) && (F) && (_) && (_) && (_) && (_) && (_) |
| 0 | 367 | 8: (F) && (_) && (_) && (_) && (_) && (_) && (_) |
- | 367 | MC/DC (cond 1): 1 - 8 |
- | 367 | MC/DC (cond 2): 1 - 7 |
- | 367 | MC/DC (cond 3): 1 - 6 |
- | 367 | MC/DC (cond 4): 1 - 5 |
- | 367 | MC/DC (cond 5): 1 - 4 |
- | 367 | MC/DC (cond 6): 1 - 3 |
- | 367 | MC/DC (cond 7): 1 - 2 |
| 368 | ) |
| 369 | { |
0 | 0 | 370 | for (i = 1 ; i <= 12 ; i++) |
| 371 | pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i]; |
| 372 | } |
| 373 | |
| 374 | m_cPalette.CreatePalette(pPal); |
| 375 | delete [] pPal; |
| 376 | |
| 377 | m_pOldPalette = m_pDC->SelectPalette(&m_cPalette, FALSE); |
| 378 | m_pDC->RealizePalette(); |
| 379 | } |
1 | | 380 | } |
| 381 | #pragma warning(default : 4244) |
| 382 | |
Top |
85 | | 383 | void CCubeView::DrawScene(void) |
| 384 | { |
| 385 | static BOOL bBusy = FALSE; |
| 386 | static GLfloat wAngleY = 10.0f; |
| 387 | static GLfloat wAngleX = 1.0f; |
| 388 | static GLfloat wAngleZ = 5.0f; |
| 389 | |
0 | 85 | 390 | if(bBusy) |
0 | | 391 | return; |
| 392 | bBusy = TRUE; |
| 393 | |
| 394 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 395 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 396 | |
| 397 | glPushMatrix(); |
| 398 | |
| 399 | glTranslatef(0.0f, 0.0f, -m_fRadius); |
| 400 | glRotatef(wAngleX, 1.0f, 0.0f, 0.0f); |
| 401 | glRotatef(wAngleY, 0.0f, 1.0f, 0.0f); |
| 402 | glRotatef(wAngleZ, 0.0f, 0.0f, 1.0f); |
| 403 | |
| 404 | wAngleX += 1.0f; |
| 405 | wAngleY += 10.0f; |
| 406 | wAngleZ += 5.0f; |
| 407 | |
| 408 | |
| 409 | glBegin(GL_QUAD_STRIP); |
| 410 | glColor3f(1.0f, 0.0f, 1.0f); |
| 411 | glVertex3f(-0.5f, 0.5f, 0.5f); |
| 412 | |
| 413 | glColor3f(1.0f, 0.0f, 0.0f); |
| 414 | glVertex3f(-0.5f, -0.5f, 0.5f); |
| 415 | |
| 416 | glColor3f(1.0f, 1.0f, 1.0f); |
| 417 | glVertex3f(0.5f, 0.5f, 0.5f); |
| 418 | |
| 419 | glColor3f(1.0f, 1.0f, 0.0f); |
| 420 | glVertex3f(0.5f, -0.5f, 0.5f); |
| 421 | |
| 422 | glColor3f(0.0f, 1.0f, 1.0f); |
| 423 | glVertex3f(0.5f, 0.5f, -0.5f); |
| 424 | |
| 425 | glColor3f(0.0f, 1.0f, 0.0f); |
| 426 | glVertex3f(0.5f, -0.5f, -0.5f); |
| 427 | |
| 428 | glColor3f(0.0f, 0.0f, 1.0f); |
| 429 | glVertex3f(-0.5f, 0.5f, -0.5f); |
| 430 | |
| 431 | glColor3f(0.0f, 0.0f, 0.0f); |
| 432 | glVertex3f(-0.5f, -0.5f, -0.5f); |
| 433 | |
| 434 | glColor3f(1.0f, 0.0f, 1.0f); |
| 435 | glVertex3f(-0.5f, 0.5f, 0.5f); |
| 436 | |
| 437 | glColor3f(1.0f, 0.0f, 0.0f); |
| 438 | glVertex3f(-0.5f, -0.5f, 0.5f); |
| 439 | |
| 440 | glEnd(); |
| 441 | |
| 442 | glBegin(GL_QUADS); |
| 443 | glColor3f(1.0f, 0.0f, 1.0f); |
| 444 | glVertex3f(-0.5f, 0.5f, 0.5f); |
| 445 | |
| 446 | glColor3f(1.0f, 1.0f, 1.0f); |
| 447 | glVertex3f(0.5f, 0.5f, 0.5f); |
| 448 | |
| 449 | glColor3f(0.0f, 1.0f, 1.0f); |
| 450 | glVertex3f(0.5f, 0.5f, -0.5f); |
| 451 | |
| 452 | glColor3f(0.0f, 0.0f, 1.0f); |
| 453 | glVertex3f(-0.5f, 0.5f, -0.5f); |
| 454 | glEnd(); |
| 455 | |
| 456 | glBegin(GL_QUADS); |
| 457 | glColor3f(1.0f, 0.0f, 0.0f); |
| 458 | glVertex3f(-0.5f, -0.5f, 0.5f); |
| 459 | |
| 460 | glColor3f(1.0f, 1.0f, 0.0f); |
| 461 | glVertex3f(0.5f, -0.5f, 0.5f); |
| 462 | |
| 463 | glColor3f(0.0f, 1.0f, 0.0f); |
| 464 | glVertex3f(0.5f, -0.5f, -0.5f); |
| 465 | |
| 466 | glColor3f(0.0f, 0.0f, 0.0f); |
| 467 | glVertex3f(-0.5f, -0.5f, -0.5f); |
| 468 | glEnd(); |
| 469 | |
| 470 | glPopMatrix(); |
| 471 | |
| 472 | glFinish(); |
| 473 | SwapBuffers(wglGetCurrentDC()); |
| 474 | |
| 475 | bBusy = FALSE; |
85 | | 476 | } |
| 477 | |
Top |
1 | | 478 | BOOL CCubeView::OnEraseBkgnd(CDC* ) |
| 479 | { |
1 | | 480 | return TRUE; |
| 481 | } |