-
Notifications
You must be signed in to change notification settings - Fork 1
/
PRTView.cpp.bak
2884 lines (2507 loc) · 81.3 KB
/
PRTView.cpp.bak
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
// PRTView.cpp : implementation of the CPRTView class
//
//#define NOMINMAX
#include "stdafx.h"
#include <conio.h>
#include <direct.h>
#include <iostream>
#include "PRT.h"
#include "PRTDoc.h"
#include "PRTView.h"
#include ".\prtview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Matrix4fT g_modelTransform = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT g_eyeLastRot = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT g_eyeThisRot = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
ArcBallT g_eyeArcBall(640.0f, 480.0f);
Matrix4fT g_envTransform = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT g_envLastRot = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
Matrix3fT g_envThisRot = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
ArcBallT g_envArcBall(640.0f, 480.0f);
typedef void (APIENTRY * PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
typedef void (APIENTRY * PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
typedef void (APIENTRY * PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
typedef void (APIENTRY * PFNGLBUFFERDATAARBPROC) (GLenum target, int size, const GLvoid *data, GLenum usage);
// VBO Extension Function Pointers
PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL; // VBO Name Generation Procedure
PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL; // VBO Bind Procedure
PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL; // VBO Data Loading Procedure
PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL; // VBO Deletion Procedure
#define n6k (6 * m_cubeRes * m_cubeRes)
#define n1k (m_cubeRes * m_cubeRes)
#define StencilVis
// CPRTView
IMPLEMENT_DYNCREATE(CPRTView, CView)
BEGIN_MESSAGE_MAP(CPRTView, CView)
// 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)
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MBUTTONDOWN()
ON_WM_MBUTTONUP()
ON_WM_ERASEBKGND()
ON_COMMAND(ID_EDIT_INITGL, OnInitGL)
ON_COMMAND(ID_EDIT_DRAWNORMAL, OnDrawNormal)
ON_COMMAND(ID_EDIT_FILLMODE, OnFillMode)
ON_COMMAND(ID_EDIT_COMPUTESAVEVISBS, OnComputeSaveVisibs)
ON_COMMAND(ID_EDIT_DRAWNORMALMAP, OnDrawNormalMap)
ON_WM_KEYDOWN()
ON_COMMAND(ID_EDIT_DRAWVISRATIO, OnDrawVisRatio)
ON_WM_MOUSEWHEEL()
ON_COMMAND(ID_FILE_OPENCUBEMAP, OnOpenCubeMap)
ON_UPDATE_COMMAND_UI(ID_EDIT_DRAWNORMAL, OnUpdateEditDrawNormal)
ON_UPDATE_COMMAND_UI(ID_EDIT_DRAWNORMALMAP, OnUpdateEditDrawNormalMap)
ON_UPDATE_COMMAND_UI(ID_EDIT_DRAWVISRATIO, OnUpdateEditDrawVisRatio)
ON_COMMAND(ID_EDIT_DRAWLIGHT, OnEditDrawLight)
ON_UPDATE_COMMAND_UI(ID_EDIT_DRAWLIGHT, OnUpdateEditDrawLight)
ON_COMMAND(ID_EDIT_COMPUTESAVET1, OnComputeSaveT1)
ON_COMMAND(ID_VIEW_32799, OnDiffuseE)
ON_COMMAND(ID_VIEW_32796, OnFwi1)
ON_COMMAND(ID_VIEW_32797, OnFwo1)
ON_UPDATE_COMMAND_UI(ID_VIEW_32799, OnUpdateDiffuseE)
ON_UPDATE_COMMAND_UI(ID_VIEW_32796, OnUpdateFwi1)
ON_UPDATE_COMMAND_UI(ID_VIEW_32797, OnUpdateFwo1)
ON_COMMAND(ID_USECOEFNUM_32, OnUseCoefNum32)
ON_COMMAND(ID_USECOEFNUM_64, OnUsecoefnum64)
ON_COMMAND(ID_USECOEFNUM_96, OnUsecoefnum96)
ON_COMMAND(ID_USECOEFNUM_128, OnUsecoefnum128)
ON_COMMAND(ID_USECOEFNUM_256, OnUsecoefnum256)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_32, OnUpdateUsecoefnum32)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_64, OnUpdateUsecoefnum64)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_96, OnUpdateUsecoefnum96)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_128, OnUpdateUsecoefnum128)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_256, OnUpdateUsecoefnum256)
ON_COMMAND(ID_USECOEFNUM_4, OnUsecoefnum4)
ON_COMMAND(ID_USECOEFNUM_8, OnUsecoefnum8)
ON_COMMAND(ID_USECOEFNUM_16, OnUsecoefnum16)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_4, OnUpdateUsecoefnum4)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_8, OnUpdateUsecoefnum8)
ON_UPDATE_COMMAND_UI(ID_USECOEFNUM_16, OnUpdateUsecoefnum16)
ON_COMMAND(ID_Menu, OnDrawSubsurfVisRatio)
ON_UPDATE_COMMAND_UI(ID_Menu, OnUpdateDrawSubsurfVisRatio)
ON_COMMAND(ID_EDIT_COMPUTESAVEDM, OnComputeSaveDm)
ON_COMMAND(ID_EDIT_DRAWDM, OnDrawDm)
ON_UPDATE_COMMAND_UI(ID_EDIT_DRAWDM, OnUpdateDrawDm)
ON_COMMAND(ID_EDIT_MSTRANS2PASS, OnMSTrans2Pass)
ON_COMMAND(ID_EDIT_COMPUTESAVEE, OnEditComputeSaveE)
ON_COMMAND(ID_EDIT_BUILDTREE, OnBuildTree)
ON_COMMAND(ID_EDIT_LOADTD, OnLoadTd)
ON_COMMAND(ID_EDIT_LOADT1, OnLoadT1Coefs)
ON_COMMAND(ID_USEPHASETERM_4, OnUsephaseterm4)
ON_UPDATE_COMMAND_UI(ID_USEPHASETERM_4, OnUpdateUsephaseterm4)
ON_COMMAND(ID_USEPHASETERM_8, OnUsephaseterm8)
ON_UPDATE_COMMAND_UI(ID_USEPHASETERM_8, OnUpdateUsephaseterm8)
ON_COMMAND(ID_USEPHASETERM_16, OnUsephaseterm16)
ON_UPDATE_COMMAND_UI(ID_USEPHASETERM_16, OnUpdateUsephaseterm16)
ON_COMMAND(ID_USEPHASETERM_32, OnUsephaseterm32)
ON_UPDATE_COMMAND_UI(ID_USEPHASETERM_32, OnUpdateUsephaseterm32)
ON_COMMAND(ID_USEPHASETERM_1, OnUsephaseterm1)
ON_UPDATE_COMMAND_UI(ID_USEPHASETERM_1, OnUpdateUsephaseterm1)
ON_COMMAND(ID_VIEW_32828, OnSpec)
ON_UPDATE_COMMAND_UI(ID_VIEW_32828, OnUpdateSpec)
ON_COMMAND(ID_VIEW_DRAWTEXTURE, OnViewDrawtexture)
ON_UPDATE_COMMAND_UI(ID_VIEW_DRAWTEXTURE, OnUpdateViewDrawtexture)
ON_COMMAND(ID_FILE_OPENTEXTURE, OnFileOpentexture)
END_MESSAGE_MAP()
// CPRTView construction/destruction
CPRTView::CPRTView()
: m_bLoadedObj(false)
, m_bLoadedVisibs(false)
, m_bLoadedRd(false)
, m_bDrawNormal(false)
, m_bDrawNormalMap(false)
, m_bDrawVisRatio(false)
, m_fillMode(0)
, m_cubeRes(32)
, m_ECoefNum(256)
, m_bLoadedTdCoefs(false)
, m_bLoadedCubeMap(false)
, m_bDrawMS(false)
, m_colorCoef(1.0f)
, m_hdrCoef(1.0f)
, m_bDrawLight(false)
, m_bDrawSS(false)
, m_SSSampleNum(16)
, m_bLoadedT1Coefs(false)
, _DiffuseE(false)
, _Fwi1(true)
, _Fwo1(true)
, m_bMSUncomp(false)
, m_fTd0(NULL)
, m_fE0(NULL)
, m_bLoadedE0T0(false)
, m_nFPS(0)
, m_nFrames(0)
, m_dwLastFPS(0)
, m_objBboxSize(5)
, m_svrKeyDownIdx(0)
, m_bDrawSubsurfVisratio(false)
, m_yita(1.3f) //index of relative refraction
, m_bLoadedDm(false)
, m_bDrawDm(false)
, _MC(true)
, m_useCoefNum(256)
, m_pECoefsTree(NULL)
, m_epsMSTdKd(0.02f) //jiaping's is 0.02f
, m_bDrawBBox(false)
, m_TdCoefNum(256)
, m_T1CoefNum(256)
, m_displayListId(0)
, m_nVBOVertices(0)
, m_nVBOColors(0)
, m_phaseSVDterm(4)
, _HG1(false)
, m_HGres(16)
, m_usePhaseTerm(4)
, _Spec(true)
, m_specCoef(35.f)
, _Tex(true)
, m_texAlpha(128)
{
//deb
m_usePhaseTerm = 1;
m_phaseSVDterm = 4;
m_SSSampleNum = 8;
m_g = 0.25f; //SSIso!!! NOTE: m_g impact sampleIntrude!!!!
m_epsMSTdKd = 0.01f;
m_leafSampleNum = 16;
m_eye = Vec3(0, 0, 3 * m_objBboxSize);
m_MSWeight = 0.022f; //to do: add a scroll bar to modify-on-fly
m_SSWeight = 1;
////////////////////////////////////////////////////////////////////////// bssrdf
////whole milk
//m_sigmaSPrime = Vec3(2.55f, 3.21f, 3.77f);//r,g,b (1-g)*sigmaS, sigmaS is scattering coef, not appeared. .Jensen01 Fig.5
//m_sigmaA = Vec3(0.0011f, 0.0024f, 0.014f);//absorption coef
//marble
//m_sigmaSPrime = Vec3(2.19f, 2.62f, 3.f);
//m_sigmaA = Vec3(0.0021f, 0.0041f, 0.0071f);
////Wang05's teaser
m_sigmaSPrime = Vec3(0.75f, 0.85f, 1.00f);//r,g,b (1-g)*sigmaS, sigmaS is scattering coef, not appeared. .Jensen01 Fig.5
m_sigmaA = Vec3(0.02f, 0.04f, 0.07f);//absorption coef
m_sigmaS = m_sigmaSPrime / (1 - m_g); //sigmaS = sigmaS'/(1-g)
m_sigmaT = m_sigmaA + m_sigmaS;
m_sigmaTLumi = 0.299f * m_sigmaT.x + 0.587f * m_sigmaT.y + 0.114f * m_sigmaT.z; //sigmaT's luminance: L*A*B color space projection of sigmaT
//float m_zr[3];
//float m_zv[3];
//float m_sigmaTr[3];
//float m_alphaPrime[3];
//m_g = 0;
float Fdr = -1.440f/(m_yita*m_yita) + 0.710f/m_yita + 0.668f + 0.0636f*m_yita;
Vec3 sigmaTPrime;
sigmaTPrime = m_sigmaA + m_sigmaSPrime; //extinction coef
m_sigmaTr = SQRTF(DP(m_sigmaA, sigmaTPrime) * 3);
m_alphaPrime = DD(m_sigmaSPrime, sigmaTPrime);
m_zr = 1 / sigmaTPrime;
m_zv = m_zr * (1 + 4.f/3.f * (1+Fdr)*(1-Fdr));
//////////////////////////////////////////////////////////////////////////
m_curLightCoefs = new Vec3[n6k];
m_bLBDown = false;
m_bRBDown = false;
m_bMBDown = false;
m_oldY = 0;//for zoom
m_dFOV = 50; //in degree
m_oldFOV = m_dFOV;
m_cubeMap.m_downRes = m_cubeRes;
m_errorString = new GLubyte[256];
m_textureImage.clear();
//test
//ComputeSaveHGMat(16);
}
CPRTView::~CPRTView()
{
if(m_curLightCoefs)
delete[] m_curLightCoefs;
if(m_fTd0)
fclose(m_fTd0);
if(m_fE0)
fclose(m_fE0);
if(m_errorString)
{
delete[] m_errorString;
m_errorString = NULL;
}
if(m_pECoefsTree)
{
delete m_pECoefsTree;
m_pECoefsTree = NULL;
}
}
BOOL CPRTView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CPRTView::OnDraw(CDC* /*pDC*/)
{
}
BOOL CPRTView::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
void CPRTView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
void CPRTView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
#ifdef _DEBUG
void CPRTView::AssertValid() const
{
CView::AssertValid();
}
void CPRTView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPRTDoc* CPRTView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPRTDoc)));
return (CPRTDoc*)m_pDocument;
}
#endif //_DEBUG
int CPRTView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetupPixelFormat();
wglMakeCurrent(NULL,NULL);
return 0;
}
BOOL CPRTView::SetupPixelFormat()
{
GLuint PixelFormat;
static PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number (?)
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select A 32Bit Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
0, // No Alpha Buffer
0, // Shift Bit Ignored (?)
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored (?)
24, // 24Bit Z-Buffer (Depth Buffer)
8, // 8 bit stencil buffer
0, // No Auxiliary Buffer (?)
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved (?)
0, 0, 0 // Layer Masks Ignored (?)
};
m_hDC = ::GetDC(m_hWnd); // Gets A Device Context For The Window
PixelFormat = ChoosePixelFormat(m_hDC, &pfd); // Finds The Closest Match To The Pixel Format We Set Above
if (!PixelFormat)
{
::MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
// This Sends A 'Message' Telling The Program To Quit
return false ; // Prevents The Rest Of The Code From Running
}
if(!SetPixelFormat(m_hDC,PixelFormat,&pfd))
{
::MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
m_hRC = wglCreateContext(m_hDC);
if(!m_hRC)
{
::MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
if(!wglMakeCurrent(m_hDC, m_hRC))
{
::MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);
PostQuitMessage(0);
return false;
}
InitGL();
return true;
}
void CPRTView::InitGL()
{
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
Matrix3fSetIdentity(&g_eyeLastRot); // Reset Rotation
Matrix3fSetIdentity(&g_eyeThisRot); // Reset Rotation
Matrix4fSetRotationFromMatrix3f(&g_modelTransform, &g_eyeThisRot); // Reset Rotation
Matrix3fSetIdentity(&g_envLastRot); // Reset Rotation
Matrix3fSetIdentity(&g_envThisRot); // Reset Rotation
Matrix4fSetRotationFromMatrix3f(&g_envTransform, &g_envThisRot); // Reset Rotation
// Setup lighting
GLfloat mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {100.0};
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
ChangeDrawElements();
}
void CPRTView::ChangeDrawElements()
{
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
if(m_bLoadedObj)
{
if(m_bDrawNormalMap)
{
//glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpVertexTexcoords[0]);
glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpVertexNormals[0]);
}
else if(m_bDrawVisRatio)
{
glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->visRatios[0]);
}
else if(m_bDrawSubsurfVisratio)
{
glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpSubsurfVisRatio[m_svrKeyDownIdx * (m_accessObj.m_pModel->nVertices + 1)]);
}
else if(m_bDrawDm && m_bLoadedDm)
{
glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpDm[0]);
}
else if(m_bDrawMS || m_bDrawSS)
{
glColorPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpColors[0]);
}
else
{
glDisableClientState(GL_COLOR_ARRAY);
glColor3f(1,1,1);
}
if(_Tex && (!m_accessObj.m_pModel->vpVertexTexcoords.empty()))
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDeleteTextures(1, &m_meshTex);
glGenTextures(1, &m_meshTex);
glBindTexture(GL_TEXTURE_2D, m_meshTex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_DECAL);// GL_MODULATE);//
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_texW, m_texH, 0, GL_RGBA, GL_UNSIGNED_BYTE, &m_textureImage[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(3, GL_FLOAT, 0, &m_accessObj.m_pModel->vpVertexTexcoords[0]);
}
else
{
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glVertexPointer(3, GL_FLOAT,0, &m_accessObj.m_pModel->vpVertices[0]);
glNormalPointer(GL_FLOAT, 0, &m_accessObj.m_pModel->vpVertexNormals[0]);
}
}
void CPRTView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
m_nHeight= cy;
m_nWidth = cx;
g_envArcBall.setBounds((GLfloat)m_nWidth, (GLfloat)m_nHeight);
g_eyeArcBall.setBounds((GLfloat)m_nWidth, (GLfloat)m_nHeight);
}
void CPRTView::OnFileOpen()
{
char szFilter[] = "obj Files (*.obj)|*.obj|obj Files (*.obj)|*.obj|All Files (*.*)|*.*||";
char workdir[128];
CFileDialog openDlg(TRUE,NULL,
NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilter,
this);
CString szFilepath;
_getcwd( workdir, 128);
if(openDlg.DoModal() == IDOK)
{
BeginWaitCursor();
m_bLoadedObj = false;
m_bLoadedVisibs =false;
m_bLoadedRd = false;
m_bLoadedTdCoefs = false;
m_bDrawVisRatio = false;
m_bDrawNormalMap = false;
m_bDrawMS = false;
m_bDrawSS = false;
m_bLoadedT1Coefs = false;
m_bMSUncomp = false;
m_bDrawSubsurfVisratio = false;
m_bLoadedSubsurfVisRatio = false;
m_bLoadedDm = false;
m_bDrawDm = false;
m_bDrawLight = false;
if(m_fTd0)
{
fclose(m_fTd0);
m_fTd0 = NULL;
}
if(m_fE0)
{
fclose(m_fE0);
m_fE0 = NULL;
}
m_bLoadedE0T0 = false;
szFilepath = openDlg.GetPathName();
if(m_accessObj.LoadOBJ((char*)(LPCSTR)szFilepath))
{
m_accessObj.CenterScaleObj(m_objBboxSize);
m_accessObj.ComputeNormals();
m_accessObj.VertTexcoords();
//tex
for (UINT i = 0; i < m_accessObj.m_pModel->nMaterials; i ++)
{
if(m_accessObj.m_pModel->pMaterials[i].bTextured)
{
m_texW = m_accessObj.m_pModel->pMaterials[i].nWidth;
m_texH = m_accessObj.m_pModel->pMaterials[i].nHeight;
m_textureImage.clear();
m_textureImage.resize(m_texW * m_texH * 4);
unsigned char* pTex = &m_textureImage[0];
unsigned char* pImg = m_accessObj.m_pModel->pMaterials[i].pTexImage;
for(int n = 0; n < m_texW * m_texH * 3; ++n)
{
*pTex = *pImg;
if(n % 3 == 2)
{
++pTex;
*pTex = 255;
}
++pTex;
++pImg;
}
}
}
//indices
m_accessObj.m_pModel->TrianglesIndices.resize(m_accessObj.m_pModel->nTriangles * 3);
for(unsigned int i = 0; i < m_accessObj.m_pModel->nTriangles; i++)
{
for(int j = 0; j < 3; ++j)
m_accessObj.m_pModel->TrianglesIndices[i * 3 + j] = m_accessObj.m_pModel->pTriangles[i].vindices[j];
}
m_accessObj.m_pModel->nVisibRes = m_cubeRes;
m_accessObj.m_pModel->nECoefNum = m_ECoefNum;
m_accessObj.m_pModel->nTdCoefNum = m_TdCoefNum;
m_accessObj.m_pModel->nT1CoefNum = m_T1CoefNum;
m_accessObj.m_pModel->nSubsurfSampleNum = m_SSSampleNum;
m_accessObj.m_pModel->nPhaseSVDterm = m_phaseSVDterm;
m_filenameNoSuffix = szFilepath.Left((int)strlen(szFilepath) - 4);
//////////////////////////////////////////////////////////////////////////MS
//CString m_visibFilename = m_filenameNoSuffix + ".vsb";
//if(m_accessObj.LoadVisibs((char*)(LPCSTR)m_visibFilename))
//{
// m_bLoadedVisibs = true;
//}
//CString eName = m_filenameNoSuffix + ".E";
//if(m_accessObj.LoadECoefs((char*)(LPCSTR)eName))
//{
// m_bLoadedECoefs = true;
//}
//CString TdName = m_filenameNoSuffix + ".Td2";
//if(m_accessObj.LoadTd2Coefs((char*)(LPCSTR)TdName))
//{
// m_bLoadedTdCoefs = true;
//}
//if(m_bLoadedTdCoefs && m_bLoadedCubeMap)
// m_bDrawMS = true;
//////////////////////////////////////////////////////////////////////////SS
//CString subsurfVisRatioName = m_filenameNoSuffix + ".svr";
//if(m_accessObj.LoadSubsurfVisRatio((char*)(LPCSTR)subsurfVisRatioName))
//{
// m_bLoadedSubsurfVisRatio = true;
//}
CString fnDm = m_filenameNoSuffix + ".Dm";
if(m_accessObj.LoadDm((char*)(LPCSTR)fnDm))
{
m_bLoadedDm = true;
}
CString T1Cname = m_filenameNoSuffix + ".T1C";
if(m_accessObj.LoadT1Coefs((char*)(LPCSTR)T1Cname))
{
m_bLoadedT1Coefs = true;
}
if(m_bLoadedT1Coefs && m_bLoadedCubeMap)
m_bDrawSS = true;
const int nHGsize = 6 * m_HGres * m_HGres;
m_pH.resize(m_phaseSVDterm * nHGsize, 1.f);
if(!_HG1)
{
//HG16g-25t32.hgH
CString fHgHn;
if(fabs(m_g - 0.25f) < Epsilon)
fHgHn = "HG16g25t32.hgH";
else if(fabs(m_g - (-0.25f)) < Epsilon)
fHgHn = "HG16g-25t32.hgH";
else if(fabs(m_g - (-0.4f)) < Epsilon)
fHgHn = "HG16g-40t32.hgH";
else if(fabs(m_g - 0.4f) < Epsilon)
fHgHn = "HG16g40t32.hgH";
else if(fabs(m_g - 0.9f) < Epsilon)
fHgHn = "HG16g90t32.hgH";
else if(fabs(m_g - (-0.90f)) < Epsilon)
fHgHn = "HG16g-90t32.hgH";
else if(fabs(m_g - 0) < Epsilon)
fHgHn = "HG16g0t32.hgH";
FILE* fHgH;
if((fHgH = fopen(((char*)(LPCSTR)fHgHn), "rb")) == NULL)
{
printf("failed open reading .hgH.\n ");
return;
}
size_t n = fread(&m_pH[0], sizeof(float), m_phaseSVDterm * nHGsize, fHgH);
assert(n == (size_t)(nHGsize * m_phaseSVDterm));
fclose(fHgH);
}
//////////////////////////////////////////////////////////////////////////
m_accessObj.m_pModel->vpTransedRadiance.clear();
m_accessObj.m_pModel->vpTransedRadiance.resize(m_accessObj.m_pModel->nVertices);
m_accessObj.m_pModel->vpColors.clear();
m_accessObj.m_pModel->vpColors.resize(m_accessObj.m_pModel->nVertices + 1);
m_bLoadedObj = true;
}
EndWaitCursor();
_chdir(workdir);
InitGL();
}
InvalidateRect(NULL,FALSE);
}
void CPRTView::OnPaint()
{
CPaintDC dc(this); // device context for painting
DrawScene();
}
void CPRTView::DrawScene()
{
wglMakeCurrent(m_hDC,m_hRC);
glDrawBuffer( GL_BACK );
glClearColor(0, 0, 0, 1);;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(!m_bLoadedObj)
{
glFlush();
SwapBuffers(m_hDC);
return;
}
assert((m_accessObj.m_pModel->nVertices != 0) && (m_accessObj.m_pModel->nTriangles !=0));
glViewport(0, 0, m_nWidth, m_nHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(m_dFOV, (GLfloat)m_nWidth / (GLfloat)m_nHeight, 0.1f, 2000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(m_eye.x, m_eye.y, m_eye.z,
0.0, 0.0, 0.0,
0,1,0);
//view
glPushMatrix();
glMultMatrixf(g_modelTransform.M);
//if(m_nVBOVertices == 1)
//{
// glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices);
// glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The Vertex Pointer To The Vertex Buffer
// glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOColors);
// glColorPointer( 3, GL_FLOAT, 0, (char *) NULL ); // Set The TexCoord Pointer To The TexCoord Buffer
////}
if(m_bLoadedCubeMap && _Tex && (m_meshTex > 0))
glBindTexture( GL_TEXTURE_2D, m_meshTex);
glDrawElements(GL_TRIANGLES, 3 * m_accessObj.m_pModel->nTriangles, GL_UNSIGNED_INT, &m_accessObj.m_pModel->TrianglesIndices[0]);
if((m_bDrawNormal || m_bDrawDm ) && (!m_accessObj.m_pModel->vpVertexNormals.empty()))
{
glColor3f(1,1,1);
glBegin(GL_LINES);
for(unsigned int i = 1; i <= m_accessObj.m_pModel->nVertices; i++)
{
Vec3* pVert = &m_accessObj.m_pModel->vpVertices[i];
Vec3* pNormal = &m_accessObj.m_pModel->vpVertexNormals[i];
glVertex3f(pVert->x, pVert->y, pVert->z);
if(m_bLoadedDm && m_bDrawDm)
{
float Dm = m_accessObj.m_pModel->vpDm[i].x;
glVertex3f(pVert->x + Dm * pNormal->x, pVert->y + Dm * pNormal->y, pVert->z + Dm * pNormal->z);
}
else
glVertex3f(pVert->x + 0.2f * pNormal->x, pVert->y + 0.2f * pNormal->y, pVert->z + 0.2f * pNormal->z);
}
glEnd();
}
//env
glPushMatrix();
glMultMatrixf(g_envTransform.M);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, (GLfloat)m_nWidth / (GLfloat)m_nHeight, 0.1f, 2000.0f);
if(m_bLoadedCubeMap)
{
glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, m_cubeTex);
DrawCube(1000);//cube size
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopMatrix();
GLenum err = glGetError();
m_errorString = (GLubyte*)gluErrorString(err);
if(err != 0)
printf("%s", m_errorString);
//// Get FPS
//if( GetTickCount() - m_dwLastFPS >= 1000 ) // When A Second Has Passed...
//{
// m_nFPS = m_nFrames * 1000 / (GetTickCount() - m_dwLastFPS); // Save The FPS
// m_dwLastFPS = GetTickCount(); // Update Our Time Variable
// m_nFrames = 0; // Reset The FPS Counter
// //printf("%d Vertices, %d FPS\n", m_accessObj.m_pModel->nVertices, m_nFPS );
// printf("%d FPS\n", m_nFPS);
//}
//m_nFrames++; // Increment Our FPS Counter
glFlush();
SwapBuffers(m_hDC);
}
void CPRTView::OnLButtonDown(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_bLBDown = true;
g_eyeLastRot = g_eyeThisRot;
Point2fT MousePt = {(GLfloat)point.x, (GLfloat)point.y};
g_eyeArcBall.click(&MousePt);
CView::OnLButtonDown(nFlags, point);
}
void CPRTView::OnLButtonUp(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_bLBDown = false;
CView::OnLButtonUp(nFlags, point);
}
void CPRTView::OnRButtonDown(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_bRBDown = true;
g_envLastRot = g_envThisRot;
Point2fT MousePt = {(GLfloat)point.x, (GLfloat)point.y};
g_envArcBall.click(&MousePt);
CView::OnRButtonDown(nFlags, point);
}
void CPRTView::OnRButtonUp(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_bRBDown = false;
CView::OnRButtonUp(nFlags, point);
}
void CPRTView::OnMButtonDown(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_oldY = point.y;
m_bMBDown = true;
}
void CPRTView::OnMButtonUp(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
m_oldFOV = m_dFOV;
m_bMBDown = false;
CView::OnMButtonUp(nFlags, point);
}
void CPRTView::OnMouseMove(UINT nFlags, CPoint point)
{
if(!m_bLoadedObj)
return;
Point2fT MousePt = {(GLfloat)point.x, (GLfloat)point.y};
Quat4fT ThisQuat = {0};
if(m_bMBDown)
{
float scale = 1.0f + (float)(m_oldY - point.y) /(float) m_nHeight;
if(scale > 0.001)
m_dFOV = m_oldFOV / scale;
std::cout<<"m_dFOV: "<<m_dFOV<<endl;
}
if(m_bLBDown)//change view, no need to recalc TransedRadiance
{
g_eyeArcBall.drag(&MousePt, &ThisQuat); // Update End v And Get Rotation As Quaternion
Matrix3fSetRotationFromQuat4f(&g_eyeThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT
Matrix3fMulMatrix3f(&g_eyeThisRot, &g_eyeLastRot); // Accumulate Last Rotation Into This One
Matrix4fSetRotationFromMatrix3f(&g_modelTransform, &g_eyeThisRot); // Set Our Final Transform's Rotation From This One
CalcColor();
}
if(m_bRBDown)//change env light
{
g_envArcBall.drag(&MousePt, &ThisQuat); // Update End v And Get Rotation As Quaternion
Matrix3fSetRotationFromQuat4f(&g_envThisRot, &ThisQuat); // Convert Quaternion Into Matrix3fT
Matrix3fMulMatrix3f(&g_envThisRot, &g_envLastRot); // Accumulate Last Rotation Into This One
Matrix4fSetRotationFromMatrix3f(&g_envTransform, &g_envThisRot); // Set Our Final Transform's Rotation From This One
TransedRadiance();
CalcColor();
}
Invalidate(); //calls OnPaint() calls DrawScene()
CView::OnMouseMove(nFlags, point);
}
BOOL CPRTView::OnEraseBkgnd(CDC* pDC)
{
return FALSE;
}
void CPRTView::OnInitGL()
{
InitGL();
}
void CPRTView::OnDrawNormal()
{
m_bDrawNormal = !m_bDrawNormal;
}
void CPRTView::OnFillMode()
{
m_fillMode = (m_fillMode + 1) % 3;
if(m_fillMode == 0)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else if(m_fillMode == 1)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else if(m_fillMode == 2)
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
}
void CPRTView::OnComputeSaveVisibs()
{
if((!m_bLoadedObj) || m_bLoadedVisibs ) //normal for vert Outrude
{
printf("!m_bLoadedObj || (m_bLoadedVisibs)!");
return;
}
CString m_visibFilename = m_filenameNoSuffix + ".vsb";
FILE* f;
if((f = fopen(((char*)(LPCSTR)m_visibFilename), "wb")) == NULL)
{
printf("failed open m_visibFilename \n ");
return;
}
assert(m_accessObj.m_pModel);
//###
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT,0, m_accessObj.m_pModel->vpVertices);
//
#ifdef StencilVis
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
glViewport(0, 0, m_cubeRes, m_cubeRes);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glClearStencil(0x0);
glStencilFunc(GL_ALWAYS, 0x1, 0x1);
glStencilOp (GL_INCR, GL_INCR, GL_INCR);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90, 1, 0.001f, m_objBboxSize * 2); //near:intrude[0] is minimal distance that should be covered in [near,far].
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
#else
glViewport(0, 0, m_cubeRes, m_cubeRes);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Reset The Projection Matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90, 1, 0.01f, m_objBboxSize * 2); //300 should be m_bboxsize*sqrtf(3)
#endif
m_accessObj.m_pModel->vpVertInvisibs.clear();
m_accessObj.m_pModel->vpVertInvisibs.resize(m_accessObj.m_pModel->nVertices * n6k);
m_accessObj.m_pModel->visRatios.clear();
m_accessObj.m_pModel->visRatios.resize(m_accessObj.m_pModel->nVertices + 1);
Vec3 VertOutrude = Vec3(0,0,0);
float outrude = 0.0001f;
Vec3 Zero3 = Vec3(0,0,0);
int nVisibsPerSide = m_cubeRes * m_cubeRes;
for(unsigned int i = 0; i < m_accessObj.m_pModel->nVertices; i++)
{
Vec3* pVert = &m_accessObj.m_pModel->vpVertices[i + 1]; //obj index starts from 1
Vec3* pNorm = &m_accessObj.m_pModel->vpVertexNormals[i + 1];
VertOutrude = *pNorm;
VertOutrude = VertOutrude * outrude;
VertOutrude = VertOutrude + *pVert;
unsigned int nVisibleLightSum = 0;
for(int side = 0; side < 6; side++)
{
nVisibleLightSum += VisibleLightSide(&m_accessObj.m_pModel->vpVertInvisibs[i * n6k + side * nVisibsPerSide], &VertOutrude, side);
}
float ratio = (float)nVisibleLightSum / (float)(m_cubeRes * m_cubeRes * 6);
m_accessObj.m_pModel->visRatios[i + 1] = Vec3(ratio, ratio, ratio);
//printf("vert%d's vis ratio is %f\t", i + 1, m_accessObj.m_pModel->visRatios[i + 1].x);
size_t n = fwrite(&m_accessObj.m_pModel->vpVertInvisibs[i * n6k], sizeof(byte), n6k, f);
assert(n == (size_t)n6k);
if(i%100 == 0)
printf("visib of vert %d of %d vertices finished. \n", i, m_accessObj.m_pModel->nVertices);
}
//untested!! flip side0's memory s to res-1-s.
for(unsigned int vIdx = 0; vIdx < m_accessObj.m_pModel->nVertices; ++vIdx)
{