-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView3DRender.cpp
executable file
·775 lines (661 loc) · 20.6 KB
/
View3DRender.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
//////////////////////////////////////////////////////////////////////
// Calques 3D - a 3D Dynamic Geometry Learning Environment
// Copyright (c) 1997-2007 Nicolas Van Labeke
//////////////////////////////////////////////////////////////////////
// This file is part of Calques 3D.
//
// Calques 3D is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Calques 3D is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Calques 3D; if not, write to The Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//////////////////////////////////////////////////////////////////////
/// @file: View3DRender.cpp
/// implementation of the CView3DRender class.
///
/// $Date: 2007-11-17 19:36:38+00 $
/// $Revision: 1.11 $
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "calques3d.h"
#include "Calques3DDoc.h"
#include "MainFrm.h"
#include "View3DRender.h"
#include "Prefs\Prefs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CView3DRender
IMPLEMENT_DYNCREATE(CView3DRender, CGLEnabledView)
BEGIN_MESSAGE_MAP(CView3DRender, CGLEnabledView)
//{{AFX_MSG_MAP(CView3DRender)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_CONTEXTMENU()
ON_COMMAND(ID_RENDERER_PROPERTY, OnShowProperty)
ON_UPDATE_COMMAND_UI(ID_RENDERER_PROPERTY, OnUpdateProperty)
ON_COMMAND_RANGE(ID_RENDERER_SILHOUETTE,ID_RENDERER_STIPPLE, OnChangeVolumeMode)
ON_UPDATE_COMMAND_UI_RANGE(ID_RENDERER_SILHOUETTE, ID_RENDERER_STIPPLE, OnUpdateVolumeMode)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CView3DRender::CView3DRender():
//m_wndTrackball(.8,unitquaternion(DegToRad(45),Y_AXIS)*unitquaternion(DegToRad(-30),X_AXIS))
m_wndTrackball(.8,unitquaternion(DegToRad(135),Z_AXIS)*unitquaternion(DegToRad(45),X_AXIS))
{
m_wndTrackball.SetColor(RGB(130,80,30));
m_wndTrackball.SetDrawConstraints();
m_pDlg = NULL;
m_nVolMode = CObject3D::RENDER_SILHOUETTE;
}
CView3DRender::~CView3DRender()
{
if (m_pDlg) delete m_pDlg;
}
/////////////////////////////////////////////////////////////////////////////
// CView3DRender diagnostics
#ifdef _DEBUG
void CView3DRender::AssertValid() const
{
CGLEnabledView::AssertValid();
}
void CView3DRender::Dump(CDumpContext& dc) const
{
CGLEnabledView::Dump(dc);
}
CCalques3DDoc* CView3DRender::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCalques3DDoc)));
return (CCalques3DDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CView3DRender message handlers
void CView3DRender::GetLightParams(int *pPos)
{
for (int i=0; i<NBVIEWPARAMS; i++)
pPos[i] = m_LightParam[i];
}
void CView3DRender::SetLightParam (short lp, int nPos)
{
m_LightParam[lp] = nPos;
SetLight();
Invalidate(FALSE);
}
void CView3DRender::SetLight ()
{
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
//====== Both surface sides are considered when calculating
//====== each pixel color with the lighting formula
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
//====== Light source position depends on the
// object sizes scaled to (0,100)
float fPos[] =
{
(m_LightParam[0]-50.f),///100,
(m_LightParam[1]-50.f),///100,
(m_LightParam[2]-50.f),///100,
1.f
};
glLightfv(GL_LIGHT0, GL_POSITION, fPos);
//====== Ambient light intensity
float f = m_LightParam[3]/100.f;
float fAmbient[4] = { f, f, f, 0.f };
glLightfv(GL_LIGHT0, GL_AMBIENT, fAmbient);
//====== Diffuse light intensity
f = m_LightParam[4]/100.f;
float fDiffuse[4] = { f, f, f, 0.f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, fDiffuse);
//====== Specular light intensity
f = m_LightParam[5]/100.f;
float fSpecular[4] = { f, f, f, 0.f };
glLightfv(GL_LIGHT0, GL_SPECULAR, fSpecular);
//====== Surface material reflection properties for
// each light component
f = m_LightParam[6]/100.f;
float fAmbMat[4] = { f, f, f, 0.f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, fAmbMat);
f = m_LightParam[7]/100.f;
float fDifMat[4] = { f, f, f, 1.f };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, fDifMat);
f = m_LightParam[8]/100.f;
float fSpecMat[4] = { f, f, f, 0.f };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fSpecMat);
//====== Material shininess
float fShine = 128 * m_LightParam[9]/100.f;
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, fShine);
//====== Material light emission property
f = m_LightParam[10]/100.f;
float fEmission[4] = { f, f, f, 0.f };
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, fEmission);
// glEnable(GL_LIGHTING); // Lighting will be used
// //====== Only one (first) source of light
// glEnable(GL_LIGHT0);
// //====== Depth of the Z-buffer will be taken into account
// glEnable(GL_DEPTH_TEST);
// //====== Material colors will be taken into account
// glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_LightParam[11]+20,m_dAspectRatio,1.0f, 100.0f);
glTranslatef(0.0f,0.0f,-6.0f);
glTranslatef(0.0f,-0.667f,0.0f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void CView3DRender::OnCreateGL()
{
// perform hidden line/surface removal (enable Z-Buffering)
glEnable(GL_DEPTH_TEST);
// set background color to black
//glClearColor(0.f,0.f,0.f,1.0f );
glClearColor(0.95f,0.95f,0.95f,1.f );
// set clear Z-Buffer value
glClearDepth(1.0f);
// create and enable a white directional light
// float color[]={1.f,1.f,1.f,1.f};// RGBA color spec
// glLightfv(GL_LIGHT0,GL_DIFFUSE,color);
// float ambient[]={.3f,.3f,.3f,1.f};// RGBA color spec
// glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
// float pos[]={1.f,1.f,1.f,0.f};
// glLightfv(GL_LIGHT0,GL_POSITION,pos);
//
// float model_ambient[] = {0.4f, 0.4f, 0.4f, 1.0f};
// //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
// //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0); //0=infiniteViewpoint, 1=localViewpoint
// glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
//
// glEnable(GL_LIGHT0);
// glEnable(GL_LIGHTING);
//====== Initial lighting params
m_LightParam[0] = 50; // X position
m_LightParam[1] = 80; // Y position
m_LightParam[2] = 100; // Z position
m_LightParam[3] = 15; // Ambient light
m_LightParam[4] = 70; // Diffuse light
m_LightParam[5] = 60; // Specular light
m_LightParam[6] = 100; // Ambient material
m_LightParam[7] = 100; // Diffuse material
m_LightParam[8] = 40; // Specular material
m_LightParam[9] = 70; // Shininess material
m_LightParam[10] = 0; // Emission material
m_LightParam[11] = 35; // Perspective Field of View
//====== Now you can issue OpenGL commands (i.e. call gl*** functions)
glEnable(GL_LIGHTING); // Lighting will be used
//====== Only one (first) source of light
glEnable(GL_LIGHT0);
//====== The depth of the Z-buffer will be taken into account
glEnable(GL_DEPTH_TEST);
//====== Material colors will be taken into account
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_POLYGON_STIPPLE);
//SetLight();
// glEnable(GL_POINT_SMOOTH);
// glEnable(GL_LINE_SMOOTH);
// glEnable(GL_POLYGON_SMOOTH);
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
// glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
// glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST);
// glShadeModel(GL_SMOOTH);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
// prepare a bunch of line segments (carthesian axes arrows)
StartStockDListDef();
glBegin(GL_LINES);
// light red x axis arrow
glColor3f(1.f,0.5f,.5f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
// light green y axis arrow
glColor3f(.5f,1.f,0.5f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
// light blue z axis arrow
glColor3f(.5f,.5f,1.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glEnd();
glBegin(GL_LINES);
// x letter & arrowhead
glColor3f(1.f,0.5f,.5f);
glVertex3f(1.1f,0.1f,0.0f);
glVertex3f(1.3f,-0.1f,0.0f);
glVertex3f(1.3f,0.1f,0.0f);
glVertex3f(1.1f,-0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,-0.1f,0.0f);
// y letter & arrowhead
glColor3f(.5f,1.f,0.5f);
glVertex3f(-0.1f,1.3f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.1f,1.3f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.f,1.2f,0.0f);
glVertex3f(0.f,1.1f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.1f,0.9f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
// z letter & arrowhead
glColor3f(.5f,.5f,1.f);
glVertex3f(0.0f,-0.1f,1.3f);
glVertex3f(0.0f,0.1f,1.3f);
glVertex3f(0.0f,0.1f,1.3f);
glVertex3f(0.0f,-0.1f,1.1f);
glVertex3f(0.0f,-0.1f,1.1f);
glVertex3f(0.0f,0.1f,1.1f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.1f,0.9f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,-0.1f,0.9f);
glEnd();
EndStockListDef();
}
void CView3DRender::OnDrawGL()
{
SetLight();
// the couple glPush - glPop is necessary because the entire
// scene is rotated of the given angle (which is absolute) at every redraw
glPushMatrix();
// apply trackball rotation
m_wndTrackball.IssueGLrotation();
// "save" the current transformation matrix for subsequent superimpose of axes
glPushMatrix();
// this should be self explanatory
const float radius=0.34f;
// {
// float mat_ambient_color[] = {255/255., 255/255.,150/255. , 0.010f};
// float no_shininess = 0.9f;
// GLdouble size = (1/3.);
//
// glPushMatrix();
// glBegin(GL_QUADS);
// glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
// glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient_color);
// //glMaterialf(GL_FRONT, GL_SHININESS, no_shininess);
// int max = TPref::TUniv.nRepPas;
// for (int i=-max;i<=max;i++)
// for (int j=-max;j<=max;j++)
// {
// glNormal3f(0.0f, 0.0f, 1.0f); //N1
// glVertex3d( 0+i*size, 0+j*size, 0); //V2
// glVertex3d( size+i*size,0+j*size,0); //V1
// glVertex3d( size+i*size,size+j*size,0); //V3
// glVertex3d( 0+i*size,size+j*size,0); //V4
// }
// glEnd();
// glPopMatrix();
// }
int nb = GetDocument()->m_cObjectSet.GetSize();
for (int i=0;i<nb;i++)
{
CObject3D* pObj = GetDocument()->m_cObjectSet.GetAt(i);
if (!pObj) continue;
pObj->Draw3DRendering(m_nVolMode);
}
m_wndTrackball.DrawBall();
//"superimpose" the RGB-carthesian-axes in a small area near the bottom-left corner of the view
glViewport(0,0,m_ClientRect.right/6,m_ClientRect.bottom/6);
glPopMatrix();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
DrawStockDispLists();
glPopAttrib();
glViewport(0,0,m_ClientRect.right,m_ClientRect.bottom);
glPopMatrix();
}
void CView3DRender::OnInitialUpdate()
{
CGLEnabledView::OnInitialUpdate();
}
#define BUFFER_LENGTH 64
void CView3DRender::ProcessSelection(int xPos, int yPos)
{
// Space for selection buffer
GLuint selectBuff[BUFFER_LENGTH];
// Hit counter and viewport storage
GLint hits, viewport[4];
// Setup selection buffer
glSelectBuffer(BUFFER_LENGTH, selectBuff);
// Get the viewport
glGetIntegerv(GL_VIEWPORT, viewport);
viewport[0] = 0;
viewport[1] = 0;
viewport[2] = m_ClientRect.right;
viewport[3] = m_ClientRect.bottom;
// Change render mode
glRenderMode(GL_SELECT);
// Switch to projection and save the matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
// Establish new clipping volume to be unit cube around
// mouse cursor point (xPos, yPos) and extending two pixels
// in the vertical and horizontal direction
glLoadIdentity();
// Since OpenGL measures
// window coordinates starting at the bottom of the window, and Windows
// measures starting at the top, we need to account for this by
// subtracting the y coordinate from the height of the window. This has
// the effect of reversing the coordinate system (y starts at top)
gluPickMatrix(xPos, viewport[3] - yPos, 50.0,50.0, viewport);
// // Apply perspective matrix
// gluPerspective(45.0f, m_dAspectRatio, 1.0, 425.0);
gluPerspective(m_LightParam[11]+20,m_dAspectRatio,1.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
// Draw the scene
OnDrawGL();
// Restore the projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Go back to modelview for normal rendering
glMatrixMode(GL_MODELVIEW);
// Collect the hits
hits = glRenderMode(GL_RENDER);
// If a single hit occured, display the info.
if(hits == 1)
ProcessObject(selectBuff);
}
void CView3DRender::ProcessObject(GLuint *pSelectBuff)
{
int id,count;
char cMessage[64];
// How many names on the name stack
count = pSelectBuff[0];
// Bottom of the name stack
id = pSelectBuff[3];
}
void CView3DRender::OnLButtonDown(UINT nFlags, CPoint point)
{
// ProcessSelection(point.x,point.y);
if (nFlags & MK_CONTROL)
m_wndTrackball.UseConstraints(CAMERA_AXES);
else if(nFlags & MK_SHIFT)
m_wndTrackball.UseConstraints(BODY_AXES);
else m_wndTrackball.UseConstraints(NO_AXES);
// remember where we clicked
m_ptOldLoc=point;
m_wndTrackball.MouseDown(point);
// capture mouse movements even outside window borders
SetCapture();
// redraw the view
Invalidate(TRUE);
}
void CView3DRender::OnLButtonUp(UINT nFlags, CPoint point)
{
// forget where we clicked
m_ptOldLoc=CPoint(0,0);
m_wndTrackball.MouseUp(point);
// release mouse capture
ReleaseCapture();
// redraw the view
Invalidate(TRUE);
}
void CView3DRender::OnMouseMove(UINT nFlags, CPoint point)
{
m_wndTrackball.MouseMove(point);
Invalidate(TRUE);
}
void CView3DRender::OnRButtonDown(UINT nFlags, CPoint point)
{
CGLEnabledView::OnRButtonDown(nFlags, point);
OnShowProperty();
}
void CView3DRender::OnShowProperty()
{
// TODO: Add your command handler code here
if (!m_pDlg)
{
m_pDlg = new CRenderPropDlg(this);
m_pDlg->Create(CRenderPropDlg::IDD);
m_pDlg->CenterWindow();
m_pDlg->ShowWindow(SW_SHOW);
m_pDlg->SetActiveWindow();
}
else
{
delete m_pDlg;
m_pDlg = NULL;
}
}
void CView3DRender::OnChangeVolumeMode(UINT cmd)
{
if (cmd==ID_RENDERER_FILL)
m_nVolMode = CObject3D::RENDER_FILL;
else if (cmd==ID_RENDERER_STIPPLE)
m_nVolMode = CObject3D::RENDER_STIPPLE;
else //if (cmd==ID_RENDERER_SILHOUETTE)
m_nVolMode = CObject3D::RENDER_SILHOUETTE;
Invalidate(TRUE);
}
void CView3DRender::OnUpdateVolumeMode(CCmdUI* pCmdUI)
{
if (pCmdUI->m_nID==ID_RENDERER_FILL)
pCmdUI->SetCheck(m_nVolMode == CObject3D::RENDER_FILL);
else if (pCmdUI->m_nID==ID_RENDERER_STIPPLE)
pCmdUI->SetCheck(m_nVolMode == CObject3D::RENDER_STIPPLE);
else if (pCmdUI->m_nID==ID_RENDERER_SILHOUETTE)
pCmdUI->SetCheck(m_nVolMode == CObject3D::RENDER_SILHOUETTE);
}
void CView3DRender::OnUpdateProperty(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_pDlg!=NULL);
}
void CView3DRender::OnSizeGL(int cx, int cy)
{
// afxDump<<"client area: "<<cx<<" x "<<cy<<"\n";
m_wndTrackball.ClientAreaResize(CRect(0,0,cx,cy));
//CGLEnabledView::OnSizeGL(cx,cy);
// set correspondence between window and OGL viewport
glViewport(0,0,cx,cy);
// update the camera
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_LightParam[11]+20,m_dAspectRatio,1.0f, 100.0f);
glTranslatef(0.0f,0.0f,-6.0f);
glTranslatef(0.0f,-0.667f,0.0f);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void CView3DRender::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(nChar==VK_SPACE) m_wndTrackball.ToggleMethod();
m_wndTrackball.Key(nChar);
CGLEnabledView::OnKeyDown(nChar, nRepCnt, nFlags);
Invalidate(TRUE);
}
CRenderPropDlg::CRenderPropDlg(CView3DRender* p) : CDialog(CRenderPropDlg::IDD, p)
{
m_pView = p;
//{{AFX_DATA_INIT(CPropDlg)
//}}AFX_DATA_INIT
}
void CRenderPropDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRenderPropDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRenderPropDlg, CDialog)
//{{AFX_MSG_MAP(CRenderPropDlg)
ON_WM_HSCROLL()
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CRenderPropDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
HWND hwnd = pScrollBar->GetSafeHwnd();
UINT nID;
int num = GetSliderNum(hwnd, nID); // Slider number
int delta, newPos;
switch (nSBCode)
{
case SB_THUMBTRACK:
case SB_THUMBPOSITION: m_Pos[num] = nPos; break;
case SB_LEFT: delta = -100; goto New_Pos;
case SB_RIGHT: delta = +100; goto New_Pos;
case SB_LINELEFT: delta = -1; goto New_Pos;
case SB_LINERIGHT: delta = +1; goto New_Pos;
case SB_PAGELEFT: delta = -20; goto New_Pos;
case SB_PAGERIGHT: delta = +20; goto New_Pos;
New_Pos:
newPos = m_Pos[num] + delta;
m_Pos[num] = newPos<0 ? 0 : newPos>100 ? 100 : newPos;
break;
case SB_ENDSCROLL:
default:
return;
}
char s[8];
sprintf (s,"%d",m_Pos[num]);
SetDlgItemText(nID, (LPCTSTR)s);
m_pView->SetLightParam (num, m_Pos[num]);
}
int CRenderPropDlg::GetSliderNum(HWND hwnd, UINT& nID)
{
switch (::GetDlgCtrlID(hwnd))
{
case IDC_RENDERER_LGTX:
nID = IDC_RENDERER_LGTXTXT;
return 0;
case IDC_RENDERER_LGTY:
nID = IDC_RENDERER_LGTYTXT;
return 1;
case IDC_RENDERER_LGTZ:
nID = IDC_RENDERER_LGTZTXT;
return 2;
case IDC_RENDERER_AMBIENT:
nID = IDC_RENDERER_AMBTXT;
return 3;
case IDC_RENDERER_DIFFUSE:
nID = IDC_RENDERER_DIFFUSETXT;
return 4;
case IDC_RENDERER_SPECULAR:
nID = IDC_RENDERER_SPECULARTXT;
return 5;
case IDC_RENDERER_AMBMAT:
nID = IDC_RENDERER_AMBMATTXT;
return 6;
case IDC_RENDERER_DIFFMAT:
nID = IDC_RENDERER_DIFFMATTXT;
return 7;
case IDC_RENDERER_SPECMAT:
nID = IDC_RENDERER_SPECMATTXT;
return 8;
case IDC_RENDERER_SHINE:
nID = IDC_RENDERER_SHINETXT;
return 9;
case IDC_RENDERER_EMISSION:
nID = IDC_RENDERER_EMISSIONTXT;
return 10;
case IDC_RENDERER_FIELDVIEW:
nID = IDC_RENDERER_FIELDVIEWTXT;
return 11;
}
return 0;
}
BOOL CRenderPropDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_pView->GetLightParams(m_Pos);
UINT IDs[] =
{
IDC_RENDERER_LGTX, IDC_RENDERER_LGTY, IDC_RENDERER_LGTZ,
IDC_RENDERER_AMBIENT,IDC_RENDERER_DIFFUSE,IDC_RENDERER_SPECULAR,
IDC_RENDERER_AMBMAT, IDC_RENDERER_DIFFMAT,IDC_RENDERER_SPECMAT,
IDC_RENDERER_SHINE, IDC_RENDERER_EMISSION, IDC_RENDERER_FIELDVIEW
};
for (int i=0; i<sizeof(IDs)/sizeof(IDs[0]); i++)
{
HWND hwnd = GetDlgItem(IDs[i])->GetSafeHwnd();
UINT nID;
int num = GetSliderNum(hwnd, nID);
::SendMessage(hwnd, TBM_SETPOS, TRUE, (LPARAM)m_Pos[i]);
char s[8];
sprintf (s,"%d",m_Pos[i]);
SetDlgItemText(nID, (LPCTSTR)s);
}
return TRUE;
}
void CRenderPropDlg::OnClose()
{
m_pView->m_pDlg = 0;
CDialog::OnClose();
delete this;
}
void CView3DRender::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
}
#define FCComZero(a) (fabsl(a) < 0.01)
void CView3DRender::OnMouse3D(UINT nIDEvent)
{
CMainFrame *pMWnd= DYNAMIC_DOWNCAST(CMainFrame,AfxGetMainWnd());
if (!pMWnd) return;
if (!pMWnd->m_g3DSensor) return;
try {
CComPtr<IAngleAxis> pRotation = pMWnd->m_g3DSensor->Rotation;
CComPtr<IVector3D> pTranslation = pMWnd->m_g3DSensor->Translation;
FCoord fAngle = pRotation->GetAngle();
FCoord fLength = pTranslation->GetLength();
if (!FCComZero(fAngle) || !FCComZero(fLength))
{
// Get the rotation
FCoord fX = pRotation->GetX() * fAngle;
FCoord fY = pRotation->GetY() * fAngle;
FCoord fZ = pRotation->GetZ() * fAngle;
TRACE3("ROT X %.2f - Y %.2f - Z %.2f\n",fX,fY,fZ);
// Get the translation
FCoord dX = pTranslation->GetX() / 20.;
FCoord dY = pTranslation->GetY() / 20.;
FCoord dZ = pTranslation->GetZ() / 2.;
TRACE3("TRN X %.2f - Y %.2f - Z %.2f\n",dX,dY,dZ);
// Translate the universe
m_wndTrackball.AddQuaternion(unitquaternion(DegToRad(-fZ/100),Y_AXIS));
m_wndTrackball.AddQuaternion(unitquaternion(DegToRad(-fX/100),X_AXIS));
m_wndTrackball.AddQuaternion(unitquaternion(DegToRad(fY/100),Z_AXIS));
int nb = m_LightParam[11];
SetLightParam (11,nb+dZ/100.);
Invalidate(TRUE);
}
else
{
//TRACE0("DEVICE AT REST\n");
}
// Release the device's components
pRotation.Release();
pTranslation.Release();
}
catch (...)
{
// Some sort of exception handling
}
}
void CView3DRender::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent==TIMER_TDXINPUT)
{
OnMouse3D(nIDEvent);
}
CGLEnabledView::OnTimer(nIDEvent);
}