// WBWaterVShader.cpp: implementation of the WBWaterVShader class. // ////////////////////////////////////////////////////////////////////// #include "WBWaterVShader.h" //#include "WBCamera.h" //#include "WBRenderState.h" #include "ShaderConstants.h" #include "SceneManager.h" #include "WBEnvPlaneTex.h" #include "SceneStateMgr.h" //extern WBCamera g_Camera; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// WBWaterVShader::WBWaterVShader() { DWORD vertexshaderdeclaration[]= { D3DVSD_STREAM(0), (D3DVSD_REG(0, D3DVSDT_FLOAT3)), // vertex position D3DVSD_REG(1, D3DVSDT_FLOAT2), // Texture D3DVSD_END() }; CreateVertexShader("c:/mp-project/Shaders/Water.vsh",vertexshaderdeclaration); m_fDisScale = 0.8f; m_fTexcoordDisScale = 0.3f; //m_fTexcoordDisScale = 5.3f; m_Const1 = D3DXVECTOR4( 0.0f, 0.5f, 1.0f, 2.0f ); m_Const2 = D3DXVECTOR4( 0.0f, 0.5f, -1.0f, 1.0f ); D3DXMatrixIdentity(&m_matTmWorld); } WBWaterVShader::~WBWaterVShader() { } void WBWaterVShader::Apply() { CSceneManager::GetDevice()->SetVertexShader(m_dwVertexShader); SetupVertexShaderConstants(); } void WBWaterVShader::SetupVertexShaderConstants() { /* CSceneManager::GetDevice()->SetVertexShaderConstant(CV_ZERO, D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f), 1); CSceneManager::GetDevice()->SetVertexShaderConstant(CV_ONE, D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f), 1); CSceneManager::GetDevice()->SetVertexShaderConstant(CV_HALF, D3DXVECTOR4(0.5f, 0.5f, 0.5f, 0.5f), 1); */ CSceneManager::GetDevice()->SetVertexShaderConstant( CV_CONSTS_1, &m_Const1, 1 ); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_CONSTS_2, &m_Const2, 1 ); int i; for( i=0; i < 4; i++ ) { CSceneStateMgr::_SetD3DTextureStageState(i, D3DTSS_MAGFILTER, D3DTEXF_LINEAR ); CSceneStateMgr::_SetD3DTextureStageState(i, D3DTSS_MINFILTER, D3DTEXF_LINEAR ); CSceneStateMgr::_SetD3DTextureStageState(i, D3DTSS_MIPFILTER, D3DTEXF_POINT ); // nearest level } CSceneStateMgr::_SetD3DRenderState( D3DRS_ZENABLE, D3DZB_TRUE ); CSceneStateMgr::_SetD3DRenderState( D3DRS_ZWRITEENABLE, true ); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_WATERBUMPSCALE, D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.12f), 1); //////////// CSceneManager::GetDevice()->SetVertexShaderConstant( CV_CALC_SXT, D3DXVECTOR4( 0.0f, 1.0f, 0.0f, 0.0f), 1 ); CSceneStateMgr::_SetD3DRenderState( D3DRS_ALPHABLENDENABLE, FALSE); // General setup CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP); CSceneStateMgr::_SetD3DRenderState( D3DRS_WRAP0, 0 ); CSceneStateMgr::_SetD3DRenderState(D3DRS_CULLMODE, D3DCULL_NONE ); D3DXVECTOR4 vec = D3DXVECTOR4( m_fDisScale, m_fDisScale, m_fDisScale, m_fDisScale ); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_DISPSCALE, &vec, 1 ); ///////////////////////////////////////////////////////////////// vec = D3DXVECTOR4( m_fTexcoordDisScale, m_fTexcoordDisScale,m_fTexcoordDisScale, m_fTexcoordDisScale ); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_OFFSETSCALE, &vec, 1 ); // Texture 0 is set elsewhere to be the normal map CSceneManager::GetDevice()->SetTexture( 1, NULL); CSceneManager::GetDevice()->SetTexture( 2, NULL); CSceneStateMgr::_SetD3DTextureStageState( 3, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP ); CSceneStateMgr::_SetD3DTextureStageState( 3, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP ); CSceneStateMgr::_SetD3DRenderState( D3DRS_WRAP3, 0 ); // CSceneManager::GetDevice()->SetTexture( 3, m_pSkyTexture ); ////////////////////////////////////////////////////////// // alpha blend for quasi-Fresnel term CSceneStateMgr::_SetD3DRenderState( D3DRS_ALPHABLENDENABLE, true ); // CSceneStateMgr::_SetD3DRenderState( D3DRS_ALPHABLENDENABLE, false ); CSceneStateMgr::_SetD3DRenderState( D3DRS_SRCBLEND, D3DBLEND_INVSRCALPHA ); CSceneStateMgr::_SetD3DRenderState( D3DRS_DESTBLEND, D3DBLEND_SRCALPHA ); /* CSceneStateMgr::_SetD3DRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE ); CSceneStateMgr::_SetD3DRenderState( D3DRS_DESTBLEND, D3DBLEND_SRCALPHA );*/ ////////////////////////////////////////////////////////// // Setup the vertex & pixel shaders // CSceneManager::GetDevice()->SetVertexShader( m_dwVSH_Dot3x2EMBM_Displace ); // CSceneManager::GetDevice()->SetPixelShader( m_dwPSH_Dot3x2EMBM_Displace ); //////////// D3DXMATRIX matWorld,matView,matProject; //CSceneManager::GetDevice()->GetTransform( D3DTS_WORLD ,&matWorld); D3DXMatrixIdentity(&matWorld); CSceneManager::GetDevice()->GetTransform( D3DTS_VIEW ,&matView); CSceneManager::GetDevice()->GetTransform( D3DTS_PROJECTION,&matProject); /*D3DXMATRIX matWorld = WBRenderState::GetWorldTransform(); D3DXMATRIX matView = WBRenderState::GetViewTransform(); D3DXMATRIX matProject = WBRenderState::GetProjectTransform();*/ D3DXMATRIX matTemp; D3DXMATRIX matWorldViewProj; D3DXMATRIX matWorldView; D3DXMatrixMultiply(&matWorld,&matWorld,&m_matTmWorld); D3DXMatrixMultiply(&matWorldView, &matWorld, &matView); D3DXMatrixMultiply(&matTemp,&matView,&matProject); D3DXMatrixMultiply(&matWorldViewProj, &matWorld, &matTemp); // Projection to clip space D3DXMatrixTranspose(&matWorldViewProj, &matWorldViewProj); D3DXMATRIX invWV; D3DXMatrixInverse( &invWV, NULL, &matTemp ); // D3DXMatrixInverse( &invWV, NULL, &matWorldView ); D3DXVECTOR4 outV, inV; inV = D3DXVECTOR4( 0.0f, 0.0f, 0.0f, 1.0f ); D3DXVec4Transform( &outV, &inV, &invWV ); // Homogenize and invert outV.x = - outV.x / outV.w; outV.y = - outV.y / outV.w; outV.z = - outV.z / outV.w; outV.w = outV.w / outV.w; outV.x = -outV.x; outV.y = -outV.y; D3DXMatrixTranspose(&matWorldView,&matWorldView); // This is now the eye location in obj space CSceneManager::GetDevice()->SetVertexShaderConstant( CV_EYE_OBJSPC, &outV, 1 ); CSceneManager::GetDevice()->SetVertexShaderConstant(CV_WORLDVIEWPROJ_0, &matWorldViewProj(0, 0), 4); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_WORLD_0, &matWorldView(0, 0), 1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_WORLD_1, &matWorldView(1, 0), 1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_WORLD_2, &matWorldView(2, 0), 1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_BASISTRANSFORM_0, &matWorldView(0, 0), 1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_BASISTRANSFORM_1, &matWorldView(1, 0), 1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_BASISTRANSFORM_2, &matWorldView(2, 0), 1); // D3DXMatrixTranspose(&(CSceneManager::m_pEnvPlaneTex->m_matReflect),&(CSceneManager::m_pEnvPlaneTex->m_matReflect)); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_REFLECTMAT_0,&(CSceneManager::m_pEnvPlaneTex->m_matReflect(0,0)),1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_REFLECTMAT_1,&(CSceneManager::m_pEnvPlaneTex->m_matReflect(1,0)),1); CSceneManager::GetDevice()->SetVertexShaderConstant( CV_REFLECTMAT_2,&(CSceneManager::m_pEnvPlaneTex->m_matReflect(2,0)),1); }