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
|
void RenduCubeMap( ID3D10Device* pd3dDevice )
{
HRESULT hr = E_FAIL;
struct CubeMapVertex
{
D3DXVECTOR3 Pos;
D3DXVECTOR3 Normal;
D3DXVECTOR2 Tex;
};
D3DXMATRIX matCubeMapView[6]; // tableau de vues pour les face du cube
ID3D10EffectTechnique* g_pRenderCubeMapTech; // techniques shader
ID3D10EffectShaderResourceVariable* ptxDiffuse;
ID3D10EffectMatrixVariable* pmWorld;
ID3D10EffectMatrixVariable* pmView;
ID3D10EffectMatrixVariable* pmProj;
ID3D10EffectMatrixVariable* pmWorldView;
ID3D10EffectMatrixVariable* pmWorldViewProj;
ID3D10Texture2D* g_pEnvMap; // Environment map
ID3D10RenderTargetView* g_pEnvMapRTV; // Render target view for the alpha map
ID3D10Texture2D* g_pEnvMapDepth; // Depth stencil for the environment map
ID3D10DepthStencilView* g_pEnvMapDSV; // Depth stencil view for environment map for all 6 faces
ID3D10DepthStencilView* g_pEnvMapOneDSV; // Depth stencil view for environment map for all 1 face
//ID3D10Buffer* g_pVBVisual; // Vertex buffer for quad used for visualization
ID3D10EffectVectorVariable* g_pvDiffuse; // élciarage diffus
ID3D10EffectVectorVariable* g_pvSpecular; // éclairage spéculaire
ID3D10EffectVectorVariable* g_pvEye; // eye dir
ID3D10EffectMatrixVariable* g_pmViewCM; // matrice de vue globale cube map
ID3D10InputLayout* g_pVertexLayout = NULL;
ID3D10InputLayout* g_pVertexLayoutCM = NULL; // layout cube map
ID3D10InputLayout* g_pVertexLayoutCMInst = NULL; // layout cube map instancié
ID3D10InputLayout* g_pVertexLayoutEnv = NULL;
D3DXMATRIX g_amCubeMapViewAdjust[6]; // Adjustment for view matrices when rendering the cube map
D3DXMATRIX g_mProjCM; // Projection matrix for cubic env map rendering
// initialisation procédure
if ( !g_stbCubemapInited )
{
g_stbCubemapInited = true; // maj drapeaux d'initialisation
// génère les cube map view matrices, orientées selon chaque face
float fHeight = 1.5f;
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, fHeight, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;
vLookDir = D3DXVECTOR3( 1.0f, fHeight, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matCubeMapView[0], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( -1.0f, fHeight, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matCubeMapView[1], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight + 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
D3DXMatrixLookAtLH( &matCubeMapView[2], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight - 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
D3DXMatrixLookAtLH( &matCubeMapView[3], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matCubeMapView[4], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight, -1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &matCubeMapView[5], &vEyePt, &vLookDir, &vUpDir );
// compilation shader
if ( !g_pCubeMapEffect )
{
WCHAR* szEffectFilePath = L"c://DOOM2011/Shaders/CubeMapGS.fx";
VerifHR( D3DX10CreateEffectFromFile(
szEffectFilePath,
NULL,
NULL,
"fx_4_0",
D3D10_SHADER_ENABLE_STRICTNESS||D3D10_SHADER_DEBUG,
0,
pd3dDevice,
NULL,
NULL,
&g_pCubeMapEffect,
NULL,
NULL
) );
if ( hr == S_OK )
OutputDebugString( L"compilation c://DOOM2011/Shaders/CubeMapGS.fx OK\n" );
}
// acquiert shader technique handles
g_pRenderCubeMapTech = g_pCubeMapEffect->GetTechniqueByName( "RenderCubeMap" );
//g_pRenderCubeMapInstTech = g_pCubeMapEffect->GetTechniqueByName( "RenderCubeMap_Inst" );
//g_pRenderSceneTech = g_pCubeMapEffect->GetTechniqueByName( "RenderScene" );
//g_pRenderEnvMappedSceneTech = g_pCubeMapEffect->GetTechniqueByName( "RenderEnvMappedScene" );
//g_pRenderEnvMappedSceneNoTexTech = g_pCubeMapEffect->GetTechniqueByName( "RenderEnvMappedScene_NoTexture" );
//g_pRenderEnvMappedGlassTech = g_pCubeMapEffect->GetTechniqueByName( "RenderEnvMappedGlass" );
// obtient les handles des paramètres variables shader
ptxDiffuse = g_pCubeMapEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource();
//g_ptxEnvMap = g_pCubeMapEffect->GetVariableByName( "g_txEnvMap" )->AsShaderResource();
//g_ptxFalloffMap = g_pCubeMapEffect->GetVariableByName( "g_txFalloff" )->AsShaderResource();
g_pvDiffuse = g_pCubeMapEffect->GetVariableByName( "vMaterialDiff" )->AsVector();
g_pvSpecular = g_pCubeMapEffect->GetVariableByName( "vMaterialSpec" )->AsVector();
g_pvEye = g_pCubeMapEffect->GetVariableByName( "vEye" )->AsVector();
g_pmViewCM = g_pCubeMapEffect->GetVariableByName( "g_mViewCM" )->AsMatrix();
pmWorld = g_pCubeMapEffect->GetVariableByName( "mWorld" )->AsMatrix();
pmView = g_pCubeMapEffect->GetVariableByName( "mView" )->AsMatrix();
pmProj = g_pCubeMapEffect->GetVariableByName( "mProj" )->AsMatrix();
pmWorldView = g_pCubeMapEffect->GetVariableByName( "mWorldView" )->AsMatrix();
pmWorldViewProj= g_pCubeMapEffect->GetVariableByName( "mWorldViewProj" )->AsMatrix();
// créé cubic depth stencil texture.
D3D10_TEXTURE2D_DESC dstex;
dstex.Width = ENVMAPSIZE;
dstex.Height = ENVMAPSIZE;
dstex.MipLevels = 1;
dstex.ArraySize = 6;
dstex.SampleDesc.Count = 1;
dstex.SampleDesc.Quality = 0;
dstex.Format = DXGI_FORMAT_D32_FLOAT;
dstex.Usage = D3D10_USAGE_DEFAULT;
dstex.BindFlags = D3D10_BIND_DEPTH_STENCIL;
dstex.CPUAccessFlags = 0;
dstex.MiscFlags = D3D10_RESOURCE_MISC_TEXTURECUBE;
VerifHR( pd3dDevice->CreateTexture2D( &dstex, NULL, &g_pEnvMapDepth ) );
// créé depth stencil view pour le cube entier
D3D10_DEPTH_STENCIL_VIEW_DESC DescDS;
DescDS.Format = DXGI_FORMAT_D32_FLOAT;
DescDS.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2DARRAY;
DescDS.Texture2DArray.FirstArraySlice = 0;
DescDS.Texture2DArray.ArraySize = 6;
DescDS.Texture2DArray.MipSlice = 0;
VerifHR( pd3dDevice->CreateDepthStencilView( g_pEnvMapDepth, &DescDS, &g_pEnvMapDSV ) );
// créé un depth stencil view pour single face rendering
DescDS.Texture2DArray.ArraySize = 1;
VerifHR( pd3dDevice->CreateDepthStencilView( g_pEnvMapDepth, &DescDS, &g_pEnvMapOneDSV ) );
// créé texture cube map avec mips pour rendu avec env map render target
dstex.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
dstex.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
dstex.MiscFlags = D3D10_RESOURCE_MISC_GENERATE_MIPS | D3D10_RESOURCE_MISC_TEXTURECUBE;
dstex.MipLevels = MIPLEVELS;
VerifHR( pd3dDevice->CreateTexture2D( &dstex, NULL, &g_pEnvMap ) );
// créé une render target view pour l'ensemble des 6 faces
D3D10_RENDER_TARGET_VIEW_DESC DescRT;
DescRT.Format = dstex.Format;
DescRT.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2DARRAY;
DescRT.Texture2DArray.FirstArraySlice = 0;
DescRT.Texture2DArray.ArraySize = 6;
DescRT.Texture2DArray.MipSlice = 0;
VerifHR( pd3dDevice->CreateRenderTargetView( g_pEnvMap, &DescRT, &g_pEnvMapRTV ) );
// créé les target views pour chaque face du cube
DescRT.Texture2DArray.ArraySize = 1;
for( int i = 0; i < 6; ++i )
{
DescRT.Texture2DArray.FirstArraySlice = i;
VerifHR( pd3dDevice->CreateRenderTargetView( g_pEnvMap, &DescRT, &g_apEnvMapOneRTV[i] ) );
}
// créé shader resource view pour la cubic env map
D3D10_SHADER_RESOURCE_VIEW_DESC SRVDesc;
ZeroMemory( &SRVDesc, sizeof( SRVDesc ) );
SRVDesc.Format = dstex.Format;
SRVDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE;
SRVDesc.TextureCube.MipLevels = MIPLEVELS;
SRVDesc.TextureCube.MostDetailedMip = 0;
VerifHR( pd3dDevice->CreateShaderResourceView( g_pEnvMap, &SRVDesc, &g_pEnvMapSRV ) );
// définit le vertex data layout
const D3D10_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
g_pRenderCubeMapTech = g_pCubeMapEffect->GetTechniqueByName( "RenderCubeMap" );
// créé le vertex layout pour la cube map
D3D10_PASS_DESC PassDesc;
g_pRenderCubeMapTech->GetPassByIndex( 0 )->GetDesc( &PassDesc );
VerifHR( pd3dDevice->CreateInputLayout( layout, 3, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &g_pVertexLayoutCM ) );
//g_pRenderCubeMapInstTech->GetPassByIndex( 0 )->GetDesc( &PassDesc );
//VerifHR( pd3dDevice->CreateInputLayout( layout, 3, PassDesc.pIAInputSignature,
// PassDesc.IAInputSignatureSize, &g_pVertexLayoutCMInst ) );
//g_pRenderEnvMappedSceneTech->GetPassByIndex( 0 )->GetDesc( &PassDesc );
//VerifHR( pd3dDevice->CreateInputLayout( layout, 3, PassDesc.pIAInputSignature,
// PassDesc.IAInputSignatureSize, &g_pVertexLayoutEnv ) );
// créé matrice de projection cube map
D3DXMatrixPerspectiveFovLH( &g_mProjCM, D3DX_PI * 0.5f, 1.0f, .5f, 1000.f );
// créé un quad vertex buffer pour la visualisation
D3D10_BUFFER_DESC bufferdesc =
{
6 * sizeof( CubeMapVertex ),
D3D10_USAGE_DEFAULT,
D3D10_BIND_VERTEX_BUFFER,
0,
0
};
CubeMapVertex QuadVertices[6] =
{
{ D3DXVECTOR3( -1.0f, 1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
{ D3DXVECTOR3( 1.0f, 1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
{ D3DXVECTOR3( -1.0f, -1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
{ D3DXVECTOR3( -1.0f, -1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
{ D3DXVECTOR3( 1.0f, 1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
{ D3DXVECTOR3( 1.0f, -1.0f, 0.5f ), D3DXVECTOR3( 0.0f, 0.0f, -1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) }
};
D3D10_SUBRESOURCE_DATA InitData =
{
QuadVertices,
sizeof( QuadVertices ),
sizeof( QuadVertices )
};
VerifHR( pd3dDevice->CreateBuffer( &bufferdesc, &InitData, &g_pVBVisual ) ); // pour visualisation ???
} // fin de if !inited
// RenderSceneIntoCubeMap()
// construction de la cube map, sauve old render target et depth stencil
ID3D10RenderTargetView* apOldRTVs[1] = { NULL };
ID3D10DepthStencilView* pOldDS = NULL;
pd3dDevice->OMGetRenderTargets( 1, apOldRTVs, &pOldDS );
// sauve old viewport
D3D10_VIEWPORT OldVP;
UINT cRT = 1;
pd3dDevice->RSGetViewports( &cRT, &OldVP );
// créé un nouveau viewport pour la cube map
D3D10_VIEWPORT SMVP;
SMVP.Height = ENVMAPSIZE;
SMVP.Width = ENVMAPSIZE;
SMVP.MinDepth = 0;
SMVP.MaxDepth = 1;
SMVP.TopLeftX = 0;
SMVP.TopLeftY = 0;
pd3dDevice->RSSetViewports( 1, &SMVP );
// génère les cube map view matrices pour chaque face
float fHeight = 1.5f;
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, fHeight, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;
vLookDir = D3DXVECTOR3( 1.0f, fHeight, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[0], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( -1.0f, fHeight, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[1], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight + 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, -1.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[2], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight - 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[3], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[4], &vEyePt, &vLookDir, &vUpDir );
vLookDir = D3DXVECTOR3( 0.0f, fHeight, -1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &g_amCubeMapViewAdjust[5], &vEyePt, &vLookDir, &vUpDir );
// Here, compute the view matrices used for cube map rendering.
// First, construct mViewAlignCM, a view matrix with the same orientation as m_mView but with eye point at the car position.
// matrice de positionnement de l'observateur pour générer chaque face
D3DXMATRIX mViewAlignCM;
D3DXMatrixIdentity( &mViewAlignCM );
mViewAlignCM._41 = -g_matView._41;
mViewAlignCM._42 = -g_matView._42;
mViewAlignCM._43 = -g_matView._43;
// combine les 6 differentes view direction, corrigé par la position de l'oeil
// pour obtenir une matrice de vue pour chaque face
D3DXMATRIX amViewCM[6];
for( int view = 0; view < 6; ++view )
{
D3DXMatrixMultiply( &amViewCM[view], &mViewAlignCM, &g_amCubeMapViewAdjust[view] );
}
pd3dDevice->IASetInputLayout( g_pVertexLayoutCM ); // assigne le layout rendu cubemap
ID3D10EffectTechnique* pTechnique = g_pCubeMapEffect->GetTechniqueByName( "RenderCubeMap" ); // aquiert handle technique
if ( pTechnique->IsValid() != TRUE )
return;
// assigne variables shader
g_pmViewCM = g_pCubeMapEffect->GetVariableByName( "g_mViewCM" )->AsMatrix();
pmWorld = g_pCubeMapEffect->GetVariableByName( "mWorld" )->AsMatrix();
pmView = g_pCubeMapEffect->GetVariableByName( "mView" )->AsMatrix();
pmProj = g_pCubeMapEffect->GetVariableByName( "mProj" )->AsMatrix();
ptxDiffuse = g_pCubeMapEffect->GetVariableByName( "g_txDiffuse" )->AsShaderResource();
// rendu de chaque face du cube
for( int view = 0; view < 6; ++view )
{
// efface cible et stencil buffer de la face
float ClearColor[4] = { 0.0f, 0.8f, 0.9f, 1.0f };
pd3dDevice->ClearRenderTargetView( g_apEnvMapOneRTV[view], ClearColor );
pd3dDevice->ClearDepthStencilView( g_pEnvMapOneDSV, D3D10_CLEAR_DEPTH, 1.0, 0 );
// fixe cible de rendu selon la face
ID3D10RenderTargetView* aRTViews[ 1 ] = { g_apEnvMapOneRTV[view] };
pd3dDevice->OMSetRenderTargets( sizeof( aRTViews ) / sizeof( aRTViews[0] ), aRTViews, g_pEnvMapOneDSV );
// assigne variables shader
pmWorld->SetMatrix( ( float* )&g_matWorld ); // matrice du monde
g_pmViewCM->SetMatrix( ( float* )&amViewCM[view] ); // matrice de vue de la face
pmProj->SetMatrix( ( float* )&g_matProj ); // matrice de projection
ptxDiffuse->SetResource( g_pTestTex1_SRV ); // texture pour la face
// MANQUE QUELQUE CHOSE ICI ??
// rendu tableau instancié étoiles sur la face du cube
Rendu_F9( pd3dDevice );
}
pd3dDevice->RSSetViewports( 1, &OldVP ); // restaure old view port
pd3dDevice->OMSetRenderTargets( 1, apOldRTVs, pOldDS ); // restaure old RT and DS buffer views
pd3dDevice->GenerateMips( g_pEnvMapSRV ); // Generate Mip Maps ???
SAFE_RELEASE( apOldRTVs[0] ); // libère les cibles de rendu
SAFE_RELEASE( pOldDS );
// fin de RenderSceneIntoCubeMap()
// DEBUG: sauve texture 2D de la map créée
// dimensions : 3072x512, 6*512 de large
D3DX10_IMAGE_FILE_FORMAT loadinfo = D3DX10_IFF_DDS;
VerifHR( D3DX10SaveTextureToFileW(
g_pEnvMap,
loadinfo,
L"C://DOOM2011/Test_cubemap.dds"
) );
// DEBUG 13-2-2011
// à faire exploiter le résultat du rendu cubemap qui de setrouve dans g_pEnvMapSRV texture cube
return;
} |
Partager