-
Notifications
You must be signed in to change notification settings - Fork 9
/
CCubeGenApp.cpp
2026 lines (1636 loc) · 68.5 KB
/
CCubeGenApp.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
//--------------------------------------------------------------------------------------
// File: CCubeGenApp.cpp
//
// CubeGen application class and member functions
//
//--------------------------------------------------------------------------------------
// (C) 2005 ATI Research, Inc., All rights reserved.
//--------------------------------------------------------------------------------------
#include "CCubeGenApp.h"
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
CCubeGenApp::CCubeGenApp(void)
{
m_bInitialized = FALSE;
m_bErrorOccurred = FALSE;
//no filtering so output cubemap not valid yet
m_bValidOutputCubemap = FALSE;
//default settings
m_FramesSinceLastRefresh = 0;
m_bOutputPeriodicRefresh = TRUE;
m_InputCubeMapSize = 128;
m_InputCubeMapFormat = D3DFMT_A8R8G8B8;
m_OutputCubeMapSize = 128;
//m_OutputCubeMapFormat = D3DFMT_A32B32G32R32F;
//m_OutputCubeMapFormat = D3DFMT_A16B16G16R16;
m_OutputCubeMapFormat = D3DFMT_A8R8G8B8;
//default display options
m_RenderTechnique = CG_RENDER_NORMAL;
m_DisplayCubeSource = CG_CUBE_DISPLAY_INPUT;
m_bMipLevelSelectEnable = TRUE;
m_MipLevelDisplayed = 0;
m_bShowAlpha = FALSE;
// SL BEGIN
m_bFixSeams = FALSE;
// SL END
m_bDrawSkySphere = FALSE;
//clamp max mip level using sampler state MaxMipLevel
m_bMipLevelClampEnable = FALSE;
// SL BEGIN
m_bUseMultithread = TRUE;
// SL END
//write mip level to alpha so the cube maps mip level can be determined by ps.2.0 or ps.2.b shaders
m_bWriteMipLevelIntoAlpha = TRUE;
//selected cube face to load/manipulate
m_SelectedCubeFace = CG_CUBE_SELECT_FACE_XPOS;
//default filtering options
// SL BEGIN
m_FilterTech = CP_FILTER_TYPE_COSINE_POWER;
// SL END
m_BaseFilterAngle = 0.0f;
m_MipInitialFilterAngle = 1.0f;
m_MipFilterAngleScale = 2.0f;
m_bUseSolidAngleWeighting = TRUE;
// SL BEGIN
m_SpecularPower = 2048.0f; // Default to 2048 because it is a fast computation when you swtich to cosinus power filter
m_CosinePowerDropPerMip = 0.25;
m_NumMipmap = 0;
m_CosinePowerMipmapChainMode = CP_COSINEPOWER_CHAIN_DROP;
m_bExcludeBase = FALSE;
m_bIrradianceCubemap = FALSE;
m_LightingModel = FALSE;
m_GlossScale = 10.0f;
m_GlossBias = 1.0f;
// SL END
// SL BEGIN
m_EdgeFixupTech = CP_FIXUP_WARP;
// SL END
m_bCubeEdgeFixup = TRUE;
m_EdgeFixupWidth = 1;
m_bWriteMipLevelIntoAlpha = FALSE;
m_bFlipFaceSurfacesOnImport = FALSE;
m_bFlipFaceSurfacesOnExport = FALSE;
m_bExportMipChain = TRUE; //Default export mipchain is true
m_InputScaleFactor = 1.0f;
m_InputDegamma = 1.0f;
m_InputMaxClamp = CG_DEFAULT_MAX_CLAMP;
m_OutputScaleFactor = 1.0f;
m_OutputGamma = 1.0f;
//initial scene filename
wcscpy_s( m_SceneFilename, CG_MAX_FILENAME_LENGTH, L"" );
//initial scene filename
wcscpy_s( m_BaseMapFilename, CG_MAX_FILENAME_LENGTH, L"" );
// wcscpy(m_SceneFilename, L"");
//initial input cubemap filename blank, so no loading cubemaps
wcscpy_s( m_InputCubeMapFilename, CG_MAX_FILENAME_LENGTH, L"" );
//initial output cubemap filename
wcscpy_s( m_OutputCubeMapFilename, CG_MAX_FILENAME_LENGTH, CG_INITIAL_OUTPUT_CUBEMAP_FILENAME );
}
//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
CCubeGenApp::~CCubeGenApp()
{
m_CubeMapProcessor.Clear();
OnDestroyDevice();
}
//--------------------------------------------------------------------------------------
//Initalizes ShadowMap App
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::Init(IDirect3DDevice9 *a_pDevice)
{
HRESULT hr;
//set device
m_pDevice = a_pDevice;
//get caps
m_pDevice->GetDeviceCaps(&m_DeviceCaps);
TextureListSetDevice(m_pDevice);
V_RETURN( InitShaders() );
V_RETURN( LoadBaseMap() );
V_RETURN( LoadInputCubeMap() );
V_RETURN( CreateOutputCubeMap(0) );
LoadObjects();
m_bInitialized = TRUE;
//invalidate current output cubemap
m_bValidOutputCubemap = FALSE;
return S_OK;
}
//--------------------------------------------------------------------------------------
//init shaders
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::InitShaders(void)
{
HRESULT hr;
hr = m_pEffect.LoadShader( CG_EFFECT_FILENAME, m_pDevice);
if(FAILED(hr))
{
//if compile error, output compile error message
if(hr != STG_E_FILENOTFOUND)
{
OutputMessage(L"Error creating Effect, Loading effects file from resource. \n\n %s", m_pEffect.m_ErrorMsg);
}
hr = m_pEffect.LoadShaderFromResource( CG_EFFECT_RESOURCE_ID, m_pDevice);
OutputFatalMessageOnFail(hr, m_pEffect.m_ErrorMsg );
}
return hr;
}
//--------------------------------------------------------------------------------------
//load basemap to apply to texture
//
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::LoadBaseMap(void)
{
if( wcscmp(m_BaseMapFilename, L"") == 0)
{ //if empty filename, create 64x64 white basemap
OutputFatalMessageOnFail(m_pBaseMap.CreateTexture(64, 64, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED),
L"Exiting, cannot create default basemap.");
m_pBaseMap.FillTexture( D3DCOLOR_RGBA(255, 255, 255, 255 ) );
//m_pBaseMap.LoadTextureFromResource(IDR_UITEXTURE );
}
else
{
//else attempt to load basemap
if(FAILED(m_pBaseMap.LoadTexture(m_BaseMapFilename)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot Load Texture %s", m_BaseMapFilename);
wcscpy_s( m_BaseMapFilename, CG_MAX_FILENAME_LENGTH, L"" );
return(LoadBaseMap());
}
}
return S_OK;
}
//--------------------------------------------------------------------------------------
//Loads entire cubemap + mipchain for the Cubemap
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::LoadInputCubeMap(void)
{
if( wcscmp(m_InputCubeMapFilename, L"") == 0)
{
D3DCOLOR faceColors[6] = {
D3DCOLOR_RGBA(255, 0, 0, 255 ), //red
D3DCOLOR_RGBA(0, 255, 255, 255 ), //cyan
D3DCOLOR_RGBA(0, 255, 0, 255 ), //green
D3DCOLOR_RGBA(255, 0, 255, 255 ), //magenta
D3DCOLOR_RGBA(0, 0, 255, 255 ), //blue
D3DCOLOR_RGBA(255, 255, 0, 255 ) //yellow
};
//if no filename, create blank cubemap
OutputFatalMessageOnFail(
m_pInputCubeMap.CreateCubeTexture(m_InputCubeMapSize, 0, 0,
m_InputCubeMapFormat, D3DPOOL_MANAGED ),
L"Fatal Error: cannot create cubemap of size %d of format %s", m_InputCubeMapSize,
DXUTD3DFormatToString(m_InputCubeMapFormat, false) );
m_pInputCubeMap.FillCubeTexture(faceColors);
}
else
{
//else load cubemap from disk
if(FAILED(m_pInputCubeMap.LoadCubeTexture(m_InputCubeMapFilename)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot Load Texture %s", m_InputCubeMapFilename);
wcscpy_s( m_InputCubeMapFilename, CG_MAX_FILENAME_LENGTH, L"" );
return(LoadInputCubeMap());
}
}
m_InputCubeMapSize = m_pInputCubeMap.m_Width;
m_InputCubeMapFormat = m_pInputCubeMap.m_Format;
//use cube map processor to flip faces surfaces horizonally on import (if needed)
/*
if(m_bFlipFaceSurfacesOnImport)
{
CCubeMapProcessor cmProc;
//generate full mip chain and 4 color channels
cmProc.Init(m_InputCubeMapSize, m_InputCubeMapSize, 0, 4);
//load cube map processor with input cube map
SetCubeMapProcessorInputCubeMap(&cmProc);
//flip faces
cmProc.FlipInputCubemapFaces();
//retrieve cube map from cube map processor
GetCubeMapProcessorInputCubeMap(&cmProc);
cmProc.Clear();
}
*/
//invalidate current output cubemap
m_bValidOutputCubemap = FALSE;
return S_OK;
}
//--------------------------------------------------------------------------------------
// LoadSelectedCubeMapFace
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::LoadSelectedCubeMapFace(WCHAR *aCubeFaceFilename)
{
LPDIRECT3DSURFACE9 pD3DSrcSurface, pD3DDstSurface;
CTexture tempTex;
if(m_SelectedCubeFace == CG_CUBE_SELECT_NONE)
{//no cube face selected, do nothing
return S_OK;
}
//attempt to load image
if(FAILED(tempTex.LoadTexture(aCubeFaceFilename)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot Load Image %s", aCubeFaceFilename);
return E_FAIL;
}
//V_RETURN( tempTex.LoadTexture(aCubeFaceFilename) );
if( tempTex.m_Width != tempTex.m_Height )
{
m_bErrorOccurred = TRUE;
//texture width and height not equal, cant load into cube face
OutputMessage(L"Error: Cubemap face must be square and power of 2 in size, but image contained in %s is %dx%d",
aCubeFaceFilename, tempTex.m_Width, tempTex.m_Height);
return E_FAIL;
}
D3DFORMAT derivedFormat;
int32 derivedSize;
derivedFormat = tempTex.m_Format;
derivedSize = tempTex.m_Width;
// make sure the face is the same dimension as the current cubemap
if ( derivedSize != m_InputCubeMapSize || derivedFormat != m_InputCubeMapFormat )
{
if ( !OutputQuestion( L"The cube map face being loaded has dimensions %dx%d with format %s,\n but the current input cubemap is %dx%d and format %s.\n\n Would you like to switch the size and format to that of the new texture?",
tempTex.m_Width, tempTex.m_Height, DXUTD3DFormatToString( tempTex.m_Format, false ), m_InputCubeMapSize, m_InputCubeMapSize, DXUTD3DFormatToString( m_InputCubeMapFormat, false ) ) )
{
derivedSize = m_pInputCubeMap.m_Width;
derivedFormat = m_InputCubeMapFormat;
}
}
//if input cubemap is not same width or format, re-create input cubemap of same format
if((m_pInputCubeMap.m_Width != derivedSize) || (m_pInputCubeMap.m_Format != derivedFormat) )
{
//Create cube texture: automatically releases old texture if successful
if(FAILED(m_pInputCubeMap.CreateCubeTexture(derivedSize, 0, 0, derivedFormat, D3DPOOL_MANAGED )))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot create cubemap of size %d and of format %s", derivedSize,
DXUTD3DFormatToString(derivedFormat, false) );
return E_FAIL;
}
m_InputCubeMapSize = m_pInputCubeMap.m_Width;
m_InputCubeMapFormat = m_pInputCubeMap.m_Format;
}
//copy image to cube map surface
m_pInputCubeMap.m_pCubeTexture->GetCubeMapSurface( (D3DCUBEMAP_FACES) m_SelectedCubeFace, 0, &pD3DDstSurface);
tempTex.m_pTexture->GetSurfaceLevel(0, &pD3DSrcSurface);
//copy one surface to the next
HRESULT hRes = D3DXLoadSurfaceFromSurface(pD3DDstSurface, NULL, NULL, pD3DSrcSurface, NULL, NULL, D3DX_FILTER_NONE, 0);
//release old surfaces
SAFE_RELEASE(pD3DSrcSurface);
SAFE_RELEASE(pD3DDstSurface);
if ( hRes == D3D_OK )
{
//generate mip sublevels
D3DXFilterTexture(m_pInputCubeMap.m_pCubeTexture, NULL, 0, D3DTEXF_LINEAR);
//invalidate current output cubemap
m_bValidOutputCubemap = FALSE;
return S_OK;
}
else
{
return hRes;
}
}
//--------------------------------------------------------------------------------------
// load cubemap cross in HDR shop cube cross format
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::LoadInputCubeCross(WCHAR *aFilename)
{
uint32 iCubeFace;
LPDIRECT3DSURFACE9 pD3DSrcSurface, pD3DDstSurface;
int32 cubeMapSize;
D3DXIMAGE_INFO imgInfo;
CImageSurface cImg; //image surface proxy used to copy face image data out of tempTex into the cube faces
// also used to flip Z- face vertically and horizonally
//u, v offsets of each face image in the cross, in units of faces (cross is layed out in a 3x4 grid)
// ___
// |Y+ |
// ___|___|___
// |X- |Z+ |X+ |
// |___|___|___|
// |Y- |
// |___|
// |Z- |
// |___|
//
// note Z- face needs to be flipped vertically and horizonally to import into D3D
int32 crossOffsets[6][2] = {
{2, 1}, //X+ face
{0, 1}, //X- face
{1, 0}, //Y+ face
{1, 2}, //Y- face
{1, 1}, //Z+ face
{1, 3}, //Z- face
};
//load cross into temporary texture
if( FAILED( D3DXGetImageInfoFromFile(aFilename, &imgInfo) ) )
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot retrieve image information from file %s. " //no comma, string continues on next line
L"File is either invalid, non-existant, or of a nonsupported format",
aFilename);
return E_FAIL;
}
//make sure cross image is 3:4 ratio
if( (imgInfo.Width / 3) != (imgInfo.Height / 4) )
{
m_bErrorOccurred = TRUE;
//texture width/height ratio must be 3/4 for cube map cross
OutputMessage(L"Error: texture width:height ratio must be 3:4 for cube map cross." //no comma, string continues on next line
L" Image Contained in %s is %dx%d.", aFilename, imgInfo.Width, imgInfo.Height );
return E_FAIL;
}
//create temp surface to hold image to read other surfaces off of
if(FAILED( m_pDevice->CreateOffscreenPlainSurface(imgInfo.Width, imgInfo.Height,
imgInfo.Format, D3DPOOL_SCRATCH, &pD3DSrcSurface, NULL)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot create offscreen, system memory buffer of size %dx%d and format %s",
imgInfo.Width, imgInfo.Height, DXUTD3DFormatToString(imgInfo.Format, false) );
return E_FAIL;
}
if(FAILED( D3DXLoadSurfaceFromFile(pD3DSrcSurface, NULL, NULL, aFilename, NULL, D3DX_FILTER_NONE, 0, &imgInfo) ) )
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot load image file %s. " //no comma, string continues on next line
L"File is either invalid, non-existant, or of an unsupported format",
aFilename);
SAFE_RELEASE(pD3DSrcSurface);
return E_FAIL;
}
cubeMapSize = imgInfo.Width / 3;
//Create input new cube texture (automatically releases old texture)
if(FAILED(m_pInputCubeMap.CreateCubeTexture(cubeMapSize, 0, 0, imgInfo.Format, D3DPOOL_MANAGED )))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot create input cubemap of size %d and format %s",
cubeMapSize, DXUTD3DFormatToString(imgInfo.Format, false) );
SAFE_RELEASE(pD3DSrcSurface);
return E_FAIL;
}
m_InputCubeMapFormat = m_pInputCubeMap.m_Format;
m_InputCubeMapSize = cubeMapSize;
//Create CImageSurface to copy image data through
cImg.Init(cubeMapSize, cubeMapSize, 4);
//iterate over cube faces
for(iCubeFace=0; iCubeFace<6; iCubeFace++)
{
RECT faceRect;
faceRect.left = crossOffsets[iCubeFace][0] * cubeMapSize;
faceRect.right = (crossOffsets[iCubeFace][0] + 1) * cubeMapSize;
faceRect.top = crossOffsets[iCubeFace][1] * cubeMapSize;
faceRect.bottom = (crossOffsets[iCubeFace][1] + 1) * cubeMapSize;
//copy image data from surface into CSurfaceImage
InitCSurfaceImageUsingD3DSurface(&cImg, 4, pD3DSrcSurface, &faceRect);
//flip Z- Cube Face
if( (D3DCUBEMAP_FACES)iCubeFace == D3DCUBEMAP_FACE_NEGATIVE_Z)
{
cImg.InPlaceHorizonalFlip();
cImg.InPlaceVerticalFlip();
}
//get cube map surface
m_pInputCubeMap.m_pCubeTexture->GetCubeMapSurface((D3DCUBEMAP_FACES)iCubeFace, 0, &pD3DDstSurface);
//copy image data from CSurfaceImage into cube face surface
CopyCSurfaceImageToD3DSurface(&cImg, pD3DDstSurface );
//release cube face surface
SAFE_RELEASE(pD3DDstSurface);
}
SAFE_RELEASE(pD3DSrcSurface);
//generate mip sublevels
D3DXFilterTexture(m_pInputCubeMap.m_pCubeTexture, NULL, 0, D3DTEXF_LINEAR);
//invalidate current output cubemap
m_bValidOutputCubemap = FALSE;
return S_OK;
}
/*
//--------------------------------------------------------------------------------------
//Loads Input Cube map as a series of textures for the app
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::LoadInputCubeMapFromFiles (WCHAR m_InputCubeMapFilenamePrefix(void)
{
HRESULT hr;
WCHAR m_InputCubeMapFilenamePrefix[CG_MAX_FILENAME_LENGTH];
D3DXIMAGE_FILEFORMAT m_InputCubeMapFilenameFileType;
WCHAR m_InputCubeMapFilenamePrefix[CG_MAX_FILENAME_LENGTH];
D3DXIMAGE_FILEFORMAT m_InputCubeMapFilenameFileType;
//load texture
//V_RETURN(m_pInputCubeMap.LoadCubeTexture(m_InputCubeMapFilename));
m_InputCubeMapSize = m_pInputCubeMap.m_Width;
m_InputCubeMapFormat = m_pInputCubeMap.m_Format;
return S_OK;
}
*/
//--------------------------------------------------------------------------------------
//save currently displayed cubemap to disk .DDS (includes cube map and all mip chains)
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::SaveOutputCubeMap(void)
{
//if output cubemap is not invalid prompt user to see if he/she/it wants to copy the
// input cubemap and save it instead
if( HandleInvalidOutputCubemapBeforeSaving() == FALSE)
{
return S_OK; //no save, but thats what the user prompted to do
}
//if no extension, add .dds to file name
if(wcsrchr(m_OutputCubeMapFilename, L'.') == NULL)
{
wcscat_s( m_OutputCubeMapFilename, CG_MAX_FILENAME_LENGTH, L".dds" );
}
if(m_bExportMipChain == TRUE)
{
//re-retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
if(FAILED(D3DXSaveTextureToFile(m_OutputCubeMapFilename,
D3DXIFF_DDS, // D3DXIMAGE_FILEFORMAT
m_pOutputCubeMap.m_pCubeTexture,
NULL)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot save cubemap file %s. " //no comma, string continues on next line
L" Please check free diskspace, write permission, and validity of the filename.",
m_OutputCubeMapFilename);
return E_FAIL;
}
}
else
{ //reallocate output cube map without mip chain
CreateOutputCubeMap(1);
//re-retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
if(FAILED(D3DXSaveTextureToFile(m_OutputCubeMapFilename,
D3DXIFF_DDS, // D3DXIMAGE_FILEFORMAT
m_pOutputCubeMap.m_pCubeTexture,
NULL)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot save cubemap file %s. " //no comma, string continues on next line
L" Please check free diskspace, write permission, and validity of the filename.",
m_OutputCubeMapFilename);
return E_FAIL;
}
//reallocate output cube map with full mipchain
CreateOutputCubeMap(0);
//re-retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
}
return S_OK;
}
//--------------------------------------------------------------------------------------
//save output cubemap surfaces to disk in any D3D compatible formats
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::SaveOutputCubeMapToFiles(WCHAR *aFilenamePrefix, D3DXIMAGE_FILEFORMAT aDestFormat)
{
IDirect3DSurface9 *pCubeMapSurface;
D3DSURFACE_DESC surfDesc;
uint32 i, j;
uint32 lowestMipLevelToSave; //lowest miplevel to save
WCHAR aFullFilename[4096];
WCHAR *fileExtension[]={
{L".bmp"},
{L".jpg"},
{L".tga"},
{L".png"},
{L".dds"},
{L".ppm"},
{L".dib"},
{L".hdr"},
{L".pfm"} };
HRESULT hr;
//if output cubemap is not invalid prompt user to see if he/she/it wants to copy the
// input cubemap and save it instead
if( HandleInvalidOutputCubemapBeforeSaving() == FALSE)
{
return S_OK; //no save, but thats what the user prompted to do
}
//peel off extension
WCHAR *extStart = wcsrchr(aFilenamePrefix, L'.');
if(extStart != NULL)
{
//if portion of string after extension is 4 characters long, then remove it
// otherwise.. keep it
if( wcslen(extStart) == 4 )
{
*extStart = L'\0';
}
}
//given export face layout, manipulate cube map faces to be exported directly from surfaces
switch(m_ExportFaceLayout)
{
case CG_EXPORT_FACE_LAYOUT_D3D:
//no swap needed
break;
case CG_EXPORT_FACE_LAYOUT_SUSHI:
case CG_EXPORT_FACE_LAYOUT_OPENGL:
//flip output faces
m_CubeMapProcessor.FlipOutputCubemapFaces();
break;
default:
break;
}
//re-retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
//whether or not to export the mipchain
if( m_bExportMipChain == TRUE )
{
lowestMipLevelToSave = m_pOutputCubeMap.m_pCubeTexture->GetLevelCount() - 1;
}
else
{
lowestMipLevelToSave = 0;
}
//save filtered cube faces out to individual image files for all miplevels required
for(j=0; j<=lowestMipLevelToSave; j++)
{
for(i=0; i<6; i++)
{
m_pOutputCubeMap.m_pCubeTexture->GetCubeMapSurface((D3DCUBEMAP_FACES)i, j, &pCubeMapSurface);
//add _m?? to filename to specify miplevel if exporting entire mip chain
if( m_bExportMipChain == TRUE )
{
swprintf_s( aFullFilename, CG_MAX_FILENAME_LENGTH, L"%s_m%02d_c%02d%s", aFilenamePrefix, j, i, fileExtension[(int32)aDestFormat] );
}
else
{
swprintf_s( aFullFilename, CG_MAX_FILENAME_LENGTH, L"%s_c%02d%s", aFilenamePrefix, i, fileExtension[(int32)aDestFormat] );
}
pCubeMapSurface->GetDesc(&surfDesc);
// (the cast to uint32 allows comparison of enumerated types)
#ifdef CG_HDR_FILE_SUPPORT
if( ((uint32)aDestFormat == (uint32)D3DXIFF_HDR) && (surfDesc.Width <=4) )
{
//workaround for bug in D3DX libraries in which .hdr files 4 pixels or smaller in width get written
// out incorrectly
CImageSurface surfImg;
//HDR writer requires 3 channel CSurfaceImage
InitCSurfaceImageUsingD3DSurface(&surfImg, 3, pCubeMapSurface, NULL);
//write out .hdr file
surfImg.WriteHDRFile(aFullFilename);
//clear surface image
surfImg.Clear();
}
else
#endif
if( ((uint32)aDestFormat == (uint32)D3DXIFF_BMP) && (surfDesc.Format == D3DFMT_X8R8G8B8) )
{
//workaround for issue in D3DX libraries in which .bmp files get written as a 32-bit BMP
// even for D3DFMT_X8R8G8B8 Instead, we would like to write out a 24-bit BMP
// ( convert to 24-bit RGB internally, then write out the 24-bit file)
IDirect3DSurface9 *pD3DDstSurface;
//create temp R8G8B8 surface to hold output images
if(FAILED( m_pDevice->CreateOffscreenPlainSurface(surfDesc.Width, surfDesc.Height,
D3DFMT_R8G8B8, D3DPOOL_SCRATCH, &pD3DDstSurface, NULL)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot create offscreen, system memory buffer of size %dx%d and format %s",
surfDesc.Width, surfDesc.Height, DXUTD3DFormatToString(D3DFMT_R8G8B8, false) );
return E_FAIL;
}
//copy face into system memory R8G8B8 surface
V( D3DXLoadSurfaceFromSurface(
pD3DDstSurface,
NULL, //no palette
NULL, //use full surface as dst rect
pCubeMapSurface,
NULL, //no palette
NULL, //use full surface src rect
D3DX_FILTER_POINT,
NULL //no color key
) );
//save out R8G8B8 surface as 24-bit bmp
if(FAILED(D3DXSaveSurfaceToFile(aFullFilename, aDestFormat, pD3DDstSurface, NULL, NULL )))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot save cubemap image file %s. " //no comma, string continues on next line
L" Please check free diskspace, write permissions, and validity of the filename.",
aFullFilename);
return E_FAIL;
}
//release created surface
SAFE_RELEASE(pD3DDstSurface);
}
else
{
if(FAILED(D3DXSaveSurfaceToFile(aFullFilename, aDestFormat, pCubeMapSurface, NULL, NULL )))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot save cubemap image file %s. " //no comma, string continues on next line
L" Please check free diskspace, write permissions, and validity of the filename.",
aFullFilename);
return E_FAIL;
}
}
SAFE_RELEASE(pCubeMapSurface);
}
}
//given export face layout, re-flip faces to normal orientation
switch(m_ExportFaceLayout)
{
case CG_EXPORT_FACE_LAYOUT_D3D:
//no swap needed
break;
case CG_EXPORT_FACE_LAYOUT_SUSHI:
case CG_EXPORT_FACE_LAYOUT_OPENGL:
//save old cube face
//flip output faces
m_CubeMapProcessor.FlipOutputCubemapFaces();
break;
default:
break;
}
//re-retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
return S_OK;
}
//--------------------------------------------------------------------------------------
//save each miplevel of the output cube map to HDRShop layout cube crosses
//--------------------------------------------------------------------------------------
HRESULT CCubeGenApp::SaveOutputCubeMapToCrosses(WCHAR *aFilenamePrefix, D3DXIMAGE_FILEFORMAT aDestFormat)
{
HRESULT hr;
uint32 iCubeFace, j;
LPDIRECT3DSURFACE9 pD3DSrcSurface, pD3DDstSurface;
//Output width, height, and format
D3DSURFACE_DESC surfDesc;
int32 crossImageWidth;
int32 crossImageHeight;
uint32 lowestMipLevelToSave; //lowest miplevel to save
CImageSurface cImg; //image surface proxy used to copy face image data out of tempTex into
// the cube faces also used to flip Z- face vertically and horizonally
WCHAR aFullFilename[4096];
WCHAR *fileExtension[]={
{L".bmp"},
{L".jpg"},
{L".tga"},
{L".png"},
{L".dds"},
{L".ppm"},
{L".dib"},
{L".hdr"},
{L".pfm"} };
//if output cubemap is not invalid prompt user to see if he/she/it wants to copy the
// input cubemap and save it instead
if( HandleInvalidOutputCubemapBeforeSaving() == FALSE)
{
return S_OK; //no save, but thats what the user prompted to do
}
//u, v offsets of each face image in the cross, in units of faces (cross is layed out in a 3x4 grid)
// ___
// |Y+ |
// ___|___|___
// |X- |Z+ |X+ |
// |___|___|___|
// |Y- |
// |___|
// |Z- |
// |___|
//
// note Z- face needs to be flipped vertically and horizonally from D3D cube face layout to cubecross layout
int32 crossOffsets[6][2] = {
{2, 1}, //X+ face
{0, 1}, //X- face
{1, 0}, //Y+ face
{1, 2}, //Y- face
{1, 1}, //Z+ face
{1, 3}, //Z- face
};
//peel off extension
WCHAR *extStart = wcsrchr(aFilenamePrefix, L'.');
if(extStart != NULL)
{
//if portion of string after extension is 4 characters long, then remove it
// otherwise.. keep it
if( wcslen(extStart) == 4 )
{
*extStart = L'\0';
}
}
//flip Z-NEG face for cube cross export in cubemap processor
for(j=0; j<m_pOutputCubeMap.m_pCubeTexture->GetLevelCount(); j++)
{
m_CubeMapProcessor.m_OutputSurface[j][CP_FACE_Z_NEG].InPlaceHorizonalFlip();
m_CubeMapProcessor.m_OutputSurface[j][CP_FACE_Z_NEG].InPlaceVerticalFlip();
}
//retreive cube map from cube map processor
GetCubeMapProcessorOutputCubeMap(&m_CubeMapProcessor);
//whether or not to export the mipchain
if( m_bExportMipChain == TRUE )
{
lowestMipLevelToSave = m_pOutputCubeMap.m_pCubeTexture->GetLevelCount() - 1;
}
else
{
lowestMipLevelToSave = 0;
}
//read filtered cube levels back out
for(j=0; j<=lowestMipLevelToSave; j++)
{
m_pOutputCubeMap.m_pCubeTexture->GetLevelDesc(j, &surfDesc);
crossImageWidth = 3 * surfDesc.Width;
crossImageHeight = 4 * surfDesc.Height;
//create temp surface to hold output cube cross
if(FAILED( m_pDevice->CreateOffscreenPlainSurface(crossImageWidth, crossImageHeight,
surfDesc.Format, D3DPOOL_SCRATCH, &pD3DDstSurface, NULL)))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: Cannot create offscreen, system memory buffer of size %dx%d and format %s",
crossImageWidth, crossImageHeight, DXUTD3DFormatToString(surfDesc.Format, false) );
return E_FAIL;
}
//copy cube faces into cubecross surface
for(iCubeFace=0; iCubeFace<6; iCubeFace++)
{
//grab cubeface surface
m_pOutputCubeMap.m_pCubeTexture->GetCubeMapSurface((D3DCUBEMAP_FACES)iCubeFace, j, &pD3DSrcSurface);
//compute region in destination cross image to copy cube face into
RECT dstRect;
dstRect.left = crossOffsets[iCubeFace][0] * surfDesc.Width;
dstRect.right = (crossOffsets[iCubeFace][0] + 1) * surfDesc.Width;
dstRect.top = crossOffsets[iCubeFace][1] * surfDesc.Height;
dstRect.bottom = (crossOffsets[iCubeFace][1] + 1) * surfDesc.Height;
//copy face into cube cross
V( D3DXLoadSurfaceFromSurface(
pD3DDstSurface,
NULL, //no palette
&dstRect, //
pD3DSrcSurface,
NULL, //no palette
NULL, //use full surface src rect
D3DX_FILTER_POINT,
NULL //no color key
) );
SAFE_RELEASE(pD3DSrcSurface);
}
//build filename from prefix and file-extension
if( m_bExportMipChain == TRUE )
{ //add _m?? after prefix to filename to specify miplevel for each cross saved
swprintf_s(aFullFilename, CG_MAX_FILENAME_LENGTH, L"%s_m%02d%s", aFilenamePrefix, j, fileExtension[(int32)aDestFormat] );
}
else
{ //save out filename with solely
swprintf_s(aFullFilename, CG_MAX_FILENAME_LENGTH, L"%s%s", aFilenamePrefix, fileExtension[(int32)aDestFormat] );
}
#ifdef CG_HDR_FILE_SUPPORT
//workaround for bug in D3DX libraries in which .hdr files 4 pixels or smaller in width get written
// out incorrectly
// (casting to uint32 allows comparison of enumerated type variables)
if( ((uint32)aDestFormat == (uint32)D3DXIFF_HDR) && (surfDesc.Width <=4) )
{
CImageSurface surfImg;
//HDR writer requires 3 channel CSurfaceImage
InitCSurfaceImageUsingD3DSurface(&surfImg, 3, pD3DDstSurface, NULL);
//write out .hdr file
surfImg.WriteHDRFile(aFullFilename);
//clear surface image
surfImg.Clear();
}
else
#endif
{
if(FAILED(D3DXSaveSurfaceToFile(aFullFilename, aDestFormat, pD3DDstSurface, NULL, NULL )))
{
m_bErrorOccurred = TRUE;
OutputMessage(L"Error: cannot save cubemap cross image file %s." //no comma, string continues on next line
L"Please check free diskspace, write permissions, and validity of the filename.",
aFullFilename);
return E_FAIL;
}
}
//release temp surface used to assemble and store the cube cross.
SAFE_RELEASE(pD3DDstSurface);
}
SAFE_RELEASE(pD3DSrcSurface);
//re-flip Z-NEG face to return cubemap in cubemap processor to normal D3D layout
for(j=0; j<m_pOutputCubeMap.m_pCubeTexture->GetLevelCount(); j++)
{
m_CubeMapProcessor.m_OutputSurface[j][CP_FACE_Z_NEG].InPlaceHorizonalFlip();
m_CubeMapProcessor.m_OutputSurface[j][CP_FACE_Z_NEG].InPlaceVerticalFlip();