[1488] | 1 |
|
---|
| 2 | /**
|
---|
| 3 | \brief Data type to define fullscreen quad vertices.
|
---|
| 4 | */
|
---|
| 5 | struct Vertex
|
---|
| 6 | {
|
---|
| 7 | float x, y, z;
|
---|
| 8 | float nx, ny, nz;
|
---|
| 9 | float tu, tv;
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | \brief Most of application-specific logic is here.
|
---|
| 14 | */
|
---|
| 15 | class EnvMap {
|
---|
| 16 |
|
---|
| 17 | /// Callback function to evaluate reflectivity integral. See GenerateCosTexture() and eval().
|
---|
| 18 | typedef float (*eval_fn)(float,float);
|
---|
| 19 |
|
---|
| 20 | IDirect3DDevice9* pd3dDevice; ///< THE D3D DEVICE
|
---|
| 21 | ID3DXEffect* g_pEffect; ///< THE EFFECT FILE
|
---|
| 22 | CModelViewerCamera* camera; ///< Rotate/zoom camera, always looking to the origin
|
---|
| 23 | HRESULT hr; // return codes
|
---|
| 24 |
|
---|
| 25 | /// \brief Cubemap that stores the environment as seen from the reference point.
|
---|
| 26 | /// \brief See method RenderCubeMap().
|
---|
| 27 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
| 28 | /// \brief Low-resolution (downsampled) cubemap, derived from #pCubeTexture.
|
---|
| 29 | /// \brief See ReduceCubeMapSize().
|
---|
| 30 | IDirect3DCubeTexture9* pCubeTextureSmall;
|
---|
| 31 | /// \brief Low-resolution cubemap that stores preconvolved data for diffuse/specular environment mapping.
|
---|
| 32 | /// \brief See method PreConvolve().
|
---|
| 33 | IDirect3DCubeTexture9* pCubeTexturePreConvolved;
|
---|
| 34 | /// Helper texture with precomputed geometric factor values
|
---|
| 35 | IDirect3DTexture9* pCosValuesTexture;
|
---|
| 36 | /// Texture to be displayed on the walls of the cubic room.
|
---|
| 37 | IDirect3DTexture9* pRoomTexture;
|
---|
| 38 |
|
---|
| 39 | bool bCubeMapIsValid; /// indicates that the cube map has to be re-generated
|
---|
| 40 | bool bShininessIsValid; /// indicates that the reflectivity integral has to be re-calculated
|
---|
| 41 |
|
---|
| 42 | Cube* cube; /// Simple cube centralMesh for the room. See EnvMap::DrawEnvObjects().
|
---|
| 43 | float roomSize; /// Size of the cubic room.
|
---|
| 44 |
|
---|
| 45 | Mesh* centralMesh; /// The diffuse/glossy object to be shaded.
|
---|
| 46 | Mesh* meshes[10]; /// Objects of the environment (fireballs, etc.).
|
---|
| 47 | int meshCount; /// Number of objects in the environment.
|
---|
| 48 | D3DXVECTOR3 reference_pos;
|
---|
| 49 |
|
---|
| 50 | public:
|
---|
| 51 |
|
---|
| 52 | EnvMap() {
|
---|
| 53 | bCubeMapIsValid = true;
|
---|
| 54 | bShininessIsValid = false;
|
---|
| 55 | pCubeTexture = NULL;
|
---|
| 56 | pCubeTextureSmall = NULL;
|
---|
| 57 | pCubeTexturePreConvolved = NULL;
|
---|
| 58 | pCosValuesTexture = NULL;
|
---|
| 59 | centralMesh = NULL;
|
---|
| 60 |
|
---|
| 61 | roomSize = 2.2;
|
---|
| 62 | reference_pos = D3DXVECTOR3(0,0,0);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void SetCamera(CModelViewerCamera* camera) { this->camera = camera; }
|
---|
| 66 |
|
---|
| 67 | void InvalidateCubeMap() { bCubeMapIsValid = false; }
|
---|
| 68 | void InvalidateShininess() { bShininessIsValid = false; }
|
---|
| 69 | void InvalidateResolution();
|
---|
| 70 |
|
---|
| 71 | D3DXVECTOR3& GetReferencePos() { return reference_pos; }
|
---|
| 72 |
|
---|
| 73 | void ChooseMesh(int whichMesh);
|
---|
| 74 | void SetMeshSize(float d) { if (centralMesh != NULL) centralMesh->SetPreferredDiameter(d); }
|
---|
| 75 | void SetMeshPosition(D3DXVECTOR3 pos) { centralMesh->SetMeshPosition(pos); }
|
---|
| 76 | D3DXVECTOR3 GetMeshPosition() { return centralMesh->GetMeshPosition(); }
|
---|
| 77 |
|
---|
| 78 | void KeyboardProc( UINT nChar, bool bKeyDown, bool bAltDown );
|
---|
| 79 |
|
---|
| 80 | void DoRendering(D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj);
|
---|
| 81 | void DrawCenterObjects( D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
| 82 | void DrawEnvObjects( D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
| 83 |
|
---|
| 84 | void SetWorldViewProj(D3DXMATRIXA16& mWorld, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
| 85 | D3DXMATRIXA16 ScaleAndOffset(float fScale, D3DXVECTOR3 vOffset);
|
---|
| 86 | D3DXMATRIXA16 ScaleAndOffset(D3DXVECTOR3 vScale, D3DXVECTOR3 vOffset);
|
---|
| 87 |
|
---|
| 88 | void RenderCubeMap(IDirect3DCubeTexture9* pCubeTexture);
|
---|
| 89 | //IDirect3DTexture9* GenerateCosTexture();
|
---|
| 90 | IDirect3DTexture9* GenerateCosTexture( eval_fn evaluator );
|
---|
| 91 |
|
---|
| 92 | //void GenerateCosTextureIfNotFound();
|
---|
| 93 | void GenerateCosTextureIfNotFound( wchar_t* fileName, eval_fn evaluator );
|
---|
| 94 |
|
---|
| 95 | void OnFrameRender( IDirect3DDevice9* pd3dDevice, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj );
|
---|
| 96 |
|
---|
| 97 | void OnCreateDevice( IDirect3DDevice9* pd3dDevice, ID3DXEffect* g_pEffect );
|
---|
| 98 | void OnDestroyDevice();
|
---|
| 99 |
|
---|
| 100 | void OnResetDevice();
|
---|
| 101 | void OnLostDevice();
|
---|
| 102 |
|
---|
| 103 | IDirect3DVertexBuffer9 *pVertexBuffer;
|
---|
| 104 | void InitFullScreenQuad()
|
---|
| 105 | {
|
---|
| 106 | Vertex g_quadVertices[] = {
|
---|
| 107 | {-1, 1, 0, 0, 0, 1, 0,0 },
|
---|
| 108 | { 1, 1, 0, 0, 0, 1, 1,0 },
|
---|
| 109 | {-1,-1, 0, 0, 0, 1, 0,1 },
|
---|
| 110 | { 1,-1, 0, 0, 0, 1, 1,1 }
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | pd3dDevice->CreateVertexBuffer( sizeof(g_quadVertices), D3DUSAGE_WRITEONLY,
|
---|
| 114 | D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1, D3DPOOL_DEFAULT,
|
---|
| 115 | &pVertexBuffer, NULL );
|
---|
| 116 | void *pVertices = NULL;
|
---|
| 117 |
|
---|
| 118 | pVertexBuffer->Lock( 0, sizeof(g_quadVertices), (void**)&pVertices, 0 );
|
---|
| 119 | memcpy( pVertices, g_quadVertices, sizeof(g_quadVertices) );
|
---|
| 120 | pVertexBuffer->Unlock();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | void DrawFullScreenQuad()
|
---|
| 124 | {
|
---|
| 125 | // draw a square
|
---|
| 126 | pd3dDevice->SetStreamSource( 0, pVertexBuffer, 0, sizeof(Vertex) );
|
---|
| 127 | pd3dDevice->SetFVF( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 );
|
---|
| 128 | pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void ReduceCubeMapSize(IDirect3DCubeTexture9* pSourceCube, IDirect3DCubeTexture9* pDestCube);
|
---|
| 132 | void PreConvolve(IDirect3DCubeTexture9* pSourceCube, IDirect3DCubeTexture9* pDestCube);
|
---|
| 133 | //void RenderToCubeTexture(IDirect3DCubeTexture9* pDestCube);
|
---|
| 134 | void SaveCubeMap(IDirect3DCubeTexture9* pCubeTexture, char* FileNamePrefix, char* FileNameSuffix = "");
|
---|
| 135 |
|
---|
| 136 | IDirect3DCubeTexture9* CreateCubeTexture( int size, D3DFORMAT Format )
|
---|
| 137 | {
|
---|
| 138 | HRESULT hr;
|
---|
| 139 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
| 140 |
|
---|
| 141 | if( FAILED(hr = D3DXCreateCubeTexture( pd3dDevice,
|
---|
| 142 | size, 1, // mipmap levels = 1
|
---|
| 143 | D3DUSAGE_RENDERTARGET,
|
---|
| 144 | Format,
|
---|
| 145 | D3DPOOL_DEFAULT,
|
---|
| 146 | &pCubeTexture )))
|
---|
| 147 | {
|
---|
| 148 | MessageBox(NULL, L"Cube texture creation failed!", L"Error", MB_OK|MB_TOPMOST);
|
---|
| 149 | exit(-1);
|
---|
| 150 | }
|
---|
| 151 | return pCubeTexture;
|
---|
| 152 | }
|
---|
| 153 | };
|
---|