-
Notifications
You must be signed in to change notification settings - Fork 0
/
AP210ViewerView.cpp
1743 lines (1500 loc) · 48.1 KB
/
AP210ViewerView.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// AP210ViewerView.cpp : implementation of the CAP210ViewerView class
//
#include "stdafx.h"
#include "AP210Viewer.h"
#include "AP210ViewerDoc.h"
#include "AP210ViewerView.h"
#include "Sdai/NonPlatedThruHole.h"
#include "Sdai/PackagedComponent.h"
#include "Sdai/MountingRestrictionArea.h"
#include "OpenGL/MatrixUtilities.h"
// GUI
#include "ComponentPlacementRestriction.h"
#include "InterconnectModulePropertiesSheet.h"
#include "ComponentPropertiesSheet.h"
#include "ComponentDefinitionSheet.h"
#include "NonPlatedThruHoleSheet.h"
#include "ComponentPlacementRestrictionAreaSheet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// selection stuff
#define MAXSELECT 100000
GLuint selectBuf[MAXSELECT];
// Notes:
//
// Since the polygons for the components aren't rendered with regard to
// orientation (i.e. clockwise or counterclockwise) culling of polygons
// by front or back facing must not be done.
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView
IMPLEMENT_DYNCREATE(CAP210ViewerView, CView)
BEGIN_MESSAGE_MAP(CAP210ViewerView, CView)
//{{AFX_MSG_MAP(CAP210ViewerView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_CHAR()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_LBUTTONDBLCLK()
ON_WM_MOUSEWHEEL()
ON_WM_RBUTTONDBLCLK()
ON_COMMAND(ID_COMPONENT_RESTRICTION_AREA, OnComponentRestrictionArea)
ON_COMMAND(ID_PRIMARY_SURFACE, OnPrimarySurface)
ON_COMMAND(ID_SECONDARY_SURFACE, OnSecondarySurface)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView construction/destruction
CAP210ViewerView::CAP210ViewerView() {
TRACE("IN CAP210ViewerView::CAP210ViewerView()\r\n");
// TODO: add construction code here
// Palette Initialization
m_pPal = NULL ;
// Centering Initialization
DeltaX = 0.0;
DeltaY = 0.0;
// Perspective Initialization
glFovy = 30.0;
glNear = 1.0;
glFar = 100.0;
// start by viewing the primary surface
orientPrimarySurface();
}
CAP210ViewerView::~CAP210ViewerView()
{
// TODO: add destructor code here
if (m_pPal) delete m_pPal ;
}
BOOL CAP210ViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
// MTK: Here is where you should initialize opengl for
// this window.
// OpenGL
TRACE0("CAP210ViewerView::PreCreateWindow\r\n") ;
// OpenGL can't render to child or sibling windows so clip them.
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
//
// OnInitialUpdate
//
void CAP210ViewerView::OnInitialUpdate() {
TRACE0("CAP210ViewerView::OnInitialUpdate\r\n") ;
// TODO: Add your specialized creation code here
#ifdef TRACKBALL
// setup trackball
trackball(curquat, 0.0, 0.0, 0.0, 0.0);
trackball(lastquat, 0.0, 0.0, 0.0, 0.0);
#endif
// Setup Geometry
PrepareScene();
// set up distance from board.
set_eyeZ();
CView::OnInitialUpdate();
}
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView drawing
void CAP210ViewerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) {
CView::OnPrepareDC(pDC, pInfo);
// TODO: Add your specialized code here and/or call the base class
// Make the HGLRC current
// for this to work properly on NT I believe we need to manipulate
// the palette here also see OnDraw().
#if 0
if (pInfo == NULL) {
BOOL bResult = wglMakeCurrent(pDC->m_hDC, m_hrc);
if (!bResult) {
// error created in print-preview.
//wglMakeCurrent Failed in CAP210ViewerView::OnDraw The pixel format is invalid.
#if 1
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
//MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
TRACE("wglMakeCurrent Failed in CAP210ViewerView::OnPrepareDC %s\r\n", lpMsgBuf ) ;
TRACE("pInfo: 0x%08x\n", pInfo);
// Free the buffer.
LocalFree( lpMsgBuf );
#else
TRACE("wglMakeCurrent Failed in CAP210ViewerView::OnPrepareDC %x\r\n", GetLastError() ) ;
#endif
}
}
#endif
}
void CAP210ViewerView::OnDraw(CDC* pDC) {
CAP210ViewerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
#ifdef VERBOSE
TRACE0("CAP210ViewerView::OnDraw\r\n") ;
#endif
// TODO: add draw code for native data here
// Select the palette.
CPalette* ppalOld = NULL;
if (m_pPal) {
ppalOld = pDC->SelectPalette(m_pPal, 0);
pDC->RealizePalette();
}
BOOL bResult = wglMakeCurrent(pDC->m_hDC, m_hrc);
if (!bResult) {
// error created in print-preview.
//wglMakeCurrent Failed in CAP210ViewerView::OnDraw The pixel format is invalid.
#if 1
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
//MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
TRACE("wglMakeCurrent Failed in CAP210ViewerView::OnPrepareDC %s\r\n", lpMsgBuf ) ;
// Free the buffer.
LocalFree( lpMsgBuf );
#else
TRACE("wglMakeCurrent Failed in CAP210ViewerView::OnPrepareDC %x\r\n", GetLastError() ) ;
#endif
}
// Draw Assembly
DrawScene();
//Swap Buffers
SwapBuffers(pDC->m_hDC) ;
// select old palette if we altered it
if (ppalOld) {
pDC->SelectPalette(ppalOld, 0);
}
wglMakeCurrent(NULL, NULL) ;
}
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView printing
// It is possible that all these print functions can be removed.
// currently having a problem with the pixel format of the
// print preview device context.
BOOL CAP210ViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAP210ViewerView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo){
// TODO: add extra initialization before printing
//
// Create a rendering context.
//
p_hrc = wglCreateContext(pDC->m_hDC);
if (!p_hrc) {
TRACE("CAP210ViewerView::OnBeginPrinting wglCreateContext Failed %x\r\n", GetLastError()) ;
}
}
void CAP210ViewerView::OnPrint(CDC* pDC, CPrintInfo* pInfo) {
// TODO: Add your specialized code here and/or call the base class
BOOL bResult = wglMakeCurrent(pDC->m_hDC, m_hrc);
if (!bResult)
{
TRACE("CAP210ViewerView::OnPrint wglMakeCurrent Failed %x\r\n", GetLastError() ) ;
return ;
}
// Call to base class.
CView::OnPrint(pDC, pInfo);
}
void CAP210ViewerView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add cleanup after printing
wglMakeCurrent(NULL,NULL) ;
if (p_hrc)
{
wglDeleteContext(p_hrc) ;
p_hrc = NULL ;
}
}
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView diagnostics
#ifdef _DEBUG
void CAP210ViewerView::AssertValid() const
{
CView::AssertValid();
}
void CAP210ViewerView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CAP210ViewerDoc* CAP210ViewerView::GetDocument() // non-debug version is inline
{
// ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAP210ViewerDoc)));
return (CAP210ViewerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView message handlers
int CAP210ViewerView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
TRACE0("CAP210ViewerView::OnCreate\r\n");
// OpenGL
CClientDC dc(this) ;
//
// Fill in the Pixel Format Descriptor
//
PIXELFORMATDESCRIPTOR pfd ;
memset(&pfd,0, sizeof(PIXELFORMATDESCRIPTOR)) ;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1 ; // Version number
pfd.dwFlags = PFD_DOUBLEBUFFER | // Use double buffer
PFD_SUPPORT_OPENGL | // Use OpenGL
// PFD_DRAW_TO_BITMAP | // from OpenGL printing example.
PFD_DRAW_TO_WINDOW ; // Pixel format is for a window.
pfd.iPixelType = PFD_TYPE_RGBA ;
pfd.cColorBits = 24; // 8-bit color
pfd.cDepthBits = 32 ; // 32-bit depth buffer
pfd.iLayerType = PFD_MAIN_PLANE ; // Layer type
int nPixelFormat = ChoosePixelFormat(dc.m_hDC, &pfd);
if (nPixelFormat == 0)
{
TRACE("ChoosePixelFormat Failed %d\r\n",GetLastError()) ;
return -1 ;
}
TRACE("Pixel Format %d\r\n", nPixelFormat) ;
BOOL bResult = SetPixelFormat(dc.m_hDC, nPixelFormat, &pfd);
if (!bResult)
{
TRACE("SetPixelFormat Failed %d\r\n",GetLastError()) ;
return -1 ;
}
//
// Create a rendering context for OpenGl
//
m_hrc = wglCreateContext(dc.m_hDC);
if (!m_hrc)
{
TRACE("wglCreateContext Failed %x\r\n", GetLastError()) ;
return -1;
}
// Create the palette
TRACE0("About to CreateRGBPalete\r\n");
CreateRGBPalette(dc.m_hDC) ;
TRACE0("Done With CreateRGBPalete\r\n");
#ifdef GL_EXT_polygon_offset
/* check for the polygon offset extension */
if (glutExtensionSupported("GL_EXT_polygon_offset")) {
polygonOffsetVersion = EXTENSION;
glPolygonOffsetEXT(-0.1, -0.002);OutputGlError("glPolygonOffsetEXT(-0.1, -0.002);") ;
} else
{
polygonOffsetVersion = MISSING;
TRACE("\nAP210Viewer: Missing polygon offset.\n");
TRACE(" Expect shadow depth aliasing artifacts.\n\n");
}
#endif
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAP210ViewerView message handlers
/////////////////////////////////////////////////////////////////////////////
// OpenGL Code
//
void CAP210ViewerView::DrawScene() {
// Enable blending for transpancy
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Enable depth calculations
glEnable(GL_DEPTH_TEST); OutputGlError("glEnable (GL_DEPTH_TEST);") ;
// Clear the color and depth buffers.
glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable writing of frame buffer color components
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// Set the material color to follow the current color
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); // The default.
glEnable(GL_COLOR_MATERIAL) ;
// Change to model view matrix stack.
glMatrixMode(GL_MODELVIEW); OutputGlError("MatrixMode");
// get to a known translation state.
glLoadIdentity();
// Translate the scene
TranslateScene();
// clear the color and depth bit buffers.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); OutputGlError("glClear");
// render the scene
RenderScene();
//
// Flush the drawing pipeline.
glFlush();
}
//
// PrepareScene()
//
// Prepare display lists for PWB, top and bottom components
//
void CAP210ViewerView::PrepareScene() {
TRACE0("CAP210ViewerView::PrepareScene()\r\n");
//
// Attach the window dc to OpenGL.
//
CClientDC dc(this) ;
BOOL bResult = wglMakeCurrent(dc.m_hDC, m_hrc);
if (!bResult) {
TRACE("wglMakeCurrent Failed in CAP210ViewerView::PrepareScene %x\r\n", GetLastError() ) ;
}
// Get PWB data
CAP210ViewerDoc *doc = GetDocument();
TRACE("About to init display mode\r\n");
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);OutputGlError("glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);") ;
// Setup Light Model
// LightModel();
// Setup select buffer
TRACE("About to create Select Buffer\r\n");
glSelectBuffer(MAXSELECT, selectBuf);OutputGlError("glSelectBuffer(MAXSELECT, selectBuf);") ;
// this shouldn't be here! MTK 3/19/2001
// Scale Display (Really just centers the display)
ScaleDisplay(); // A Test
// Prepare PWB display list
doc->PWB->Draw(); // Draw Green
// Setup for non plated thru holes
AP210ObjectListItor NPTHitor(&(doc->NonPlatedThruHoles));
// Prepare the non plated thru hole display lists
NonPlatedThruHole *npth;
NPTHitor.Reset();
while((npth = (NonPlatedThruHole *)NPTHitor.NextDirectInstanceOf(NonPlatedThruHole::TypeID())) != NULL) {
npth->Draw();
}
// Setup for components
AP210ObjectListItor Pitor(&(doc->Packages));
// Prepare the package display lists
Package *p;
Pitor.Reset();
while((p = (Package *)Pitor.NextDirectInstanceOf(Package::TypeID())) != NULL) {
p->Draw();
}
// Setup for mechanical components
AP210ObjectListItor CDitor(&(doc->ComponentDefinitions));
ComponentDefinition *cd;
// Prepare the mechanical components display list
CDitor.Reset();
while((cd = (ComponentDefinition *)CDitor.NextDirectInstanceOf(ComponentDefinition::TypeID())) != NULL) {
cd->Draw();
}
// Setup for mounting restriction areas
AP210ObjectListItor MRAitor(&(doc->MountingRestrictionAreas));
MountingRestrictionArea *mra;
// Prepare the mounting restriction areas display list
MRAitor.Reset();
while((mra = (MountingRestrictionArea *)MRAitor.NextDirectInstanceOf(MountingRestrictionArea::TypeID())) != NULL) {
mra->Draw();
}
wglMakeCurrent(NULL, NULL);
}
//
// OutputGlError
//
void CAP210ViewerView::OutputGlError(char* label) {
GLenum errorno = glGetError() ;
if (errorno != GL_NO_ERROR) {
TRACE("CAP210ViewerView: %s had error: #(%d) %s\r\n", label, errorno, gluErrorString(errorno)) ;
#if 0
while(errorno != GL_NO_ERROR) {
errorno = glGetError();
TRACE(" %s had error: #(%d) %s\r\n", label, errorno, gluErrorString(errorno)) ;
}
#endif
}
}
//////////////////////////////////////////////////////////////////////////////////////
//
// OpenGLpalette
//
unsigned char CAP210ViewerView::m_threeto8[8] = {
0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
};
unsigned char CAP210ViewerView::m_twoto8[4] = {
0, 0x55, 0xaa, 0xff
};
unsigned char CAP210ViewerView::m_oneto8[2] = {
0, 255
};
int CAP210ViewerView::m_defaultOverride[13] = {
0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
};
PALETTEENTRY CAP210ViewerView::m_defaultPalEntry[20] = {
{ 0, 0, 0, 0 }, //0
{ 0x80,0, 0, 0 },
{ 0, 0x80,0, 0 },
{ 0x80,0x80,0, 0 },
{ 0, 0, 0x80, 0 },
{ 0x80,0, 0x80, 0 },
{ 0, 0x80,0x80, 0 },
{ 0xC0,0xC0,0xC0, 0 }, //7
{ 192, 220, 192, 0 }, //8
{ 166, 202, 240, 0 },
{ 255, 251, 240, 0 },
{ 160, 160, 164, 0 }, //11
{ 0x80,0x80,0x80, 0 }, //12
{ 0xFF,0, 0, 0 },
{ 0, 0xFF,0, 0 },
{ 0xFF,0xFF,0, 0 },
{ 0, 0, 0xFF, 0 },
{ 0xFF,0, 0xFF, 0 },
{ 0, 0xFF,0xFF, 0 },
{ 0xFF,0xFF,0xFF, 0 } //19
};
//
// ComponentFromIndex
//
unsigned char CAP210ViewerView::ComponentFromIndex(int i, UINT nbits, UINT shift)
{
unsigned char val;
val = (unsigned char) (i >> shift);
switch (nbits) {
case 1:
val &= 0x1;
return m_oneto8[val];
case 2:
val &= 0x3;
return m_twoto8[val];
case 3:
val &= 0x7;
return m_threeto8[val];
default:
return 0;
}
}
//
// CreateRGBPalette
//
BOOL CAP210ViewerView::CreateRGBPalette(HDC hDC)
{
//
// Check to see if we need a palette
//
TRACE0("Checking palette\r\n") ;
PIXELFORMATDESCRIPTOR pfd;
int n = GetPixelFormat(hDC);
DescribePixelFormat(hDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (!(pfd.dwFlags & PFD_NEED_PALETTE)) return FALSE ;
TRACE0("Creating palette\r\n") ;
// allocate a log pal and fill it with the color table info
LOGPALETTE* pPal = (LOGPALETTE*) malloc(sizeof(LOGPALETTE)
+ 256 * sizeof(PALETTEENTRY));
if (!pPal)
{
TRACE("Out of memory for logpal");
return FALSE;
}
pPal->palVersion = 0x300; // Windows 3.0
pPal->palNumEntries = 256; // table size
//
// Create RGB Palette
//
ASSERT( pfd.cColorBits == 8) ;
n = 1 << pfd.cColorBits;
for (int i=0; i<n; i++)
{
pPal->palPalEntry[i].peRed =
ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
pPal->palPalEntry[i].peGreen =
ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
pPal->palPalEntry[i].peBlue =
ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
pPal->palPalEntry[i].peFlags = 0;
}
//
// Fix up color table with system colors.
//
if ((pfd.cColorBits == 8) &&
(pfd.cRedBits == 3) && (pfd.cRedShift == 0) &&
(pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
(pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)
)
{
for (int j = 1 ; j <= 12 ; j++)
pPal->palPalEntry[m_defaultOverride[j]] = m_defaultPalEntry[j];
}
// Delete any existing GDI palette
if (m_pPal) delete m_pPal ;
m_pPal = new CPalette ;
BOOL bResult = m_pPal->CreatePalette(pPal);
free (pPal);
return bResult;
}
//
// OnSize
//
void CAP210ViewerView::OnSize(UINT nType, int cx, int cy) {
TRACE0("CAP210ViewerView::OnSize\r\n");
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if ( (cx <= 0) || (cy <= 0) )
return;
CClientDC dc(this);
//
// Make the rendering context m_hrc current
//
BOOL bResult = wglMakeCurrent(dc.m_hDC, m_hrc);
if (!bResult) {
TRACE("wglMakeCurrent Failed %x\r\n", GetLastError() );
return;
}
// Set the OpenGL perspective
glMatrixMode(GL_PROJECTION); OutputGlError("OnSize MatrixMode");
glLoadIdentity();
Perspective();
//
// No rendering context will be current.
//
wglMakeCurrent(NULL, NULL);
}
//
// OnDestroy
//
void CAP210ViewerView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
wglMakeCurrent(NULL,NULL);
if (m_hrc)
{
wglDeleteContext(m_hrc) ;
m_hrc = NULL ;
}
}
//
// OnEraseBkgnd
//
BOOL CAP210ViewerView::OnEraseBkgnd(CDC* pDC) {
// TODO: Add your message handler code here and/or call default
return TRUE ;
//return CView::OnEraseBkgnd(pDC);
}
void CAP210ViewerView::DrawRuler(GLdouble x, GLdouble y) {
double tick = 0.1;
double length = 1.0;
// white
glColor3f(1.0, 1.0, 1.0);
// line
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(length+x, y);
glEnd();
// start
glBegin(GL_LINES);
glVertex2f(x, -tick+y);
glVertex2f(x, tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.125*length + x, -0.25*tick+y);
glVertex2f(0.125*length + x, 0.25*tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.25*length + x, -0.5*tick+y);
glVertex2f(0.25*length + x, 0.5*tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.375*length + x, -0.25*tick+y);
glVertex2f(0.375*length + x, 0.25*tick+y);
glEnd();
// middle
glBegin(GL_LINES);
glVertex2f(0.5*length + x, -0.75*tick+y);
glVertex2f(0.5*length + x, 0.75*tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.625*length + x, -0.25*tick+y);
glVertex2f(0.625*length + x, 0.25*tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.75*length + x, -0.5*tick+y);
glVertex2f(0.75*length + x, 0.5*tick+y);
glEnd();
glBegin(GL_LINES);
glVertex2f(0.875*length + x, -0.25*tick+y);
glVertex2f(0.875*length + x, 0.25*tick+y);
glEnd();
// end
glBegin(GL_LINES);
glVertex2f(1.0 + x, -tick+y);
glVertex2f(1.0 + x, tick+y);
glEnd();
}
void CAP210ViewerView::DrawOrigin(GLdouble x, GLdouble y) {
glBegin(GL_LINES);
glVertex2f (-1.0+x, y);
glVertex2f ( 1.0+x, y);
glEnd();
glBegin(GL_LINES);
glVertex2f (x, -1.0+y);
glVertex2f (x, 1.0+y);
glEnd();
}
// This could be accomplished with a transformation.
// no scaling is being done, the origin is being moved
// to the center of the board. The min and max should
// be calculated ExtractGeometry.
void CAP210ViewerView::ScaleDisplay() {
double Height;
double Width;
CAP210ViewerDoc* pDoc = GetDocument();
AP210Point *point;
AP210ObjectListItor itor(&(pDoc->PWB->Points));
double MinX = HUGE_VAL;
double MaxX = -HUGE_VAL;
double MinY = HUGE_VAL;
double MaxY = -HUGE_VAL;
itor.Reset();
while((point = (AP210Point *)itor.NextInstanceOf(AP210Point::TypeID())) != NULL) {
if (point->X < MinX) {
MinX = point->X;
}
if (point->X > MaxX) {
MaxX = point->X;
}
if (point->Y < MinY) {
MinY = point->Y;
}
if (point->Y > MaxY) {
MaxY = point->Y;
}
}
Width = MaxX - MinX;
Height = MaxY - MinY;
// here is the key to centering the board
DeltaX = (MaxX + MinX)/2.0;
DeltaY = (MaxY + MinY)/2.0;
#ifdef VERBOSE
TRACE("MaxX: %f, MinX: %f\n", MaxX, MinX);
TRACE("MaxX + MinX: %f\r\n", MaxX + MinX);
TRACE("MaxY: %f, MinY: %f\n", MaxY, MinY);
TRACE("MaxY + MinY: %f\r\n", MaxY + MinY);
TRACE("DeltaX: %f, DeltaY: %f\n", DeltaX, DeltaY);
#endif
// Center the eye on the board.
// This really just centers the view it
// doesn't change the point of rotation.
eyeX = DeltaX;
eyeY = DeltaY;
centerX = eyeX;
centerY = eyeY;
}
void CAP210ViewerView::OnMouseMove(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
if (focus == TRUE) {
if (nFlags & MK_LBUTTON) {
#ifdef TRACKBALL
int W = 300, H = 300;
trackball(lastquat,
(2.0 * startx - W) / W,
(H - 2.0 * starty) / H,
(2.0 * point.x - W) / W,
(H - 2.0 * point.y) / H
);
newModel = 1;
#else
#ifdef VERBOSE
TRACE("Move Point: (%ld, %ld)\r\n", point.x, point.y);
TRACE(" Start: (%ld, %ld)\r\n", startx, starty);
TRACE(" Angles: (%f, %f)\r\n", angle, angle2);
#endif
angle = angle + (point.x - startx);
angle2 = angle2 + (point.y - starty);
#ifdef VERBOSE
TRACE(" Angles: (%f, %f)\r\n", angle, angle2);
#endif
#endif
// reset start
startx = point.x;
starty = point.y;
Invalidate(); // force a repaint
}
}
CView::OnMouseMove(nFlags, point);
}
void CAP210ViewerView::OnLButtonDown(UINT nFlags, CPoint point) {
// TODO: Add your message handler code here and/or call default
if (focus == TRUE) {
#ifdef VERBOSE
TRACE("Down Point: (%ld, %ld)\r\n", point.x, point.y);
TRACE(" Start: (%ld, %ld)\r\n", startx, starty);
TRACE(" Angles: (%f, %f)\r\n", angle, angle2);
#endif
startx = point.x;
starty = point.y;
/* Implement this from mouse
mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
moving = 1;
startx = x;
starty = y;
}
if (state == GLUT_UP) {
moving = 0;
}
}
if (button == GLUT_MIDDLE_BUTTON) {
if (state == GLUT_DOWN) {
lightMoving = 1;
lightStartX = x;
lightStartY = y;
}
if (state == GLUT_UP) {
lightMoving = 0;
}
}
}
*/
}
CView::OnLButtonDown(nFlags, point);
}
BOOL CAP210ViewerView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) {
// TODO: Add your message handler code here and/or call default
static short accum = 0; // accumulate zDeltas, should this
// be set to zero in OnChar() also?
if (focus == TRUE) {
#ifdef VERBOSE
TRACE("Wheel Point: (%ld, %ld)\r\n", pt.x, pt.y);
TRACE(" zDelta: %ld\r\n", zDelta);
#endif
accum += zDelta;
if (accum >= 120) {
accum = 0;
eyeZ++; // Move away from model
Invalidate(); // force a repaint
}
else if (accum <= -120) {
accum = 0;
eyeZ--; // Move toward model
Invalidate(); // force a repaint
}
}
return CView::OnMouseWheel(nFlags, zDelta, pt);
}
void CAP210ViewerView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {
// TODO: Add your message handler code here and/or call default
if (focus == TRUE) {
#ifdef VERBOSE
TRACE("OnChar nChar: %ld - '%c'\r\n", nChar, nChar);
TRACE(" nRepCnt: %ld\r\n", nRepCnt);
#endif
switch(nChar) {
case '+':
eyeZ-=0.1;
// eyeZ--;
#ifdef VERBOSE
TRACE("eyeZ: %f\r\n", eyeZ);
#endif
Invalidate();
break;
case '-':
// eyeZ++;
eyeZ+=0.1;
#ifdef VERBOSE
TRACE("eyeZ: %f\r\n", eyeZ);
#endif
Invalidate();
break;
// eyeX and eyeY need to be incremented in smaller amounts.
case '8':
eyeY-=0.1;
centerY-=0.1;
Invalidate();
break;
case '2':
eyeY+=0.1;
centerY+=0.1;
Invalidate();
break;
case '6':
eyeX-=0.1;
centerX-=0.1;
Invalidate();
break;
case '4':
eyeX+=0.1;
centerX+=0.1;
Invalidate();
break;
}
}
CView::OnChar(nChar, nRepCnt, nFlags);
}
#ifdef TRACKBALL
void CAP210ViewerView::recalcModelView() {
GLfloat m[4][4];
// This is really weird why a pop than a push?
glPopMatrix();
glPushMatrix();
add_quats(lastquat, curquat, curquat);
build_rotmatrix(m, curquat);
glMultMatrixf(&m[0][0]);
#if 0
if (scalefactor == 1.0) {
glDisable(GL_NORMALIZE);
}
else {
glEnable(GL_NORMALIZE);
}
glScalef(scalefactor, scalefactor, scalefactor);
glTranslatef(-8, -8, -bodyWidth / 2);
#endif
newModel = 0;