1 |
|
---|
2 | struct Vertex
|
---|
3 | {
|
---|
4 | float x, y, z;
|
---|
5 | float nx, ny, nz;
|
---|
6 | float tu, tv;
|
---|
7 | };
|
---|
8 |
|
---|
9 | class EnvMap {
|
---|
10 |
|
---|
11 | IDirect3DDevice9* pd3dDevice;
|
---|
12 | IDirect3DTexture9* pRoomTexture;
|
---|
13 | /// Cubemap that stores the environment as seen from the reference point.
|
---|
14 | /// See method RenderCubeMap().
|
---|
15 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
16 | IDirect3DCubeTexture9* pCubeTextureFromFile;
|
---|
17 | IDirect3DCubeTexture9* pCubeTextureSmall; // low-res cube map for diffuse calculation
|
---|
18 | /// Low-resolution cubemap that stores preconvolved data for diffuse/specular environment mapping.
|
---|
19 | /// See method PreConvolve().
|
---|
20 | IDirect3DCubeTexture9* pCubeTexturePreConvolved;
|
---|
21 | IDirect3DTexture9* pCosValuesTexture; // precomputed geometric factors
|
---|
22 | ID3DXEffect* g_pEffect; // D3DX effect interface
|
---|
23 | HRESULT hr; // return codes
|
---|
24 | bool bCubeMapIsValid, bShininessIsValid;
|
---|
25 |
|
---|
26 | // mesh data
|
---|
27 | Cube* cube;
|
---|
28 | Mesh* mesh;
|
---|
29 | Mesh* meshes[10];
|
---|
30 | int meshCount;
|
---|
31 | int whichMesh;
|
---|
32 | float roomSize;
|
---|
33 | D3DXVECTOR3 reference_pos;
|
---|
34 |
|
---|
35 | public:
|
---|
36 | CModelViewerCamera* camera;
|
---|
37 |
|
---|
38 | EnvMap() {
|
---|
39 | bCubeMapIsValid = true;
|
---|
40 | bShininessIsValid = false;
|
---|
41 | pCubeTexture = NULL;
|
---|
42 | pCubeTextureSmall = NULL;
|
---|
43 | pCubeTexturePreConvolved = NULL;
|
---|
44 | pCubeTextureFromFile = NULL;
|
---|
45 | pCosValuesTexture = NULL;
|
---|
46 |
|
---|
47 | whichMesh = 1;
|
---|
48 | roomSize = 2.2;
|
---|
49 | reference_pos = D3DXVECTOR3(0,0,0);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void InvalidateCubeMap() { bCubeMapIsValid = false; }
|
---|
53 | void InvalidateShininess() { bShininessIsValid = false; }
|
---|
54 | D3DXVECTOR3& GetReferencePos() { return reference_pos; }
|
---|
55 | void ChooseMesh(int whichMesh);
|
---|
56 | void KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown );
|
---|
57 |
|
---|
58 | void DoRendering(D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj);
|
---|
59 | void DrawCenterObjects( D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
60 | void DrawEnvObjects( D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
61 |
|
---|
62 | void SetWorldViewProj(D3DXMATRIXA16& mWorld, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
63 | D3DXMATRIXA16 ScaleAndOffset(float fScale, D3DXVECTOR3 vOffset);
|
---|
64 | D3DXMATRIXA16 ScaleAndOffset(D3DXVECTOR3 vScale, D3DXVECTOR3 vOffset);
|
---|
65 |
|
---|
66 | void RenderCubeMap(IDirect3DCubeTexture9* pCubeTexture);
|
---|
67 | IDirect3DTexture9* GenerateCosTexture();
|
---|
68 | void GenerateCosTextureIfNotFound();
|
---|
69 | float eval( float cos_theta, float dw );
|
---|
70 |
|
---|
71 | void OnFrameRender( IDirect3DDevice9* pd3dDevice, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
72 |
|
---|
73 | void OnCreateDevice( IDirect3DDevice9* pd3dDevice, ID3DXEffect* g_pEffect );
|
---|
74 | void OnDestroyDevice();
|
---|
75 |
|
---|
76 | void OnResetDevice();
|
---|
77 | void OnLostDevice();
|
---|
78 |
|
---|
79 | IDirect3DVertexBuffer9 *pVertexBuffer;
|
---|
80 | void InitFullScreenQuad()
|
---|
81 | {
|
---|
82 | Vertex g_quadVertices[] = {
|
---|
83 | {-1, 1, 0, 0, 0, 1, 0,0 },
|
---|
84 | { 1, 1, 0, 0, 0, 1, 1,0 },
|
---|
85 | {-1,-1, 0, 0, 0, 1, 0,1 },
|
---|
86 | { 1,-1, 0, 0, 0, 1, 1,1 }
|
---|
87 | };
|
---|
88 |
|
---|
89 | pd3dDevice->CreateVertexBuffer( sizeof(g_quadVertices), D3DUSAGE_WRITEONLY,
|
---|
90 | D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1, D3DPOOL_DEFAULT,
|
---|
91 | &pVertexBuffer, NULL );
|
---|
92 | void *pVertices = NULL;
|
---|
93 |
|
---|
94 | pVertexBuffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 );
|
---|
95 | memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) );
|
---|
96 | pVertexBuffer->Unlock();
|
---|
97 | }
|
---|
98 |
|
---|
99 | void DrawFullScreenQuad()
|
---|
100 | {
|
---|
101 | // draw a square
|
---|
102 | pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(Vertex) );
|
---|
103 | pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
|
---|
104 | pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
|
---|
105 | }
|
---|
106 |
|
---|
107 | void ReduceCubeMapSize(IDirect3DCubeTexture9* pSourceCube, IDirect3DCubeTexture9* pDestCube);
|
---|
108 | void PreConvolve(IDirect3DCubeTexture9* pSourceCube, IDirect3DCubeTexture9* pDestCube);
|
---|
109 | //void RenderToCubeTexture(IDirect3DCubeTexture9* pDestCube);
|
---|
110 | void SaveCubeMap(IDirect3DCubeTexture9* pCubeTexture, char* FileNamePrefix, char* FileNameSuffix = "");
|
---|
111 |
|
---|
112 | IDirect3DCubeTexture9* CreateCubeTexture( int size, D3DFORMAT Format )
|
---|
113 | {
|
---|
114 | HRESULT hr;
|
---|
115 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
116 |
|
---|
117 | if( FAILED(hr = D3DXCreateCubeTexture( pd3dDevice,
|
---|
118 | size, 1, // mipmap levels = 1
|
---|
119 | D3DUSAGE_RENDERTARGET,
|
---|
120 | Format,
|
---|
121 | D3DPOOL_DEFAULT,
|
---|
122 | &pCubeTexture )))
|
---|
123 | {
|
---|
124 | MessageBox(NULL, L"Cube texture creation failed!", L"Error", MB_OK|MB_TOPMOST);
|
---|
125 | exit(-1);
|
---|
126 | }
|
---|
127 | return pCubeTexture;
|
---|
128 | }
|
---|
129 | };
|
---|